Bug 1369466 - Don't load the RemotePageManager on content processes before necessary. r=Mossop draft
authorFelipe Gomes <felipc@gmail.com>
Thu, 26 Jul 2018 22:09:23 -0300
changeset 823336 ddcc9c649af520d46bb2f7804dffac864a51bbd1
parent 823335 23e8de70022a12abc3585f5a080dd3267479544c
child 823337 87e8629b6133175a6f6770e47fb81494e0ddb06d
push id117637
push userfelipc@gmail.com
push dateFri, 27 Jul 2018 01:14:51 +0000
reviewersMossop
bugs1369466
milestone63.0a1
Bug 1369466 - Don't load the RemotePageManager on content processes before necessary. r=Mossop This moves the code responsible for listening for document creation out of RemotePageManagerChild.jsm into process-content.js, so that the rest of the code in RemotePageManagerChild.jsm is not loaded unecessarily. Note: The Register/Unregister messages are included in this patch for correctness, but they will be removed by optimizations from the next patch MozReview-Commit-ID: LxV481rfanV
toolkit/components/remotepagemanager/RemotePageManagerChild.jsm
toolkit/content/process-content.js
--- a/toolkit/components/remotepagemanager/RemotePageManagerChild.jsm
+++ b/toolkit/components/remotepagemanager/RemotePageManagerChild.jsm
@@ -83,43 +83,8 @@ ChildMessagePort.prototype.message = fun
   };
   this.listener.callListeners(Cu.cloneInto(message, this.window));
 };
 
 ChildMessagePort.prototype.destroy = function() {
   this.window = null;
   MessagePort.prototype.destroy.call(this);
 };
-
-
-// Listen for pages in any process we're loaded in
-var registeredURLs = new Set(Services.cpmm.initialProcessData["RemotePageManager:urls"]);
-
-var observer = (window) => {
-  // Strip the hash from the URL, because it's not part of the origin.
-  let url = window.document.documentURI.replace(/[\#|\?].*$/, "");
-
-  if (!registeredURLs.has(url))
-    return;
-
-  // Get the frame message manager for this window so we can associate this
-  // page with a browser element
-  let messageManager = window.QueryInterface(Ci.nsIInterfaceRequestor)
-                             .getInterface(Ci.nsIDocShell)
-                             .QueryInterface(Ci.nsIInterfaceRequestor)
-                             .getInterface(Ci.nsIContentFrameMessageManager);
-  // Set up the child side of the message port
-  new ChildMessagePort(messageManager, window);
-};
-Services.obs.addObserver(observer, "chrome-document-global-created");
-Services.obs.addObserver(observer, "content-document-global-created");
-
-// A message from chrome telling us what pages to listen for
-Services.cpmm.addMessageListener("RemotePage:Register", ({ data }) => {
-  for (let url of data.urls)
-    registeredURLs.add(url);
-});
-
-// A message from chrome telling us what pages to stop listening for
-Services.cpmm.addMessageListener("RemotePage:Unregister", ({ data }) => {
-  for (let url of data.urls)
-    registeredURLs.delete(url);
-});
--- a/toolkit/content/process-content.js
+++ b/toolkit/content/process-content.js
@@ -2,61 +2,100 @@
  * 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";
 
 // Creates a new PageListener for this process. This will listen for page loads
 // and for those that match URLs provided by the parent process will set up
 // a dedicated message port and notify the parent process.
-ChromeUtils.import("resource://gre/modules/remotepagemanager/RemotePageManagerChild.jsm");
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 const gInContentProcess = Services.appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_CONTENT;
 
 Services.cpmm.addMessageListener("gmp-plugin-crash", msg => {
   let gmpservice = Cc["@mozilla.org/gecko-media-plugin-service;1"]
                      .getService(Ci.mozIGeckoMediaPluginService);
 
   gmpservice.RunPluginCrashCallbacks(msg.data.pluginID, msg.data.pluginName);
 });
 
+let TOPICS = [
+  "chrome-document-global-created",
+  "content-document-global-created",
+];
+
 if (gInContentProcess) {
-  let ProcessObserver = {
-    TOPICS: [
-      "inner-window-destroyed",
-      "xpcom-shutdown",
-    ],
+  TOPICS.push("inner-window-destroyed");
+  TOPICS.push("xpcom-shutdown");
+}
+
+let registeredURLs = new Set(Services.cpmm.initialProcessData["RemotePageManager:urls"]);
 
-    init() {
-      for (let topic of this.TOPICS) {
-        Services.obs.addObserver(this, topic);
-      }
-    },
+let ProcessObserver = {
+  init() {
+    for (let topic of TOPICS) {
+      Services.obs.addObserver(this, topic);
+    }
+  },
 
-    uninit() {
-      for (let topic of this.TOPICS) {
-        Services.obs.removeObserver(this, topic);
-      }
-    },
+  uninit() {
+    for (let topic of TOPICS) {
+      Services.obs.removeObserver(this, topic);
+    }
+  },
+
+  observe(subject, topic, data) {
+    switch (topic) {
+      case "chrome-document-global-created":
+      case "content-document-global-created": {
+
+        // Strip the hash from the URL, because it's not part of the origin.
+        let window = subject;
+        let url = window.document.documentURI.replace(/[\#|\?].*$/, "");
+
+        if (!registeredURLs.has(url))
+          return;
 
-    observe(subject, topic, data) {
-      switch (topic) {
-        case "inner-window-destroyed": {
-          // Forward inner-window-destroyed notifications with the
-          // inner window ID, so that code in the parent that should
-          // do something when content windows go away can do it
-          let innerWindowID =
-            subject.QueryInterface(Ci.nsISupportsPRUint64).data;
-          Services.cpmm.sendAsyncMessage("Toolkit:inner-window-destroyed",
-                                         innerWindowID);
-          break;
-        }
-        case "xpcom-shutdown": {
-          this.uninit();
-          break;
-        }
+        // Get the frame message manager for this window so we can associate this
+        // page with a browser element
+        let messageManager = window.QueryInterface(Ci.nsIInterfaceRequestor)
+                                   .getInterface(Ci.nsIDocShell)
+                                   .QueryInterface(Ci.nsIInterfaceRequestor)
+                                   .getInterface(Ci.nsIContentFrameMessageManager);
+
+        let { ChildMessagePort } =
+          ChromeUtils.import("resource://gre/modules/remotepagemanager/RemotePageManagerChild.jsm", {});
+        // Set up the child side of the message port
+        new ChildMessagePort(messageManager, window);
+        break;
       }
-    },
-  };
+      case "inner-window-destroyed": {
+        // Forward inner-window-destroyed notifications with the
+        // inner window ID, so that code in the parent that should
+        // do something when content windows go away can do it
+        let innerWindowID =
+          subject.QueryInterface(Ci.nsISupportsPRUint64).data;
+        Services.cpmm.sendAsyncMessage("Toolkit:inner-window-destroyed",
+                                       innerWindowID);
+        break;
+      }
+      case "xpcom-shutdown": {
+        this.uninit();
+        break;
+      }
+    }
+  },
+};
 
-  ProcessObserver.init();
-}
+ProcessObserver.init();
+
+// A message from chrome telling us what pages to listen for
+Services.cpmm.addMessageListener("RemotePage:Register", ({ data }) => {
+  for (let url of data.urls)
+    registeredURLs.add(url);
+});
+
+// A message from chrome telling us what pages to stop listening for
+Services.cpmm.addMessageListener("RemotePage:Unregister", ({ data }) => {
+  for (let url of data.urls)
+    registeredURLs.delete(url);
+});