-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Realm.ts
338 lines (292 loc) · 8.61 KB
/
Realm.ts
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* @license
* Copyright 2024 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {EventEmitter} from '../../common/EventEmitter.js';
import {inertIfDisposed, throwIfDisposed} from '../../util/decorators.js';
import {DisposableStack, disposeSymbol} from '../../util/disposable.js';
import type {BidiConnection} from '../Connection.js';
import type {Browser} from './Browser.js';
import type {BrowsingContext} from './BrowsingContext.js';
import type {Session} from './Session.js';
/**
* @internal
*/
export type CallFunctionOptions = Omit<
Bidi.Script.CallFunctionParameters,
'functionDeclaration' | 'awaitPromise' | 'target'
>;
/**
* @internal
*/
export type EvaluateOptions = Omit<
Bidi.Script.EvaluateParameters,
'expression' | 'awaitPromise' | 'target'
>;
/**
* @internal
*/
export abstract class Realm extends EventEmitter<{
/** Emitted whenever the realm has updated. */
updated: Realm;
/** Emitted when the realm is destroyed. */
destroyed: {reason: string};
/** Emitted when a dedicated worker is created in the realm. */
worker: DedicatedWorkerRealm;
/** Emitted when a shared worker is created in the realm. */
sharedworker: SharedWorkerRealm;
}> {
#reason?: string;
protected readonly disposables = new DisposableStack();
readonly id: string;
readonly origin: string;
protected executionContextId?: number;
protected constructor(id: string, origin: string) {
super();
this.id = id;
this.origin = origin;
}
get disposed(): boolean {
return this.#reason !== undefined;
}
protected abstract get session(): Session;
get target(): Bidi.Script.Target {
return {realm: this.id};
}
@inertIfDisposed
protected dispose(reason?: string): void {
this.#reason = reason;
this[disposeSymbol]();
}
@throwIfDisposed<Realm>(realm => {
// SAFETY: Disposal implies this exists.
return realm.#reason!;
})
async disown(handles: string[]): Promise<void> {
await this.session.send('script.disown', {
target: this.target,
handles,
});
}
@throwIfDisposed<Realm>(realm => {
// SAFETY: Disposal implies this exists.
return realm.#reason!;
})
async callFunction(
functionDeclaration: string,
awaitPromise: boolean,
options: CallFunctionOptions = {}
): Promise<Bidi.Script.EvaluateResult> {
const {result} = await this.session.send('script.callFunction', {
functionDeclaration,
awaitPromise,
target: this.target,
...options,
});
return result;
}
@throwIfDisposed<Realm>(realm => {
// SAFETY: Disposal implies this exists.
return realm.#reason!;
})
async evaluate(
expression: string,
awaitPromise: boolean,
options: EvaluateOptions = {}
): Promise<Bidi.Script.EvaluateResult> {
const {result} = await this.session.send('script.evaluate', {
expression,
awaitPromise,
target: this.target,
...options,
});
return result;
}
@throwIfDisposed<Realm>(realm => {
// SAFETY: Disposal implies this exists.
return realm.#reason!;
})
async resolveExecutionContextId(): Promise<number> {
if (!this.executionContextId) {
const {result} = await (this.session.connection as BidiConnection).send(
'cdp.resolveRealm',
{realm: this.id}
);
this.executionContextId = result.executionContextId;
}
return this.executionContextId;
}
[disposeSymbol](): void {
this.#reason ??=
'Realm already destroyed, probably because all associated browsing contexts closed.';
this.emit('destroyed', {reason: this.#reason});
this.disposables.dispose();
super[disposeSymbol]();
}
}
/**
* @internal
*/
export class WindowRealm extends Realm {
static from(context: BrowsingContext, sandbox?: string): WindowRealm {
const realm = new WindowRealm(context, sandbox);
realm.#initialize();
return realm;
}
readonly browsingContext: BrowsingContext;
readonly sandbox?: string;
readonly #workers = new Map<string, DedicatedWorkerRealm>();
private constructor(context: BrowsingContext, sandbox?: string) {
super('', '');
this.browsingContext = context;
this.sandbox = sandbox;
}
#initialize(): void {
const browsingContextEmitter = this.disposables.use(
new EventEmitter(this.browsingContext)
);
browsingContextEmitter.on('closed', ({reason}) => {
this.dispose(reason);
});
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
sessionEmitter.on('script.realmCreated', info => {
if (
info.type !== 'window' ||
info.context !== this.browsingContext.id ||
info.sandbox !== this.sandbox
) {
return;
}
(this as any).id = info.realm;
(this as any).origin = info.origin;
this.executionContextId = undefined;
this.emit('updated', this);
});
sessionEmitter.on('script.realmCreated', info => {
if (info.type !== 'dedicated-worker') {
return;
}
if (!info.owners.includes(this.id)) {
return;
}
const realm = DedicatedWorkerRealm.from(this, info.realm, info.origin);
this.#workers.set(realm.id, realm);
const realmEmitter = this.disposables.use(new EventEmitter(realm));
realmEmitter.once('destroyed', () => {
realmEmitter.removeAllListeners();
this.#workers.delete(realm.id);
});
this.emit('worker', realm);
});
}
override get session(): Session {
return this.browsingContext.userContext.browser.session;
}
override get target(): Bidi.Script.Target {
return {context: this.browsingContext.id, sandbox: this.sandbox};
}
}
/**
* @internal
*/
export type DedicatedWorkerOwnerRealm =
| DedicatedWorkerRealm
| SharedWorkerRealm
| WindowRealm;
/**
* @internal
*/
export class DedicatedWorkerRealm extends Realm {
static from(
owner: DedicatedWorkerOwnerRealm,
id: string,
origin: string
): DedicatedWorkerRealm {
const realm = new DedicatedWorkerRealm(owner, id, origin);
realm.#initialize();
return realm;
}
readonly #workers = new Map<string, DedicatedWorkerRealm>();
readonly owners: Set<DedicatedWorkerOwnerRealm>;
private constructor(
owner: DedicatedWorkerOwnerRealm,
id: string,
origin: string
) {
super(id, origin);
this.owners = new Set([owner]);
}
#initialize(): void {
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
sessionEmitter.on('script.realmDestroyed', info => {
if (info.realm !== this.id) {
return;
}
this.dispose('Realm already destroyed.');
});
sessionEmitter.on('script.realmCreated', info => {
if (info.type !== 'dedicated-worker') {
return;
}
if (!info.owners.includes(this.id)) {
return;
}
const realm = DedicatedWorkerRealm.from(this, info.realm, info.origin);
this.#workers.set(realm.id, realm);
const realmEmitter = this.disposables.use(new EventEmitter(realm));
realmEmitter.once('destroyed', () => {
this.#workers.delete(realm.id);
});
this.emit('worker', realm);
});
}
override get session(): Session {
// SAFETY: At least one owner will exist.
return this.owners.values().next().value.session;
}
}
/**
* @internal
*/
export class SharedWorkerRealm extends Realm {
static from(browser: Browser, id: string, origin: string): SharedWorkerRealm {
const realm = new SharedWorkerRealm(browser, id, origin);
realm.#initialize();
return realm;
}
readonly #workers = new Map<string, DedicatedWorkerRealm>();
readonly browser: Browser;
private constructor(browser: Browser, id: string, origin: string) {
super(id, origin);
this.browser = browser;
}
#initialize(): void {
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
sessionEmitter.on('script.realmDestroyed', info => {
if (info.realm !== this.id) {
return;
}
this.dispose('Realm already destroyed.');
});
sessionEmitter.on('script.realmCreated', info => {
if (info.type !== 'dedicated-worker') {
return;
}
if (!info.owners.includes(this.id)) {
return;
}
const realm = DedicatedWorkerRealm.from(this, info.realm, info.origin);
this.#workers.set(realm.id, realm);
const realmEmitter = this.disposables.use(new EventEmitter(realm));
realmEmitter.once('destroyed', () => {
this.#workers.delete(realm.id);
});
this.emit('worker', realm);
});
}
override get session(): Session {
return this.browser.session;
}
}