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