file_app_isolation.html 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. foobar!
  5. </body>
  6. <script>
  7. var data = [
  8. { id: "0", name: "foo" },
  9. ];
  10. var action = window.location.search.substring(1);
  11. var finished = false;
  12. var created = false; // We use that for 'read-no' action.
  13. function finish(value) {
  14. value ? alert('success') : alert('failure');
  15. finished = true;
  16. }
  17. var request = window.indexedDB.open('AppIsolationTest');
  18. request.onupgradeneeded = function(event) {
  19. if (finished) {
  20. finish(false);
  21. return;
  22. }
  23. switch (action) {
  24. case 'read-no':
  25. created = true;
  26. break;
  27. case 'read-yes':
  28. finish(false);
  29. break;
  30. case 'write':
  31. created = true;
  32. var db = event.target.result;
  33. var objectStore = db.createObjectStore("test", { keyPath: "id" });
  34. for (var i in data) {
  35. objectStore.add(data[i]);
  36. }
  37. break;
  38. }
  39. }
  40. request.onsuccess = function(event) {
  41. if (finished) {
  42. finish(false);
  43. return;
  44. }
  45. var db = event.target.result;
  46. // Think about close the db!
  47. switch (action) {
  48. case 'read-no':
  49. db.close();
  50. if (created) { // That means we have created it.
  51. indexedDB.deleteDatabase('AppIsolationTest').onsuccess = function() {
  52. finish(true);
  53. };
  54. } else {
  55. finish(false);
  56. }
  57. break;
  58. case 'read-yes':
  59. db.transaction("test").objectStore("test").get("0").onsuccess = function(event) {
  60. var name = event.target.result.name;
  61. db.close();
  62. indexedDB.deleteDatabase('AppIsolationTest').onsuccess = function() {
  63. finish(name == 'foo');
  64. };
  65. };
  66. break;
  67. case 'write':
  68. db.close();
  69. // Success only if the db was actually created.
  70. finish(created);
  71. break;
  72. }
  73. };
  74. </script>
  75. </html>