fune/remote/webdriver-bidi/modules/ModuleRegistry.jsm
Julian Descottes 69fa6f5492 Bug 1726800 - [remote] Verify commands as early as possible in MessageHandler r=webdriver-reviewers,whimboo
Depends on D123655
With this patch, the MessageHandler can immediately check if a command is implemented by the modules, and therefore reject as early as possible.
This is implemented via a checkCommand method on MessageHandler.

Other required changes:
- ModuleRegistry now owns the logic to import BiDi modules.
- ModuleCache exposes a `getAllModuleClasses` to get all the relevant modules for a moduleName+destination pair.

Error messages have been improved and are verified with a dedicated test

Differential Revision: https://phabricator.services.mozilla.com/D123655
2021-10-12 16:48:43 +00:00

61 lines
1.7 KiB
JavaScript

/* 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/. */
"use strict";
var EXPORTED_SYMBOLS = ["getModuleClass"];
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const modules = {
root: {},
"windowglobal-in-root": {},
windowglobal: {},
};
XPCOMUtils.defineLazyModuleGetters(modules.root, {
log: "chrome://remote/content/webdriver-bidi/modules/root/log.jsm",
session: "chrome://remote/content/webdriver-bidi/modules/root/session.jsm",
});
XPCOMUtils.defineLazyModuleGetters(modules["windowglobal-in-root"], {
log:
"chrome://remote/content/webdriver-bidi/modules/windowglobal-in-root/log.jsm",
});
XPCOMUtils.defineLazyModuleGetters(modules.windowglobal, {
log: "chrome://remote/content/webdriver-bidi/modules/windowglobal/log.jsm",
});
/**
* Retrieve the WebDriver BiDi module class matching the provided module name
* and folder.
*
* @param {String} moduleName
* The name of the module to get the class for.
* @param {String} moduleFolder
* A valid folder name for modules.
* @return {Class=}
* The class corresponding to the module name and folder, null if no match
* was found.
* @throws {Error}
* If the provided module folder is unexpected.
**/
const getModuleClass = function(moduleName, moduleFolder) {
if (!modules[moduleFolder]) {
throw new Error(
`Invalid module folder "${moduleFolder}", expected one of "${Object.keys(
modules
)}"`
);
}
if (!modules[moduleFolder][moduleName]) {
return null;
}
return modules[moduleFolder][moduleName];
};