2014-08-01 09:42:00 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WebChannel is an abstraction that uses the Message Manager and Custom Events
|
|
|
|
* to create a two-way communication channel between chrome and content code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["WebChannel", "WebChannelBroker"];
|
|
|
|
|
|
|
|
const ERRNO_UNKNOWN_ERROR = 999;
|
|
|
|
const ERROR_UNKNOWN = "UNKNOWN_ERROR";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2014-08-01 09:42:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WebChannelBroker is a global object that helps manage WebChannel objects.
|
|
|
|
* This object handles channel registration, origin validation and message multiplexing.
|
|
|
|
*/
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var WebChannelBroker = Object.create({
|
2014-08-01 09:42:00 +04:00
|
|
|
/**
|
|
|
|
* Register a new channel that callbacks messages
|
|
|
|
* based on proper origin and channel name
|
|
|
|
*
|
|
|
|
* @param channel {WebChannel}
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
registerChannel(channel) {
|
2014-08-01 09:42:00 +04:00
|
|
|
if (!this._channelMap.has(channel)) {
|
|
|
|
this._channelMap.set(channel);
|
|
|
|
} else {
|
|
|
|
Cu.reportError("Failed to register the channel. Channel already exists.");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister a channel
|
|
|
|
*
|
|
|
|
* @param channelToRemove {WebChannel}
|
|
|
|
* WebChannel to remove from the channel map
|
|
|
|
*
|
|
|
|
* Removes the specified channel from the channel map
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
unregisterChannel(channelToRemove) {
|
2014-08-01 09:42:00 +04:00
|
|
|
if (!this._channelMap.delete(channelToRemove)) {
|
|
|
|
Cu.reportError("Failed to unregister the channel. Channel not found.");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object to store pairs of message origins and callback functions
|
|
|
|
*/
|
|
|
|
_channelMap: new Map(),
|
2019-11-02 03:39:35 +03:00
|
|
|
|
2014-08-01 09:42:00 +04:00
|
|
|
/**
|
2019-11-02 03:39:35 +03:00
|
|
|
* Deliver a message to a registered channel.
|
2014-08-01 09:42:00 +04:00
|
|
|
*
|
2019-11-02 03:39:35 +03:00
|
|
|
* @returns bool whether we managed to find a registered channel.
|
2014-08-01 09:42:00 +04:00
|
|
|
*/
|
2019-11-02 03:39:35 +03:00
|
|
|
tryToDeliver(data, sendingContext) {
|
|
|
|
let validChannelFound = false;
|
|
|
|
data.message = data.message || {};
|
2014-08-01 09:42:00 +04:00
|
|
|
|
2019-11-02 03:39:35 +03:00
|
|
|
for (var channel of this._channelMap.keys()) {
|
|
|
|
if (
|
|
|
|
channel.id === data.id &&
|
|
|
|
channel._originCheckCallback(sendingContext.principal)
|
|
|
|
) {
|
|
|
|
validChannelFound = true;
|
|
|
|
channel.deliver(data, sendingContext);
|
|
|
|
}
|
2014-08-01 09:42:00 +04:00
|
|
|
}
|
2019-11-02 03:39:35 +03:00
|
|
|
return validChannelFound;
|
2014-08-01 09:42:00 +04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new WebChannel that listens and sends messages over some channel id
|
|
|
|
*
|
|
|
|
* @param id {String}
|
|
|
|
* WebChannel id
|
2014-11-07 10:12:26 +03:00
|
|
|
* @param originOrPermission {nsIURI/string}
|
2016-12-02 02:46:00 +03:00
|
|
|
* If an nsIURI, incoming events will be accepted from any origin matching
|
|
|
|
* that URI's origin.
|
|
|
|
* If a string, it names a permission, and incoming events will be accepted
|
|
|
|
* from any https:// origin that has been granted that permission by the
|
|
|
|
* permission manager.
|
2014-08-01 09:42:00 +04:00
|
|
|
* @constructor
|
|
|
|
*/
|
2014-11-07 10:12:26 +03:00
|
|
|
var WebChannel = function(id, originOrPermission) {
|
|
|
|
if (!id || !originOrPermission) {
|
|
|
|
throw new Error("WebChannel id and originOrPermission are required.");
|
2014-08-01 09:42:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.id = id;
|
2014-11-07 10:12:26 +03:00
|
|
|
// originOrPermission can be either an nsIURI or a string representing a
|
|
|
|
// permission name.
|
|
|
|
if (typeof originOrPermission == "string") {
|
|
|
|
this._originCheckCallback = requestPrincipal => {
|
2016-12-02 02:46:00 +03:00
|
|
|
// Accept events from any secure origin having the named permission.
|
2014-11-07 10:12:26 +03:00
|
|
|
// The permission manager operates on domain names rather than true
|
|
|
|
// origins (bug 1066517). To mitigate that, we explicitly check that
|
|
|
|
// the scheme is https://.
|
2016-11-25 07:52:02 +03:00
|
|
|
let uri = Services.io.newURI(requestPrincipal.originNoSuffix);
|
2014-11-07 10:12:26 +03:00
|
|
|
if (uri.scheme != "https") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// OK - we have https - now we can check the permission.
|
|
|
|
let perm = Services.perms.testExactPermissionFromPrincipal(
|
|
|
|
requestPrincipal,
|
|
|
|
originOrPermission
|
|
|
|
);
|
|
|
|
return perm == Ci.nsIPermissionManager.ALLOW_ACTION;
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2014-11-07 10:12:26 +03:00
|
|
|
} else {
|
2016-12-02 02:46:00 +03:00
|
|
|
// Accept events from any origin matching the given URI.
|
|
|
|
// We deliberately use `originNoSuffix` here because we only want to
|
|
|
|
// restrict based on the site's origin, not on other origin attributes
|
|
|
|
// such as containers or private browsing.
|
2014-11-07 10:12:26 +03:00
|
|
|
this._originCheckCallback = requestPrincipal => {
|
2016-11-25 07:52:02 +03:00
|
|
|
return originOrPermission.prePath === requestPrincipal.originNoSuffix;
|
2017-10-15 21:50:30 +03:00
|
|
|
};
|
2014-11-07 10:12:26 +03:00
|
|
|
}
|
|
|
|
this._originOrPermission = originOrPermission;
|
2014-08-01 09:42:00 +04:00
|
|
|
};
|
|
|
|
|
2020-01-30 00:50:04 +03:00
|
|
|
WebChannel.prototype = {
|
2014-08-01 09:42:00 +04:00
|
|
|
/**
|
|
|
|
* WebChannel id
|
|
|
|
*/
|
|
|
|
id: null,
|
|
|
|
|
|
|
|
/**
|
2014-11-07 10:12:26 +03:00
|
|
|
* The originOrPermission value passed to the constructor, mainly for
|
|
|
|
* debugging and tests.
|
|
|
|
*/
|
|
|
|
_originOrPermission: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback that will be called with the principal of an incoming message
|
|
|
|
* to check if the request should be dispatched to the listeners.
|
2014-08-01 09:42:00 +04:00
|
|
|
*/
|
2014-11-07 10:12:26 +03:00
|
|
|
_originCheckCallback: null,
|
2014-08-01 09:42:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WebChannelBroker that manages WebChannels
|
|
|
|
*/
|
|
|
|
_broker: WebChannelBroker,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback that will be called with the contents of an incoming message
|
|
|
|
*/
|
|
|
|
_deliverCallback: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the callback for messages on this channel
|
|
|
|
* Registers the channel itself with the WebChannelBroker
|
|
|
|
*
|
|
|
|
* @param callback {Function}
|
|
|
|
* Callback that will be called when there is a message
|
|
|
|
* @param {String} id
|
|
|
|
* The WebChannel id that was used for this message
|
|
|
|
* @param {Object} message
|
|
|
|
* The message itself
|
2015-04-24 09:07:33 +03:00
|
|
|
* @param sendingContext {Object}
|
|
|
|
* The sending context of the source of the message. Can be passed to
|
|
|
|
* `send` to respond to a message.
|
|
|
|
* @param sendingContext.browser {browser}
|
|
|
|
* The <browser> object that captured the
|
|
|
|
* WebChannelMessageToChrome.
|
|
|
|
* @param sendingContext.eventTarget {EventTarget}
|
|
|
|
* The <EventTarget> where the message was sent.
|
|
|
|
* @param sendingContext.principal {Principal}
|
|
|
|
* The <Principal> of the EventTarget where the
|
|
|
|
* message was sent.
|
2014-08-01 09:42:00 +04:00
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
listen(callback) {
|
2014-08-01 09:42:00 +04:00
|
|
|
if (this._deliverCallback) {
|
|
|
|
throw new Error("Failed to listen. Listener already attached.");
|
|
|
|
} else if (!callback) {
|
|
|
|
throw new Error("Failed to listen. Callback argument missing.");
|
|
|
|
} else {
|
|
|
|
this._deliverCallback = callback;
|
|
|
|
this._broker.registerChannel(this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the callback for messages on this channel
|
|
|
|
* Removes the channel from the WebChannelBroker
|
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
stopListening() {
|
2014-08-01 09:42:00 +04:00
|
|
|
this._broker.unregisterChannel(this);
|
|
|
|
this._deliverCallback = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends messages over the WebChannel id using the "WebChannelMessageToContent" event
|
|
|
|
*
|
|
|
|
* @param message {Object}
|
|
|
|
* The message object that will be sent
|
2015-04-24 09:07:33 +03:00
|
|
|
* @param target {Object}
|
|
|
|
* A <target> with the information of where to send the message.
|
2019-11-02 03:39:35 +03:00
|
|
|
* @param target.browsingContext {BrowsingContext}
|
|
|
|
* The browsingContext we should send the message to.
|
2015-04-24 09:07:33 +03:00
|
|
|
* @param target.principal {Principal}
|
|
|
|
* Principal of the target. Prevents messages from
|
|
|
|
* being dispatched to unexpected origins. The system principal
|
|
|
|
* can be specified to send to any target.
|
|
|
|
* @param [target.eventTarget] {EventTarget}
|
|
|
|
* Optional eventTarget within the browser, use to send to a
|
2019-11-02 03:39:35 +03:00
|
|
|
* specific element. Can be null; if not null, should be
|
|
|
|
* a ContentDOMReference.
|
2014-08-01 09:42:00 +04:00
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
send(message, target) {
|
2019-11-02 03:39:35 +03:00
|
|
|
let { browsingContext, principal, eventTarget } = target;
|
2015-04-24 09:07:33 +03:00
|
|
|
|
2019-11-02 03:39:35 +03:00
|
|
|
if (message && browsingContext && principal) {
|
|
|
|
let { currentWindowGlobal } = browsingContext;
|
|
|
|
if (!currentWindowGlobal) {
|
|
|
|
Cu.reportError(
|
|
|
|
"Failed to send a WebChannel message. No currentWindowGlobal."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
currentWindowGlobal
|
|
|
|
.getActor("WebChannel")
|
|
|
|
.sendAsyncMessage("WebChannelMessageToContent", {
|
2014-08-01 09:42:00 +04:00
|
|
|
id: this.id,
|
2016-12-30 02:34:54 +03:00
|
|
|
message,
|
2019-11-02 03:39:35 +03:00
|
|
|
eventTarget,
|
|
|
|
principal,
|
|
|
|
});
|
2014-08-01 09:42:00 +04:00
|
|
|
} else if (!message) {
|
|
|
|
Cu.reportError("Failed to send a WebChannel message. Message not set.");
|
|
|
|
} else {
|
|
|
|
Cu.reportError("Failed to send a WebChannel message. Target invalid.");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deliver WebChannel messages to the set "_channelCallback"
|
|
|
|
*
|
|
|
|
* @param data {Object}
|
|
|
|
* Message data
|
2015-04-24 09:07:33 +03:00
|
|
|
* @param sendingContext {Object}
|
|
|
|
* Message sending context.
|
2019-11-02 03:39:35 +03:00
|
|
|
* @param sendingContext.browsingContext {BrowsingContext}
|
|
|
|
* The browsingcontext from which the
|
|
|
|
* WebChannelMessageToChrome was sent.
|
2015-04-24 09:07:33 +03:00
|
|
|
* @param sendingContext.eventTarget {EventTarget}
|
|
|
|
* The <EventTarget> where the message was sent.
|
2019-11-02 03:39:35 +03:00
|
|
|
* Can be null; if not null, should be a ContentDOMReference.
|
2015-04-24 09:07:33 +03:00
|
|
|
* @param sendingContext.principal {Principal}
|
|
|
|
* The <Principal> of the EventTarget where the message was sent.
|
|
|
|
*
|
2014-08-01 09:42:00 +04:00
|
|
|
*/
|
2016-12-30 02:34:54 +03:00
|
|
|
deliver(data, sendingContext) {
|
2014-08-01 09:42:00 +04:00
|
|
|
if (this._deliverCallback) {
|
|
|
|
try {
|
2015-04-24 09:07:33 +03:00
|
|
|
this._deliverCallback(data.id, data.message, sendingContext);
|
2014-08-01 09:42:00 +04:00
|
|
|
} catch (ex) {
|
|
|
|
this.send(
|
|
|
|
{
|
|
|
|
errno: ERRNO_UNKNOWN_ERROR,
|
|
|
|
error: ex.message ? ex.message : ERROR_UNKNOWN,
|
2015-04-24 09:07:33 +03:00
|
|
|
},
|
|
|
|
sendingContext
|
|
|
|
);
|
2015-08-20 02:44:09 +03:00
|
|
|
Cu.reportError("Failed to execute WebChannel callback:");
|
|
|
|
Cu.reportError(ex);
|
2014-08-01 09:42:00 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Cu.reportError("No callback set for this channel.");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|