Bug 1395535 - trigger addon reload from devtools-startup if devtools addon failed to start;r=ochameau draft
authorJulian Descottes <jdescottes@mozilla.com>
Tue, 05 Sep 2017 10:26:32 +0200
changeset 659091 aff9ca5b49f4b965b3473e6f6e873cca789243e8
parent 659090 05cd79e037b5ed7baa640f796e1aeb67f79f9f77
child 659092 c1fd6ed947089b3c6f86807457e526bbd9a24794
push id78008
push userjdescottes@mozilla.com
push dateTue, 05 Sep 2017 13:43:47 +0000
reviewersochameau
bugs1395535
milestone57.0a1
Bug 1395535 - trigger addon reload from devtools-startup if devtools addon failed to start;r=ochameau MozReview-Commit-ID: KLK92FkB5te
devtools/shim/devtools-startup.js
--- a/devtools/shim/devtools-startup.js
+++ b/devtools/shim/devtools-startup.js
@@ -178,17 +178,27 @@ DevToolsStartup.prototype = {
    */
   recorded: false,
 
   /**
    * Flag that indicates if the developer toggle was already added to customizableUI.
    */
   developerToggleCreated: false,
 
+  /**
+   * Flag that checks if the DevTools addon has been installed and started.
+   */
+  isDevToolsAvailable: false,
+
   handle: function (cmdLine) {
+    // Update isDevToolsAvailable by checking if resource://devtools is registered.
+    this.isDevToolsAvailable = Services.io.getProtocolHandler("resource")
+                               .QueryInterface(Ci.nsIResProtocolHandler)
+                               .hasSubstitution("devtools");
+
     let consoleFlag = cmdLine.handleFlag("jsconsole", false);
     let debuggerFlag = cmdLine.handleFlag("jsdebugger", false);
     let devtoolsFlag = cmdLine.handleFlag("devtools", false);
 
     if (consoleFlag) {
       this.handleConsoleFlag(cmdLine);
     }
     if (debuggerFlag) {
@@ -382,16 +392,33 @@ DevToolsStartup.prototype = {
     // Bug 371900: command event is fired only if "oncommand" attribute is set.
     k.setAttribute("oncommand", ";");
     k.addEventListener("command", oncommand);
 
     return k;
   },
 
   initDevTools: function (reason) {
+    if (!this.isDevToolsAvailable) {
+      // Under very specific circumstances, DevTools addon might only be installed but not
+      // started. In this case we attempt to reload the addon.
+      const { AddonManager } = Components.utils.import("resource://gre/modules/AddonManager.jsm", {});
+      AddonManager.getAddonByID("devtools@mozilla.org", (addon) => {
+        addon.enabled = true;
+        addon.active = true;
+        addon.reload();
+
+        // DevTools should now be ready to be used.
+        this.isDevToolsAvailable = true;
+      });
+
+      throw new Error("Firefox DevTools addon had to be reloaded , please try again." +
+                      "Force addon scan via `--setpref extensions.startupScanScopes=15`");
+    }
+
     if (reason && !this.recorded) {
       // Only save the first call for each firefox run as next call
       // won't necessarely start the tool. For example key shortcuts may
       // only change the currently selected tool.
       try {
         Services.telemetry.getHistogramById("DEVTOOLS_ENTRY_POINT")
                           .add(reason);
       } catch (e) {