Bug 1368152: Part 1 - Move API extension registration to ExtensionAPI.jsm. r?aswan
MozReview-Commit-ID: 3kCXYXVxLRS
--- a/toolkit/components/extensions/ExtensionAPI.jsm
+++ b/toolkit/components/extensions/ExtensionAPI.jsm
@@ -5,17 +5,16 @@
"use strict";
this.EXPORTED_SYMBOLS = ["ExtensionAPI", "ExtensionAPIs"];
/* exported ExtensionAPIs */
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
-Cu.import("resource://gre/modules/ExtensionManagement.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ConsoleAPI",
"resource://gre/modules/Console.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "EventEmitter",
"resource://gre/modules/EventEmitter.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Schemas",
@@ -42,17 +41,17 @@ class ExtensionAPI {
}
getAPI(context) {
throw new Error("Not Implemented");
}
}
var ExtensionAPIs = {
- apis: ExtensionManagement.APIs.apis,
+ apis: new Map(),
load(apiName) {
let api = this.apis.get(apiName);
if (api.loadPromise) {
return api.loadPromise;
}
@@ -90,9 +89,25 @@ var ExtensionAPIs = {
let {schema} = api;
Schemas.unload(schema);
Cu.nukeSandbox(api.sandbox);
api.sandbox = null;
api.loadPromise = null;
},
+
+ register(namespace, schema, script) {
+ if (this.apis.has(namespace)) {
+ throw new Error(`API namespace already exists: ${namespace}`);
+ }
+
+ this.apis.set(namespace, {schema, script});
+ },
+
+ unregister(namespace) {
+ if (!this.apis.has(namespace)) {
+ throw new Error(`API namespace does not exist: ${namespace}`);
+ }
+
+ this.apis.delete(namespace);
+ },
};
--- a/toolkit/components/extensions/ExtensionManagement.jsm
+++ b/toolkit/components/extensions/ExtensionManagement.jsm
@@ -39,36 +39,16 @@ function parseScriptOptions(options) {
includeGlobs: options.include_globs && options.include_globs.map(glob => new MatchGlob(glob)),
excludeGlobs: options.include_globs && options.exclude_globs.map(glob => new MatchGlob(glob)),
jsPaths: options.js || [],
cssPaths: options.css || [],
};
}
-var APIs = {
- apis: new Map(),
-
- register(namespace, schema, script) {
- if (this.apis.has(namespace)) {
- throw new Error(`API namespace already exists: ${namespace}`);
- }
-
- this.apis.set(namespace, {schema, script});
- },
-
- unregister(namespace) {
- if (!this.apis.has(namespace)) {
- throw new Error(`API namespace does not exist: ${namespace}`);
- }
-
- this.apis.delete(namespace);
- },
-};
-
function getURLForExtension(id, path = "") {
let uuid = UUIDMap.get(id, false);
if (!uuid) {
Cu.reportError(`Called getURLForExtension on unmapped extension ${id}`);
return null;
}
return `moz-extension://${uuid}/${path}`;
}
@@ -113,18 +93,13 @@ var ExtensionManagement = {
policy.active = true;
},
// Called when an extension is unloaded.
shutdownExtension(extension) {
extension.policy.active = false;
},
- registerAPI: APIs.register.bind(APIs),
- unregisterAPI: APIs.unregister.bind(APIs),
-
getURLForExtension,
-
- APIs,
};
XPCOMUtils.defineLazyPreferenceGetter(ExtensionManagement, "useRemoteWebExtensions",
"extensions.webextensions.remote", false);
--- a/toolkit/components/extensions/test/xpcshell/test_ext_experiments.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_experiments.js
@@ -137,18 +137,18 @@ add_task(async function test_experiments
Services.obs.removeObserver(observer, "webext-api-loaded");
Services.obs.removeObserver(observer, "webext-api-hello");
});
// Install API add-on.
let apiAddon = await AddonManager.installTemporaryAddon(apiAddonFile);
- let {APIs} = Cu.import("resource://gre/modules/ExtensionManagement.jsm", {});
- ok(APIs.apis.has("meh"), "Should have meh API.");
+ let {ExtensionAPIs} = Cu.import("resource://gre/modules/ExtensionAPI.jsm", {});
+ ok(ExtensionAPIs.apis.has("meh"), "Should have meh API.");
// Install boring WebExtension add-on.
let boringAddon = await AddonManager.installTemporaryAddon(boringAddonFile);
await promiseAddonStartup();
// Install interesting WebExtension add-on.
--- a/toolkit/mozapps/extensions/internal/APIExtensionBootstrap.js
+++ b/toolkit/mozapps/extensions/internal/APIExtensionBootstrap.js
@@ -1,17 +1,17 @@
/* 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";
-/* exported startup, shutdown, install, uninstall */
+/* exported startup, shutdown, install, uninstall, ExtensionAPIs */
-Components.utils.import("resource://gre/modules/ExtensionManagement.jsm");
+Components.utils.import("resource://gre/modules/ExtensionAPI.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
var namespace;
var resource;
var resProto;
function install(data, reason) {
}
@@ -20,22 +20,22 @@ function startup(data, reason) {
namespace = data.id.replace(/@.*/, "");
resource = `extension-${namespace}-api`;
resProto = Services.io.getProtocolHandler("resource")
.QueryInterface(Components.interfaces.nsIResProtocolHandler);
resProto.setSubstitution(resource, data.resourceURI);
- ExtensionManagement.registerAPI(
+ ExtensionAPIs.register(
namespace,
`resource://${resource}/schema.json`,
`resource://${resource}/api.js`);
}
function shutdown(data, reason) {
resProto.setSubstitution(resource, null);
- ExtensionManagement.unregisterAPI(namespace);
+ ExtensionAPIs.unregister(namespace);
}
function uninstall(data, reason) {
}