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/. */
7 var EXPORTED_SYMBOLS = ["BootstrapLoader"];
9 const {AddonManager} = ChromeUtils.import("resource://gre/modules/AddonManager.jsm");
10 const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
12 XPCOMUtils.defineLazyModuleGetters(this, {
13 AddonInternal: "resource://gre/modules/addons/XPIDatabase.jsm",
14 Blocklist: "resource://gre/modules/Blocklist.jsm",
15 ConsoleAPI: "resource://gre/modules/Console.jsm",
16 InstallRDF: "resource://legacy/RDFManifestConverter.jsm",
17 Services: "resource://gre/modules/Services.jsm",
20 XPCOMUtils.defineLazyGetter(this, "BOOTSTRAP_REASONS", () => {
21 const {XPIProvider} = ChromeUtils.import("resource://gre/modules/addons/XPIProvider.jsm");
22 return XPIProvider.BOOTSTRAP_REASONS;
25 const {Log} = ChromeUtils.import("resource://gre/modules/Log.jsm");
26 var logger = Log.repository.getLogger("addons.bootstrap");
29 * Valid IDs fit this pattern.
31 var gIDTest = /^(\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}|[a-z0-9-\._]*\@[a-z0-9-\._]+)$/i;
33 // Properties that exist in the install manifest
34 const PROP_METADATA = ["id", "version", "type", "internalName", "updateURL",
35 "optionsURL", "optionsType", "aboutURL", "iconURL"];
36 const PROP_LOCALE_SINGLE = ["name", "description", "creator", "homepageURL"];
37 const PROP_LOCALE_MULTI = ["developers", "translators", "contributors"];
39 // Map new string type identifiers to old style nsIUpdateItem types.
41 // 32 = multipackage xpi file
51 const COMPATIBLE_BY_DEFAULT_TYPES = {
56 const hasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
58 function isXPI(filename) {
59 let ext = filename.slice(-4).toLowerCase();
60 return ext === ".xpi" || ext === ".zip";
64 * Gets an nsIURI for a file within another file, either a directory or an XPI
65 * file. If aFile is a directory then this will return a file: URI, if it is an
66 * XPI file then it will return a jar: URI.
68 * @param {nsIFile} aFile
69 * The file containing the resources, must be either a directory or an
71 * @param {string} aPath
72 * The path to find the resource at, "/" separated. If aPath is empty
73 * then the uri to the root of the contained files will be returned
75 * An nsIURI pointing at the resource
77 function getURIForResourceInFile(aFile, aPath) {
78 if (!isXPI(aFile.leafName)) {
79 let resource = aFile.clone();
81 aPath.split("/").forEach(part => resource.append(part));
83 return Services.io.newFileURI(resource);
86 return buildJarURI(aFile, aPath);
90 * Creates a jar: URI for a file inside a ZIP file.
92 * @param {nsIFile} aJarfile
93 * The ZIP file as an nsIFile
94 * @param {string} aPath
95 * The path inside the ZIP file
97 * An nsIURI for the file
99 function buildJarURI(aJarfile, aPath) {
100 let uri = Services.io.newFileURI(aJarfile);
101 uri = "jar:" + uri.spec + "!/" + aPath;
102 return Services.io.newURI(uri);
105 var BootstrapLoader = {
107 manifestFile: "install.rdf",
108 async loadManifest(pkg) {
110 * Reads locale properties from either the main install manifest root or
111 * an em:localized section in the install manifest.
113 * @param {Object} aSource
114 * The resource to read the properties from.
115 * @param {boolean} isDefault
116 * True if the locale is to be read from the main install manifest
118 * @param {string[]} aSeenLocales
119 * An array of locale names already seen for this install manifest.
120 * Any locale names seen as a part of this function will be added to
123 * an object containing the locale properties
125 function readLocale(aSource, isDefault, aSeenLocales) {
129 for (let localeName of aSource.locales || []) {
131 logger.warn("Ignoring empty locale in localized properties");
134 if (aSeenLocales.includes(localeName)) {
135 logger.warn("Ignoring duplicate locale in localized properties");
138 aSeenLocales.push(localeName);
139 locale.locales.push(localeName);
142 if (locale.locales.length == 0) {
143 logger.warn("Ignoring localized properties with no listed locales");
148 for (let prop of [...PROP_LOCALE_SINGLE, ...PROP_LOCALE_MULTI]) {
149 if (hasOwnProperty(aSource, prop)) {
150 locale[prop] = aSource[prop];
157 let manifestData = await pkg.readString("install.rdf");
158 let manifest = InstallRDF.loadFromString(manifestData).decode();
160 let addon = new AddonInternal();
161 for (let prop of PROP_METADATA) {
162 if (hasOwnProperty(manifest, prop)) {
163 addon[prop] = manifest[prop];
168 addon.type = "extension";
170 let type = addon.type;
172 for (let name in TYPES) {
173 if (TYPES[name] == type) {
180 if (!(addon.type in TYPES))
181 throw new Error("Install manifest specifies unknown type: " + addon.type);
184 throw new Error("No ID in install manifest");
185 if (!gIDTest.test(addon.id))
186 throw new Error("Illegal add-on ID " + addon.id);
188 throw new Error("No version in install manifest");
190 addon.strictCompatibility = (!(addon.type in COMPATIBLE_BY_DEFAULT_TYPES) ||
191 manifest.strictCompatibility == "true");
193 // Only read these properties for extensions.
194 if (addon.type == "extension") {
195 if (manifest.bootstrap != "true") {
196 throw new Error("Non-restartless extensions no longer supported");
199 if (addon.optionsType &&
200 addon.optionsType != AddonManager.OPTIONS_TYPE_INLINE_BROWSER &&
201 addon.optionsType != AddonManager.OPTIONS_TYPE_TAB) {
202 throw new Error("Install manifest specifies unknown optionsType: " + addon.optionsType);
205 // Convert legacy dictionaries into a format the WebExtension
206 // dictionary loader can process.
207 if (addon.type === "dictionary") {
209 let dictionaries = {};
210 await pkg.iterFiles(({path}) => {
211 let match = /^dictionaries\/([^\/]+)\.dic$/.exec(path);
213 let lang = match[1].replace(/_/g, "-");
214 dictionaries[lang] = match[0];
217 addon.startupData = {dictionaries};
220 // Only extensions are allowed to provide an optionsURL, optionsType,
221 // optionsBrowserStyle, or aboutURL. For all other types they are silently ignored
222 addon.aboutURL = null;
223 addon.optionsBrowserStyle = null;
224 addon.optionsType = null;
225 addon.optionsURL = null;
228 addon.defaultLocale = readLocale(manifest, true);
230 let seenLocales = [];
232 for (let localeData of manifest.localized || []) {
233 let locale = readLocale(localeData, false, seenLocales);
235 addon.locales.push(locale);
238 let dependencies = new Set(manifest.dependencies);
239 addon.dependencies = Object.freeze(Array.from(dependencies));
241 let seenApplications = [];
242 addon.targetApplications = [];
243 for (let targetApp of manifest.targetApplications || []) {
244 if (!targetApp.id || !targetApp.minVersion ||
245 !targetApp.maxVersion) {
246 logger.warn("Ignoring invalid targetApplication entry in install manifest");
249 if (seenApplications.includes(targetApp.id)) {
250 logger.warn("Ignoring duplicate targetApplication entry for " + targetApp.id +
251 " in install manifest");
254 seenApplications.push(targetApp.id);
255 addon.targetApplications.push(targetApp);
258 // Note that we don't need to check for duplicate targetPlatform entries since
259 // the RDF service coalesces them for us.
260 addon.targetPlatforms = [];
261 for (let targetPlatform of manifest.targetPlatforms || []) {
267 let pos = targetPlatform.indexOf("_");
269 platform.os = targetPlatform.substring(0, pos);
270 platform.abi = targetPlatform.substring(pos + 1);
272 platform.os = targetPlatform;
275 addon.targetPlatforms.push(platform);
278 addon.userDisabled = false;
279 addon.softDisabled = addon.blocklistState == Blocklist.STATE_SOFTBLOCKED;
280 addon.applyBackgroundUpdates = AddonManager.AUTOUPDATE_DEFAULT;
282 addon.userPermissions = null;
285 if (await pkg.hasResource("icon.png")) {
286 addon.icons[32] = "icon.png";
287 addon.icons[48] = "icon.png";
290 if (await pkg.hasResource("icon64.png")) {
291 addon.icons[64] = "icon64.png";
298 let file = addon.file || addon._sourceBundle;
299 let uri = getURIForResourceInFile(file, "bootstrap.js").spec;
300 let principal = Services.scriptSecurityManager.getSystemPrincipal();
302 let sandbox = new Cu.Sandbox(principal, {
305 wantGlobalProperties: ["ChromeUtils"],
306 metadata: { addonID: addon.id, URI: uri },
310 Object.assign(sandbox, BOOTSTRAP_REASONS);
312 XPCOMUtils.defineLazyGetter(sandbox, "console", () =>
313 new ConsoleAPI({ consoleID: `addon/${addon.id}` }));
315 Services.scriptloader.loadSubScript(uri, sandbox);
317 logger.warn(`Error loading bootstrap.js for ${addon.id}`, e);
320 function findMethod(name) {
322 return sandbox[name];
326 let method = Cu.evalInSandbox(name, sandbox);
331 logger.warn(`Add-on ${addon.id} is missing bootstrap method ${name}`);
335 let install = findMethod("install");
336 let uninstall = findMethod("uninstall");
337 let startup = findMethod("startup");
338 let shutdown = findMethod("shutdown");
341 install: (...args) => install(...args),
345 // Forget any cached files we might've had from this extension.
346 Services.obs.notifyObservers(null, "startupcache-invalidate");
350 if (addon.type == "extension") {
351 logger.debug(`Registering manifest for ${file.path}\n`);
352 Components.manager.addBootstrappedManifestLocation(file);
354 return startup(...args);
357 shutdown(data, reason) {
359 return shutdown(data, reason);
363 if (reason != BOOTSTRAP_REASONS.APP_SHUTDOWN) {
364 logger.debug(`Removing manifest for ${file.path}\n`);
365 Components.manager.removeBootstrappedManifestLocation(file);