This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathActorManagerChild.jsm
320 lines (262 loc) · 8.02 KB
/
ActorManagerChild.jsm
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* vim: set ts=2 sw=2 sts=2 et tw=80: */
/* 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";
/**
* This module implements logic for creating JavaScript IPC actors, as defined
* in ActorManagerParent, for frame message manager contexts. See
* ActorManagerParent.jsm for more information.
*/
var EXPORTED_SYMBOLS = ["ActorManagerChild"];
ChromeUtils.import("resource://gre/modules/ExtensionUtils.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.defineModuleGetter(this, "WebNavigationFrames",
"resource://gre/modules/WebNavigationFrames.jsm");
const {DefaultMap} = ExtensionUtils;
const {sharedData} = Services.cpmm;
XPCOMUtils.defineLazyPreferenceGetter(this, "simulateFission",
"browser.fission.simulate", false);
function getMessageManager(window) {
return window.docShell.messageManager;
}
class Dispatcher {
constructor(mm, data) {
this.mm = mm;
this.actors = data.actors;
this.events = data.events;
this.messages = data.messages;
this.observers = data.observers;
this.instances = new Map();
}
init() {
for (let msg of this.messages.keys()) {
// This is directly called on the message manager
// because this.addMessageListener is meant to handle
// additions after initialization.
this.mm.addMessageListener(msg, this);
}
for (let topic of this.observers.keys()) {
Services.obs.addObserver(this, topic, true);
}
for (let {event, options, actor} of this.events) {
this.addEventListener(event, actor, options);
}
this.mm.addEventListener("unload", this);
}
cleanup() {
for (let topic of this.observers.keys()) {
Services.obs.removeObserver(this, topic);
}
}
get window() {
return this.mm.content;
}
get frameId() {
// 0 for top-level windows, outerWindowId otherwise
return WebNavigationFrames.getFrameId(this.window);
}
get browsingContextId() {
return this.window.docShell.browsingContext.id;
}
addEventListener(event, actor, options) {
let listener = this.handleActorEvent.bind(this, actor);
this.mm.addEventListener(event, listener, options);
}
addMessageListener(msg, actor) {
let actors = this.messages.get(msg);
if (!actors) {
actors = [];
this.messages.set(msg, actors);
}
if (actors.length == 0) {
this.mm.addMessageListener(msg, this);
}
if (!actors.includes(actor)) {
actors.push(actor);
}
}
getActor(actorName) {
let inst = this.instances.get(actorName);
if (!inst) {
let actor = this.actors.get(actorName);
let obj = {};
ChromeUtils.import(actor.module, obj);
inst = new obj[actorName](this);
this.instances.set(actorName, inst);
}
return inst;
}
handleEvent(event) {
if (event.type == "unload") {
this.cleanup();
}
}
handleActorEvent(actor, event) {
let targetWindow = null;
if (simulateFission) {
targetWindow = event.target.ownerGlobal;
if (targetWindow != this.window) {
// events can't propagate across frame boundaries because the
// frames will be hosted on separated processes.
return;
}
}
this.getActor(actor).handleEvent(event);
}
receiveMessage(message) {
let actors = this.messages.get(message.name);
if (simulateFission) {
let match = false;
let data = message.data || {};
if (data.hasOwnProperty("frameId")) {
match = (data.frameId == this.frameId);
} else if (data.hasOwnProperty("browsingContextId")) {
match = (data.browsingContextId == this.browsingContextId);
} else {
// if no specific target was given, just dispatch it to
// top-level actors.
match = (this.frameId == 0);
}
if (!match) {
return;
}
}
for (let actor of actors) {
try {
this.getActor(actor).receiveMessage(message);
} catch (e) {
Cu.reportError(e);
}
}
}
observe(subject, topic, data) {
let actors = this.observers.get(topic);
for (let actor of actors) {
try {
this.getActor(actor).observe(subject, topic, data);
} catch (e) {
Cu.reportError(e);
}
}
}
}
Dispatcher.prototype.QueryInterface =
ChromeUtils.generateQI(["nsIObserver",
"nsISupportsWeakReference"]);
class SingletonDispatcher extends Dispatcher {
constructor(window, data) {
super(getMessageManager(window), data);
window.addEventListener("pageshow", this, {mozSystemGroup: true});
window.addEventListener("pagehide", this, {mozSystemGroup: true});
this._window = window;
this.listeners = [];
}
init() {
super.init();
for (let actor of this.instances.values()) {
if (typeof actor.init === "function") {
try {
actor.init();
} catch (e) {
Cu.reportError(e);
}
}
}
}
cleanup() {
super.cleanup();
for (let msg of this.messages.keys()) {
this.mm.removeMessageListener(msg, this);
}
for (let [event, listener, options] of this.listeners) {
this.mm.removeEventListener(event, listener, options);
}
for (let actor of this.instances.values()) {
try {
actor.cleanup();
} catch (e) {
Cu.reportError(e);
}
}
this.listeners = [];
}
get window() {
return this._window;
}
handleEvent(event) {
if (event.type == "pageshow") {
if (this.hidden) {
this.init();
}
this.hidden = false;
} else if (event.type == "pagehide") {
this.hidden = true;
this.cleanup();
}
}
handleActorEvent(actor, event) {
if (event.target.ownerGlobal == this.window) {
this.getActor(actor).handleEvent(event);
}
}
addEventListener(event, actor, options) {
let listener = this.handleActorEvent.bind(this, actor);
this.listeners.push([event, listener, options]);
this.mm.addEventListener(event, listener, options);
}
}
/* globals MatchPatternSet, MozDocumentMatcher, MozDocumentObserver */
var ActorManagerChild = {
groups: new DefaultMap(group => {
return sharedData.get(`ChildActors:${group || ""}`);
}),
singletons: new Map(),
init() {
let singletons = sharedData.get("ChildSingletonActors");
for (let [filter, data] of singletons.entries()) {
let options = {
matches: new MatchPatternSet(filter.matches, {restrictSchemes: false}),
allFrames: filter.allFrames,
matchAboutBlank: filter.matchAboutBlank,
};
this.singletons.set(new MozDocumentMatcher(options), data);
}
this.observer = new MozDocumentObserver(this);
this.observer.observe(this.singletons.keys());
this.init = null;
},
/**
* MozDocumentObserver callbacks. These handle instantiating singleton actors
* for documents which match their MozDocumentMatcher filters.
*/
onNewDocument(matcher, window) {
new SingletonDispatcher(window, this.singletons.get(matcher)).init();
},
onPreloadDocument(matcher, loadInfo) {
},
/**
* Attaches the appropriate set of actors to the given frame message manager.
*
* @param {ContentFrameMessageManager} mm
* The message manager to which to attach the actors.
* @param {string} [group]
* The messagemanagergroup of the <browser> to which the caller frame
* script belongs. This restricts the attached set of actors based on
* the "group" that their actor definitions specify.
*/
attach(mm, group = null) {
new Dispatcher(mm, this.groups.get(group)).init();
},
getActor(mm, actorName) {
for (let dispatcher of this.dispatchers.get(mm)) {
let actor = dispatcher.getActor(actorName);
if (actor) {
return actor;
}
}
return null;
},
};
ActorManagerChild.init();