Module.sys.mjs (3746B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const lazy = {}; 6 7 ChromeUtils.defineESModuleGetters(lazy, { 8 error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs", 9 Log: "chrome://remote/content/shared/Log.sys.mjs", 10 }); 11 12 ChromeUtils.defineLazyGetter(lazy, "disabledExperimentalAPI", () => { 13 return !Services.prefs.getBoolPref("remote.experimental.enabled"); 14 }); 15 16 ChromeUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get()); 17 18 export class Module { 19 #messageHandler; 20 #moduleName; 21 22 /** 23 * Create a new module instance. 24 * 25 * @param {MessageHandler} messageHandler 26 * The MessageHandler instance which owns this Module instance. 27 */ 28 constructor(messageHandler) { 29 this.#messageHandler = messageHandler; 30 } 31 32 /** 33 * Clean-up the module instance. 34 */ 35 destroy() { 36 lazy.logger.warn( 37 `Module ${this.constructor.name} is missing a destroy method` 38 ); 39 } 40 41 /** 42 * Emit a message handler event. 43 * 44 * Such events should bubble up to the root of a MessageHandler network. 45 * 46 * @param {string} name 47 * Name of the event. Protocol level events should be of the 48 * form [module name].[event name]. 49 * @param {object} data 50 * The event's data. 51 * @param {ContextInfo=} contextInfo 52 * The event's context info, see MessageHandler:emitEvent. Optional. 53 */ 54 emitEvent(name, data, contextInfo) { 55 this.messageHandler.emitEvent(name, data, contextInfo); 56 } 57 58 /** 59 * Intercept an event and modify the payload. 60 * 61 * It's required to be implemented in windowglobal-in-root modules. 62 * 63 * @param {string} name 64 * Name of the event. 65 * @param {object} _payload 66 * The event's payload. 67 * @returns {object} 68 * The modified event payload. 69 */ 70 interceptEvent(name, _payload) { 71 throw new Error( 72 `Could not intercept event ${name}, interceptEvent is not implemented in windowglobal-in-root module` 73 ); 74 } 75 76 /** 77 * Assert if experimental commands are enabled. 78 * 79 * @param {string} methodName 80 * Name of the command. 81 * 82 * @throws {UnknownCommandError} 83 * If experimental commands are disabled. 84 */ 85 assertExperimentalCommandsEnabled(methodName) { 86 // TODO: 1778987. Move it to a BiDi specific place. 87 if (lazy.disabledExperimentalAPI) { 88 throw new lazy.error.UnknownCommandError(methodName); 89 } 90 } 91 92 /** 93 * Assert if experimental events are enabled. 94 * 95 * @param {string} moduleName 96 * Name of the module. 97 * 98 * @param {string} event 99 * Name of the event. 100 * 101 * @throws {InvalidArgumentError} 102 * If experimental events are disabled. 103 */ 104 assertExperimentalEventsEnabled(moduleName, event) { 105 // TODO: 1778987. Move it to a BiDi specific place. 106 if (lazy.disabledExperimentalAPI) { 107 throw new lazy.error.InvalidArgumentError( 108 `Module ${moduleName} does not support event ${event}` 109 ); 110 } 111 } 112 113 /** 114 * Instance shortcut for supportsMethod to avoid reaching the constructor for 115 * consumers which directly deal with an instance. 116 */ 117 supportsMethod(methodName) { 118 return this.constructor.supportsMethod(methodName); 119 } 120 121 get messageHandler() { 122 return this.#messageHandler; 123 } 124 125 static get supportedEvents() { 126 return []; 127 } 128 129 static supportsEvent(event) { 130 return this.supportedEvents.includes(event); 131 } 132 133 static supportsMethod(methodName) { 134 return typeof this.prototype[methodName] === "function"; 135 } 136 137 get moduleName() { 138 return this.#moduleName; 139 } 140 141 set moduleName(name) { 142 this.#moduleName = name; 143 } 144 }