Bug 1269347 - Part1 Expose the optional embedded webextension as a builtin SDK module. r=krizsa
This commit integrates the `webExtension` bootstrap method parameter, which can is
set from the XPIProvider when the `hasEmbeddedWebExtension` property is true in the addon
install.rdf, into a new "sdk/webextension" SDK module, to make the creation of
SDK hybrid addons easier.
MozReview-Commit-ID: 4OUPZZyBPJv
--- a/addon-sdk/moz.build
+++ b/addon-sdk/moz.build
@@ -178,16 +178,20 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] != "gonk
'source/lib/sdk/windows/tabs-fennec.js',
]
EXTRA_JS_MODULES.commonjs += [
'source/lib/index.js',
'source/lib/test.js',
]
+EXTRA_JS_MODULES.commonjs.sdk += [
+ 'source/lib/sdk/webextension.js',
+]
+
EXTRA_JS_MODULES.commonjs.dev += [
'source/lib/dev/debuggee.js',
'source/lib/dev/frame-script.js',
'source/lib/dev/panel.js',
'source/lib/dev/ports.js',
'source/lib/dev/theme.js',
'source/lib/dev/toolbox.js',
'source/lib/dev/utils.js',
--- a/addon-sdk/source/lib/sdk/addon/bootstrap.js
+++ b/addon-sdk/source/lib/sdk/addon/bootstrap.js
@@ -118,27 +118,30 @@ Bootstrap.prototype = {
paths: Object.assign({
"": "resource://gre/modules/commonjs/",
"devtools/": "resource://devtools/",
"./": baseURI
}, readPaths(id)),
manifest: metadata,
metadata: metadata,
modules: {
- "@test/options": {}
+ "@test/options": {},
},
noQuit: prefs.get(`extensions.${id}.sdk.test.no-quit`, false)
});
self.loader = loader;
const module = Module("package.json", `${baseURI}package.json`);
const require = Require(loader, module);
const main = command === "test" ? "sdk/test/runner" : null;
const prefsURI = `${baseURI}defaults/preferences/prefs.js`;
+ // Init the 'sdk/webextension' module from the bootstrap addon parameter.
+ require("sdk/webextension").initFromBootstrapAddonParam(addon);
+
const { startup } = require("sdk/addon/runner");
startup(reason, {loader, main, prefsURI});
}.bind(this)).catch(error => {
console.error(`Failed to start ${id} addon`, error);
throw error;
});
},
shutdown(addon, code) {
new file mode 100644
--- /dev/null
+++ b/addon-sdk/source/lib/sdk/webextension.js
@@ -0,0 +1,43 @@
+/* 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"
+};
+
+let webExtension;
+let waitForWebExtensionAPI;
+
+module.exports = {
+ initFromBootstrapAddonParam(data) {
+ if (webExtension) {
+ throw new Error("'sdk/webextension' module has been already initialized");
+ }
+
+ webExtension = data.webExtension;
+ },
+
+ startup() {
+ if (!webExtension) {
+ return Promise.reject(new Error(
+ "'sdk/webextension' module is currently disabled. " +
+ "('hasEmbeddedWebExtension' option is missing or set to false)"
+ ));
+ }
+
+ // NOTE: calling `startup` more than once raises an "Embedded Extension already started"
+ // error, but given that SDK addons are going to have access to the startup method through
+ // an SDK module that can be required in any part of the addon, it will be nicer if any
+ // additional startup calls return the startup promise instead of raising an exception,
+ // so that the SDK addon can access the API object in the other addon modules without the
+ // need to manually pass this promise around.
+ if (!waitForWebExtensionAPI) {
+ waitForWebExtensionAPI = webExtension.startup();
+ }
+
+ return waitForWebExtensionAPI;
+ }
+};