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/. */
7 var EXPORTED_SYMBOLS = ["Domain"];
10 constructor(session) {
11 this.session = session;
12 this.name = this.constructor.name;
14 this.eventListeners_ = new Set();
15 this._requestCounter = 0;
20 emit(eventName, params = {}) {
21 for (const listener of this.eventListeners_) {
23 if (isEventHandler(listener)) {
24 listener.onEvent(eventName, params);
26 listener.call(this, eventName, params);
35 * Execute the provided method in the child domain that has the same domain
36 * name. eg. calling this.executeInChild from domains/parent/Input.jsm will
37 * attempt to execute the method in domains/content/Input.jsm.
39 * This can only be called from parent domains managed by a TabSession.
41 * @param {String} method
42 * Name of the method to call on the child domain.
43 * @param {Object} params
44 * Optional parameters. Must be serializable.
46 executeInChild(method, params) {
47 if (!this.session.executeInChild) {
49 "executeInChild can only be used in Domains managed by a TabSession"
52 this._requestCounter++;
53 const id = this.name + "-" + this._requestCounter;
54 return this.session.executeInChild(id, this.name, method, params);
57 addEventListener(listener) {
58 if (typeof listener != "function" && !isEventHandler(listener)) {
59 throw new TypeError();
61 this.eventListeners_.add(listener);
66 static implements(command) {
67 return command && typeof this.prototype[command] == "function";
71 function isEventHandler(listener) {
73 listener && "onEvent" in listener && typeof listener.onEvent == "function"