summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/self.js
blob: c2114a92692ffcfc321092cbb62c3527c6e57a80 (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
/* 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": "stable"
};

const { CC } = require('chrome');
const options = require('@loader/options');

const { get } = require("./preferences/service");
const { readURISync } = require('./net/url');

const id = options.id;

const readPref = key => get("extensions." + id + ".sdk." + key);

const name = readPref("name") || options.name;
const version = readPref("version") || options.version;
const loadReason = readPref("load.reason") || options.loadReason;
const rootURI = readPref("rootURI") || options.rootURI || "";
const baseURI = readPref("baseURI") || options.prefixURI + name + "/"
const addonDataURI = baseURI + "data/";
const metadata = options.metadata || {};
const permissions = metadata.permissions || {};
const isPacked = rootURI && rootURI.indexOf("jar:") === 0;

const isPrivateBrowsingSupported = 'private-browsing' in permissions &&
                                   permissions['private-browsing'] === true;

const uri = (path="") =>
  path.includes(":") ? path : addonDataURI + path.replace(/^\.\//, "");

var preferencesBranch = ("preferences-branch" in metadata)
                            ? metadata["preferences-branch"]
                            : options.preferencesBranch

if (/[^\w{@}.-]/.test(preferencesBranch)) {
  preferencesBranch = id;
  console.warn("Ignoring preferences-branch (not a valid branch name)");
}

// Some XPCOM APIs require valid URIs as an argument for certain operations
// (see `nsILoginManager` for example). This property represents add-on
// associated unique URI string that can be used for that.
exports.uri = 'addon:' + id;
exports.id = id;
exports.preferencesBranch = preferencesBranch || id;
exports.name = name;
exports.loadReason = loadReason;
exports.version = version;
exports.packed = isPacked;
exports.data = Object.freeze({
  url: uri,
  load: function read(path) {
    return readURISync(uri(path));
  }
});
exports.isPrivateBrowsingSupported = isPrivateBrowsingSupported;