Bug 1725111 - [remote] Use contextDescriptor as broadcasting argument for MessageHandler commands r=webdriver-reviewers,whimboo

Instead of using a broadcast boolean flag in the CommandDestination, use a contextDescriptor.
The only context descriptor type supported here is CONTEXT_DESCRIPTOR_TYPES.ALL, and the behavior is unchanged.

Differential Revision: https://phabricator.services.mozilla.com/D131439
This commit is contained in:
Julian Descottes 2021-11-23 13:02:27 +00:00
parent 81698c54eb
commit db1ebd69a8
4 changed files with 38 additions and 9 deletions

View File

@ -169,9 +169,14 @@ class MessageHandler extends EventEmitter {
* @typedef {Object} CommandDestination
* @property {String} type
* One of MessageHandler.type.
* @property {String} id
* @property {String=} id
* Unique context identifier. The format depends on the type.
* For WINDOW_GLOBAL destinations, this is a browsing context id.
* Optional, should only be provided if `contextDescriptor` is missing.
* @property {ContextDescriptor=} contextDescriptor
* Descriptor used to match several contexts, which will all receive the
* command.
* Optional, should only be provided if `id` is missing.
*/
/**

View File

@ -31,6 +31,9 @@ function sendTestBroadcastCommand(module, command, params, rootMessageHandler) {
const { WindowGlobalMessageHandler } = ChromeUtils.import(
"chrome://remote/content/shared/messagehandler/WindowGlobalMessageHandler.jsm"
);
const { CONTEXT_DESCRIPTOR_TYPES } = ChromeUtils.import(
"chrome://remote/content/shared/messagehandler/MessageHandler.jsm"
);
info("Send a test broadcast command");
return rootMessageHandler.handleCommand({
@ -38,8 +41,10 @@ function sendTestBroadcastCommand(module, command, params, rootMessageHandler) {
commandName: command,
params,
destination: {
contextDescriptor: {
type: CONTEXT_DESCRIPTOR_TYPES.ALL,
},
type: WindowGlobalMessageHandler.type,
broadcast: true,
},
});
}

View File

@ -13,6 +13,8 @@ const { XPCOMUtils } = ChromeUtils.import(
XPCOMUtils.defineLazyModuleGetters(this, {
Services: "resource://gre/modules/Services.jsm",
CONTEXT_DESCRIPTOR_TYPES:
"chrome://remote/content/shared/messagehandler/MessageHandler.jsm",
MessageHandlerFrameActor:
"chrome://remote/content/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameActor.jsm",
});
@ -46,9 +48,9 @@ class FrameTransport {
* being processed by WINDOW_GLOBAL MessageHandlers.
*/
forwardCommand(command) {
if (command.destination.id && command.destination.broadcast) {
if (command.destination.id && command.destination.contextDescriptor) {
throw new Error(
"Invalid command destination with both 'id' and 'broadcast' properties"
"Invalid command destination with both 'id' and 'contextDescriptor' properties"
);
}
@ -63,18 +65,21 @@ class FrameTransport {
return this._sendCommandToBrowsingContext(command, browsingContext);
}
// ... otherwise broadcast to all registered destinations.
if (command.destination.broadcast) {
// ... otherwise broadcast to destinations matching the contextDescriptor.
if (command.destination.contextDescriptor) {
return this._broadcastCommand(command);
}
throw new Error(
"Unrecognized command destination, missing 'id' or 'broadcast' properties"
"Unrecognized command destination, missing 'id' or 'contextDescriptor' properties"
);
}
_broadcastCommand(command) {
const browsingContexts = this._getAllBrowsingContexts();
const { contextDescriptor } = command.destination;
const browsingContexts = this._getBrowsingContextsForDescriptor(
contextDescriptor
);
return Promise.all(
browsingContexts.map(async browsingContext => {
@ -104,6 +109,18 @@ class FrameTransport {
return `[object ${this.constructor.name} ${this._messageHandler.name}]`;
}
_getBrowsingContextsForDescriptor(contextDescriptor) {
const { type } = contextDescriptor;
if (type === CONTEXT_DESCRIPTOR_TYPES.ALL) {
return this._getAllBrowsingContexts();
}
// TODO: Handle other types of context descriptors.
throw new Error(
`Unsupported contextDescriptor type for broadcasting: ${type}`
);
}
_getAllBrowsingContexts() {
let browsingContexts = [];
// Fetch all top level window's browsing contexts

View File

@ -11,6 +11,8 @@ const { XPCOMUtils } = ChromeUtils.import(
);
XPCOMUtils.defineLazyModuleGetters(this, {
CONTEXT_DESCRIPTOR_TYPES:
"chrome://remote/content/shared/messagehandler/MessageHandler.jsm",
Module: "chrome://remote/content/shared/messagehandler/Module.jsm",
});
@ -30,7 +32,7 @@ class Log extends Module {
moduleName: "log",
category: "event",
contextDescriptor: {
type: "all",
type: CONTEXT_DESCRIPTOR_TYPES.ALL,
},
values: ["log.entryAdded"],
});