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
|
/* 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";
/**
* Main entry point for DevTools in content processes.
*
* This module is loaded early when a content process is started.
* Note that (at least) JS XPCOM registered at app-startup, will be running before.
* It is used by the multiprocess browser toolbox in order to debug privileged resources.
* When debugging a Web page loaded in a Tab, DevToolsFrame JS Window Actor is used instead
* (DevToolsFrameParent.jsm and DevToolsFrameChild.jsm).
*
* This module won't do anything unless DevTools codebase starts adding some data
* in `Services.cpmm.sharedData` object or send a message manager message via `Services.cpmm`.
* Also, this module is only loaded, on-demand from process-helper if devtools are watching for process targets.
*/
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const SHARED_DATA_KEY_NAME = "DevTools:watchedPerWatcher";
class ContentProcessStartup {
constructor() {
// The map is indexed by the Watcher Actor ID.
// The values are objects containing the following properties:
// - connection: the DevToolsServerConnection itself
// - actor: the ContentProcessTargetActor instance
this._connections = new Map();
this.observe = this.observe.bind(this);
this.receiveMessage = this.receiveMessage.bind(this);
this.addListeners();
this.maybeCreateExistingTargetActors();
}
observe(subject, topic, data) {
switch (topic) {
case "xpcom-shutdown": {
this.destroy();
break;
}
}
}
destroy() {
this.removeListeners();
for (const [, connectionInfo] of this._connections) {
connectionInfo.connection.close();
}
this._connections.clear();
}
addListeners() {
Services.obs.addObserver(this.observe, "xpcom-shutdown");
Services.cpmm.addMessageListener(
"debug:instantiate-already-available",
this.receiveMessage
);
Services.cpmm.addMessageListener(
"debug:destroy-target",
this.receiveMessage
);
Services.cpmm.addMessageListener(
"debug:add-watcher-data-entry",
this.receiveMessage
);
Services.cpmm.addMessageListener(
"debug:remove-watcher-data-entry",
this.receiveMessage
);
Services.cpmm.addMessageListener(
"debug:destroy-process-script",
this.receiveMessage
);
}
removeListeners() {
Services.obs.removeObserver(this.observe, "xpcom-shutdown");
Services.cpmm.removeMessageListener(
"debug:instantiate-already-available",
this.receiveMessage
);
Services.cpmm.removeMessageListener(
"debug:destroy-target",
this.receiveMessage
);
Services.cpmm.removeMessageListener(
"debug:add-watcher-data-entry",
this.receiveMessage
);
Services.cpmm.removeMessageListener(
"debug:remove-watcher-data-entry",
this.receiveMessage
);
Services.cpmm.removeMessageListener(
"debug:destroy-process-script",
this.receiveMessage
);
}
receiveMessage(msg) {
switch (msg.name) {
case "debug:instantiate-already-available":
this.createTargetActor(
msg.data.watcherActorID,
msg.data.connectionPrefix,
msg.data.watchedData,
true
);
break;
case "debug:destroy-target":
this.destroyTarget(msg.data.watcherActorID);
break;
case "debug:add-watcher-data-entry":
this.addWatcherDataEntry(
msg.data.watcherActorID,
msg.data.type,
msg.data.entries
);
break;
case "debug:remove-watcher-data-entry":
this.addWatcherDataEntry(
msg.data.watcherActorID,
msg.data.type,
msg.data.entries
);
break;
case "debug:destroy-process-script":
this.destroy();
break;
default:
throw new Error(`Unsupported message name ${msg.name}`);
}
}
/**
* Called when the content process just started.
* This will start creating ContentProcessTarget actors, but only if DevTools code (WatcherActor / WatcherRegistry.jsm)
* put some data in `sharedData` telling us to do so.
*/
maybeCreateExistingTargetActors() {
const { sharedData } = Services.cpmm;
// Accessing `sharedData` right off the app-startup returns null.
// Spinning the event loop with dispatchToMainThread seems enough,
// but it means that we let some more Javascript code run before
// instantiating the target actor.
// So we may miss a few resources and will register the breakpoints late.
if (!sharedData) {
Services.tm.dispatchToMainThread(
this.maybeCreateExistingTargetActors.bind(this)
);
return;
}
const watchedDataByWatcherActor = sharedData.get(SHARED_DATA_KEY_NAME);
if (!watchedDataByWatcherActor) {
return;
}
// Create one Target actor for each prefix/client which listen to process
for (const [watcherActorID, watchedData] of watchedDataByWatcherActor) {
const { connectionPrefix, targets } = watchedData;
// This is where we only do something significant only if DevTools are opened
// and requesting to create target actor for content processes
if (targets.includes("process")) {
this.createTargetActor(watcherActorID, connectionPrefix, watchedData);
}
}
}
/**
* Instantiate a new ContentProcessTarget for the given connection.
* This is where we start doing some significant computation that only occurs when DevTools are opened.
*
* @param String watcherActorID
* The ID of the WatcherActor who requested to observe and create these target actors.
* @param String parentConnectionPrefix
* The prefix of the DevToolsServerConnection of the Watcher Actor.
* This is used to compute a unique ID for the target actor.
* @param Object initialData
* All data managed by the Watcher Actor and WatcherRegistry.jsm, containing
* target types, resources types to be listened as well as breakpoints and any
* other data meant to be shared across processes and threads.
* @param Object options Dictionary with optional values:
* @param Boolean options.ignoreAlreadyCreated
* If true, do not throw if the target actor has already been created.
*/
createTargetActor(
watcherActorID,
parentConnectionPrefix,
initialData,
ignoreAlreadyCreated = false
) {
if (this._connections.get(watcherActorID)) {
if (ignoreAlreadyCreated) {
return;
}
throw new Error(
"ContentProcessStartup createTargetActor was called more than once" +
` for the Watcher Actor (ID: "${watcherActorID}")`
);
}
// Compute a unique prefix, just for this content process,
// which will be used to create a ChildDebuggerTransport pair between content and parent processes.
// This is slightly hacky as we typicaly compute Prefix and Actor ID via `DevToolsServerConnection.allocID()`,
// but here, we can't have access to any DevTools connection as we are really early in the content process startup
const prefix =
parentConnectionPrefix + "contentProcess" + Services.appinfo.processID;
//TODO: probably merge content-process.jsm with this module
const { initContentProcessTarget } = ChromeUtils.import(
"resource://devtools/server/startup/content-process.jsm"
);
const { actor, connection } = initContentProcessTarget({
target: Services.cpmm,
data: {
watcherActorID,
parentConnectionPrefix,
prefix,
},
});
this._connections.set(watcherActorID, {
actor,
connection,
});
// Pass initialization data to the target actor
for (const type in initialData) {
actor.addWatcherDataEntry(type, initialData[type]);
}
}
destroyTarget(watcherActorID) {
const connectionInfo = this._connections.get(watcherActorID);
// This connection has already been cleaned?
if (!connectionInfo) {
throw new Error(
`Trying to destroy a content process target actor that doesn't exists, or has already been destroyed. Watcher Actor ID:${watcherActorID}`
);
}
connectionInfo.connection.close();
this._connections.delete(watcherActorID);
}
async addWatcherDataEntry(watcherActorID, type, entries) {
const connectionInfo = this._connections.get(watcherActorID);
if (!connectionInfo) {
throw new Error(
`No content process target actor for this Watcher Actor ID:"${watcherActorID}"`
);
}
const { actor } = connectionInfo;
await actor.addWatcherDataEntry(type, entries);
Services.cpmm.sendAsyncMessage("debug:add-watcher-data-entry-done", {
watcherActorID,
});
}
removeWatcherDataEntry(watcherActorID, type, entries) {
const connectionInfo = this._connections.get(watcherActorID);
if (!connectionInfo) {
return;
}
const { actor } = connectionInfo;
actor.removeWatcherDataEntry(type, entries);
}
}
// Only start this component for content processes.
// i.e. explicitely avoid running it for the parent process
if (Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT) {
new ContentProcessStartup();
}
|