diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /remote/shared/messagehandler/transports | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'remote/shared/messagehandler/transports')
5 files changed, 475 insertions, 0 deletions
diff --git a/remote/shared/messagehandler/transports/FrameContextUtils.sys.mjs b/remote/shared/messagehandler/transports/FrameContextUtils.sys.mjs new file mode 100644 index 0000000000..341e7918ee --- /dev/null +++ b/remote/shared/messagehandler/transports/FrameContextUtils.sys.mjs @@ -0,0 +1,57 @@ +/* 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/. */ + +function isExtensionContext(browsingContext) { + let principal; + if (CanonicalBrowsingContext.isInstance(browsingContext)) { + principal = browsingContext.currentWindowGlobal.documentPrincipal; + } else { + principal = browsingContext.window.document.nodePrincipal; + } + + // In practice, note that the principal will never be an expanded principal. + // The are only used for content scripts executed in a Sandbox, and do not + // have a browsing context on their own. + // But we still use this flag because there is no isAddonPrincipal flag. + return principal.isAddonOrExpandedAddonPrincipal; +} + +function isParentProcess(browsingContext) { + if (CanonicalBrowsingContext.isInstance(browsingContext)) { + return browsingContext.currentWindowGlobal.osPid === -1; + } + + // If `browsingContext` is not a `CanonicalBrowsingContext`, then we are + // necessarily in a content process page. + return false; +} + +/** + * Check if the given browsing context is valid for the message handler + * to use. + * + * @param {BrowsingContext} browsingContext + * The browsing context to check. + * @param {Object=} options + * @param {String=} options.browserId + * The id of the browser to filter the browsing contexts by (optional). + * @return {Boolean} + * True if the browsing context is valid, false otherwise. + */ +export function isBrowsingContextCompatible(browsingContext, options = {}) { + const { browserId } = options; + + // If a browserId was provided, skip browsing contexts which are not + // associated with this browserId. + if (browserId !== undefined && browsingContext.browserId !== browserId) { + return false; + } + + // Skip: + // - extension contexts until we support debugging webextensions, see Bug 1755014. + // - privileged contexts until we support debugging Chrome context, see Bug 1713440. + return ( + !isExtensionContext(browsingContext) && !isParentProcess(browsingContext) + ); +} diff --git a/remote/shared/messagehandler/transports/FrameTransport.sys.mjs b/remote/shared/messagehandler/transports/FrameTransport.sys.mjs new file mode 100644 index 0000000000..e2d48e00f4 --- /dev/null +++ b/remote/shared/messagehandler/transports/FrameTransport.sys.mjs @@ -0,0 +1,191 @@ +/* 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/. */ + +import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + ContextDescriptorType: + "chrome://remote/content/shared/messagehandler/MessageHandler.sys.mjs", + isBrowsingContextCompatible: + "chrome://remote/content/shared/messagehandler/transports/FrameContextUtils.sys.mjs", + Log: "chrome://remote/content/shared/Log.sys.mjs", + MessageHandlerFrameActor: + "chrome://remote/content/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameActor.sys.mjs", + TabManager: "chrome://remote/content/shared/TabManager.sys.mjs", +}); + +XPCOMUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get()); + +const MAX_RETRY_ATTEMPTS = 10; + +/** + * FrameTransport is intended to be used from a ROOT MessageHandler to communicate + * with WINDOW_GLOBAL MessageHandlers via the MessageHandlerFrame JSWindow + * actors. + */ +export class FrameTransport { + /** + * @param {MessageHandler} + * The MessageHandler instance which owns this FrameTransport instance. + */ + constructor(messageHandler) { + this._messageHandler = messageHandler; + + // FrameTransport will rely on the MessageHandlerFrame JSWindow actors. + // Make sure they are registered when instanciating a FrameTransport. + lazy.MessageHandlerFrameActor.register(); + } + + /** + * Forward the provided command to WINDOW_GLOBAL MessageHandlers via the + * MessageHandlerFrame actors. + * + * @param {Command} command + * The command to forward. See type definition in MessageHandler.js + * @return {Promise} + * Returns a promise that resolves with the result of the command after + * being processed by WINDOW_GLOBAL MessageHandlers. + */ + forwardCommand(command) { + if (command.destination.id && command.destination.contextDescriptor) { + throw new Error( + "Invalid command destination with both 'id' and 'contextDescriptor' properties" + ); + } + + // With an id given forward the command to only this specific destination. + if (command.destination.id) { + const browsingContext = BrowsingContext.get(command.destination.id); + if (!browsingContext) { + throw new Error( + "Unable to find a BrowsingContext for id " + command.destination.id + ); + } + return this._sendCommandToBrowsingContext(command, browsingContext); + } + + // ... otherwise broadcast to destinations matching the contextDescriptor. + if (command.destination.contextDescriptor) { + return this._broadcastCommand(command); + } + + throw new Error( + "Unrecognized command destination, missing 'id' or 'contextDescriptor' properties" + ); + } + + _broadcastCommand(command) { + const { contextDescriptor } = command.destination; + const browsingContexts = this._getBrowsingContextsForDescriptor( + contextDescriptor + ); + + return Promise.all( + browsingContexts.map(async browsingContext => { + try { + return await this._sendCommandToBrowsingContext( + command, + browsingContext + ); + } catch (e) { + console.error( + `Failed to broadcast a command to browsingContext ${browsingContext.id}`, + e + ); + return null; + } + }) + ); + } + + async _sendCommandToBrowsingContext(command, browsingContext) { + const name = `${command.moduleName}.${command.commandName}`; + + // The browsing context might be destroyed by a navigation. Keep a reference + // to the webProgress, which will persist, and always use it to retrieve the + // currently valid browsing context. + const webProgress = browsingContext.webProgress; + + const { retryOnAbort = false } = command; + + let attempts = 0; + while (true) { + try { + return await webProgress.browsingContext.currentWindowGlobal + .getActor("MessageHandlerFrame") + .sendCommand(command, this._messageHandler.sessionId); + } catch (e) { + if (!retryOnAbort || e.name != "AbortError") { + // Only retry if the command supports retryOnAbort and when the + // JSWindowActor pair gets destroyed. + throw e; + } + + if (++attempts > MAX_RETRY_ATTEMPTS) { + lazy.logger.trace( + `FrameTransport reached the limit of retry attempts (${MAX_RETRY_ATTEMPTS})` + + ` for command ${name} and browsing context ${webProgress.browsingContext.id}.` + ); + throw e; + } + + lazy.logger.trace( + `FrameTransport retrying command ${name} for ` + + `browsing context ${webProgress.browsingContext.id}, attempt: ${attempts}.` + ); + await new Promise(resolve => Services.tm.dispatchToMainThread(resolve)); + } + } + } + + toString() { + return `[object ${this.constructor.name} ${this._messageHandler.name}]`; + } + + _getBrowsingContextsForDescriptor(contextDescriptor) { + const { id, type } = contextDescriptor; + + if (type === lazy.ContextDescriptorType.All) { + return this._getBrowsingContexts(); + } + + if (type === lazy.ContextDescriptorType.TopBrowsingContext) { + return this._getBrowsingContexts({ browserId: id }); + } + + // TODO: Handle other types of context descriptors. + throw new Error( + `Unsupported contextDescriptor type for broadcasting: ${type}` + ); + } + + /** + * Get all browsing contexts, optionally matching the provided options. + * + * @param {Object} options + * @param {String=} options.browserId + * The id of the browser to filter the browsing contexts by (optional). + * @return {Array<BrowsingContext>} + * The browsing contexts matching the provided options or all browsing contexts + * if no options are provided. + */ + _getBrowsingContexts(options = {}) { + // extract browserId from options + const { browserId } = options; + let browsingContexts = []; + + // Fetch all tab related browsing contexts for top-level windows. + for (const { browsingContext } of lazy.TabManager.browsers) { + if (lazy.isBrowsingContextCompatible(browsingContext, { browserId })) { + browsingContexts = browsingContexts.concat( + browsingContext.getAllBrowsingContextsInSubtree() + ); + } + } + + return browsingContexts; + } +} diff --git a/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameActor.sys.mjs b/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameActor.sys.mjs new file mode 100644 index 0000000000..b0ec801098 --- /dev/null +++ b/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameActor.sys.mjs @@ -0,0 +1,51 @@ +/* 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/. */ + +import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + ActorManagerParent: "resource://gre/modules/ActorManagerParent.sys.mjs", + + Log: "chrome://remote/content/shared/Log.sys.mjs", +}); + +XPCOMUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get()); + +const FRAME_ACTOR_CONFIG = { + parent: { + esModuleURI: + "chrome://remote/content/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameParent.sys.mjs", + }, + child: { + esModuleURI: + "chrome://remote/content/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameChild.sys.mjs", + events: { + DOMWindowCreated: {}, + }, + }, + allFrames: true, + messageManagerGroups: ["browsers"], +}; + +/** + * MessageHandlerFrameActor exposes a simple registration helper to lazily + * register MessageHandlerFrame JSWindow actors. + */ +export const MessageHandlerFrameActor = { + registered: false, + + register() { + if (this.registered) { + return; + } + + lazy.ActorManagerParent.addJSWindowActors({ + MessageHandlerFrame: FRAME_ACTOR_CONFIG, + }); + this.registered = true; + lazy.logger.trace("Registered MessageHandlerFrame actors"); + }, +}; diff --git a/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameChild.sys.mjs b/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameChild.sys.mjs new file mode 100644 index 0000000000..4f0430df1b --- /dev/null +++ b/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameChild.sys.mjs @@ -0,0 +1,77 @@ +/* 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/. */ + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + isBrowsingContextCompatible: + "chrome://remote/content/shared/messagehandler/transports/FrameContextUtils.sys.mjs", + MessageHandlerRegistry: + "chrome://remote/content/shared/messagehandler/MessageHandlerRegistry.sys.mjs", + WindowGlobalMessageHandler: + "chrome://remote/content/shared/messagehandler/WindowGlobalMessageHandler.sys.mjs", +}); + +/** + * Child actor for the MessageHandlerFrame JSWindowActor. The + * MessageHandlerFrame actor is used by FrameTransport to communicate between + * ROOT MessageHandlers and WINDOW_GLOBAL MessageHandlers. + */ +export class MessageHandlerFrameChild extends JSWindowActorChild { + actorCreated() { + this.type = lazy.WindowGlobalMessageHandler.type; + this.context = this.manager.browsingContext; + + this._registry = new lazy.MessageHandlerRegistry(this.type, this.context); + this._onRegistryEvent = this._onRegistryEvent.bind(this); + + // MessageHandlerFrameChild is responsible for forwarding events from + // WindowGlobalMessageHandler to the parent process. + // Such events are re-emitted on the MessageHandlerRegistry to avoid + // setting up listeners on individual MessageHandler instances. + this._registry.on("message-handler-registry-event", this._onRegistryEvent); + } + + handleEvent({ type }) { + if (type == "DOMWindowCreated") { + if (lazy.isBrowsingContextCompatible(this.manager.browsingContext)) { + this._registry.createAllMessageHandlers(); + } + } + } + + async receiveMessage(message) { + if (message.name === "MessageHandlerFrameParent:sendCommand") { + const { sessionId, command } = message.data; + const messageHandler = this._registry.getOrCreateMessageHandler( + sessionId + ); + try { + return await messageHandler.handleCommand(command); + } catch (e) { + if (e?.isRemoteError) { + return { + error: e.toJSON(), + isMessageHandlerError: e.isMessageHandlerError, + }; + } + throw e; + } + } + + return null; + } + + _onRegistryEvent(eventName, wrappedEvent) { + this.sendAsyncMessage( + "MessageHandlerFrameChild:messageHandlerEvent", + wrappedEvent + ); + } + + didDestroy() { + this._registry.off("message-handler-registry-event", this._onRegistryEvent); + this._registry.destroy(); + } +} diff --git a/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameParent.sys.mjs b/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameParent.sys.mjs new file mode 100644 index 0000000000..9524fb08a5 --- /dev/null +++ b/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameParent.sys.mjs @@ -0,0 +1,99 @@ +/* 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/. */ + +import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + error: "chrome://remote/content/shared/messagehandler/Errors.sys.mjs", + RootMessageHandlerRegistry: + "chrome://remote/content/shared/messagehandler/RootMessageHandlerRegistry.sys.mjs", + WindowGlobalMessageHandler: + "chrome://remote/content/shared/messagehandler/WindowGlobalMessageHandler.sys.mjs", +}); + +XPCOMUtils.defineLazyGetter(lazy, "WebDriverError", () => { + return ChromeUtils.importESModule( + "chrome://remote/content/shared/webdriver/Errors.sys.mjs" + ).error.WebDriverError; +}); + +/** + * Parent actor for the MessageHandlerFrame JSWindowActor. The + * MessageHandlerFrame actor is used by FrameTransport to communicate between + * ROOT MessageHandlers and WINDOW_GLOBAL MessageHandlers. + */ +export class MessageHandlerFrameParent extends JSWindowActorParent { + async receiveMessage(message) { + switch (message.name) { + case "MessageHandlerFrameChild:messageHandlerEvent": + const { name, contextInfo, data, sessionId } = message.data; + const [moduleName] = name.split("."); + + // Re-emit the event on the RootMessageHandler. + const messageHandler = lazy.RootMessageHandlerRegistry.getExistingMessageHandler( + sessionId + ); + // TODO: getModuleInstance expects a CommandDestination in theory, + // but only uses the MessageHandler type in practice, see Bug 1776389. + const module = messageHandler.moduleCache.getModuleInstance( + moduleName, + { type: lazy.WindowGlobalMessageHandler.type } + ); + let eventPayload = data; + + // Modify an event payload if there is a special method in the targeted module. + // If present it can be found in windowglobal-in-root module. + if (module?.interceptEvent) { + eventPayload = await module.interceptEvent(name, data); + + // Make sure that an event payload is returned. + if (!eventPayload) { + throw new Error( + `${moduleName}.interceptEvent doesn't return the event payload` + ); + } + } + messageHandler.emitEvent(name, eventPayload, contextInfo); + + break; + default: + throw new Error("Unsupported message:" + message.name); + } + } + + /** + * Send a command to the corresponding MessageHandlerFrameChild actor via a + * JSWindowActor query. + * + * @param {Command} command + * The command to forward. See type definition in MessageHandler.js + * @param {String} sessionId + * ID of the session that sent the command. + * @return {Promise} + * Promise that will resolve with the result of query sent to the + * MessageHandlerFrameChild actor. + */ + async sendCommand(command, sessionId) { + const result = await this.sendQuery( + "MessageHandlerFrameParent:sendCommand", + { + command, + sessionId, + } + ); + + if (result?.error) { + if (result.isMessageHandlerError) { + throw lazy.error.MessageHandlerError.fromJSON(result.error); + } + + // TODO: Do not assume WebDriver is the session protocol, see Bug 1779026. + throw lazy.WebDriverError.fromJSON(result.error); + } + + return result; + } +} |