Bug 1472491: Part 5z - Add WebChannelChild actor. r=markh f=mconley draft
authorKris Maglione <maglione.k@gmail.com>
Sun, 29 Jul 2018 23:36:12 -0700
changeset 828461 7d641bfec99ec680485869e2af185aaa33c02b8b
parent 828460 90d2c9ae0dfd529e91490e25e5e17f9ea3ee9ecc
child 828462 e4eddd659ca25ade7dcfe68f0392911c7822752a
push id118680
push usermaglione.k@gmail.com
push dateFri, 10 Aug 2018 23:04:22 +0000
reviewersmarkh
bugs1472491
milestone63.0a1
Bug 1472491: Part 5z - Add WebChannelChild actor. r=markh f=mconley MozReview-Commit-ID: 1f056kpyJW6
toolkit/actors/WebChannelChild.jsm
toolkit/actors/moz.build
toolkit/content/browser-content.js
toolkit/modules/ActorManagerParent.jsm
toolkit/modules/WebChannelContent.jsm
toolkit/modules/moz.build
rename from toolkit/modules/WebChannelContent.jsm
rename to toolkit/actors/WebChannelChild.jsm
--- a/toolkit/modules/WebChannelContent.jsm
+++ b/toolkit/actors/WebChannelChild.jsm
@@ -1,59 +1,60 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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/. */
 
 /* eslint no-unused-vars: ["error", {args: "none"}] */
 
-var EXPORTED_SYMBOLS = ["WebChannelContent"];
+var EXPORTED_SYMBOLS = ["WebChannelChild"];
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
+ChromeUtils.import("resource://gre/modules/ActorChild.jsm");
 
 function getMessageManager(event) {
   let window = Cu.getGlobalForObject(event.target);
 
   return window.docShell.messageManager;
 }
 
-var WebChannelContent = {
-  // Preference containing the list (space separated) of origins that are
-  // allowed to send non-string values through a WebChannel, mainly for
-  // backwards compatability. See bug 1238128 for more information.
-  URL_WHITELIST_PREF: "webchannel.allowObject.urlWhitelist",
+// Preference containing the list (space separated) of origins that are
+// allowed to send non-string values through a WebChannel, mainly for
+// backwards compatability. See bug 1238128 for more information.
+const URL_WHITELIST_PREF = "webchannel.allowObject.urlWhitelist";
 
-  // Cached list of whitelisted principals, we avoid constructing this if the
-  // value in `_lastWhitelistValue` hasn't changed since we constructed it last.
-  _cachedWhitelist: [],
-  _lastWhitelistValue: "",
+// Cached list of whitelisted principals, we avoid constructing this if the
+// value in `_lastWhitelistValue` hasn't changed since we constructed it last.
+let _cachedWhitelist = [];
+let _lastWhitelistValue = "";
 
+class WebChannelChild extends ActorChild {
   handleEvent(event) {
     if (event.type === "WebChannelMessageToChrome") {
       return this._onMessageToChrome(event);
     }
     return undefined;
-  },
+  }
 
   receiveMessage(msg) {
     if (msg.name === "WebChannelMessageToContent") {
       return this._onMessageToContent(msg);
     }
     return undefined;
-  },
+  }
 
   _getWhitelistedPrincipals() {
-    let whitelist = Services.prefs.getCharPref(this.URL_WHITELIST_PREF);
-    if (whitelist != this._lastWhitelistValue) {
+    let whitelist = Services.prefs.getCharPref(URL_WHITELIST_PREF);
+    if (whitelist != _lastWhitelistValue) {
       let urls = whitelist.split(/\s+/);
-      this._cachedWhitelist = urls.map(origin =>
+      _cachedWhitelist = urls.map(origin =>
         Services.scriptSecurityManager.createCodebasePrincipalFromOrigin(origin));
     }
-    return this._cachedWhitelist;
-  },
+    return _cachedWhitelist;
+  }
 
   _onMessageToChrome(e) {
     // If target is window then we want the document principal, otherwise fallback to target itself.
     let principal = e.target.nodePrincipal ? e.target.nodePrincipal : e.target.document.nodePrincipal;
 
     if (e.detail) {
       if (typeof e.detail != "string") {
         // Check if the principal is one of the ones that's allowed to send
@@ -69,17 +70,17 @@ var WebChannelContent = {
       }
 
       let mm = getMessageManager(e);
 
       mm.sendAsyncMessage("WebChannelMessageToChrome", e.detail, { eventTarget: e.target }, principal);
     } else {
       Cu.reportError("WebChannel message failed. No message detail.");
     }
-  },
+  }
 
   _onMessageToContent(msg) {
     if (msg.data) {
       // msg.objects.eventTarget will be defined if sending a response to
       // a WebChannelMessageToChrome event. An unsolicited send
       // may not have an eventTarget defined, in this case send to the
       // main content window.
       let eventTarget = msg.objects.eventTarget || msg.target.content;
@@ -99,10 +100,10 @@ var WebChannelContent = {
           }, targetWindow),
         }));
       } else {
         Cu.reportError("WebChannel message failed. Principal mismatch.");
       }
     } else {
       Cu.reportError("WebChannel message failed. No message data.");
     }
-  },
-};
+  }
+}
--- a/toolkit/actors/moz.build
+++ b/toolkit/actors/moz.build
@@ -8,10 +8,11 @@ FINAL_TARGET_FILES.actors += [
     'AudioPlaybackChild.jsm',
     'ExtFindChild.jsm',
     'FindBarChild.jsm',
     'PopupBlockingChild.jsm',
     'PrintingChild.jsm',
     'SelectChild.jsm',
     'SelectionSourceChild.jsm',
     'ThumbnailsChild.jsm',
+    'WebChannelChild.jsm',
     'ZoomChild.jsm',
 ]
--- a/toolkit/content/browser-content.js
+++ b/toolkit/content/browser-content.js
@@ -31,19 +31,16 @@ XPCOMUtils.defineLazyProxy(this, "Shield
 });
 
 XPCOMUtils.defineLazyProxy(this, "UITourListener", () => {
   let tmp = {};
   ChromeUtils.import("resource:///modules/ContentUITour.jsm", tmp);
   return new tmp.UITourListener(global);
 });
 
-XPCOMUtils.defineLazyProxy(this, "WebChannelContent",
-  "resource://gre/modules/WebChannelContent.jsm");
-
 XPCOMUtils.defineLazyProxy(this, "DateTimePickerContent", () => {
   let tmp = {};
   ChromeUtils.import("resource://gre/modules/DateTimePickerContent.jsm", tmp);
   return new tmp.DateTimePickerContent(this);
 });
 
 // Lazily load the finder code
 addMessageListener("Finder:Initialize", function() {
@@ -62,20 +59,16 @@ var AutoScrollListener = {
       this._controller.handleEvent(event);
     }
   }
 };
 Services.els.addSystemEventListener(global, "mousedown", AutoScrollListener, true);
 
 addEventListener("MozOpenDateTimePicker", DateTimePickerContent);
 
-addEventListener("WebChannelMessageToChrome", WebChannelContent,
-                 true, true);
-addMessageListener("WebChannelMessageToContent", WebChannelContent);
-
 var UnselectedTabHoverObserver = {
   init() {
     addMessageListener("Browser:UnselectedTabHover", this);
     addEventListener("UnselectedTabHover:Enable", this);
     addEventListener("UnselectedTabHover:Disable", this);
     this.init = null;
   },
   receiveMessage(message) {
--- a/toolkit/modules/ActorManagerParent.jsm
+++ b/toolkit/modules/ActorManagerParent.jsm
@@ -182,16 +182,28 @@ let ACTORS = {
       messages: [
         "Browser:Thumbnail:Request",
         "Browser:Thumbnail:CheckState",
         "Browser:Thumbnail:GetOriginalURL",
       ],
     },
   },
 
+  WebChannel: {
+    child: {
+      module: "resource://gre/actors/WebChannelChild.jsm",
+      events: {
+        "WebChannelMessageToChrome": {capture: true, wantUntrusted: true},
+      },
+      messages: [
+        "WebChannelMessageToContent",
+      ],
+    },
+  },
+
   Zoom: {
     child: {
       module: "resource://gre/actors/ZoomChild.jsm",
       events: {
         "FullZoomChange": {},
         "TextZoomChange": {},
         "ZoomChangeUsingMouseWheel": {},
       },
--- a/toolkit/modules/moz.build
+++ b/toolkit/modules/moz.build
@@ -247,17 +247,16 @@ EXTRA_JS_MODULES += [
     'sessionstore/ScrollPosition.jsm',
     'ShortcutUtils.jsm',
     'Sqlite.jsm',
     'Task.jsm',
     'Timer.jsm',
     'Troubleshoot.jsm',
     'UpdateUtils.jsm',
     'WebChannel.jsm',
-    'WebChannelContent.jsm',
     'WebNavigationChild.jsm',
     'WebProgressChild.jsm',
     'WindowDraggingUtils.jsm',
     'ZipUtils.jsm',
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa':
     EXTRA_JS_MODULES += [