summaryrefslogtreecommitdiffstats
path: root/remote/shared/messagehandler/transports/js-window-actors
diff options
context:
space:
mode:
Diffstat (limited to 'remote/shared/messagehandler/transports/js-window-actors')
-rw-r--r--remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameActor.sys.mjs53
-rw-r--r--remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameChild.sys.mjs111
-rw-r--r--remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameParent.sys.mjs120
3 files changed, 284 insertions, 0 deletions
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..e516e92614
--- /dev/null
+++ b/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameActor.sys.mjs
@@ -0,0 +1,53 @@
+/* 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: {},
+ pagehide: {},
+ pageshow: {},
+ },
+ },
+ 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..52a8fdc4c9
--- /dev/null
+++ b/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameChild.sys.mjs
@@ -0,0 +1,111 @@
+/* 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/BrowsingContextUtils.sys.mjs",
+ MessageHandlerRegistry:
+ "chrome://remote/content/shared/messagehandler/MessageHandlerRegistry.sys.mjs",
+ WindowGlobalMessageHandler:
+ "chrome://remote/content/shared/messagehandler/WindowGlobalMessageHandler.sys.mjs",
+});
+
+/**
+ * Map from MessageHandlerRegistry to MessageHandlerFrameChild actor. This will
+ * allow a WindowGlobalMessageHandler to find the JSWindowActorChild instance to
+ * use to send commands.
+ */
+const registryToActor = new WeakMap();
+
+/**
+ * Retrieve the MessageHandlerFrameChild which is linked to the provided
+ * WindowGlobalMessageHandler instance.
+ *
+ * @param {WindowGlobalMessageHandler} messageHandler
+ * The WindowGlobalMessageHandler for which to get the JSWindowActor.
+ * @returns {MessageHandlerFrameChild}
+ * The corresponding MessageHandlerFrameChild instance.
+ */
+export function getMessageHandlerFrameChildActor(messageHandler) {
+ return registryToActor.get(messageHandler.registry);
+}
+
+/**
+ * Child actor for the MessageHandlerFrame JSWindowActor. The
+ * MessageHandlerFrame actor is used by RootTransport 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);
+ registryToActor.set(this._registry, this);
+
+ 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({ persisted, type }) {
+ if (type == "DOMWindowCreated" || (type == "pageshow" && persisted)) {
+ // When the window is created or is retrieved from BFCache, instantiate
+ // a MessageHandler for all sessions which might need it.
+ if (lazy.isBrowsingContextCompatible(this.manager.browsingContext)) {
+ this._registry.createAllMessageHandlers();
+ }
+ } else if (type == "pagehide" && persisted) {
+ // When the page is moved to BFCache, all the currently created message
+ // handlers should be destroyed.
+ this._registry.destroy();
+ }
+ }
+
+ 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;
+ }
+
+ sendCommand(command, sessionId) {
+ return this.sendQuery("MessageHandlerFrameChild:sendCommand", {
+ command,
+ sessionId,
+ });
+ }
+
+ _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..f845b2fa01
--- /dev/null
+++ b/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameParent.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 { 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 RootTransport to communicate between
+ * ROOT MessageHandlers and WINDOW_GLOBAL MessageHandlers.
+ */
+export class MessageHandlerFrameParent extends JSWindowActorParent {
+ async receiveMessage(message) {
+ switch (message.name) {
+ case "MessageHandlerFrameChild:sendCommand": {
+ return this.#handleSendCommandMessage(message.data);
+ }
+ case "MessageHandlerFrameChild:messageHandlerEvent": {
+ return this.#handleMessageHandlerEventMessage(message.data);
+ }
+ 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.
+ * @returns {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;
+ }
+
+ async #handleMessageHandlerEventMessage(messageData) {
+ const { name, contextInfo, data, sessionId } = messageData;
+ 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);
+ }
+
+ async #handleSendCommandMessage(messageData) {
+ const { sessionId, command } = messageData;
+ const messageHandler =
+ lazy.RootMessageHandlerRegistry.getExistingMessageHandler(sessionId);
+ try {
+ return await messageHandler.handleCommand(command);
+ } catch (e) {
+ if (e?.isRemoteError) {
+ return {
+ error: e.toJSON(),
+ isMessageHandlerError: e.isMessageHandlerError,
+ };
+ }
+ throw e;
+ }
+ }
+}