blob_worker_crash_iframe.html 3.0 KB

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