summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/lib/sdk/indexed-db.js
blob: d4d166c02208feaa0ff708a7badc546978950491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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;