Bug 1487078 - Copy addon helper to aboutdebugging-new;r=daisuke
authorJulian Descottes <jdescottes@mozilla.com>
Fri, 31 Aug 2018 08:12:16 +0000
changeset 482619 c42178e6c20793d921d770010bb2f1bdd6b2f2c6
parent 482618 bc09f31ad41fb40241111822c17e0ec0fbd00f28
child 482620 9ed022a889da3f522a18817ea9588e729a100717
push id232
push userfmarier@mozilla.com
push dateWed, 05 Sep 2018 20:45:54 +0000
reviewersdaisuke
bugs1487078
milestone63.0a1
Bug 1487078 - Copy addon helper to aboutdebugging-new;r=daisuke Depends on D4548 Differential Revision: https://phabricator.services.mozilla.com/D4646
devtools/client/aboutdebugging-new/src/actions/runtime.js
devtools/client/aboutdebugging-new/src/middleware/extension-component-data.js
devtools/client/aboutdebugging-new/src/modules/extensions-helper.js
devtools/client/aboutdebugging-new/src/modules/moz.build
devtools/client/aboutdebugging/modules/addon.js
devtools/client/aboutdebugging/test/test_addon_path.js
--- a/devtools/client/aboutdebugging-new/src/actions/runtime.js
+++ b/devtools/client/aboutdebugging-new/src/actions/runtime.js
@@ -8,17 +8,17 @@ const { AddonManager } = require("resour
 const { DebuggerClient } = require("devtools/shared/client/debugger-client");
 const { DebuggerServer } = require("devtools/server/main");
 const { gDevToolsBrowser } = require("devtools/client/framework/devtools-browser");
 
 const {
   debugLocalAddon,
   openTemporaryExtension,
   uninstallAddon,
-} = require("devtools/client/aboutdebugging/modules/addon");
+} = require("devtools/client/aboutdebugging-new/src/modules/extensions-helper");
 
 const {
   CONNECT_RUNTIME_FAILURE,
   CONNECT_RUNTIME_START,
   CONNECT_RUNTIME_SUCCESS,
   DEBUG_TARGETS,
   DISCONNECT_RUNTIME_FAILURE,
   DISCONNECT_RUNTIME_START,
--- a/devtools/client/aboutdebugging-new/src/middleware/extension-component-data.js
+++ b/devtools/client/aboutdebugging-new/src/middleware/extension-component-data.js
@@ -7,17 +7,17 @@
 const {
   DEBUG_TARGETS,
   REQUEST_EXTENSIONS_SUCCESS,
 } = require("../constants");
 
 const {
   getExtensionUuid,
   parseFileUri
-} = require("devtools/client/aboutdebugging/modules/addon");
+} = require("devtools/client/aboutdebugging-new/src/modules/extensions-helper");
 
 /**
  * This middleware converts extensions object that get from DebuggerClient.listAddons()
  * to data which is used in DebugTargetItem.
  */
 const extensionComponentDataMiddleware = store => next => action => {
   switch (action.type) {
     case REQUEST_EXTENSIONS_SUCCESS: {
copy from devtools/client/aboutdebugging/modules/addon.js
copy to devtools/client/aboutdebugging-new/src/modules/extensions-helper.js
--- a/devtools/client/aboutdebugging/modules/addon.js
+++ b/devtools/client/aboutdebugging-new/src/modules/extensions-helper.js
@@ -3,22 +3,21 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 const { Cc, Ci } = require("chrome");
 loader.lazyImporter(this, "BrowserToolboxProcess",
   "resource://devtools/client/framework/ToolboxProcess.jsm");
 loader.lazyImporter(this, "AddonManager", "resource://gre/modules/AddonManager.jsm");
-loader.lazyImporter(this, "AddonManagerPrivate", "resource://gre/modules/AddonManager.jsm");
 
-var {TargetFactory} = require("devtools/client/framework/target");
-var {Toolbox} = require("devtools/client/framework/toolbox");
+const {TargetFactory} = require("devtools/client/framework/target");
+const {Toolbox} = require("devtools/client/framework/toolbox");
 
-var {gDevTools} = require("devtools/client/framework/devtools");
+const {gDevTools} = require("devtools/client/framework/devtools");
 
 let browserToolboxProcess = null;
 let remoteAddonToolbox = null;
 function closeToolbox() {
   if (browserToolboxProcess) {
     browserToolboxProcess.close();
   }
 
@@ -77,33 +76,16 @@ exports.debugRemoteAddon = async functio
  * Uninstall the addon with the provided id.
  * Resolves when the addon shutdown has completed.
  */
 exports.uninstallAddon = async function(addonID) {
   const addon = await AddonManager.getAddonByID(addonID);
   return addon && addon.uninstall();
 };
 
-exports.isTemporaryID = function(addonID) {
-  return AddonManagerPrivate.isTemporaryInstallID(addonID);
-};
-
-exports.isLegacyTemporaryExtension = function(addonForm) {
-  if (!addonForm.type) {
-    // If about:debugging is connected to an older then 59 remote Firefox, and type is
-    // not available on the addon/webextension actors, return false to avoid showing
-    // irrelevant warning messages.
-    return false;
-  }
-  return addonForm.type == "extension" &&
-         addonForm.temporarilyInstalled &&
-         !addonForm.isWebExtension &&
-         !addonForm.isAPIExtension;
-};
-
 exports.parseFileUri = function(url) {
   // Strip a leading slash from Windows drive letter URIs.
   // file:///home/foo ~> /home/foo
   // file:///C:/foo ~> C:/foo
   const windowsRegex = /^file:\/\/\/([a-zA-Z]:\/.*)/;
   if (windowsRegex.test(url)) {
     return windowsRegex.exec(url)[1];
   }
--- a/devtools/client/aboutdebugging-new/src/modules/moz.build
+++ b/devtools/client/aboutdebugging-new/src/modules/moz.build
@@ -1,7 +1,8 @@
 # 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/.
 
 DevToolsModules(
+    'extensions-helper.js',
     'network-locations.js',
 )
--- a/devtools/client/aboutdebugging/modules/addon.js
+++ b/devtools/client/aboutdebugging/modules/addon.js
@@ -1,91 +1,30 @@
 /* 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";
 
-const { Cc, Ci } = require("chrome");
-loader.lazyImporter(this, "BrowserToolboxProcess",
-  "resource://devtools/client/framework/ToolboxProcess.jsm");
-loader.lazyImporter(this, "AddonManager", "resource://gre/modules/AddonManager.jsm");
 loader.lazyImporter(this, "AddonManagerPrivate", "resource://gre/modules/AddonManager.jsm");
 
-var {TargetFactory} = require("devtools/client/framework/target");
-var {Toolbox} = require("devtools/client/framework/toolbox");
-
-var {gDevTools} = require("devtools/client/framework/devtools");
-
-let browserToolboxProcess = null;
-let remoteAddonToolbox = null;
-function closeToolbox() {
-  if (browserToolboxProcess) {
-    browserToolboxProcess.close();
-  }
-
-  if (remoteAddonToolbox) {
-    remoteAddonToolbox.destroy();
-  }
-}
+const {
+  debugLocalAddon,
+  debugRemoteAddon,
+  getExtensionUuid,
+  openTemporaryExtension,
+  parseFileUri,
+  uninstallAddon,
+} = require("devtools/client/aboutdebugging-new/src/modules/extensions-helper");
 
 /**
- * Start debugging an addon in the current instance of Firefox.
- *
- * @param {String} addonID
- *        String id of the addon to debug.
- */
-exports.debugLocalAddon = async function(addonID) {
-  // Close previous addon debugging toolbox.
-  closeToolbox();
-
-  browserToolboxProcess = BrowserToolboxProcess.init({
-    addonID,
-    onClose: () => {
-      browserToolboxProcess = null;
-    }
-  });
-};
-
-/**
- * Start debugging an addon in a remote instance of Firefox.
- *
- * @param {Object} addonForm
- *        Necessary to create an addon debugging target.
- * @param {DebuggerClient} client
- *        Required for remote debugging.
+ * Most of the implementation for this module has been moved to
+ * devtools/client/aboutdebugging-new/src/modules/extensions-helper.js
+ * The only methods implemented here are the ones used in the old aboutdebugging only.
  */
-exports.debugRemoteAddon = async function(addonForm, client) {
-  // Close previous addon debugging toolbox.
-  closeToolbox();
-
-  const options = {
-    form: addonForm,
-    chrome: true,
-    client,
-    isBrowsingContext: addonForm.isWebExtension
-  };
-
-  const target = await TargetFactory.forRemoteTab(options);
-
-  const hostType = Toolbox.HostType.WINDOW;
-  remoteAddonToolbox = await gDevTools.showToolbox(target, null, hostType);
-  remoteAddonToolbox.once("destroy", () => {
-    remoteAddonToolbox = null;
-  });
-};
-
-/**
- * Uninstall the addon with the provided id.
- * Resolves when the addon shutdown has completed.
- */
-exports.uninstallAddon = async function(addonID) {
-  const addon = await AddonManager.getAddonByID(addonID);
-  return addon && addon.uninstall();
-};
 
 exports.isTemporaryID = function(addonID) {
   return AddonManagerPrivate.isTemporaryInstallID(addonID);
 };
 
 exports.isLegacyTemporaryExtension = function(addonForm) {
   if (!addonForm.type) {
     // If about:debugging is connected to an older then 59 remote Firefox, and type is
@@ -94,62 +33,19 @@ exports.isLegacyTemporaryExtension = fun
     return false;
   }
   return addonForm.type == "extension" &&
          addonForm.temporarilyInstalled &&
          !addonForm.isWebExtension &&
          !addonForm.isAPIExtension;
 };
 
-exports.parseFileUri = function(url) {
-  // Strip a leading slash from Windows drive letter URIs.
-  // file:///home/foo ~> /home/foo
-  // file:///C:/foo ~> C:/foo
-  const windowsRegex = /^file:\/\/\/([a-zA-Z]:\/.*)/;
-  if (windowsRegex.test(url)) {
-    return windowsRegex.exec(url)[1];
-  }
-  return url.slice("file://".length);
-};
-
-exports.getExtensionUuid = function(extension) {
-  const { manifestURL } = extension;
-  // Strip off the protocol and rest, leaving us with just the UUID.
-  return manifestURL ? /moz-extension:\/\/([^/]*)/.exec(manifestURL)[1] : null;
-};
+/**
+ * See JSDoc in devtools/client/aboutdebugging-new/src/modules/extensions-helper for all
+ * the methods exposed below.
+ */
 
-/**
- * Open a file picker to allow the user to locate a temporary extension. A temporary
- * extension can either be:
- * - a folder
- * - a .xpi file
- * - a .zip file
- *
- * @param  {Window} win
- *         The window object where the filepicker should be opened.
- *         Note: We cannot use the global window object here because it is undefined if
- *         this module is loaded from a file outside of devtools/client/aboutdebugging/.
- *         See browser-loader.js `uri.startsWith(baseURI)` for more details.
- * @param  {String} message
- *         The help message that should be displayed to the user in the filepicker.
- * @return {Promise} returns a promise that resolves a File object corresponding to the
- *         file selected by the user.
- */
-exports.openTemporaryExtension = function(win, message) {
-  return new Promise(resolve => {
-    const fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
-    fp.init(win, message, Ci.nsIFilePicker.modeOpen);
-    fp.open(res => {
-      if (res == Ci.nsIFilePicker.returnCancel || !fp.file) {
-        return;
-      }
-      let file = fp.file;
-      // AddonManager.installTemporaryAddon accepts either
-      // addon directory or final xpi file.
-      if (!file.isDirectory() &&
-          !file.leafName.endsWith(".xpi") && !file.leafName.endsWith(".zip")) {
-        file = file.parent;
-      }
-
-      resolve(file);
-    });
-  });
-};
+exports.debugLocalAddon = debugLocalAddon;
+exports.debugRemoteAddon = debugRemoteAddon;
+exports.getExtensionUuid = getExtensionUuid;
+exports.openTemporaryExtension = openTemporaryExtension;
+exports.parseFileUri = parseFileUri;
+exports.uninstallAddon = uninstallAddon;
--- a/devtools/client/aboutdebugging/test/test_addon_path.js
+++ b/devtools/client/aboutdebugging/test/test_addon_path.js
@@ -1,14 +1,14 @@
 /* global equal */
 
 "use strict";
 
 const { parseFileUri } =
-  require("devtools/client/aboutdebugging/modules/addon");
+  require("devtools/client/aboutdebugging-new/src/modules/extensions-helper");
 
 add_task(async function testParseFileUri() {
   equal(
     parseFileUri("file:///home/me/my-extension/"),
     "/home/me/my-extension/",
     "UNIX paths are supported");
 
   equal(