blob_worker_crash_iframe.html (3064B)
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> 8 9 <script type="text/javascript"> 10 function report(result) { 11 var message = { source: "iframe" }; 12 message.result = result; 13 window.parent.postMessage(message, "*"); 14 } 15 16 function runIndexedDBTest() { 17 var db = null; 18 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 } 36 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 }, 42).onsuccess = refetchBlob; 42 } 43 44 function refetchBlob() { 45 var foo = db.transaction("foo").objectStore("foo"); 46 foo.get(42).onsuccess = fetchedBlobCreateWorkerAndSendBlob; 47 } 48 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" }); 53 54 function workerScript() { 55 /* eslint-env worker */ 56 onmessage = function(event) { 57 // Save the Blob to the worker's global scope. 58 self.holdOntoBlob = event.data; 59 // Send any message so we can serialize and keep our runtime behaviour 60 // consistent. 61 postMessage("kung fu death grip established"); 62 }; 63 } 64 65 var url = 66 URL.createObjectURL(new Blob(["(", workerScript.toString(), ")()"])); 67 68 // Keep a reference to the worker on the window. 69 var worker = window.worker = new Worker(url); 70 worker.postMessage(compositeBlob); 71 worker.onmessage = workerLatchedBlobDeleteFromDB; 72 } 73 74 function workerLatchedBlobDeleteFromDB() { 75 // Delete the reference to the Blob from the database leaving the worker 76 // thread reference as the only live reference once a GC has cleaned 77 // out our references that we sent to the worker. The page that owns 78 // us triggers a GC just for that reason. 79 var objectStore = db.transaction("foo", "readwrite").objectStore("foo"); 80 objectStore.delete(42).onsuccess = closeDBTellOwningThread; 81 } 82 83 function closeDBTellOwningThread() { 84 // Now that worker has latched the blob, clean up the database. 85 db.close(); 86 db = null; 87 report("ready"); 88 } 89 90 createDatastore(); 91 } 92 </script> 93 94 </head> 95 96 <body onload="runIndexedDBTest();"> 97 </body> 98 99 </html>