Bug 1000814 - Move WorkerDebuggerTransport to its own module. r=jryans draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Wed, 11 Jul 2018 08:44:55 -0700
changeset 817216 0a540c2a59ed486320544a81f890742756631f8f
parent 817215 f39cf0c5198341ba0aaf68004c1a1470706a6132
child 817217 3ce0fd34fb1f459cb5d50c8a6d89d277f28cd56e
child 817221 6cea5c41ced31d3c792f004692de28ff362213a8
child 817604 a18bef7ae21cc9c3f67cf48324301ee81bcea537
push id115987
push userbmo:poirot.alex@gmail.com
push dateThu, 12 Jul 2018 07:12:00 +0000
reviewersjryans
bugs1000814
milestone63.0a1
Bug 1000814 - Move WorkerDebuggerTransport to its own module. r=jryans MozReview-Commit-ID: I1eFQCDLaDh
devtools/server/main.js
devtools/shared/transport/moz.build
devtools/shared/transport/transport.js
devtools/shared/transport/worker-transport.js
--- a/devtools/server/main.js
+++ b/devtools/server/main.js
@@ -7,25 +7,25 @@
 /**
  * Toolkit glue for the remote debugging protocol, loaded into the
  * debugging global.
  */
 var { Ci, Cc } = require("chrome");
 var Services = require("Services");
 var { ActorPool, OriginalLocation, RegisteredActorFactory,
       ObservedActorFactory } = require("devtools/server/actors/common");
-var { WorkerDebuggerTransport } =
-  require("devtools/shared/transport/transport");
 var DevToolsUtils = require("devtools/shared/DevToolsUtils");
 var { dumpn } = DevToolsUtils;
 
 loader.lazyRequireGetter(this, "DebuggerSocket", "devtools/shared/security/socket", true);
 loader.lazyRequireGetter(this, "Authentication", "devtools/shared/security/auth");
 loader.lazyRequireGetter(this, "LocalDebuggerTransport", "devtools/shared/transport/local-transport", true);
 loader.lazyRequireGetter(this, "ChildDebuggerTransport", "devtools/shared/transport/child-transport", true);
+loader.lazyRequireGetter(this, "WorkerThreadWorkerDebuggerTransport", "devtools/shared/transport/worker-transport", true);
+loader.lazyRequireGetter(this, "MainThreadWorkerDebuggerTransport", "devtools/shared/transport/worker-transport", true);
 
 loader.lazyGetter(this, "generateUUID", () => {
   // eslint-disable-next-line no-shadow
   const { generateUUID } = Cc["@mozilla.org/uuid-generator;1"]
                            .getService(Ci.nsIUUIDGenerator);
   return generateUUID;
 });
 
@@ -595,17 +595,17 @@ var DebuggerServer = {
    *    actor names. This connection will use messages named
    *    "debug:<prefix>:packet", and all its actors will have names
    *    beginning with "<prefix>/".
    */
   connectToParent(prefix, scopeOrManager) {
     this._checkInit();
 
     const transport = isWorker ?
-                    new WorkerDebuggerTransport(scopeOrManager, prefix) :
+                    new WorkerThreadWorkerDebuggerTransport(scopeOrManager, prefix) :
                     new ChildDebuggerTransport(scopeOrManager, prefix);
 
     return this._onConnection(transport, prefix, true);
   },
 
   /**
    * Start a DevTools server in a content process (representing the entire process, not
    * just a single frame) and add it as a child server for an active connection.
@@ -764,17 +764,17 @@ var DebuggerServer = {
             return;
           }
 
           // The initial connection message has been received, don't
           // need to listen any longer
           dbg.removeListener(listener);
 
           // Step 7: Create a transport for the connection to the worker.
-          const transport = new WorkerDebuggerTransport(dbg, id);
+          const transport = new MainThreadWorkerDebuggerTransport(dbg, id);
           transport.ready();
           transport.hooks = {
             onClosed: () => {
               if (!dbg.isClosed) {
                 // If the worker happens to be shutting down while we are trying
                 // to close the connection, there is a small interval during
                 // which no more runnables can be dispatched to the worker, but
                 // the worker debugger has not yet been closed. In that case,
--- a/devtools/shared/transport/moz.build
+++ b/devtools/shared/transport/moz.build
@@ -8,9 +8,10 @@ XPCSHELL_TESTS_MANIFESTS += ['tests/unit
 
 DevToolsModules(
     'child-transport.js',
     'local-transport.js',
     'packets.js',
     'stream-utils.js',
     'transport.js',
     'websocket-transport.js',
+    'worker-transport.js',
 )
--- a/devtools/shared/transport/transport.js
+++ b/devtools/shared/transport/transport.js
@@ -478,126 +478,8 @@ DebuggerTransport.prototype = {
     }
     this._incomingHeader = "";
     this._incoming = null;
   }
 
 };
 
 exports.DebuggerTransport = DebuggerTransport;
-
-// WorkerDebuggerTransport is defined differently depending on whether we are
-// on the main thread or a worker thread. In the former case, we are required
-// by the devtools loader, and isWorker will be false. Otherwise, we are
-// required by the worker loader, and isWorker will be true.
-//
-// Each worker debugger supports only a single connection to the main thread.
-// However, its theoretically possible for multiple servers to connect to the
-// same worker. Consequently, each transport has a connection id, to allow
-// messages from multiple connections to be multiplexed on a single channel.
-
-if (!this.isWorker) {
-  // Main thread
-  (function() {
-    /**
-     * A transport that uses a WorkerDebugger to send packets from the main
-     * thread to a worker thread.
-     */
-    function WorkerDebuggerTransport(dbg, id) {
-      this._dbg = dbg;
-      this._id = id;
-      this.onMessage = this._onMessage.bind(this);
-    }
-
-    WorkerDebuggerTransport.prototype = {
-      constructor: WorkerDebuggerTransport,
-
-      ready: function() {
-        this._dbg.addListener(this);
-      },
-
-      close: function() {
-        this._dbg.removeListener(this);
-        if (this.hooks) {
-          this.hooks.onClosed();
-        }
-      },
-
-      send: function(packet) {
-        this._dbg.postMessage(JSON.stringify({
-          type: "message",
-          id: this._id,
-          message: packet
-        }));
-      },
-
-      startBulkSend: function() {
-        throw new Error("Can't send bulk data from worker threads!");
-      },
-
-      _onMessage: function(message) {
-        const packet = JSON.parse(message);
-        if (packet.type !== "message" || packet.id !== this._id) {
-          return;
-        }
-
-        if (this.hooks) {
-          this.hooks.onPacket(packet.message);
-        }
-      }
-    };
-
-    exports.WorkerDebuggerTransport = WorkerDebuggerTransport;
-  }).call(this);
-} else {
-  // Worker thread
-  (function() {
-    /**
-     * A transport that uses a WorkerDebuggerGlobalScope to send packets from a
-     * worker thread to the main thread.
-     */
-    function WorkerDebuggerTransport(scope, id) {
-      this._scope = scope;
-      this._id = id;
-      this._onMessage = this._onMessage.bind(this);
-    }
-
-    WorkerDebuggerTransport.prototype = {
-      constructor: WorkerDebuggerTransport,
-
-      ready: function() {
-        this._scope.addEventListener("message", this._onMessage);
-      },
-
-      close: function() {
-        this._scope.removeEventListener("message", this._onMessage);
-        if (this.hooks) {
-          this.hooks.onClosed();
-        }
-      },
-
-      send: function(packet) {
-        this._scope.postMessage(JSON.stringify({
-          type: "message",
-          id: this._id,
-          message: packet
-        }));
-      },
-
-      startBulkSend: function() {
-        throw new Error("Can't send bulk data from worker threads!");
-      },
-
-      _onMessage: function(event) {
-        const packet = JSON.parse(event.data);
-        if (packet.type !== "message" || packet.id !== this._id) {
-          return;
-        }
-
-        if (this.hooks) {
-          this.hooks.onPacket(packet.message);
-        }
-      }
-    };
-
-    exports.WorkerDebuggerTransport = WorkerDebuggerTransport;
-  }).call(this);
-}
new file mode 100644
--- /dev/null
+++ b/devtools/shared/transport/worker-transport.js
@@ -0,0 +1,110 @@
+/* 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/. */
+
+"use strict";
+
+// Each worker debugger supports only a single connection to the main thread.
+// However, its theoretically possible for multiple servers to connect to the
+// same worker. Consequently, each transport has a connection id, to allow
+// messages from multiple connections to be multiplexed on a single channel.
+
+/**
+ * A transport that uses a WorkerDebugger to send packets from the main
+ * thread to a worker thread.
+ */
+function MainThreadWorkerDebuggerTransport(dbg, id) {
+  this._dbg = dbg;
+  this._id = id;
+  this.onMessage = this._onMessage.bind(this);
+}
+
+MainThreadWorkerDebuggerTransport.prototype = {
+  constructor: MainThreadWorkerDebuggerTransport,
+
+  ready: function() {
+    this._dbg.addListener(this);
+  },
+
+  close: function() {
+    this._dbg.removeListener(this);
+    if (this.hooks) {
+      this.hooks.onClosed();
+    }
+  },
+
+  send: function(packet) {
+    this._dbg.postMessage(JSON.stringify({
+      type: "message",
+      id: this._id,
+      message: packet
+    }));
+  },
+
+  startBulkSend: function() {
+    throw new Error("Can't send bulk data from worker threads!");
+  },
+
+  _onMessage: function(message) {
+    const packet = JSON.parse(message);
+    if (packet.type !== "message" || packet.id !== this._id) {
+      return;
+    }
+
+    if (this.hooks) {
+      this.hooks.onPacket(packet.message);
+    }
+  }
+};
+
+exports.MainThreadWorkerDebuggerTransport = MainThreadWorkerDebuggerTransport;
+
+/**
+ * A transport that uses a WorkerDebuggerGlobalScope to send packets from a
+ * worker thread to the main thread.
+ */
+function WorkerThreadWorkerDebuggerTransport(scope, id) {
+  this._scope = scope;
+  this._id = id;
+  this._onMessage = this._onMessage.bind(this);
+}
+
+WorkerThreadWorkerDebuggerTransport.prototype = {
+  constructor: WorkerThreadWorkerDebuggerTransport,
+
+  ready: function() {
+    this._scope.addEventListener("message", this._onMessage);
+  },
+
+  close: function() {
+    this._scope.removeEventListener("message", this._onMessage);
+    if (this.hooks) {
+      this.hooks.onClosed();
+    }
+  },
+
+  send: function(packet) {
+    this._scope.postMessage(JSON.stringify({
+      type: "message",
+      id: this._id,
+      message: packet
+    }));
+  },
+
+  startBulkSend: function() {
+    throw new Error("Can't send bulk data from worker threads!");
+  },
+
+  _onMessage: function(event) {
+    const packet = JSON.parse(event.data);
+    if (packet.type !== "message" || packet.id !== this._id) {
+      return;
+    }
+
+    if (this.hooks) {
+      this.hooks.onPacket(packet.message);
+    }
+  }
+};
+
+exports.WorkerThreadWorkerDebuggerTransport = WorkerThreadWorkerDebuggerTransport;