Bug 1539221 - Register main process target on server start. r=ato
authorAlexandre Poirot <poirot.alex@gmail.com>
Thu, 04 Apr 2019 11:16:27 +0000
changeset 467970 8cfc47737c533f9cb8b584c3b86292c0e0c8b255
parent 467969 d16ffacbce08ded8e18a6500435274d2bffe02c0
child 467971 a0adda35967e606236d485d2be5ff2825dffec59
push id112667
push useraiakab@mozilla.com
push dateThu, 04 Apr 2019 16:12:45 +0000
treeherdermozilla-inbound@230bb363f2f3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato
bugs1539221
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 1539221 - Register main process target on server start. r=ato Depends on D25556 Differential Revision: https://phabricator.services.mozilla.com/D25557
remote/RemoteAgent.jsm
remote/targets/MainProcessTarget.jsm
remote/test/browser/browser.ini
remote/test/browser/browser_main_target.js
remote/test/browser/browser_target.js
--- a/remote/RemoteAgent.jsm
+++ b/remote/RemoteAgent.jsm
@@ -83,17 +83,21 @@ class RemoteAgentClass {
     }
 
     if (this.listening) {
       return;
     }
 
     try {
       this.server._start(port, host);
-      log.info(`Remote debugging agent listening on ${this.scheme}://${this.host}:${this.port}/`);
+
+      // Immediatly instantiate the main process target in order
+      // to be accessible via HTTP endpoint on startup
+      const mainTarget = this.targets.getMainProcessTarget();
+      log.info(`Remote debugging agent listening on ${mainTarget.wsDebuggerURL}`);
     } catch (e) {
       throw new Error(`Unable to start remote agent: ${e.message}`, e);
     }
 
     Preferences.set(RecommendedPreferences);
   }
 
   async close() {
--- a/remote/targets/MainProcessTarget.jsm
+++ b/remote/targets/MainProcessTarget.jsm
@@ -5,49 +5,52 @@
 "use strict";
 
 var EXPORTED_SYMBOLS = ["MainProcessTarget"];
 
 const {Target} = ChromeUtils.import("chrome://remote/content/targets/Target.jsm");
 const {Session} = ChromeUtils.import("chrome://remote/content/sessions/Session.jsm");
 const {RemoteAgent} = ChromeUtils.import("chrome://remote/content/RemoteAgent.jsm");
 
+const UUIDGen = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
+
 /**
  * The main process Target.
  *
  * Matches BrowserDevToolsAgentHost from chromium, and only support a couple of Domains:
  * https://cs.chromium.org/chromium/src/content/browser/devtools/browser_devtools_agent_host.cc?dr=CSs&g=0&l=80-91
  */
 class MainProcessTarget extends Target {
   /*
    * @param Targets targets
    */
   constructor(targets) {
     super(targets, Session);
 
     this.type = "main-process";
+    this.id = UUIDGen.generateUUID().toString().slice(1, -1);
 
     // Define the HTTP path to query this target
-    this.path = "/devtools/browser";
+    this.path = `/devtools/browser/${this.id}`;
   }
 
   get wsDebuggerURL() {
     const {host, port} = RemoteAgent;
     return `ws://${host}:${port}${this.path}`;
   }
 
   toString() {
     return `[object MainProcessTarget]`;
   }
 
   toJSON() {
     return {
       description: "Main process target",
       devtoolsFrontendUrl: "",
       faviconUrl: "",
-      id: "main-process",
+      id: this.id,
       title: "Main process target",
       type: this.type,
       url: "",
       webSocketDebuggerUrl: this.wsDebuggerURL,
     };
   }
 }
--- a/remote/test/browser/browser.ini
+++ b/remote/test/browser/browser.ini
@@ -1,10 +1,11 @@
 [DEFAULT]
 tags = remote
 subsuite = remote
 support-files =
   chrome-remote-interface.js
   head.js
 
 [browser_cdp.js]
+[browser_main_target.js]
 [browser_tabs.js]
 [browser_target.js]
copy from remote/test/browser/browser_target.js
copy to remote/test/browser/browser_main_target.js
--- a/remote/test/browser/browser_target.js
+++ b/remote/test/browser/browser_main_target.js
@@ -1,78 +1,41 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
-/* global getCDP */
+// Test very basic CDP features.
 
 const {RemoteAgent} = ChromeUtils.import("chrome://remote/content/RemoteAgent.jsm");
 
-// Test the Target domain
+const TEST_URI = "data:text/html;charset=utf-8,default-test-page";
 
 add_task(async function() {
-  try {
-    await testCDP();
-  } catch (e) {
-    // Display better error message with the server side stacktrace
-    // if an error happened on the server side:
-    if (e.response) {
-      throw new Error("CDP Exception:\n" + e.response + "\n");
-    } else {
-      throw e;
-    }
-  }
-});
-
-async function testCDP() {
   // Start the CDP server
   RemoteAgent.init();
   RemoteAgent.tabs.start();
   RemoteAgent.listen(Services.io.newURI("http://localhost:9222"));
 
-  // Retrieve the chrome-remote-interface library object
+  const { mainProcessTarget } = RemoteAgent.targets;
+  ok(mainProcessTarget,
+    "The main process target is instantiated after the call to `listen`");
+
+  const targetURL = mainProcessTarget.wsDebuggerURL;
+
   const CDP = await getCDP();
 
-  // Connect to the server
-  const {webSocketDebuggerUrl} = await CDP.Version();
-  is(webSocketDebuggerUrl, "ws://localhost:9222/devtools/browser", "Version endpoint refers to /devtools/browser target");
-
-  const client = await CDP({"target": webSocketDebuggerUrl});
+  const client = await CDP({"target": targetURL});
   ok(true, "CDP client has been instantiated");
 
-  const {Target} = client;
-  ok("Target" in client, "Target domain is available");
-
-  const targetCreatedForAlreadyOpenedTab = Target.targetCreated();
-
-  // Instruct the server to fire Target.targetCreated events
-  Target.setDiscoverTargets({ discover: true });
-
-  // Calling `setDiscoverTargets` will dispatch `targetCreated` event for all
-  // already opened tabs
-  await targetCreatedForAlreadyOpenedTab;
+  const {Browser, Target} = client;
+  ok(Browser, "The main process target exposes Browser domain");
+  ok(Target, "The main process target exposes Target domain");
 
-  // Create a new target so that the test runs against a fresh new tab
-  const targetCreated = Target.targetCreated();
-  const {targetId} = await Target.createTarget();
-  ok(true, "Target created");
-  ok(!!targetId, "createTarget returns a non-empty target id");
-  const {targetInfo} = await targetCreated;
-  is(targetId, targetInfo.targetId, "createTarget and targetCreated refers to the same target id");
-  is(targetInfo.type, "page", "The target is a page");
+  const version = await Browser.getVersion();
+  is(version.product, "Firefox", "Browser.getVersion works");
 
-  const attachedToTarget = Target.attachedToTarget();
-  const {sessionId} = await Target.attachToTarget({ targetId });
-  ok(true, "Target attached");
-
-  const attachedEvent = await attachedToTarget;
-  ok(true, "Received Target.attachToTarget event");
-  is(attachedEvent.sessionId, sessionId, "attachedToTarget and attachToTarget returns the same session id");
-  is(attachedEvent.targetInfo.type, "page", "attachedToTarget creates a tab by default");
-
-  await client.close();
-  ok(true, "The client is closed");
-
-  BrowserTestUtils.removeTab(gBrowser.selectedTab);
+  const {webSocketDebuggerUrl} = await CDP.Version();
+  is(webSocketDebuggerUrl, targetURL,
+    "Version endpoint refers to the same Main process target");
 
   await RemoteAgent.close();
-}
+});
--- a/remote/test/browser/browser_target.js
+++ b/remote/test/browser/browser_target.js
@@ -29,18 +29,16 @@ async function testCDP() {
   RemoteAgent.tabs.start();
   RemoteAgent.listen(Services.io.newURI("http://localhost:9222"));
 
   // Retrieve the chrome-remote-interface library object
   const CDP = await getCDP();
 
   // Connect to the server
   const {webSocketDebuggerUrl} = await CDP.Version();
-  is(webSocketDebuggerUrl, "ws://localhost:9222/devtools/browser", "Version endpoint refers to /devtools/browser target");
-
   const client = await CDP({"target": webSocketDebuggerUrl});
   ok(true, "CDP client has been instantiated");
 
   const {Target} = client;
   ok("Target" in client, "Target domain is available");
 
   const targetCreatedForAlreadyOpenedTab = Target.targetCreated();