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
|
/* 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 { Domain } from "chrome://remote/content/cdp/domains/Domain.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
ContextualIdentityService:
"resource://gre/modules/ContextualIdentityService.sys.mjs",
generateUUID: "chrome://remote/content/shared/UUID.sys.mjs",
TabManager: "chrome://remote/content/shared/TabManager.sys.mjs",
TabSession: "chrome://remote/content/cdp/sessions/TabSession.sys.mjs",
UserContextManager:
"chrome://remote/content/shared/UserContextManager.sys.mjs",
windowManager: "chrome://remote/content/shared/WindowManager.sys.mjs",
});
let browserContextIds = 1;
// Default filter from CDP specification
const defaultFilter = [
{ type: "browser", exclude: true },
{ type: "tab", exclude: true },
{},
];
export class Target extends Domain {
#browserContextIds;
#discoverTargetFilter;
constructor(session) {
super(session);
this.#browserContextIds = new Set();
this._onTargetCreated = this._onTargetCreated.bind(this);
this._onTargetDestroyed = this._onTargetDestroyed.bind(this);
}
getBrowserContexts() {
const browserContextIds =
lazy.ContextualIdentityService.getPublicUserContextIds().filter(id =>
this.#browserContextIds.has(id)
);
return { browserContextIds };
}
createBrowserContext() {
const identity = lazy.ContextualIdentityService.create(
"remote-agent-" + browserContextIds++
);
this.#browserContextIds.add(identity.userContextId);
return { browserContextId: identity.userContextId };
}
disposeBrowserContext(options = {}) {
const { browserContextId } = options;
lazy.ContextualIdentityService.remove(browserContextId);
lazy.ContextualIdentityService.closeContainerTabs(browserContextId);
this.#browserContextIds.delete(browserContextId);
}
getTargets(options = {}) {
const { filter = defaultFilter } = options;
const { targetList } = this.session.target;
this._validateTargetFilter(filter);
const targetInfos = [...targetList]
.filter(target => this._filterIncludesTarget(target, filter))
.map(target => this._getTargetInfo(target));
return {
targetInfos,
};
}
setDiscoverTargets(options = {}) {
const { discover, filter } = options;
const { targetList } = this.session.target;
if (typeof discover !== "boolean") {
throw new TypeError("discover: boolean value expected");
}
if (discover === false && filter !== undefined) {
throw new Error("filter: should not be present when discover is false");
}
// null filter should not be defaulted
const targetFilter = filter === undefined ? defaultFilter : filter;
this._validateTargetFilter(targetFilter);
// Store active filter for filtering in event listeners (targetCreated, targetDestroyed, targetInfoChanged)
this.#discoverTargetFilter = targetFilter;
if (discover) {
targetList.on("target-created", this._onTargetCreated);
targetList.on("target-destroyed", this._onTargetDestroyed);
for (const target of targetList) {
this._onTargetCreated("target-created", target);
}
} else {
targetList.off("target-created", this._onTargetCreated);
targetList.off("target-destroyed", this._onTargetDestroyed);
}
}
async createTarget(options = {}) {
const { browserContextId, url } = options;
if (typeof url !== "string") {
throw new TypeError("url: string value expected");
}
let validURL;
try {
validURL = Services.io.newURI(url);
} catch (e) {
// If we failed to parse given URL, use about:blank instead
validURL = Services.io.newURI("about:blank");
}
const { targetList, window } = this.session.target;
const onTarget = targetList.once("target-created");
const tab = await lazy.TabManager.addTab({
focus: true,
userContextId:
// Bug 1878649: Use UserContextManager ids consistently in CDP.
lazy.UserContextManager.getIdByInternalId(browserContextId),
window,
});
const target = await onTarget;
if (tab.linkedBrowser != target.browser) {
throw new Error(
"Unexpected tab opened: " + tab.linkedBrowser.currentURI.spec
);
}
target.browsingContext.loadURI(validURL, {
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
});
return { targetId: target.id };
}
async closeTarget(options = {}) {
const { targetId } = options;
const { targetList } = this.session.target;
const target = targetList.getById(targetId);
if (!target) {
throw new Error(`Unable to find target with id '${targetId}'`);
}
await lazy.TabManager.removeTab(target.tab);
}
async activateTarget(options = {}) {
const { targetId } = options;
const { targetList, window } = this.session.target;
const target = targetList.getById(targetId);
if (!target) {
throw new Error(`Unable to find target with id '${targetId}'`);
}
// Focus the window, and select the corresponding tab
await lazy.windowManager.focusWindow(window);
await lazy.TabManager.selectTab(target.tab);
}
attachToTarget(options = {}) {
const { targetId } = options;
const { targetList } = this.session.target;
const target = targetList.getById(targetId);
if (!target) {
throw new Error(`Unable to find target with id '${targetId}'`);
}
const tabSession = new lazy.TabSession(
this.session.connection,
target,
lazy.generateUUID()
);
this.session.connection.registerSession(tabSession);
this._emitAttachedToTarget(target, tabSession);
return {
sessionId: tabSession.id,
};
}
setAutoAttach() {}
sendMessageToTarget(options = {}) {
const { sessionId, message } = options;
const { connection } = this.session;
connection.sendMessageToTarget(sessionId, message);
}
/**
* Internal methods: the following methods are not part of CDP;
* note the _ prefix.
*/
_emitAttachedToTarget(target, tabSession) {
const targetInfo = this._getTargetInfo(target);
this.emit("Target.attachedToTarget", {
targetInfo,
sessionId: tabSession.id,
waitingForDebugger: false,
});
}
_getTargetInfo(target) {
const attached = [...this.session.connection.sessions.values()].some(
session => session.target.id === target.id
);
return {
targetId: target.id,
type: target.type,
title: target.title,
url: target.url,
attached,
browserContextId: target.browserContextId,
};
}
_filterIncludesTarget(target, filter) {
for (const entry of filter) {
if ([undefined, target.type].includes(entry.type)) {
return !entry.exclude;
}
}
return false;
}
_validateTargetFilter(filter) {
if (!Array.isArray(filter)) {
throw new TypeError("filter: array value expected");
}
for (const entry of filter) {
if (entry === null || Array.isArray(entry) || typeof entry !== "object") {
throw new TypeError("filter: object values expected in array");
}
if (!["undefined", "string"].includes(typeof entry.type)) {
throw new TypeError("filter: type: string value expected");
}
if (!["undefined", "boolean"].includes(typeof entry.exclude)) {
throw new TypeError("filter: exclude: boolean value expected");
}
}
}
_onTargetCreated(eventName, target) {
if (!this._filterIncludesTarget(target, this.#discoverTargetFilter)) {
return;
}
const targetInfo = this._getTargetInfo(target);
this.emit("Target.targetCreated", { targetInfo });
}
_onTargetDestroyed(eventName, target) {
if (!this._filterIncludesTarget(target, this.#discoverTargetFilter)) {
return;
}
this.emit("Target.targetDestroyed", {
targetId: target.id,
});
}
}
|