indexed-db.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. module.metadata = {
  6. "stability": "experimental"
  7. };
  8. const { Cc, Ci } = require("chrome");
  9. const { id } = require("./self");
  10. // placeholder, copied from bootstrap.js
  11. let sanitizeId = function(id){
  12. let uuidRe =
  13. /^\{([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\}$/;
  14. let domain = id.
  15. toLowerCase().
  16. replace(/@/g, "-at-").
  17. replace(/\./g, "-dot-").
  18. replace(uuidRe, "$1");
  19. return domain
  20. };
  21. const PSEUDOURI = "indexeddb://" + sanitizeId(id) // https://bugzilla.mozilla.org/show_bug.cgi?id=779197
  22. // Firefox 26 and earlier releases don't support `indexedDB` in sandboxes
  23. // automatically, so we need to inject `indexedDB` to `this` scope ourselves.
  24. if (typeof(indexedDB) === "undefined") {
  25. Cc["@mozilla.org/dom/indexeddb/manager;1"].
  26. getService(Ci.nsIIndexedDatabaseManager).
  27. initWindowless(this);
  28. // Firefox 14 gets this with a prefix
  29. if (typeof(indexedDB) === "undefined")
  30. this.indexedDB = mozIndexedDB;
  31. }
  32. // Use XPCOM because `require("./url").URL` doesn't expose the raw uri object.
  33. let principaluri = Cc["@mozilla.org/network/io-service;1"].
  34. getService(Ci.nsIIOService).
  35. newURI(PSEUDOURI, null, null);
  36. let principal = Cc["@mozilla.org/scriptsecuritymanager;1"].
  37. getService(Ci.nsIScriptSecurityManager).
  38. getCodebasePrincipal(principaluri);
  39. exports.indexedDB = Object.freeze({
  40. open: indexedDB.openForPrincipal.bind(indexedDB, principal),
  41. deleteDatabase: indexedDB.deleteForPrincipal.bind(indexedDB, principal),
  42. cmp: indexedDB.cmp
  43. });
  44. exports.IDBKeyRange = IDBKeyRange;
  45. exports.DOMException = Ci.nsIDOMDOMException;