test_sandbox.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>indexedDB in JS Sandbox</title>
  5. <script src="/tests/SimpleTest/SimpleTest.js"></script>
  6. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"></link>
  7. </head>
  8. <body>
  9. <script type="application/javascript">
  10. SimpleTest.waitForExplicitFinish();
  11. // This runs inside a same-origin sandbox.
  12. // The intent being to show that the data store is the same.
  13. function storeValue() {
  14. function createDB_inner() {
  15. var op = indexedDB.open('db');
  16. op.onupgradeneeded = e => {
  17. var db = e.target.result;
  18. db.createObjectStore('store');
  19. };
  20. return new Promise(resolve => {
  21. op.onsuccess = e => resolve(e.target.result);
  22. });
  23. }
  24. function add(k, v) {
  25. return createDB_inner().then(db => {
  26. var tx = db.transaction('store', 'readwrite');
  27. var store = tx.objectStore('store');
  28. var op = store.add(v, k);
  29. return new Promise((resolve, reject) => {
  30. op.onsuccess = e => resolve(e.target.result);
  31. op.onerror = _ => reject(op.error);
  32. tx.onabort = _ => reject(tx.error);
  33. });
  34. });
  35. }
  36. return add('x', [ 10, {} ])
  37. .then(_ => step_done(),
  38. _ => ok(false, 'failed to store'));
  39. }
  40. function createDB_outer() {
  41. var op = indexedDB.open('db');
  42. op.onupgradeneeded = e => {
  43. ok(false, 'upgrade should not be needed');
  44. var db = e.target.result;
  45. db.createObjectStore('store');
  46. };
  47. return new Promise(resolve => {
  48. op.onsuccess = e => resolve(e.target.result);
  49. });
  50. }
  51. function get(k) {
  52. return createDB_outer().then(db => {
  53. var tx = db.transaction('store', 'readonly');
  54. var store = tx.objectStore('store');
  55. var op = store.get(k);
  56. return new Promise((resolve, reject) => {
  57. op.onsuccess = e => resolve(e.target.result);
  58. op.onerror = _ => reject(op.error);
  59. tx.onabort = _ => reject(tx.error);
  60. });
  61. });
  62. }
  63. function runInSandbox(sandbox, testFunc) {
  64. is(typeof testFunc, 'function');
  65. var resolvePromise;
  66. var testPromise = new Promise(r => resolvePromise = r);
  67. SpecialPowers.Cu.exportFunction(_ => resolvePromise(), sandbox,
  68. { defineAs: 'step_done' });
  69. SpecialPowers.Cu.evalInSandbox('(' + testFunc.toSource() + ')()' +
  70. '.then(step_done);', sandbox);
  71. return testPromise;
  72. }
  73. // Use the window principal for the sandbox; location.origin is not sufficient.
  74. var sb = new SpecialPowers.Cu.Sandbox(window,
  75. { wantGlobalProperties: ['indexedDB'] });
  76. sb.ok = SpecialPowers.Cu.exportFunction(ok, sb);
  77. Promise.resolve()
  78. .then(_ => runInSandbox(sb, storeValue))
  79. .then(_ => get('x'))
  80. .then(x => {
  81. ok(x, 'a value should be present');
  82. is(x.length, 2);
  83. is(x[0], 10);
  84. is(typeof x[1], 'object');
  85. is(Object.keys(x[1]).length, 0);
  86. })
  87. .then(_ => SimpleTest.finish());
  88. </script>
  89. </body>
  90. </html>