addon-sdk/source/lib/sdk/indexed-db.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/indexed-db.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,58 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +module.metadata = {
    1.11 +  "stability": "experimental"
    1.12 +};
    1.13 +
    1.14 +const { Cc, Ci } = require("chrome");
    1.15 +const { id } = require("./self");
    1.16 +
    1.17 +// placeholder, copied from bootstrap.js
    1.18 +let sanitizeId = function(id){
    1.19 +  let uuidRe =
    1.20 +    /^\{([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\}$/;
    1.21 +
    1.22 +  let domain = id.
    1.23 +    toLowerCase().
    1.24 +    replace(/@/g, "-at-").
    1.25 +    replace(/\./g, "-dot-").
    1.26 +    replace(uuidRe, "$1");
    1.27 +
    1.28 +  return domain
    1.29 +};
    1.30 +
    1.31 +const PSEUDOURI = "indexeddb://" + sanitizeId(id) // https://bugzilla.mozilla.org/show_bug.cgi?id=779197
    1.32 +
    1.33 +// Firefox 26 and earlier releases don't support `indexedDB` in sandboxes
    1.34 +// automatically, so we need to inject `indexedDB` to `this` scope ourselves.
    1.35 +if (typeof(indexedDB) === "undefined") {
    1.36 +  Cc["@mozilla.org/dom/indexeddb/manager;1"].
    1.37 +    getService(Ci.nsIIndexedDatabaseManager).
    1.38 +    initWindowless(this);
    1.39 +
    1.40 +  // Firefox 14 gets this with a prefix
    1.41 +  if (typeof(indexedDB) === "undefined")
    1.42 +    this.indexedDB = mozIndexedDB;
    1.43 +}
    1.44 +
    1.45 +// Use XPCOM because `require("./url").URL` doesn't expose the raw uri object.
    1.46 +let principaluri = Cc["@mozilla.org/network/io-service;1"].
    1.47 +              getService(Ci.nsIIOService).
    1.48 +              newURI(PSEUDOURI, null, null);
    1.49 +
    1.50 +let principal = Cc["@mozilla.org/scriptsecuritymanager;1"].
    1.51 +	               getService(Ci.nsIScriptSecurityManager).
    1.52 +	               getCodebasePrincipal(principaluri);
    1.53 +
    1.54 +exports.indexedDB = Object.freeze({
    1.55 +  open: indexedDB.openForPrincipal.bind(indexedDB, principal),
    1.56 +  deleteDatabase: indexedDB.deleteForPrincipal.bind(indexedDB, principal),
    1.57 +  cmp: indexedDB.cmp
    1.58 +});
    1.59 +
    1.60 +exports.IDBKeyRange = IDBKeyRange;
    1.61 +exports.DOMException = Ci.nsIDOMDOMException;

mercurial