Bug 1649121: part 26) Move `CollectTopMostChildContentsCompletelyInRange`. r=masayuki
[gecko.git] / remote / domains / Domain.jsm
blob4887d4dc748625d4a82cb2118515991fc3d91641
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/. */
5 "use strict";
7 var EXPORTED_SYMBOLS = ["Domain"];
9 class Domain {
10   constructor(session) {
11     this.session = session;
12     this.name = this.constructor.name;
14     this.eventListeners_ = new Set();
15     this._requestCounter = 0;
16   }
18   destructor() {}
20   emit(eventName, params = {}) {
21     for (const listener of this.eventListeners_) {
22       try {
23         if (isEventHandler(listener)) {
24           listener.onEvent(eventName, params);
25         } else {
26           listener.call(this, eventName, params);
27         }
28       } catch (e) {
29         Cu.reportError(e);
30       }
31     }
32   }
34   /**
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.
38    *
39    * This can only be called from parent domains managed by a TabSession.
40    *
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.
45    */
46   executeInChild(method, params) {
47     if (!this.session.executeInChild) {
48       throw new Error(
49         "executeInChild can only be used in Domains managed by a TabSession"
50       );
51     }
52     this._requestCounter++;
53     const id = this.name + "-" + this._requestCounter;
54     return this.session.executeInChild(id, this.name, method, params);
55   }
57   addEventListener(listener) {
58     if (typeof listener != "function" && !isEventHandler(listener)) {
59       throw new TypeError();
60     }
61     this.eventListeners_.add(listener);
62   }
64   // static
66   static implements(command) {
67     return command && typeof this.prototype[command] == "function";
68   }
71 function isEventHandler(listener) {
72   return (
73     listener && "onEvent" in listener && typeof listener.onEvent == "function"
74   );