Bug 1543098 - Implement Runtime.executionContextCreated. r=ato
authorAlexandre Poirot <poirot.alex@gmail.com>
Mon, 29 Apr 2019 14:58:55 +0000
changeset 530727 0a13ac4fa9d75c4287bdc7866cffb2d5efe9ab65
parent 530726 022cef1ce6b9179ac2db6811067314824a370d47
child 530728 142fc2db9173dff66a413ce64a4d8a2b8a217425
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato
bugs1543098
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 1543098 - Implement Runtime.executionContextCreated. r=ato Differential Revision: https://phabricator.services.mozilla.com/D27523
remote/domains/content/Runtime.jsm
remote/test/browser/browser.ini
remote/test/browser/browser_runtime_executionContext.js
--- a/remote/domains/content/Runtime.jsm
+++ b/remote/domains/content/Runtime.jsm
@@ -18,17 +18,39 @@ class Runtime extends ContentProcessDoma
     this.disable();
   }
 
   // commands
 
   async enable() {
     if (!this.enabled) {
       this.enabled = true;
+      this.chromeEventHandler.addEventListener("DOMWindowCreated", this,
+        {mozSystemGroup: true});
     }
   }
 
   disable() {
     if (this.enabled) {
       this.enabled = false;
+      this.chromeEventHandler.removeEventListener("DOMWindowCreated", this,
+        {mozSystemGroup: true});
+    }
+  }
+
+  handleEvent({type, target}) {
+    const frameId = target.defaultView.windowUtils.outerWindowID;
+    const id = target.defaultView.windowUtils.currentInnerWindowID;
+    switch (type) {
+    case "DOMWindowCreated":
+      this.emit("Runtime.executionContextCreated", {
+        context: {
+          id,
+          auxData: {
+            isDefault: target == this.content.document,
+            frameId,
+          },
+        },
+      });
+      break;
     }
   }
 }
--- a/remote/test/browser/browser.ini
+++ b/remote/test/browser/browser.ini
@@ -4,10 +4,11 @@ prefs = remote.enabled=true
 support-files =
   chrome-remote-interface.js
   head.js
 skip-if = debug || asan # bug 1546945
 
 [browser_cdp.js]
 [browser_main_target.js]
 [browser_page_frameNavigated.js]
+[browser_runtime_executionContext.js]
 [browser_tabs.js]
 [browser_target.js]
new file mode 100644
--- /dev/null
+++ b/remote/test/browser/browser_runtime_executionContext.js
@@ -0,0 +1,75 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+/* global getCDP */
+
+const {RemoteAgent} = ChromeUtils.import("chrome://remote/content/RemoteAgent.jsm");
+const {RemoteAgentError} = ChromeUtils.import("chrome://remote/content/Error.jsm");
+
+// Test the Runtime execution context events
+
+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 RemoteAgentError.fromJSON(e.response);
+    } else {
+      throw e;
+    }
+  }
+});
+
+async function testCDP() {
+  // Open a test page, to prevent debugging the random default page
+  await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URI);
+
+  // 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 CDP = await getCDP();
+
+  // Connect to the server
+  const client = await CDP({
+    target(list) {
+      // Ensure debugging the right target, i.e. the one for our test tab.
+      return list.find(target => target.url == TEST_URI);
+    },
+  });
+  ok(true, "CDP client has been instantiated");
+
+  const {Page, Runtime} = client;
+
+  // turn on navigation related events, such as DOMContentLoaded et al.
+  await Runtime.enable();
+  ok(true, "Runtime domain has been enabled");
+
+  const executionContextCreated = Runtime.executionContextCreated();
+
+  const url = "data:text/html;charset=utf-8,test-page";
+  const { frameId } = await Page.navigate({ url  });
+  ok(true, "A new page has been loaded");
+  ok(frameId, "Page.navigate returned a frameId");
+
+  const { context } = await executionContextCreated;
+  ok(!!context.id, "The execution context has an id");
+  ok(context.auxData.isDefault, "The execution context is the default one");
+  is(context.auxData.frameId, frameId, "The execution context frame id is the same " +
+    "the one returned by Page.navigate");
+
+  await client.close();
+  ok(true, "The client is closed");
+
+  BrowserTestUtils.removeTab(gBrowser.selectedTab);
+
+  await RemoteAgent.close();
+}