andler = lazy.RootMessageHandlerRegistry.getExistingMessageHandler(sessionId); if (!messageHandler) { // If there is no message handler for the provided session id, this is a // late event for an already destroyed session, bail out. // Bug 1730913: A trace could be added here once Bug 1730913 is resolved. // Until that, this would lead to too much log pollution, because content // process modules will not be destroyed until the corresponding window // is destroyed (either closed or navigates). return; } // 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); if (eventPayload === null) { lazy.logger.trace( `${moduleName}.interceptEvent returned null, skipping event: ${name}, data: ${data}` ); return; } // 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; } } async #sendPing(command) { const commandName = `${command.moduleName}.${command.commandName}`; const destination = command.destination.id; if (this.#destroyed) { // If the JSWindowActor was destroyed already, no need to send a ping. return; } lazy.logger.trace( `MessageHandlerFrameParent command ${commandName} to ${destination} ` + `takes more than ${lazy.commandDelay / 1000} seconds to resolve, sending ping` ); try { const result = await Promise.race([ this.sendQuery("MessageHandlerFrameParent:sendPing"), new Promise(r => lazy.setTimeout(() => r(PING_TIMEOUT), PING_DELAY)), ]); if (result === PING_TIMEOUT) { lazy.logger.warn( `MessageHandlerFrameParent ping for command ${commandName} to ${destination} timed out` ); } else { lazy.logger.trace( `MessageHandlerFrameParent ping for command ${commandName} to ${destination} was successful` ); } } catch (e) { if (!this.#destroyed) { // Only swallow errors if the JSWindowActor pair was destroyed while // waiting for the ping response. throw e; } lazy.logger.trace( `MessageHandlerFrameParent ping for command ${commandName} to ${destination}` + ` lost after JSWindowActor was destroyed` ); } } } PK