summaryrefslogtreecommitdiffstats
path: root/remote/shared/messagehandler/transports/js-window-actors/MessageHandlerFrameChild.sys.mjs
blob: 52a8fdc4c99e82770cba615c3cb9d82286f998fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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();
  }
}