summaryrefslogtreecommitdiffstats
path: root/mobile/android/modules/geckoview/GeckoViewModule.sys.mjs
blob: b05244612aafbfb1280142b190fe6dfed73c5127 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* 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/. */

import { GeckoViewUtils } from "resource://gre/modules/GeckoViewUtils.sys.mjs";

const { debug, warn } = GeckoViewUtils.initLogging("Module");

export class GeckoViewModule {
  static initLogging(aModuleName) {
    const tag = aModuleName.replace("GeckoView", "");
    return GeckoViewUtils.initLogging(tag);
  }

  constructor(aModuleInfo) {
    this._info = aModuleInfo;

    this._isContentLoaded = false;
    this._eventProxy = new EventProxy(this, this.eventDispatcher);

    this.onInitBrowser();
  }

  get name() {
    return this._info.name;
  }

  get enabled() {
    return this._info.enabled;
  }

  get window() {
    return this.moduleManager.window;
  }

  getActor(aActorName) {
    return this.moduleManager.getActor(aActorName);
  }

  get browser() {
    return this.moduleManager.browser;
  }

  get messageManager() {
    return this.moduleManager.messageManager;
  }

  get eventDispatcher() {
    return this.moduleManager.eventDispatcher;
  }

  get settings() {
    return this.moduleManager.settings;
  }

  get moduleManager() {
    return this._info.manager;
  }

  // Override to initialize the browser before it is bound to the window.
  onInitBrowser() {}

  // Override to cleanup when the browser is destroyed.
  onDestroyBrowser() {}

  // Override to initialize module.
  onInit() {}

  // Override to cleanup when the window is closed
  onDestroy() {}

  // Override to detect settings change. Access settings via this.settings.
  onSettingsUpdate() {}

  // Override to enable module after setting a Java delegate.
  onEnable() {}

  // Override to disable module after clearing the Java delegate.
  onDisable() {}

  // Override to perform actions when content module has started loading;
  // by default, pause events so events that depend on content modules can work.
  onLoadContentModule() {
    this._eventProxy.enableQueuing(true);
  }

  // Override to perform actions when content module has finished loading;
  // by default, un-pause events and flush queued events.
  onContentModuleLoaded() {
    this._eventProxy.enableQueuing(false);
    this._eventProxy.dispatchQueuedEvents();
  }

  registerListener(aEventList) {
    this._eventProxy.registerListener(aEventList);
  }

  unregisterListener(aEventList) {
    this._eventProxy.unregisterListener(aEventList);
  }
}

class EventProxy {
  constructor(aListener, aEventDispatcher) {
    this.listener = aListener;
    this.eventDispatcher = aEventDispatcher;
    this._eventQueue = [];
    this._registeredEvents = [];
    this._enableQueuing = false;
  }

  registerListener(aEventList) {
    debug`registerListener ${aEventList}`;
    this.eventDispatcher.registerListener(this, aEventList);
    this._registeredEvents = this._registeredEvents.concat(aEventList);
  }

  unregisterListener(aEventList) {
    debug`unregisterListener`;
    if (this._registeredEvents.length === 0) {
      return;
    }

    if (!aEventList) {
      this.eventDispatcher.unregisterListener(this, this._registeredEvents);
      this._registeredEvents = [];
    } else {
      this.eventDispatcher.unregisterListener(this, aEventList);
      this._registeredEvents = this._registeredEvents.filter(
        e => !aEventList.includes(e)
      );
    }
  }

  onEvent(aEvent, aData) {
    if (this._enableQueuing) {
      debug`queue ${aEvent}, data=${aData}`;
      this._eventQueue.unshift(arguments);
    } else {
      this._dispatch(...arguments);
    }
  }

  enableQueuing(aEnable) {
    debug`enableQueuing ${aEnable}`;
    this._enableQueuing = aEnable;
  }

  _dispatch(aEvent, aData) {
    debug`dispatch ${aEvent}, data=${aData}`;
    if (this.listener.onEvent) {
      this.listener.onEvent(...arguments);
    } else {
      this.listener(...arguments);
    }
  }

  dispatchQueuedEvents() {
    debug`dispatchQueued`;
    while (this._eventQueue.length) {
      const args = this._eventQueue.pop();
      this._dispatch(...args);
    }
  }
}