Bug 1493714 - move toolbox specific listeners to threadClient events to the toolbox; r=jdescottes
authoryulia <ystartsev@mozilla.com>
Wed, 15 May 2019 08:26:31 +0000
changeset 532885 7811523177894d6658e481200b644686126d7fd1
parent 532884 e55213bb470a88e0bd26a75b91497235fd3e9d97
child 532886 91d4f30e0ddad63c153baee71e8bc496987204f4
push id11276
push userrgurzau@mozilla.com
push dateMon, 20 May 2019 13:11:24 +0000
treeherdermozilla-beta@847755a7c325 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdescottes
bugs1493714
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1493714 - move toolbox specific listeners to threadClient events to the toolbox; r=jdescottes This is the first part of getting rid of framework/attach-thread.js -- here we move the toolbox related logic back into the toolbox. Differential Revision: https://phabricator.services.mozilla.com/D29193
devtools/client/framework/attach-thread.js
devtools/client/framework/toolbox.js
--- a/devtools/client/framework/attach-thread.js
+++ b/devtools/client/framework/attach-thread.js
@@ -1,74 +1,26 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
 /* 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/. */
 
 const Services = require("Services");
-const {LocalizationHelper} = require("devtools/shared/l10n");
-const L10N = new LocalizationHelper("devtools/client/locales/toolbox.properties");
 
-function handleThreadState(toolbox, event, packet) {
-  // Suppress interrupted events by default because the thread is
-  // paused/resumed a lot for various actions.
-  if (event === "paused" && packet.why.type === "interrupted") {
-    return;
-  }
-
-  if (event === "paused") {
-    toolbox.highlightTool("jsdebugger");
-
-    if (packet.why.type === "debuggerStatement" ||
-       packet.why.type === "breakpoint" ||
-       packet.why.type === "exception") {
-      toolbox.raise();
-      toolbox.selectTool("jsdebugger", packet.why.type);
-    }
-  } else if (event === "resumed") {
-    toolbox.unhighlightTool("jsdebugger");
-  }
-}
-
-async function attachThread(toolbox) {
-  const target = toolbox.target;
+async function attachThread(target) {
   const threadOptions = {
     autoBlackBox: false,
     ignoreFrameEnvironment: true,
     pauseOnExceptions:
       Services.prefs.getBoolPref("devtools.debugger.pause-on-exceptions"),
     ignoreCaughtExceptions:
       Services.prefs.getBoolPref("devtools.debugger.ignore-caught-exceptions"),
   };
 
   const [, threadClient] = await target.attachThread(threadOptions);
   if (!threadClient.paused) {
     throw new Error("Thread in wrong state when starting up, should be paused.");
   }
-
-  try {
-    await threadClient.resume();
-  } catch (ex) {
-    // Interpret a possible error thrown by ThreadActor.resume
-    if (ex.error === "wrongOrder") {
-      const box = toolbox.getNotificationBox();
-      box.appendNotification(
-        L10N.getStr("toolbox.resumeOrderWarning"),
-        "wrong-resume-order",
-        "",
-        box.PRIORITY_WARNING_HIGH
-      );
-    } else {
-      throw ex;
-    }
-  }
-  threadClient.addListener("paused", handleThreadState.bind(null, toolbox));
-  threadClient.addListener("resumed", handleThreadState.bind(null, toolbox));
   return threadClient;
 }
 
-function detachThread(threadClient) {
-  threadClient.removeListener("paused");
-  threadClient.removeListener("resumed");
-}
-
-module.exports = { attachThread, detachThread };
+module.exports = { attachThread };
--- a/devtools/client/framework/toolbox.js
+++ b/devtools/client/framework/toolbox.js
@@ -19,17 +19,17 @@ var {Ci, Cc} = require("chrome");
 var promise = require("promise");
 const { debounce } = require("devtools/shared/debounce");
 var Services = require("Services");
 var ChromeUtils = require("ChromeUtils");
 var {gDevTools} = require("devtools/client/framework/devtools");
 var EventEmitter = require("devtools/shared/event-emitter");
 var Telemetry = require("devtools/client/shared/telemetry");
 const { getUnicodeUrl } = require("devtools/client/shared/unicode-url");
-var { attachThread, detachThread } = require("./attach-thread");
+var { attachThread } = require("./attach-thread");
 var { DOMHelpers } = require("resource://devtools/client/shared/DOMHelpers.jsm");
 const { KeyCodes } = require("devtools/client/shared/keycodes");
 var Startup = Cc["@mozilla.org/devtools/startup-clh;1"].getService(Ci.nsISupports)
   .wrappedJSObject;
 
 const { BrowserLoader } =
   ChromeUtils.import("resource://devtools/client/shared/browser-loader.js");
 
@@ -179,16 +179,18 @@ function Toolbox(target, selectedTool, h
   this.updateToolboxButtonsVisibility = this.updateToolboxButtonsVisibility.bind(this);
   this.updateToolboxButtons = this.updateToolboxButtons.bind(this);
   this.selectTool = this.selectTool.bind(this);
   this._pingTelemetrySelectTool = this._pingTelemetrySelectTool.bind(this);
   this.toggleSplitConsole = this.toggleSplitConsole.bind(this);
   this.toggleOptions = this.toggleOptions.bind(this);
   this.togglePaintFlashing = this.togglePaintFlashing.bind(this);
   this.toggleDragging = this.toggleDragging.bind(this);
+  this._onPausedState = this._onPausedState.bind(this);
+  this._onResumedState = this._onResumedState.bind(this);
   this.isPaintFlashing = false;
 
   this._target.on("close", this._onTargetClosed);
 
   if (!selectedTool) {
     selectedTool = Services.prefs.getCharPref(this._prefs.LAST_TOOL);
   }
   this._defaultToolId = selectedTool;
@@ -448,16 +450,47 @@ Toolbox.prototype = {
     if (!this._splitConsole) {
       return false;
     }
     const focusedWin = Services.focus.focusedWindow;
     return focusedWin && focusedWin ===
       this.doc.querySelector("#toolbox-panel-iframe-webconsole").contentWindow;
   },
 
+  _onPausedState: function(_, packet) {
+    // Suppress interrupted events by default because the thread is
+    // paused/resumed a lot for various actions.
+    if (packet.why.type === "interrupted") {
+      return;
+    }
+
+    this.highlightTool("jsdebugger");
+
+    if (packet.why.type === "debuggerStatement" ||
+       packet.why.type === "breakpoint" ||
+       packet.why.type === "exception") {
+      this.raise();
+      this.selectTool("jsdebugger", packet.why.type);
+    }
+  },
+
+  _onResumedState: function() {
+    this.unhighlightTool("jsdebugger");
+  },
+
+  _startThreadClientListeners: function() {
+    this.threadClient.addListener("paused", this._onPausedState);
+    this.threadClient.addListener("resumed", this._onResumedState);
+  },
+
+  _stopThreadClientListeners: function() {
+    this.threadClient.removeListener("paused", this._onPausedState);
+    this.threadClient.removeListener("resumed", this._onResumedState);
+  },
+
   /**
    * Open the toolbox
    */
   open: function() {
     return (async function() {
       const isToolboxURL = this.win.location.href.startsWith(this._URL);
       if (isToolboxURL) {
         // Update the URL so that onceDOMReady watch for the right url.
@@ -488,17 +521,37 @@ Toolbox.prototype = {
       // (Workers and potentially others don't manage the console client in the target.)
       if (this._target.activeConsole) {
         await this._target.activeConsole.startListeners([
           "NetworkActivity",
         ]);
       }
 
       // Attach the thread
-      this._threadClient = await attachThread(this);
+      this._threadClient = await attachThread(this._target);
+
+      try {
+        await this._threadClient.resume();
+      } catch (ex) {
+        // Interpret a possible error thrown by ThreadActor.resume
+        if (ex.error === "wrongOrder") {
+          const box = this.getNotificationBox();
+          box.appendNotification(
+            L10N.getStr("toolbox.resumeOrderWarning"),
+            "wrong-resume-order",
+            "",
+            box.PRIORITY_WARNING_HIGH
+          );
+        } else {
+          throw ex;
+        }
+      }
+
+      this._startThreadClientListeners();
+
       await domReady;
 
       this.browserRequire = BrowserLoader({
         window: this.win,
         useOnlyShared: true,
       }).require;
 
       // The web console is immediately loaded when replaying, so that the
@@ -3137,17 +3190,17 @@ Toolbox.prototype = {
 
     // Destroying the walker and inspector fronts
     outstanding.push(this.destroyInspector());
 
     // Reset preferences set by the toolbox
     outstanding.push(this.resetPreference());
 
     // Detach the thread
-    detachThread(this._threadClient);
+    this._stopThreadClientListeners();
     this._threadClient = null;
 
     // Unregister buttons listeners
     this.toolbarButtons.forEach(button => {
       if (typeof button.teardown == "function") {
         // teardown arguments have already been bound in _createButtonState
         button.teardown();
       }