dom/indexedDB/test/blob_worker_crash_iframe.html

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 <!--
     2   Any copyright is dedicated to the Public Domain.
     3   http://creativecommons.org/publicdomain/zero/1.0/
     4 -->
     5 <html>
     6 <head>
     7   <title>Indexed Database Test</title>
     9   <script type="text/javascript">
    10   function report(result) {
    11     var message = { source: "iframe" };
    12     message.result = result;
    13     window.parent.postMessage(message, "*");
    14   }
    16   function runIndexedDBTest() {
    17     var db = null;
    19     // Create the data-store
    20     function createDatastore() {
    21       try {
    22         var request = indexedDB.open(window.location.pathname, 1);
    23         request.onupgradeneeded = function(event) {
    24           event.target.result.createObjectStore("foo");
    25         }
    26         request.onsuccess = function(event) {
    27           db = event.target.result;
    28           createAndStoreBlob();
    29         }
    30       }
    31       catch (e) {
    32 dump("EXCEPTION IN CREATION: " + e + "\n " + e.stack + "\n");
    33         report(false);
    34       }
    35     }
    37     function createAndStoreBlob() {
    38       const BLOB_DATA = ["fun ", "times ", "all ", "around!"];
    39       var blob = new Blob(BLOB_DATA, { type: "text/plain" });
    40       var objectStore = db.transaction("foo", "readwrite").objectStore("foo");
    41       objectStore.add({ blob: blob }, 42).onsuccess = refetchBlob;
    42     }
    44     function refetchBlob() {
    45       var foo = db.transaction("foo").objectStore("foo");
    46       foo.get(42).onsuccess = fetchedBlobCreateWorkerAndSendBlob;
    47     }
    49     function fetchedBlobCreateWorkerAndSendBlob(event) {
    50       var idbBlob = event.target.result.blob;
    51       var compositeBlob = new Blob(['I like the following blob: ', idbBlob],
    52                                    { type: "text/fancy" });
    54       function workerScript() {
    55         onmessage = function(event) {
    56           // Save the Blob to the worker's global scope.
    57           self.holdOntoBlob = event.data;
    58           // Send any message so we can serialize and keep our runtime behaviour
    59           // consistent.
    60           postMessage('kung fu death grip established');
    61         }
    62       }
    64       var url =
    65         URL.createObjectURL(new Blob(["(", workerScript.toSource(), ")()"]));
    67       // Keep a reference to the worker on the window.
    68       var worker = window.worker = new Worker(url);
    69       worker.postMessage(compositeBlob);
    70       worker.onmessage = workerLatchedBlobDeleteFromDB;
    71     }
    73     function workerLatchedBlobDeleteFromDB() {
    74       // Delete the reference to the Blob from the database leaving the worker
    75       // thread reference as the only live reference once a GC has cleaned
    76       // out our references that we sent to the worker.  The page that owns
    77       // us triggers a GC just for that reason.
    78       var objectStore = db.transaction("foo", "readwrite").objectStore("foo");
    79       objectStore.delete(42).onsuccess = closeDBTellOwningThread;
    80     }
    82     function closeDBTellOwningThread(event) {
    83       // Now that worker has latched the blob, clean up the database.
    84       db.close();
    85       db = null;
    86       report('ready');
    87     }
    89     createDatastore();
    90   }
    91   </script>
    93 </head>
    95 <body onload="runIndexedDBTest();">
    96 </body>
    98 </html>

mercurial