Bug 1340464 - PART 2: handle connection state in util/client;r=Honza
authorFred Lin <gasolin@mozilla.com>
Wed, 22 Feb 2017 18:19:45 +0800
changeset 374523 9703c14de85386497b3af5c514b48768b3a8e5c4
parent 374522 44fc24be2928d4975a054db66281689a1e47ee83
child 374524 0b8279c5d55d1e5e57b5dc016520b1d10c4aabb4
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersHonza
bugs1340464
milestone54.0a1
Bug 1340464 - PART 2: handle connection state in util/client;r=Honza MozReview-Commit-ID: JWC4ZbfLS8W
devtools/client/netmonitor/netmonitor-controller.js
devtools/client/netmonitor/utils/client.js
devtools/client/netmonitor/utils/moz.build
--- a/devtools/client/netmonitor/netmonitor-controller.js
+++ b/devtools/client/netmonitor/netmonitor-controller.js
@@ -9,16 +9,20 @@ const { CurlUtils } = require("devtools/
 const { ACTIVITY_TYPE, EVENTS } = require("./constants");
 const { configureStore } = require("./store");
 const Actions = require("./actions/index");
 const {
   fetchHeaders,
   formDataURI,
 } = require("./utils/request-utils");
 const {
+  onFirefoxConnect,
+  onFirefoxDisconnect,
+} = require("./utils/client");
+const {
   getRequestById,
   getDisplayedRequestById,
 } = require("./selectors/index");
 
 const gStore = window.gStore = configureStore();
 
 /**
  * Object defining the network monitor controller components.
@@ -48,17 +52,18 @@ var NetMonitorController = {
    *         A promise that is resolved when the monitor finishes shutdown.
    */
   shutdownNetMonitor() {
     if (this._shutdown) {
       return this._shutdown;
     }
     this._shutdown = new Promise(async (resolve) => {
       gStore.dispatch(Actions.batchReset());
-      this.TargetEventsHandler.disconnect();
+      onFirefoxDisconnect(this._target);
+      this._target.off("close", this._onTabDetached);
       this.NetworkEventsHandler.disconnect();
       await this.disconnect();
       resolve();
     });
 
     return this._shutdown;
   },
 
@@ -70,16 +75,18 @@ var NetMonitorController = {
    *
    * @return object
    *         A promise that is resolved when the monitor finishes connecting.
    */
   connect() {
     if (this._connection) {
       return this._connection;
     }
+    this._onTabDetached = this.shutdownNetMonitor.bind(this);
+
     this._connection = new Promise(async (resolve) => {
       // Some actors like AddonActor or RootActor for chrome debugging
       // aren't actual tabs.
       if (this._target.isTabActor) {
         this.tabClient = this._target.activeTab;
       }
       this.webConsoleClient = this._target.activeConsole;
 
@@ -90,17 +97,18 @@ var NetMonitorController = {
           this.timelineFront = new TimelineFront(this._target.client,
             this._target.form);
           return this.timelineFront.start({ withDocLoadingEvents: true });
         }
         return undefined;
       };
       await connectTimeline();
 
-      this.TargetEventsHandler.connect();
+      onFirefoxConnect(this._target);
+      this._target.on("close", this._onTabDetached);
       this.NetworkEventsHandler.connect();
 
       window.emit(EVENTS.CONNECTED);
 
       resolve();
       this._connected = true;
     });
     return this._connection;
@@ -352,88 +360,16 @@ var NetMonitorController = {
 
       window.on(EVENTS.NETWORK_EVENT, onRequest);
       window.on(EVENTS.RECEIVED_EVENT_TIMINGS, onTimings);
     });
   },
 };
 
 /**
- * Functions handling target-related lifetime events.
- */
-function TargetEventsHandler() {
-  this._onTabNavigated = this._onTabNavigated.bind(this);
-  this._onTabDetached = this._onTabDetached.bind(this);
-}
-
-TargetEventsHandler.prototype = {
-  get target() {
-    return NetMonitorController._target;
-  },
-
-  /**
-   * Listen for events emitted by the current tab target.
-   */
-  connect: function () {
-    this.target.on("close", this._onTabDetached);
-    this.target.on("navigate", this._onTabNavigated);
-    this.target.on("will-navigate", this._onTabNavigated);
-  },
-
-  /**
-   * Remove events emitted by the current tab target.
-   */
-  disconnect: function () {
-    if (!this.target) {
-      return;
-    }
-    this.target.off("close", this._onTabDetached);
-    this.target.off("navigate", this._onTabNavigated);
-    this.target.off("will-navigate", this._onTabNavigated);
-  },
-
-  /**
-   * Called for each location change in the monitored tab.
-   *
-   * @param string type
-   *        Packet type.
-   * @param object packet
-   *        Packet received from the server.
-   */
-  _onTabNavigated: function (type, packet) {
-    switch (type) {
-      case "will-navigate": {
-        // Reset UI.
-        if (!Services.prefs.getBoolPref("devtools.webconsole.persistlog")) {
-          gStore.dispatch(Actions.batchReset());
-          gStore.dispatch(Actions.clearRequests());
-        } else {
-          // If the log is persistent, just clear all accumulated timing markers.
-          gStore.dispatch(Actions.clearTimingMarkers());
-        }
-
-        window.emit(EVENTS.TARGET_WILL_NAVIGATE);
-        break;
-      }
-      case "navigate": {
-        window.emit(EVENTS.TARGET_DID_NAVIGATE);
-        break;
-      }
-    }
-  },
-
-  /**
-   * Called when the monitored tab is closed.
-   */
-  _onTabDetached: function () {
-    NetMonitorController.shutdownNetMonitor();
-  }
-};
-
-/**
  * Functions handling target network events.
  */
 function NetworkEventsHandler() {
   this.addRequest = this.addRequest.bind(this);
   this.updateRequest = this.updateRequest.bind(this);
   this.getString = this.getString.bind(this);
   this._onNetworkEvent = this._onNetworkEvent.bind(this);
   this._onNetworkEventUpdate = this._onNetworkEventUpdate.bind(this);
@@ -884,13 +820,15 @@ NetworkEventsHandler.prototype = {
     if (typeof stringGrip === "string") {
       return Promise.resolve(stringGrip);
     }
 
     return this.webConsoleClient.getString(stringGrip);
   }
 };
 
-NetMonitorController.TargetEventsHandler = new TargetEventsHandler();
+/**
+ * Preliminary setup for the NetMonitorController object.
+ */
 NetMonitorController.NetworkEventsHandler = new NetworkEventsHandler();
 window.gNetwork = NetMonitorController.NetworkEventsHandler;
 
 exports.NetMonitorController = NetMonitorController;
new file mode 100644
--- /dev/null
+++ b/devtools/client/netmonitor/utils/client.js
@@ -0,0 +1,65 @@
+/* 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/. */
+
+/* global gStore */
+
+"use strict";
+
+const Services = require("Services");
+const Actions = require("../actions/index");
+const { EVENTS } = require("../constants");
+
+/**
+ * Called for each location change in the monitored tab.
+ *
+ * @param {String} type Packet type.
+ * @param {Object} packet Packet received from the server.
+ */
+function navigated(type) {
+  window.emit(EVENTS.TARGET_DID_NAVIGATE);
+}
+
+/**
+ * Called for each location change in the monitored tab.
+ *
+ * @param {String} type Packet type.
+ * @param {Object} packet Packet received from the server.
+ */
+function willNavigate(type) {
+  // Reset UI.
+  if (!Services.prefs.getBoolPref("devtools.webconsole.persistlog")) {
+    gStore.dispatch(Actions.batchReset());
+    gStore.dispatch(Actions.clearRequests());
+  } else {
+    // If the log is persistent, just clear all accumulated timing markers.
+    gStore.dispatch(Actions.clearTimingMarkers());
+  }
+
+  window.emit(EVENTS.TARGET_WILL_NAVIGATE);
+}
+
+/**
+ * Process connection events.
+ *
+ * @param {Object} tabTarget
+ */
+function onFirefoxConnect(tabTarget) {
+  tabTarget.on("navigate", navigated);
+  tabTarget.on("will-navigate", willNavigate);
+}
+
+/**
+ * Process disconnect events.
+ *
+ * @param {Object} tabTarget
+ */
+function onFirefoxDisconnect(tabTarget) {
+  tabTarget.off("navigate", navigated);
+  tabTarget.off("will-navigate", willNavigate);
+}
+
+module.exports = {
+  onFirefoxConnect,
+  onFirefoxDisconnect,
+};
--- a/devtools/client/netmonitor/utils/moz.build
+++ b/devtools/client/netmonitor/utils/moz.build
@@ -1,14 +1,15 @@
 # vim: set filetype=python:
 # 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(
+    'client.js',
     'filter-predicates.js',
     'format-utils.js',
     'l10n.js',
     'mdn-utils.js',
     'prefs.js',
     'request-utils.js',
     'sort-predicates.js',
 )