summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/blob_worker_crash_iframe.html
blob: 15d8277515c9b8c7001fefacc004940dd5cd7b8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
  <title>Indexed Database Test</title>

  <script type="text/javascript">
  function report(result) {
    var message = { source: "iframe" };
    message.result = result;
    window.parent.postMessage(message, "*");
  }

  function runIndexedDBTest() {
    var db = null;

    // Create the data-store
    function createDatastore() {
      try {
        var request = indexedDB.open(window.location.pathname, 1);
        request.onupgradeneeded = function(event) {
          event.target.result.createObjectStore("foo");
        };
        request.onsuccess = function(event) {
          db = event.target.result;
          createAndStoreBlob();
        };
      }
      catch (e) {
dump("EXCEPTION IN CREATION: " + e + "\n " + e.stack + "\n");
        report(false);
      }
    }

    function createAndStoreBlob() {
      const BLOB_DATA = ["fun ", "times ", "all ", "around!"];
      var blob = new Blob(BLOB_DATA, { type: "text/plain" });
      var objectStore = db.transaction("foo", "readwrite").objectStore("foo");
      objectStore.add({ blob }, 42).onsuccess = refetchBlob;
    }

    function refetchBlob() {
      var foo = db.transaction("foo").objectStore("foo");
      foo.get(42).onsuccess = fetchedBlobCreateWorkerAndSendBlob;
    }

    function fetchedBlobCreateWorkerAndSendBlob(event) {
      var idbBlob = event.target.result.blob;
      var compositeBlob = new Blob(["I like the following blob: ", idbBlob],
                                   { type: "text/fancy" });

      function workerScript() {
        /* eslint-env worker */
        onmessage = function(event) {
          // Save the Blob to the worker's global scope.
          self.holdOntoBlob = event.data;
          // Send any message so we can serialize and keep our runtime behaviour
          // consistent.
          postMessage("kung fu death grip established");
        };
      }

      var url =
        URL.createObjectURL(new Blob(["(", workerScript.toString(), ")()"]));

      // Keep a reference to the worker on the window.
      var worker = window.worker = new Worker(url);
      worker.postMessage(compositeBlob);
      worker.onmessage = workerLatchedBlobDeleteFromDB;
    }

    function workerLatchedBlobDeleteFromDB() {
      // Delete the reference to the Blob from the database leaving the worker
      // thread reference as the only live reference once a GC has cleaned
      // out our references that we sent to the worker.  The page that owns
      // us triggers a GC just for that reason.
      var objectStore = db.transaction("foo", "readwrite").objectStore("foo");
      objectStore.delete(42).onsuccess = closeDBTellOwningThread;
    }

    function closeDBTellOwningThread(event) {
      // Now that worker has latched the blob, clean up the database.
      db.close();
      db = null;
      report("ready");
    }

    createDatastore();
  }
  </script>

</head>

<body onload="runIndexedDBTest();">
</body>

</html>