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
|
/* 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";
var Services = require("Services");
var DevToolsUtils = require("devtools/shared/DevToolsUtils");
var { dumpn } = DevToolsUtils;
const { Pool } = require("devtools/shared/protocol/Pool");
loader.lazyRequireGetter(
this,
"DevToolsServer",
"devtools/server/devtools-server",
true
);
loader.lazyRequireGetter(
this,
"ChildDebuggerTransport",
"devtools/shared/transport/child-transport",
true
);
loader.lazyRequireGetter(this, "EventEmitter", "devtools/shared/event-emitter");
/**
* Start a DevTools server in a remote frame's process and add it as a child server for
* an active connection.
*
* @param object connection
* The devtools server connection to use.
* @param Element frame
* The frame element with remote content to connect to.
* @param function [onDestroy]
* Optional function to invoke when the child process closes or the connection
* shuts down. (Need to forget about the related target actor.)
* @return object
* A promise object that is resolved once the connection is established.
*/
function connectToFrame(connection, frame, onDestroy, { addonId } = {}) {
return new Promise(resolve => {
// Get messageManager from XUL browser (which might be a specialized tunnel for RDM)
// or else fallback to asking the frameLoader itself.
const mm = frame.messageManager || frame.frameLoader.messageManager;
mm.loadFrameScript("resource://devtools/server/startup/frame.js", false);
const spawnInParentActorPool = new Pool(
connection,
"connectToFrame-spawnInParent"
);
connection.addActor(spawnInParentActorPool);
const trackMessageManager = () => {
mm.addMessageListener("debug:setup-in-parent", onSetupInParent);
mm.addMessageListener(
"debug:spawn-actor-in-parent",
onSpawnActorInParent
);
if (!actor) {
mm.addMessageListener("debug:actor", onActorCreated);
}
};
const untrackMessageManager = () => {
mm.removeMessageListener("debug:setup-in-parent", onSetupInParent);
mm.removeMessageListener(
"debug:spawn-actor-in-parent",
onSpawnActorInParent
);
if (!actor) {
mm.removeMessageListener("debug:actor", onActorCreated);
}
};
let actor, childTransport;
const prefix = connection.allocID("child");
// Compute the same prefix that's used by DevToolsServerConnection
const connPrefix = prefix + "/";
// provides hook to actor modules that need to exchange messages
// between e10s parent and child processes
const parentModules = [];
const onSetupInParent = function(msg) {
// We may have multiple connectToFrame instance running for the same frame and
// need to filter the messages.
if (msg.json.prefix != connPrefix) {
return false;
}
const { module, setupParent } = msg.json;
let m;
try {
m = require(module);
if (!(setupParent in m)) {
dumpn(`ERROR: module '${module}' does not export '${setupParent}'`);
return false;
}
parentModules.push(m[setupParent]({ mm, prefix: connPrefix }));
return true;
} catch (e) {
const errorMessage =
"Exception during actor module setup running in the parent process: ";
DevToolsUtils.reportException(errorMessage + e);
dumpn(
`ERROR: ${errorMessage}\n\t module: '${module}'\n\t ` +
`setupParent: '${setupParent}'\n${DevToolsUtils.safeErrorString(e)}`
);
return false;
}
};
const parentActors = [];
const onSpawnActorInParent = function(msg) {
// We may have multiple connectToFrame instance running for the same tab
// and need to filter the messages.
if (msg.json.prefix != connPrefix) {
return;
}
const { module, constructor, args, spawnedByActorID } = msg.json;
let m;
try {
m = require(module);
if (!(constructor in m)) {
dump(`ERROR: module '${module}' does not export '${constructor}'`);
return;
}
const Constructor = m[constructor];
// Bind the actor to parent process connection so that these actors
// directly communicates with the client as regular actors instanciated from
// parent process
const instance = new Constructor(connection, ...args, mm);
instance.conn = connection;
instance.parentID = spawnedByActorID;
// Manually set the actor ID in order to insert parent actorID as prefix
// in order to help identifying actor hierarchy via actor IDs.
// Remove `/` as it may confuse message forwarding between processes.
const contentPrefix = spawnedByActorID
.replace(connection.prefix, "")
.replace("/", "-");
instance.actorID = connection.allocID(
contentPrefix + "/" + instance.typeName
);
spawnInParentActorPool.manage(instance);
mm.sendAsyncMessage("debug:spawn-actor-in-parent:actor", {
prefix: connPrefix,
actorID: instance.actorID,
});
parentActors.push(instance);
} catch (e) {
const errorMessage =
"Exception during actor module setup running in the parent process: ";
DevToolsUtils.reportException(errorMessage + e + "\n" + e.stack);
dumpn(
`ERROR: ${errorMessage}\n\t module: '${module}'\n\t ` +
`constructor: '${constructor}'\n${DevToolsUtils.safeErrorString(e)}`
);
}
};
const onActorCreated = DevToolsUtils.makeInfallible(function(msg) {
if (msg.json.prefix != prefix) {
return;
}
mm.removeMessageListener("debug:actor", onActorCreated);
// Pipe Debugger message from/to parent/child via the message manager
childTransport = new ChildDebuggerTransport(mm, prefix);
childTransport.hooks = {
// Pipe all the messages from content process actors back to the client
// through the parent process connection.
onPacket: connection.send.bind(connection),
};
childTransport.ready();
connection.setForwarding(prefix, childTransport);
dumpn(`Start forwarding for frame with prefix ${prefix}`);
actor = msg.json.actor;
resolve(actor);
});
const destroy = DevToolsUtils.makeInfallible(function() {
EventEmitter.off(connection, "closed", destroy);
Services.obs.removeObserver(
onMessageManagerClose,
"message-manager-close"
);
// provides hook to actor modules that need to exchange messages
// between e10s parent and child processes
parentModules.forEach(mod => {
if (mod.onDisconnected) {
mod.onDisconnected();
}
});
// TODO: Remove this deprecated path once it's no longer needed by add-ons.
DevToolsServer.emit("disconnected-from-child:" + connPrefix, {
mm,
prefix: connPrefix,
});
// Destroy all actors created via spawnActorInParentProcess
// in case content wasn't able to destroy them via a message
spawnInParentActorPool.destroy();
if (actor) {
// The FrameTargetActor within the child process doesn't necessary
// have time to uninitialize itself when the frame is closed/killed.
// So ensure telling the client that the related actor is detached.
connection.send({ from: actor.actor, type: "tabDetached" });
actor = null;
}
if (childTransport) {
// If we have a child transport, the actor has already
// been created. We need to stop using this message manager.
childTransport.close();
childTransport = null;
connection.cancelForwarding(prefix);
// ... and notify the child process to clean the target-scoped actors.
try {
// Bug 1169643: Ignore any exception as the child process
// may already be destroyed by now.
mm.sendAsyncMessage("debug:disconnect", { prefix });
} catch (e) {
// Nothing to do
}
} else {
// Otherwise, the frame has been closed before the actor
// had a chance to be created, so we are not able to create
// the actor.
resolve(null);
}
if (onDestroy) {
onDestroy(mm);
}
// Cleanup all listeners
untrackMessageManager();
});
// Listen for various messages and frame events
trackMessageManager();
// Listen for app process exit
const onMessageManagerClose = function(subject, topic, data) {
if (subject == mm) {
destroy();
}
};
Services.obs.addObserver(onMessageManagerClose, "message-manager-close");
// Listen for connection close to cleanup things
// when user unplug the device or we lose the connection somehow.
EventEmitter.on(connection, "closed", destroy);
mm.sendAsyncMessage("debug:connect", { prefix, addonId });
});
}
exports.connectToFrame = connectToFrame;
|