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/WindowGlobalMessageHandler.sys.mjs | |
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/WindowGlobalMessageHandler.sys.mjs')
-rw-r--r-- | remote/shared/messagehandler/WindowGlobalMessageHandler.sys.mjs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/remote/shared/messagehandler/WindowGlobalMessageHandler.sys.mjs b/remote/shared/messagehandler/WindowGlobalMessageHandler.sys.mjs new file mode 100644 index 0000000000..109bc81d1f --- /dev/null +++ b/remote/shared/messagehandler/WindowGlobalMessageHandler.sys.mjs @@ -0,0 +1,120 @@ +/* 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 { + ContextDescriptorType, + MessageHandler, +} from "chrome://remote/content/shared/messagehandler/MessageHandler.sys.mjs"; + +/** + * A WindowGlobalMessageHandler is dedicated to debugging a single window + * global. It follows the lifecycle of the corresponding window global and will + * therefore not survive any navigation. This MessageHandler cannot forward + * commands further to other MessageHandlers and represents a leaf node in a + * MessageHandler network. + */ +export class WindowGlobalMessageHandler extends MessageHandler { + #innerWindowId; + + constructor() { + super(...arguments); + + this.#innerWindowId = this.context.window.windowGlobalChild.innerWindowId; + } + + /** + * Returns the WindowGlobalMessageHandler module path. + * + * @return {String} + */ + static get modulePath() { + return "windowglobal"; + } + + /** + * Returns the WindowGlobalMessageHandler type. + * + * @return {String} + */ + static get type() { + return "WINDOW_GLOBAL"; + } + + /** + * For WINDOW_GLOBAL MessageHandlers, `context` is a BrowsingContext, + * and BrowsingContext.id can be used as the context id. + * + * @param {BrowsingContext} context + * WindowGlobalMessageHandler contexts are expected to be + * BrowsingContexts. + * @return {String} + * The browsing context id. + */ + static getIdFromContext(context) { + return context.id; + } + + get innerWindowId() { + return this.#innerWindowId; + } + + get window() { + return this.context.window; + } + + async applyInitialSessionDataItems(sessionDataItems) { + if (!Array.isArray(sessionDataItems)) { + return; + } + + const destination = { + type: WindowGlobalMessageHandler.type, + }; + + const sessionDataPromises = sessionDataItems.map(sessionDataItem => { + const { moduleName, category, contextDescriptor } = sessionDataItem; + if (!this.matchesContext(contextDescriptor)) { + return Promise.resolve(); + } + + // Don't apply session data if the module is not present + // for the destination. + if (!this.moduleCache.hasModule(moduleName, destination)) { + return Promise.resolve(); + } + + return this.handleCommand({ + moduleName, + commandName: "_applySessionData", + params: { + category, + sessionData: sessionDataItems, + }, + destination, + }); + }); + + await Promise.all(sessionDataPromises); + + // With the session data applied the handler is now ready to be used. + this.emitEvent("window-global-handler-created", { + contextId: this.contextId, + innerWindowId: this.#innerWindowId, + }); + } + + forwardCommand(command) { + throw new Error( + `Cannot forward commands from a "WINDOW_GLOBAL" MessageHandler` + ); + } + + matchesContext(contextDescriptor) { + return ( + contextDescriptor.type === ContextDescriptorType.All || + (contextDescriptor.type === ContextDescriptorType.TopBrowsingContext && + contextDescriptor.id === this.context.browserId) + ); + } +} |