Bug 1246030 - Allow reloading an add-on in about:debugging. r?ochameau draft
authorKumar McMillan <kumar.mcmillan@gmail.com>
Thu, 31 Mar 2016 12:01:02 -0500
changeset 348538 c2fa8bd0bf2ae8474d307c051e5f22b6b7fd5e8a
parent 348168 626461a96c266c821e7bc9cfef173cd5604e4e1b
child 517862 12a08c84c47533dafed80d6c2fc133d39d2c152b
push id14839
push userbmo:kumar.mcmillan@gmail.com
push dateThu, 07 Apr 2016 17:16:40 +0000
reviewersochameau
bugs1246030
milestone48.0a1
Bug 1246030 - Allow reloading an add-on in about:debugging. r?ochameau MozReview-Commit-ID: Lh2iwPgmlhU
devtools/client/aboutdebugging/components/addon-target.js
devtools/client/aboutdebugging/components/addons-tab.js
devtools/client/aboutdebugging/test/browser.ini
devtools/client/aboutdebugging/test/browser_addons_install.js
devtools/client/aboutdebugging/test/browser_addons_reload.js
devtools/client/aboutdebugging/test/browser_addons_toggle_debug.js
devtools/client/aboutdebugging/test/head.js
devtools/client/locales/en-US/aboutdebugging.properties
devtools/server/actors/addon.js
--- a/devtools/client/aboutdebugging/components/addon-target.js
+++ b/devtools/client/aboutdebugging/components/addon-target.js
@@ -31,17 +31,32 @@ module.exports = createClass({
       }),
       dom.div({ className: "target-details" },
         dom.div({ className: "target-name" }, target.name)
       ),
       dom.button({
         className: "debug-button",
         onClick: this.debug,
         disabled: debugDisabled,
-      }, Strings.GetStringFromName("debug"))
+      }, Strings.GetStringFromName("debug")),
+      dom.button({
+        className: "reload-button",
+        onClick: this.reloadAddon
+      }, Strings.GetStringFromName("reload"))
     );
   },
 
   debug() {
     let { target } = this.props;
     BrowserToolboxProcess.init({ addonID: target.addonID });
   },
+
+  reloadAddon() {
+    let { client, target } = this.props;
+    client.request({
+      to: target.addonActor,
+      type: "reload"
+    }).then(() => {}, (error) => {
+      throw new Error(
+        "Error reloading addon " + target.addonID + ": " + error);
+    });
+  },
 });
--- a/devtools/client/aboutdebugging/components/addons-tab.js
+++ b/devtools/client/aboutdebugging/components/addons-tab.js
@@ -77,27 +77,31 @@ module.exports = createClass({
     let debugDisabled =
       !Services.prefs.getBoolPref(CHROME_ENABLED_PREF) ||
       !Services.prefs.getBoolPref(REMOTE_ENABLED_PREF);
 
     this.setState({ debugDisabled });
   },
 
   updateAddonsList() {
-    AddonManager.getAllAddons(addons => {
-      let extensions = addons.filter(addon => addon.isDebuggable).map(addon => {
-        return {
-          name: addon.name,
-          icon: addon.iconURL || ExtensionIcon,
-          addonID: addon.id
-        };
+    this.props.client.listAddons()
+      .then(({addons}) => {
+        let extensions = addons.filter(addon => addon.debuggable).map(addon => {
+          return {
+            name: addon.name,
+            icon: addon.iconURL || ExtensionIcon,
+            addonID: addon.id,
+            addonActor: addon.actor
+          };
+        });
+
+        this.setState({ extensions });
+      }, error => {
+        throw new Error("Client error while listing addons: " + error);
       });
-
-      this.setState({ extensions });
-    });
   },
 
   /**
    * Mandatory callback as AddonManager listener.
    */
   onInstalled() {
     this.updateAddonsList();
   },
--- a/devtools/client/aboutdebugging/test/browser.ini
+++ b/devtools/client/aboutdebugging/test/browser.ini
@@ -7,14 +7,15 @@ support-files =
   addons/unpacked/install.rdf
   service-workers/empty-sw.html
   service-workers/empty-sw.js
   service-workers/push-sw.html
   service-workers/push-sw.js
 
 [browser_addons_debugging_initial_state.js]
 [browser_addons_install.js]
+[browser_addons_reload.js]
 [browser_addons_toggle_debug.js]
 [browser_service_workers.js]
 [browser_service_workers_push.js]
 [browser_service_workers_start.js]
 [browser_service_workers_timeout.js]
 skip-if = true # Bug 1232931
--- a/devtools/client/aboutdebugging/test/browser_addons_install.js
+++ b/devtools/client/aboutdebugging/test/browser_addons_install.js
@@ -2,27 +2,35 @@
    http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
 const ADDON_ID = "test-devtools@mozilla.org";
 const ADDON_NAME = "test-devtools";
 
 add_task(function* () {
   let { tab, document } = yield openAboutDebugging("addons");
+  yield waitForInitialAddonList(document);
+
+  const addonWasInstalled = waitForMutation(
+    document.querySelector("#addons .targets"), { childList: true });
 
   yield installAddon(document, "addons/unpacked/install.rdf", "test-devtools");
+  yield addonWasInstalled;
 
   // Check that the addon appears in the UI
   let names = [...document.querySelectorAll("#addons .target-name")];
   names = names.map(element => element.textContent);
   ok(names.includes(ADDON_NAME),
     "The addon name appears in the list of addons: " + names);
 
   // Now uninstall this addon
+  const onAddonRemoved = waitForMutation(
+    document.querySelector("#addons .targets"), { childList: true });
   yield uninstallAddon(ADDON_ID);
+  yield onAddonRemoved;
 
   // Ensure that the UI removes the addon from the list
   names = [...document.querySelectorAll("#addons .target-name")];
   names = names.map(element => element.textContent);
   ok(!names.includes(ADDON_NAME),
     "After uninstall, the addon name disappears from the list of addons: "
     + names);
 
new file mode 100644
--- /dev/null
+++ b/devtools/client/aboutdebugging/test/browser_addons_reload.js
@@ -0,0 +1,71 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+"use strict";
+
+const ADDON_ID = "test-devtools@mozilla.org";
+const ADDON_NAME = "test-devtools";
+
+/**
+ * Returns a promise that resolves when the given add-on event is fired. The
+ * resolved value is an array of arguments passed for the event.
+ */
+function promiseAddonEvent(event) {
+  return new Promise(resolve => {
+    let listener = {
+      [event]: function(...args) {
+        AddonManager.removeAddonListener(listener);
+        resolve(args);
+      }
+    };
+
+    AddonManager.addAddonListener(listener);
+  });
+}
+
+add_task(function* () {
+  const { tab, document } = yield openAboutDebugging("addons");
+  yield waitForInitialAddonList(document);
+
+  info("Install a test addon.");
+
+  const addonWasInstalled = waitForMutation(
+    document.querySelector("#addons .targets"), { childList: true });
+
+  yield installAddon(document, "addons/unpacked/install.rdf", ADDON_NAME);
+  yield addonWasInstalled;
+
+  // Retrieve the Reload button.
+  const names = [...document.querySelectorAll("#addons .target-name")];
+  const name = names.filter(element => element.textContent === ADDON_NAME)[0];
+  ok(name, "Found " + ADDON_NAME + " add-on in the list");
+  const targetElement = name.parentNode.parentNode;
+  const reloadButton = targetElement.querySelector(".reload-button");
+  ok(reloadButton, "Found its reload button");
+
+  const onDisabled = promiseAddonEvent("onDisabled");
+  const onEnabled = promiseAddonEvent("onEnabled");
+
+  const onBootstrapInstallCalled = new Promise(done => {
+    Services.obs.addObserver(function listener() {
+      Services.obs.removeObserver(listener, ADDON_NAME, false);
+      ok(true, "Add-on was installed: " + ADDON_NAME);
+      done();
+    }, ADDON_NAME, false);
+  });
+
+  reloadButton.click();
+
+  const [disabledAddon] = yield onDisabled;
+  ok(disabledAddon.name === ADDON_NAME,
+     "Add-on was disabled: " + disabledAddon.name);
+
+  const [enabledAddon] = yield onEnabled;
+  ok(enabledAddon.name === ADDON_NAME,
+     "Add-on was re-enabled: " + enabledAddon.name);
+
+  yield onBootstrapInstallCalled;
+
+  info("Uninstall addon installed earlier.");
+  yield uninstallAddon(ADDON_ID);
+  yield closeAboutDebugging(tab);
+});
--- a/devtools/client/aboutdebugging/test/browser_addons_toggle_debug.js
+++ b/devtools/client/aboutdebugging/test/browser_addons_toggle_debug.js
@@ -14,16 +14,17 @@ add_task(function* () {
     let options = {"set": [
       ["devtools.chrome.enabled", false],
       ["devtools.debugger.remote-enabled", false],
     ]};
     SpecialPowers.pushPrefEnv(options, resolve);
   });
 
   let { tab, document } = yield openAboutDebugging("addons");
+  yield waitForInitialAddonList(document);
 
   info("Install a test addon.");
   yield installAddon(document, "addons/unpacked/install.rdf", "test-devtools");
 
   let addonDebugCheckbox = document.querySelector("#enable-addon-debugging");
   ok(!addonDebugCheckbox.checked, "Addons debugging should be disabled.");
 
   info("Check all debug buttons are disabled.");
--- a/devtools/client/aboutdebugging/test/head.js
+++ b/devtools/client/aboutdebugging/test/head.js
@@ -1,16 +1,17 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 /* eslint-env browser */
 /* eslint-disable mozilla/no-cpows-in-tests */
 /* exported openAboutDebugging, closeAboutDebugging, installAddon,
    uninstallAddon, waitForMutation, assertHasTarget,
-   waitForServiceWorkerRegistered, unregisterServiceWorker */
+   waitForInitialAddonList, waitForServiceWorkerRegistered,
+   unregisterServiceWorker */
 /* global sendAsyncMessage */
 
 "use strict";
 
 var { utils: Cu, classes: Cc, interfaces: Ci } = Components;
 
 const { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
 const { AddonManager } = Cu.import("resource://gre/modules/AddonManager.jsm", {});
@@ -129,16 +130,30 @@ function uninstallAddon(addonId) {
       };
       AddonManager.addAddonListener(listener);
       addon.uninstall();
     });
   });
 }
 
 /**
+ * Returns a promise that will resolve when the add-on list has been updated.
+ *
+ * @param {Node} document
+ * @return {Promise}
+ */
+function waitForInitialAddonList(document) {
+  return new Promise(resolve => {
+    // Wait for actor to load initial list of add-ons.
+    resolve(waitForMutation(document.querySelector("#addons .targets"),
+                            { childList: true }));
+  });
+}
+
+/**
  * Returns a promise that will resolve after receiving a mutation matching the
  * provided mutation options on the provided target.
  * @param {Node} target
  * @param {Object} mutationOptions
  * @return {Promise}
  */
 function waitForMutation(target, mutationOptions) {
   return new Promise(resolve => {
--- a/devtools/client/locales/en-US/aboutdebugging.properties
+++ b/devtools/client/locales/en-US/aboutdebugging.properties
@@ -8,16 +8,17 @@ start = Start
 
 addons = Add-ons
 addonDebugging.label = Enable add-on debugging
 addonDebugging.tooltip = Turning this on will allow you to debug add-ons and various other parts of the browser chrome
 addonDebugging.moreInfo = more info
 loadTemporaryAddon = Load Temporary Add-on
 extensions = Extensions
 selectAddonFromFile2 = Select Manifest File or Package (.xpi)
+reload = Reload
 
 workers = Workers
 serviceWorkers = Service Workers
 sharedWorkers = Shared Workers
 otherWorkers = Other Workers
 
 nothing = Nothing yet.
 
--- a/devtools/server/actors/addon.js
+++ b/devtools/server/actors/addon.js
@@ -78,16 +78,17 @@ BrowserAddonActor.prototype = {
       this._contextPool.addActor(this._consoleActor);
     }
 
     return {
       actor: this.actorID,
       id: this.id,
       name: this._addon.name,
       url: this.url,
+      iconURL: this._addon.iconURL,
       debuggable: this._addon.isDebuggable,
       consoleActor: this._consoleActor.actorID,
 
       traits: {
         highlightable: false,
         networkMonitor: false,
       },
     };
@@ -150,16 +151,23 @@ BrowserAddonActor.prototype = {
     this._contextPool.removeActor(this.threadActor);
 
     this.threadActor = null;
     this._sources = null;
 
     return { type: "detached" };
   },
 
+  onReload: function BAA_onReload() {
+    return this._addon.reload()
+      .then(() => {
+        return {}; // send an empty response
+      });
+  },
+
   preNest: function() {
     let e = Services.wm.getEnumerator(null);
     while (e.hasMoreElements()) {
       let win = e.getNext();
       let windowUtils = win.QueryInterface(Ci.nsIInterfaceRequestor)
                            .getInterface(Ci.nsIDOMWindowUtils);
       windowUtils.suppressEventHandling(true);
       windowUtils.suspendTimeouts();
@@ -244,17 +252,18 @@ BrowserAddonActor.prototype = {
    */
   _findDebuggees: function (dbg) {
     return dbg.findAllGlobals().filter(this._shouldAddNewGlobalAsDebuggee);
   }
 };
 
 BrowserAddonActor.prototype.requestTypes = {
   "attach": BrowserAddonActor.prototype.onAttach,
-  "detach": BrowserAddonActor.prototype.onDetach
+  "detach": BrowserAddonActor.prototype.onDetach,
+  "reload": BrowserAddonActor.prototype.onReload
 };
 
 /**
  * The AddonConsoleActor implements capabilities needed for the add-on web
  * console feature.
  *
  * @constructor
  * @param object aAddon