Bug 1344158 - PART 1: wrap webconsole;r=honza draft
authorFred Lin <gasolin@mozilla.com>
Mon, 06 Mar 2017 18:09:18 +0800
changeset 503309 a87a1c1bde5eeb67216abdf8b9ace05000c9583c
parent 503306 7513b3f42058e9bcf9950d4acf4647d4ad2240f0
child 503310 1e07c1736112178b3838063632fe7da4f8ea76f3
push id50531
push userbmo:gasolin@mozilla.com
push dateThu, 23 Mar 2017 02:04:49 +0000
reviewershonza
bugs1344158
milestone55.0a1
Bug 1344158 - PART 1: wrap webconsole;r=honza MozReview-Commit-ID: ERugtMV58ak
devtools/client/netmonitor/netmonitor-controller.js
devtools/client/netmonitor/utils/client.js
--- a/devtools/client/netmonitor/netmonitor-controller.js
+++ b/devtools/client/netmonitor/netmonitor-controller.js
@@ -8,16 +8,17 @@ const { TimelineFront } = require("devto
 const { CurlUtils } = require("devtools/client/shared/curl");
 const { ACTIVITY_TYPE, EVENTS } = require("./constants");
 const Actions = require("./actions/index");
 const {
   fetchHeaders,
   formDataURI,
 } = require("./utils/request-utils");
 const {
+  getWebConsoleClient,
   onFirefoxConnect,
   onFirefoxDisconnect,
 } = require("./utils/client");
 const {
   getRequestById,
   getDisplayedRequestById,
 } = require("./selectors/index");
 
@@ -82,34 +83,37 @@ var NetMonitorController = {
     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;
 
       let connectTimeline = () => {
         // Don't start up waiting for timeline markers if the server isn't
         // recent enough to emit the markers we're interested in.
         if (this._target.getTrait("documentLoadingMarkers")) {
           this.timelineFront = new TimelineFront(this._target.client,
             this._target.form);
           return this.timelineFront.start({ withDocLoadingEvents: true });
         }
         return undefined;
       };
       await connectTimeline();
 
       onFirefoxConnect(this._target);
       this._target.on("close", this._onTabDetached);
+
+      this.webConsoleClient = getWebConsoleClient();
+      this.NetworkEventsHandler = new NetworkEventsHandler();
       this.NetworkEventsHandler.connect();
 
+      window.gNetwork = this.NetworkEventsHandler;
       window.emit(EVENTS.CONNECTED);
 
       resolve();
       this._connected = true;
     });
     return this._connection;
   },
 
@@ -819,16 +823,9 @@ NetworkEventsHandler.prototype = {
     if (typeof stringGrip === "string") {
       return Promise.resolve(stringGrip);
     }
 
     return this.webConsoleClient.getString(stringGrip);
   }
 };
 
-/**
- * Preliminary setup for the NetMonitorController object.
- */
-NetMonitorController.NetworkEventsHandler = new NetworkEventsHandler();
-window.NetMonitorController = NetMonitorController;
-window.gNetwork = NetMonitorController.NetworkEventsHandler;
-
 exports.NetMonitorController = NetMonitorController;
--- a/devtools/client/netmonitor/utils/client.js
+++ b/devtools/client/netmonitor/utils/client.js
@@ -5,16 +5,18 @@
 /* global gStore */
 
 "use strict";
 
 const Services = require("Services");
 const Actions = require("../actions/index");
 const { EVENTS } = require("../constants");
 
+let activeConsole;
+
 /**
  * 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);
@@ -40,26 +42,38 @@ function willNavigate(type) {
 }
 
 /**
  * Process connection events.
  *
  * @param {Object} tabTarget
  */
 function onFirefoxConnect(tabTarget) {
+  activeConsole = tabTarget.activeConsole;
   tabTarget.on("navigate", navigated);
   tabTarget.on("will-navigate", willNavigate);
 }
 
 /**
  * Process disconnect events.
  *
  * @param {Object} tabTarget
  */
 function onFirefoxDisconnect(tabTarget) {
+  activeConsole = null;
   tabTarget.off("navigate", navigated);
   tabTarget.off("will-navigate", willNavigate);
 }
 
+/**
+ * Retrieve webconsole object
+ *
+ * @returns {Object} webConsole
+ */
+function getWebConsoleClient() {
+  return activeConsole;
+}
+
 module.exports = {
+  getWebConsoleClient,
   onFirefoxConnect,
   onFirefoxDisconnect,
 };