fune/remote/shared/messagehandler/test/browser/browser_windowglobal_to_root.js
Julian Descottes e1574164ba Bug 1831411 - [remote] Commands send to root modules from windowglobal should run in the parent r=webdriver-reviewers,Sasha
Depends on D177181

Quick summary:

Handling a command is mostly done in 2 steps
- first check if there is a module in the current layer which matches the command
- if yes: send the command to this module, if no: try to forward the command (using forwardCommand which has different implementations for each message handler)

The initial patch to allow windowglobal -> root communication was flawed, because it updated the ModuleCache to return "root" modules when the destination was of type ROOT. However that's not how MessageHandler modules are supposed to work. The ModuleCache should only ever return modules which can be loaded in the current "layer". So if we are in a windowglobal ModuleCache, we should never return a "root" module.

Instead the ModuleCache should return (and store) `null` for this load attempt, so that the MessageHandler can go to the `forwardCommand` step.
So here I slightly changed the ModuleCache implementation to have a single `#getModuleClass` private helper, which can quickly check if we are in a "not root" -> "root" scenario and just return null early.

Follow up: I think some of the logic currently kept in the module cache should move to the MessageHandler, and the ModuleCache should only care about caching module classes/instances. All the destination-to-folder logic is really tied to handling commands, so that should be done by the base MessageHandler class. Or at least it should be closer to the `handleCommand` implementation.

Differential Revision: https://phabricator.services.mozilla.com/D177202
2023-05-05 13:38:21 +00:00

47 lines
1.3 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { RootMessageHandler } = ChromeUtils.importESModule(
"chrome://remote/content/shared/messagehandler/RootMessageHandler.sys.mjs"
);
add_task(async function test_windowGlobal_to_root_command() {
// Navigate to a page to make sure that the windowglobal modules run in a
// different process than the root module.
const tab = BrowserTestUtils.addTab(
gBrowser,
"https://example.com/document-builder.sjs?html=tab"
);
await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
const browsingContextId = tab.linkedBrowser.browsingContext.id;
const rootMessageHandler = createRootMessageHandler(
"session-id-windowglobal-to-rootModule"
);
for (const commandName of [
"testHandleCommandToRoot",
"testSendRootCommand",
]) {
const valueFromRoot = await rootMessageHandler.handleCommand({
moduleName: "windowglobaltoroot",
commandName,
destination: {
type: WindowGlobalMessageHandler.type,
id: browsingContextId,
},
});
is(
valueFromRoot,
"root-value-called-from-windowglobal",
"Retrieved the expected value from windowglobaltoroot using " +
commandName
);
}
rootMessageHandler.destroy();
gBrowser.removeTab(tab);
});