Bug 1543098 - Emit executionContextCreated for existing context when calling Runtime.enable. r=ato
authorAlexandre Poirot <poirot.alex@gmail.com>
Mon, 29 Apr 2019 14:58:57 +0000
changeset 530728 142fc2db9173dff66a413ce64a4d8a2b8a217425
parent 530727 0a13ac4fa9d75c4287bdc7866cffb2d5efe9ab65
child 530729 f5645934c8ecab2ef3eb965fd3818697dcd8e20e
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 - Emit executionContextCreated for existing context when calling Runtime.enable. r=ato Differential Revision: https://phabricator.services.mozilla.com/D28958
remote/domains/content/Runtime.jsm
remote/test/browser/browser_runtime_executionContext.js
--- a/remote/domains/content/Runtime.jsm
+++ b/remote/domains/content/Runtime.jsm
@@ -2,16 +2,17 @@
  * 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";
 
 var EXPORTED_SYMBOLS = ["Runtime"];
 
 const {ContentProcessDomain} = ChromeUtils.import("chrome://remote/content/domains/ContentProcessDomain.jsm");
+const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 class Runtime extends ContentProcessDomain {
   constructor(session) {
     super(session);
     this.enabled = false;
   }
 
   destructor() {
@@ -20,16 +21,32 @@ class Runtime extends ContentProcessDoma
 
   // commands
 
   async enable() {
     if (!this.enabled) {
       this.enabled = true;
       this.chromeEventHandler.addEventListener("DOMWindowCreated", this,
         {mozSystemGroup: true});
+
+      // Spin the event loop in order to send the `executionContextCreated` event right
+      // after we replied to `enable` request.
+      Services.tm.dispatchToMainThread(() => {
+        const frameId = this.content.windowUtils.outerWindowID;
+        const id = this.content.windowUtils.currentInnerWindowID;
+        this.emit("Runtime.executionContextCreated", {
+          context: {
+            id,
+            auxData: {
+              isDefault: true,
+              frameId,
+            },
+          },
+        });
+      });
     }
   }
 
   disable() {
     if (this.enabled) {
       this.enabled = false;
       this.chromeEventHandler.removeEventListener("DOMWindowCreated", this,
         {mozSystemGroup: true});
--- a/remote/test/browser/browser_runtime_executionContext.js
+++ b/remote/test/browser/browser_runtime_executionContext.js
@@ -48,27 +48,34 @@ async function testCDP() {
   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");
 
+  // Calling Runtime.enable will emit executionContextCreated for the existing contexts
+  const { context: context1 } = await Runtime.executionContextCreated();
+  ok(!!context1.id, "The execution context has an id");
+  ok(context1.auxData.isDefault, "The execution context is the default one");
+  ok(!!context1.auxData.frameId, "The execution context has a frame id set");
+
   const executionContextCreated = Runtime.executionContextCreated();
 
   const url = "data:text/html;charset=utf-8,test-page";
-  const { frameId } = await Page.navigate({ url  });
+  const { frameId } = await Page.navigate({ url });
   ok(true, "A new page has been loaded");
-  ok(frameId, "Page.navigate returned a frameId");
+  is(frameId, context1.auxData.frameId, "Page.navigate returns the same frameId than executionContextCreated");
 
-  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 " +
+  const { context: context2 } = await executionContextCreated;
+  ok(!!context2.id, "The execution context has an id");
+  isnot(context1.id, context2.id, "The new execution context has a different id");
+  ok(context2.auxData.isDefault, "The execution context is the default one");
+  is(context2.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();