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