Bug 1535487 - determine rootUrl directly in buglist creator r=tomprince
[gecko.git] / toolkit / actors / WebChannelChild.jsm
blob8513e97bc768f749d5ca6053188ea6781e95a7f6
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* eslint no-unused-vars: ["error", {args: "none"}] */
8 var EXPORTED_SYMBOLS = ["WebChannelChild"];
10 const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
11 const {ActorChild} = ChromeUtils.import("resource://gre/modules/ActorChild.jsm");
13 function getMessageManager(event) {
14   let window = Cu.getGlobalForObject(event.target);
16   return window.docShell.messageManager;
19 // Preference containing the list (space separated) of origins that are
20 // allowed to send non-string values through a WebChannel, mainly for
21 // backwards compatability. See bug 1238128 for more information.
22 const URL_WHITELIST_PREF = "webchannel.allowObject.urlWhitelist";
24 // Cached list of whitelisted principals, we avoid constructing this if the
25 // value in `_lastWhitelistValue` hasn't changed since we constructed it last.
26 let _cachedWhitelist = [];
27 let _lastWhitelistValue = "";
29 class WebChannelChild extends ActorChild {
30   handleEvent(event) {
31     if (event.type === "WebChannelMessageToChrome") {
32       return this._onMessageToChrome(event);
33     }
34     return undefined;
35   }
37   receiveMessage(msg) {
38     if (msg.name === "WebChannelMessageToContent") {
39       return this._onMessageToContent(msg);
40     }
41     return undefined;
42   }
44   _getWhitelistedPrincipals() {
45     let whitelist = Services.prefs.getCharPref(URL_WHITELIST_PREF);
46     if (whitelist != _lastWhitelistValue) {
47       let urls = whitelist.split(/\s+/);
48       _cachedWhitelist = urls.map(origin =>
49         Services.scriptSecurityManager.createCodebasePrincipalFromOrigin(origin));
50     }
51     return _cachedWhitelist;
52   }
54   _onMessageToChrome(e) {
55     // If target is window then we want the document principal, otherwise fallback to target itself.
56     let principal = e.target.nodePrincipal ? e.target.nodePrincipal : e.target.document.nodePrincipal;
58     if (e.detail) {
59       if (typeof e.detail != "string") {
60         // Check if the principal is one of the ones that's allowed to send
61         // non-string values for e.detail.  They're whitelisted by site origin,
62         // so we compare on originNoSuffix in order to avoid other origin attributes
63         // that are not relevant here, such as containers or private browsing.
64         let objectsAllowed = this._getWhitelistedPrincipals().some(whitelisted =>
65           principal.originNoSuffix == whitelisted.originNoSuffix);
66         if (!objectsAllowed) {
67           Cu.reportError("WebChannelMessageToChrome sent with an object from a non-whitelisted principal");
68           return;
69         }
70       }
72       let mm = getMessageManager(e);
74       mm.sendAsyncMessage("WebChannelMessageToChrome", e.detail, { eventTarget: e.target }, principal);
75     } else {
76       Cu.reportError("WebChannel message failed. No message detail.");
77     }
78   }
80   _onMessageToContent(msg) {
81     if (msg.data) {
82       // msg.objects.eventTarget will be defined if sending a response to
83       // a WebChannelMessageToChrome event. An unsolicited send
84       // may not have an eventTarget defined, in this case send to the
85       // main content window.
86       let eventTarget = msg.objects.eventTarget || msg.target.content;
88       // Use nodePrincipal if available, otherwise fallback to document principal.
89       let targetPrincipal = eventTarget instanceof Ci.nsIDOMWindow ? eventTarget.document.nodePrincipal : eventTarget.nodePrincipal;
91       if (msg.principal.subsumes(targetPrincipal)) {
92         // If eventTarget is a window, use it as the targetWindow, otherwise
93         // find the window that owns the eventTarget.
94         let targetWindow = eventTarget instanceof Ci.nsIDOMWindow ? eventTarget : eventTarget.ownerGlobal;
96         eventTarget.dispatchEvent(new targetWindow.CustomEvent("WebChannelMessageToContent", {
97           detail: Cu.cloneInto({
98             id: msg.data.id,
99             message: msg.data.message,
100           }, targetWindow),
101         }));
102       } else {
103         Cu.reportError("WebChannel message failed. Principal mismatch.");
104       }
105     } else {
106       Cu.reportError("WebChannel message failed. No message data.");
107     }
108   }