Bug 1269347 - Part1 Expose the optional embedded webextension as a builtin SDK module. r=krizsa draft
authorLuca Greco <lgreco@mozilla.com>
Mon, 19 Sep 2016 13:58:50 +0200
changeset 415067 32ac6eaa9603604bb110c494019e6c1523c1e731
parent 415066 fc69febcbf6c0dcc4b3dfc7a346d8d348798a65f
child 415068 287501db8707de2679d3dfffb23555319c706593
push id29785
push userluca.greco@alcacoop.it
push dateMon, 19 Sep 2016 12:08:18 +0000
reviewerskrizsa
bugs1269347
milestone51.0a1
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
addon-sdk/moz.build
addon-sdk/source/lib/sdk/addon/bootstrap.js
addon-sdk/source/lib/sdk/webextension.js
--- 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;
+  }
+};