Bug 1299503 - Support connecting to remote targets via about:devtools-toolbox query parameters. r=jryans draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Wed, 31 Aug 2016 07:42:23 -0700
changeset 408554 cd644f22ff59bd09800b6607072405fbe589a332
parent 408553 ae834b182fa4f95317a70c2d73e6855783e18263
child 408555 cc4f3553df68e286f0f6c32a7816dfd4534c09b0
child 408647 167d7435f1aab8bca719805c639a29e65932a890
child 408955 d27c7f0a5e33f5c1ddfe2ba1cf11a74decfc02fa
push id28249
push userbmo:poirot.alex@gmail.com
push dateThu, 01 Sep 2016 09:44:27 +0000
reviewersjryans
bugs1299503
milestone51.0a1
Bug 1299503 - Support connecting to remote targets via about:devtools-toolbox query parameters. r=jryans MozReview-Commit-ID: 7EFCxnKkO6r
devtools/client/framework/target-from-url.js
devtools/client/framework/test/browser_target_from_url.js
--- a/devtools/client/framework/target-from-url.js
+++ b/devtools/client/framework/target-from-url.js
@@ -9,16 +9,23 @@ const { Cu, Ci } = require("chrome");
 const { TargetFactory } = require("devtools/client/framework/target");
 const { DebuggerServer } = require("devtools/server/main");
 const { DebuggerClient } = require("devtools/shared/client/main");
 const { Task } = require("devtools/shared/task");
 
 /**
  * Construct a Target for a given URL object having various query parameters:
  *
+ * host:
+ *    {String} The hostname or IP address to connect to.
+ * port:
+ *    {Number} The TCP port to connect to, to use with `host` argument.
+ * ws:
+ *    {Boolean} If true, connect via websocket instread of regular TCP connection.
+ *
  * type: tab, process
  *    {String} The type of target to connect to.  Currently tabs and processes are supported types.
  *
  * If type="tab":
  * id:
  *    {Number} the tab outerWindowID
  * chrome: Optional
  *    {Boolean} Force the creation of a chrome target. Gives more privileges to the tab
@@ -40,19 +47,17 @@ exports.targetFromURL = Task.async(funct
   if (!type) {
     throw new Error("targetFromURL, missing type parameter");
   }
   let id = params.get("id");
   // Allows to spawn a chrome enabled target for any context
   // (handy to debug chrome stuff in a child process)
   let chrome = params.has("chrome");
 
-  // Once about:debugging start supporting remote targets and use this helper,
-  // client will also be defined by url params.
-  let client = createClient();
+  let client = yield createClient(params);
 
   yield client.connect();
 
   let form, isTabActor;
   if (type === "tab") {
     // Fetch target for a remote tab
     id = parseInt(id);
     if (isNaN(id)) {
@@ -90,17 +95,26 @@ exports.targetFromURL = Task.async(funct
     }
   } else {
     throw new Error("targetFromURL, unsupported type='" + type + "' parameter");
   }
 
   return TargetFactory.forRemoteTab({ client, form, chrome, isTabActor });
 });
 
-function createClient() {
-  // Setup a server if we don't have one already running
-  if (!DebuggerServer.initialized) {
-    DebuggerServer.init();
-    DebuggerServer.addBrowserActors();
+function* createClient(params) {
+  let host = params.get("host");
+  let port = params.get("port");
+  let webSocket = params.get("ws");
+
+  let transport;
+  if (port) {
+    transport = yield DebuggerClient.socketConnect({ host, port, webSocket });
+  } else {
+    // Setup a server if we don't have one already running
+    if (!DebuggerServer.initialized) {
+      DebuggerServer.init();
+      DebuggerServer.addBrowserActors();
+    }
+    transport = DebuggerServer.connectPipe()
   }
-
-  return new DebuggerClient(DebuggerServer.connectPipe());
+  return new DebuggerClient(transport);
 }
--- a/devtools/client/framework/test/browser_target_from_url.js
+++ b/devtools/client/framework/test/browser_target_from_url.js
@@ -1,16 +1,25 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 const TEST_URI = "data:text/html;charset=utf-8," +
   "<p>browser_target-from-url.js</p>";
 
+const { DevToolsLoader } = Cu.import("resource://devtools/shared/Loader.jsm", {});
 const { targetFromURL } = require("devtools/client/framework/target-from-url");
 
+Services.prefs.setBoolPref("devtools.debugger.remote-enabled", true);
+Services.prefs.setBoolPref("devtools.debugger.prompt-connection", false);
+
+SimpleTest.registerCleanupFunction(() => {
+  Services.prefs.clearUserPref("devtools.debugger.remote-enabled");
+  Services.prefs.clearUserPref("devtools.debugger.prompt-connection");
+});
+
 function assertIsTabTarget(target, chrome = false) {
   is(target.url, TEST_URI);
   is(target.isLocalTab, false);
   is(target.chrome, chrome);
   is(target.isTabActor, true);
   is(target.isRemote, true);
 }
 
@@ -48,11 +57,78 @@ add_task(function* () {
   target = yield targetFromURL(new URL("http://foo?type=process"));
   let topWindow = Services.wm.getMostRecentWindow("navigator:browser");
   is(target.url, topWindow.location.href);
   is(target.isLocalTab, false);
   is(target.chrome, true);
   is(target.isTabActor, true);
   is(target.isRemote, true);
 
-  yield target.client.close();
+  yield testRemoteTCP();
+  yield testRemoteWebSocket();
+
   gBrowser.removeCurrentTab();
 });
+
+function* setupDebuggerServer(websocket) {
+  info("Create a separate loader instance for the DebuggerServer.");
+  let loader = new DevToolsLoader();
+  let { DebuggerServer } = loader.require("devtools/server/main");
+
+  DebuggerServer.init();
+  DebuggerServer.addBrowserActors();
+  DebuggerServer.allowChromeProcess = true;
+
+  let listener = DebuggerServer.createListener();
+  ok(listener, "Socket listener created");
+  // Pass -1 to automatically choose an available port
+  listener.portOrPath = -1;
+  listener.webSocket = websocket;
+  yield listener.open();
+  is(DebuggerServer.listeningSockets, 1, "1 listening socket");
+
+  return { DebuggerServer, listener };
+}
+
+function teardownDebuggerServer({ DebuggerServer, listener }) {
+  info("Close the listener socket");
+  listener.close();
+  is(DebuggerServer.listeningSockets, 0, "0 listening sockets");
+
+  info("Destroy the temporary debugger server");
+  DebuggerServer.destroy();
+}
+
+function* testRemoteTCP() {
+  info("Test remote process via TCP Connection");
+
+  let server = yield setupDebuggerServer(false);
+
+  let { port } = server.listener;
+  let target = yield targetFromURL(new URL("http://foo?type=process&host=127.0.0.1&port=" + port));
+  let topWindow = Services.wm.getMostRecentWindow("navigator:browser");
+  is(target.url, topWindow.location.href);
+  is(target.isLocalTab, false);
+  is(target.chrome, true);
+  is(target.isTabActor, true);
+  is(target.isRemote, true);
+  yield target.client.close();
+
+  teardownDebuggerServer(server);
+}
+
+function* testRemoteWebSocket() {
+  info("Test remote process via WebSocket Connection");
+
+  let server = yield setupDebuggerServer(true);
+
+  let { port } = server.listener;
+  let target = yield targetFromURL(new URL("http://foo?type=process&host=127.0.0.1&port=" + port + "&ws=true"));
+  let topWindow = Services.wm.getMostRecentWindow("navigator:browser");
+  is(target.url, topWindow.location.href);
+  is(target.isLocalTab, false);
+  is(target.chrome, true);
+  is(target.isTabActor, true);
+  is(target.isRemote, true);
+  yield target.client.close();
+
+  teardownDebuggerServer(server);
+}