dom/indexedDB/test/blob_worker_crash_iframe.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/blob_worker_crash_iframe.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +<!--
     1.5 +  Any copyright is dedicated to the Public Domain.
     1.6 +  http://creativecommons.org/publicdomain/zero/1.0/
     1.7 +-->
     1.8 +<html>
     1.9 +<head>
    1.10 +  <title>Indexed Database Test</title>
    1.11 +
    1.12 +  <script type="text/javascript">
    1.13 +  function report(result) {
    1.14 +    var message = { source: "iframe" };
    1.15 +    message.result = result;
    1.16 +    window.parent.postMessage(message, "*");
    1.17 +  }
    1.18 +
    1.19 +  function runIndexedDBTest() {
    1.20 +    var db = null;
    1.21 +
    1.22 +    // Create the data-store
    1.23 +    function createDatastore() {
    1.24 +      try {
    1.25 +        var request = indexedDB.open(window.location.pathname, 1);
    1.26 +        request.onupgradeneeded = function(event) {
    1.27 +          event.target.result.createObjectStore("foo");
    1.28 +        }
    1.29 +        request.onsuccess = function(event) {
    1.30 +          db = event.target.result;
    1.31 +          createAndStoreBlob();
    1.32 +        }
    1.33 +      }
    1.34 +      catch (e) {
    1.35 +dump("EXCEPTION IN CREATION: " + e + "\n " + e.stack + "\n");
    1.36 +        report(false);
    1.37 +      }
    1.38 +    }
    1.39 +
    1.40 +    function createAndStoreBlob() {
    1.41 +      const BLOB_DATA = ["fun ", "times ", "all ", "around!"];
    1.42 +      var blob = new Blob(BLOB_DATA, { type: "text/plain" });
    1.43 +      var objectStore = db.transaction("foo", "readwrite").objectStore("foo");
    1.44 +      objectStore.add({ blob: blob }, 42).onsuccess = refetchBlob;
    1.45 +    }
    1.46 +
    1.47 +    function refetchBlob() {
    1.48 +      var foo = db.transaction("foo").objectStore("foo");
    1.49 +      foo.get(42).onsuccess = fetchedBlobCreateWorkerAndSendBlob;
    1.50 +    }
    1.51 +
    1.52 +    function fetchedBlobCreateWorkerAndSendBlob(event) {
    1.53 +      var idbBlob = event.target.result.blob;
    1.54 +      var compositeBlob = new Blob(['I like the following blob: ', idbBlob],
    1.55 +                                   { type: "text/fancy" });
    1.56 +
    1.57 +      function workerScript() {
    1.58 +        onmessage = function(event) {
    1.59 +          // Save the Blob to the worker's global scope.
    1.60 +          self.holdOntoBlob = event.data;
    1.61 +          // Send any message so we can serialize and keep our runtime behaviour
    1.62 +          // consistent.
    1.63 +          postMessage('kung fu death grip established');
    1.64 +        }
    1.65 +      }
    1.66 +
    1.67 +      var url =
    1.68 +        URL.createObjectURL(new Blob(["(", workerScript.toSource(), ")()"]));
    1.69 +
    1.70 +      // Keep a reference to the worker on the window.
    1.71 +      var worker = window.worker = new Worker(url);
    1.72 +      worker.postMessage(compositeBlob);
    1.73 +      worker.onmessage = workerLatchedBlobDeleteFromDB;
    1.74 +    }
    1.75 +
    1.76 +    function workerLatchedBlobDeleteFromDB() {
    1.77 +      // Delete the reference to the Blob from the database leaving the worker
    1.78 +      // thread reference as the only live reference once a GC has cleaned
    1.79 +      // out our references that we sent to the worker.  The page that owns
    1.80 +      // us triggers a GC just for that reason.
    1.81 +      var objectStore = db.transaction("foo", "readwrite").objectStore("foo");
    1.82 +      objectStore.delete(42).onsuccess = closeDBTellOwningThread;
    1.83 +    }
    1.84 +
    1.85 +    function closeDBTellOwningThread(event) {
    1.86 +      // Now that worker has latched the blob, clean up the database.
    1.87 +      db.close();
    1.88 +      db = null;
    1.89 +      report('ready');
    1.90 +    }
    1.91 +
    1.92 +    createDatastore();
    1.93 +  }
    1.94 +  </script>
    1.95 +
    1.96 +</head>
    1.97 +
    1.98 +<body onload="runIndexedDBTest();">
    1.99 +</body>
   1.100 +
   1.101 +</html>

mercurial