summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/indexed-db.js
diff options
context:
space:
mode:
authorMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
committerMatt A. Tobin <email@mattatobin.com>2018-02-09 06:46:43 -0500
commitac46df8daea09899ce30dc8fd70986e258c746bf (patch)
tree2750d3125fc253fd5b0671e4bd268eff1fd97296 /toolkit/jetpack/sdk/indexed-db.js
parent8cecf8d5208f3945b35f879bba3015bb1a11bec6 (diff)
downloadUXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.gz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.lz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.tar.xz
UXP-ac46df8daea09899ce30dc8fd70986e258c746bf.zip
Move Add-on SDK source to toolkit/jetpack
Diffstat (limited to 'toolkit/jetpack/sdk/indexed-db.js')
-rw-r--r--toolkit/jetpack/sdk/indexed-db.js79
1 files changed, 79 insertions, 0 deletions
diff --git a/toolkit/jetpack/sdk/indexed-db.js b/toolkit/jetpack/sdk/indexed-db.js
new file mode 100644
index 000000000..d4d166c02
--- /dev/null
+++ b/toolkit/jetpack/sdk/indexed-db.js
@@ -0,0 +1,79 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+module.metadata = {
+ "stability": "experimental"
+};
+
+const { Cc, Ci } = require("chrome");
+const { id } = require("./self");
+
+// placeholder, copied from bootstrap.js
+var sanitizeId = function(id){
+ let uuidRe =
+ /^\{([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\}$/;
+
+ let domain = id.
+ toLowerCase().
+ replace(/@/g, "-at-").
+ replace(/\./g, "-dot-").
+ replace(uuidRe, "$1");
+
+ return domain
+};
+
+const PSEUDOURI = "indexeddb://" + sanitizeId(id) // https://bugzilla.mozilla.org/show_bug.cgi?id=779197
+
+// Use XPCOM because `require("./url").URL` doesn't expose the raw uri object.
+var principaluri = Cc["@mozilla.org/network/io-service;1"].
+ getService(Ci.nsIIOService).
+ newURI(PSEUDOURI, null, null);
+
+var ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
+ .getService(Ci.nsIScriptSecurityManager);
+var principal = ssm.createCodebasePrincipal(principaluri, {});
+
+function toArray(args) {
+ return Array.prototype.slice.call(args);
+}
+
+function openInternal(args, forPrincipal, deleting) {
+ if (forPrincipal) {
+ args = toArray(args);
+ } else {
+ args = [principal].concat(toArray(args));
+ }
+ if (args.length == 2) {
+ args.push({ storage: "persistent" });
+ } else if (!deleting && args.length >= 3 && typeof args[2] === "number") {
+ args[2] = { version: args[2], storage: "persistent" };
+ }
+
+ if (deleting) {
+ return indexedDB.deleteForPrincipal.apply(indexedDB, args);
+ }
+
+ return indexedDB.openForPrincipal.apply(indexedDB, args);
+}
+
+exports.indexedDB = Object.freeze({
+ open: function () {
+ return openInternal(arguments, false, false);
+ },
+ deleteDatabase: function () {
+ return openInternal(arguments, false, true);
+ },
+ openForPrincipal: function () {
+ return openInternal(arguments, true, false);
+ },
+ deleteForPrincipal: function () {
+ return openInternal(arguments, true, true);
+ },
+ cmp: indexedDB.cmp.bind(indexedDB)
+});
+
+exports.IDBKeyRange = IDBKeyRange;
+exports.DOMException = Ci.nsIDOMDOMException;