chore: handle disposal of core/bidi resources (#11730)

This commit is contained in:
jrandolf 2024-01-24 10:10:42 +01:00 committed by GitHub
parent bc7bd01d85
commit 69e44fc808
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 612 additions and 340 deletions

View File

@ -126,11 +126,11 @@ export class BidiBrowser extends Browser {
}
#initialize() {
this.#browserCore.once('disconnect', () => {
this.#browserCore.once('disconnected', () => {
this.emit(BrowserEvent.Disconnected, undefined);
});
this.#process?.once('close', () => {
this.#browserCore.dispose('Browser process closed.', true);
this.#process?.once('close', async () => {
this.#browserCore.dispose('Browser process exited.', true);
this.connection.dispose();
});

View File

@ -7,7 +7,8 @@
import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {EventEmitter} from '../../common/EventEmitter.js';
import {throwIfDisposed} from '../../util/decorators.js';
import {inertIfDisposed, throwIfDisposed} from '../../util/decorators.js';
import {DisposableStack, disposeSymbol} from '../../util/disposable.js';
import type {BrowsingContext} from './BrowsingContext.js';
import type {SharedWorkerRealm} from './Realm.js';
@ -28,13 +29,13 @@ export type AddPreloadScriptOptions = Omit<
* @internal
*/
export class Browser extends EventEmitter<{
/** Emitted after the browser closes. */
/** Emitted before the browser closes. */
closed: {
/** The reason for closing the browser. */
reason: string;
};
/** Emitted after the browser disconnects. */
disconnect: {
disconnected: {
/** The reason for disconnecting the browser. */
reason: string;
};
@ -51,14 +52,15 @@ export class Browser extends EventEmitter<{
}
// keep-sorted start
#closed = false;
#reason: string | undefined;
readonly #disposables = new DisposableStack();
readonly #userContexts = new Map();
readonly session: Session;
// keep-sorted end
private constructor(session: Session) {
super();
// keep-sorted start
this.session = session;
// keep-sorted end
@ -67,49 +69,44 @@ export class Browser extends EventEmitter<{
}
async #initialize() {
// ///////////////////////
// Session listeners //
// ///////////////////////
const session = this.#session;
session.on('script.realmCreated', info => {
const sessionEmitter = this.#disposables.use(
new EventEmitter(this.session)
);
sessionEmitter.once('ended', ({reason}) => {
this.dispose(reason);
});
sessionEmitter.on('script.realmCreated', info => {
if (info.type === 'shared-worker') {
// TODO: Create a SharedWorkerRealm.
}
});
// ///////////////////
// Parent listeners //
// ///////////////////
this.session.once('ended', ({reason}) => {
this.dispose(reason);
});
await this.#syncBrowsingContexts();
}
// //////////////////////////////
// Asynchronous initialization //
// //////////////////////////////
async #syncBrowsingContexts() {
// In case contexts are created or destroyed during `getTree`, we use this
// set to detect them.
const contextIds = new Set<string>();
const created = (info: {context: string}) => {
let contexts: Bidi.BrowsingContext.Info[];
{
using sessionEmitter = new EventEmitter(this.session);
sessionEmitter.on('browsingContext.contextCreated', info => {
contextIds.add(info.context);
};
const destroyed = (info: {context: string}) => {
});
sessionEmitter.on('browsingContext.contextDestroyed', info => {
contextIds.delete(info.context);
};
session.on('browsingContext.contextCreated', created);
session.on('browsingContext.contextDestroyed', destroyed);
const {
result: {contexts},
} = await session.send('browsingContext.getTree', {});
session.off('browsingContext.contextDestroyed', destroyed);
session.off('browsingContext.contextCreated', created);
});
const {result} = await this.session.send('browsingContext.getTree', {});
contexts = result.contexts;
}
// Simulating events so contexts are created naturally.
for (const info of contexts) {
if (contextIds.has(info.context)) {
session.emit('browsingContext.contextCreated', info);
this.session.emit('browsingContext.contextCreated', info);
}
if (info.children) {
contexts.push(...info.children);
@ -117,48 +114,45 @@ export class Browser extends EventEmitter<{
}
}
get #session() {
return this.session;
// keep-sorted start block=yes
get closed(): boolean {
return this.#closed;
}
get disposed(): boolean {
return this.#reason !== undefined;
}
get defaultUserContext(): UserContext {
// SAFETY: A UserContext is always created for the default context.
return this.#userContexts.get('')!;
}
get disconnected(): boolean {
return this.#reason !== undefined;
}
get disposed(): boolean {
return this.disconnected;
}
get userContexts(): Iterable<UserContext> {
return this.#userContexts.values();
}
// keep-sorted end
dispose(reason?: string, close?: boolean): void {
if (this.disposed) {
return;
}
this.#reason = reason ?? `Browser was disposed.`;
if (close) {
this.emit('closed', {reason: this.#reason});
}
this.emit('disconnect', {reason: this.#reason});
this.removeAllListeners();
@inertIfDisposed
dispose(reason?: string, closed = false): void {
this.#closed = closed;
this.#reason = reason;
this[disposeSymbol]();
}
@throwIfDisposed((browser: Browser) => {
@throwIfDisposed<Browser>(browser => {
// SAFETY: By definition of `disposed`, `#reason` is defined.
return browser.#reason!;
})
async close(): Promise<void> {
try {
await this.#session.send('browser.close', {});
await this.session.send('browser.close', {});
} finally {
this.dispose(`Browser was closed.`, true);
this.dispose('Browser already closed.', true);
}
}
@throwIfDisposed((browser: Browser) => {
@throwIfDisposed<Browser>(browser => {
// SAFETY: By definition of `disposed`, `#reason` is defined.
return browser.#reason!;
})
@ -168,7 +162,7 @@ export class Browser extends EventEmitter<{
): Promise<string> {
const {
result: {script},
} = await this.#session.send('script.addPreloadScript', {
} = await this.session.send('script.addPreloadScript', {
functionDeclaration,
...options,
contexts: options.contexts?.map(context => {
@ -178,13 +172,25 @@ export class Browser extends EventEmitter<{
return script;
}
@throwIfDisposed((browser: Browser) => {
@throwIfDisposed<Browser>(browser => {
// SAFETY: By definition of `disposed`, `#reason` is defined.
return browser.#reason!;
})
async removePreloadScript(script: string): Promise<void> {
await this.#session.send('script.removePreloadScript', {
await this.session.send('script.removePreloadScript', {
script,
});
}
[disposeSymbol](): void {
this.#reason ??=
'Browser was disconnected, probably because the session ended.';
if (this.closed) {
this.emit('closed', {reason: this.#reason});
}
this.emit('disconnected', {reason: this.#reason});
this.#disposables.dispose();
super[disposeSymbol]();
}
}

View File

@ -7,7 +7,8 @@
import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {EventEmitter} from '../../common/EventEmitter.js';
import {throwIfDisposed} from '../../util/decorators.js';
import {inertIfDisposed, throwIfDisposed} from '../../util/decorators.js';
import {DisposableStack, disposeSymbol} from '../../util/disposable.js';
import type {AddPreloadScriptOptions} from './Browser.js';
import {Navigation} from './Navigation.js';
@ -60,8 +61,11 @@ export type SetViewportOptions = Omit<
* @internal
*/
export class BrowsingContext extends EventEmitter<{
/** Emitted when this context is destroyed. */
destroyed: void;
/** Emitted when this context is closed. */
closed: {
/** The reason the browsing context was closed */
reason: string;
};
/** Emitted when a child browsing context is created. */
browsingcontext: {
/** The newly created child browsing context. */
@ -105,15 +109,16 @@ export class BrowsingContext extends EventEmitter<{
// keep-sorted start
#navigation: Navigation | undefined;
#reason?: string;
#url: string;
readonly #children = new Map<string, BrowsingContext>();
readonly #disposables = new DisposableStack();
readonly #realms = new Map<string, WindowRealm>();
readonly #requests = new Map<string, Request>();
readonly defaultRealm: WindowRealm;
readonly id: string;
readonly parent: BrowsingContext | undefined;
readonly userContext: UserContext;
disposed = false;
// keep-sorted end
private constructor(
@ -123,7 +128,6 @@ export class BrowsingContext extends EventEmitter<{
url: string
) {
super();
// keep-sorted start
this.#url = url;
this.id = id;
@ -135,11 +139,17 @@ export class BrowsingContext extends EventEmitter<{
}
#initialize() {
// ///////////////////////
// Session listeners //
// ///////////////////////
const session = this.#session;
session.on('browsingContext.contextCreated', info => {
const userContextEmitter = this.#disposables.use(
new EventEmitter(this.userContext)
);
userContextEmitter.once('closed', ({reason}) => {
this.dispose(`Browsing context already closed: ${reason}`);
});
const sessionEmitter = this.#disposables.use(
new EventEmitter(this.#session)
);
sessionEmitter.on('browsingContext.contextCreated', info => {
if (info.parent !== this.id) {
return;
}
@ -150,24 +160,27 @@ export class BrowsingContext extends EventEmitter<{
info.context,
info.url
);
browsingContext.on('destroyed', () => {
this.#children.set(info.context, browsingContext);
const browsingContextEmitter = this.#disposables.use(
new EventEmitter(browsingContext)
);
browsingContextEmitter.once('closed', () => {
browsingContextEmitter.removeAllListeners();
this.#children.delete(browsingContext.id);
});
this.#children.set(info.context, browsingContext);
this.emit('browsingcontext', {browsingContext});
});
session.on('browsingContext.contextDestroyed', info => {
sessionEmitter.on('browsingContext.contextDestroyed', info => {
if (info.context !== this.id) {
return;
}
this.disposed = true;
this.emit('destroyed', undefined);
this.removeAllListeners();
this.dispose('Browsing context already closed.');
});
session.on('browsingContext.domContentLoaded', info => {
sessionEmitter.on('browsingContext.domContentLoaded', info => {
if (info.context !== this.id) {
return;
}
@ -175,7 +188,7 @@ export class BrowsingContext extends EventEmitter<{
this.emit('DOMContentLoaded', undefined);
});
session.on('browsingContext.load', info => {
sessionEmitter.on('browsingContext.load', info => {
if (info.context !== this.id) {
return;
}
@ -183,22 +196,31 @@ export class BrowsingContext extends EventEmitter<{
this.emit('load', undefined);
});
session.on('browsingContext.navigationStarted', info => {
sessionEmitter.on('browsingContext.navigationStarted', info => {
if (info.context !== this.id) {
return;
}
this.#url = info.url;
this.#requests.clear();
// Note the navigation ID is null for this event.
this.#navigation = Navigation.from(this, info.url);
this.#navigation.on('fragment', ({url}) => {
this.#navigation = Navigation.from(this);
const navigationEmitter = this.#disposables.use(
new EventEmitter(this.#navigation)
);
for (const eventName of ['fragment', 'failed', 'aborted'] as const) {
navigationEmitter.once(eventName, ({url}) => {
navigationEmitter[disposeSymbol]();
this.#url = url;
});
}
this.emit('navigation', {navigation: this.#navigation});
});
session.on('network.beforeRequestSent', event => {
sessionEmitter.on('network.beforeRequestSent', event => {
if (event.context !== this.id) {
return;
}
@ -206,12 +228,12 @@ export class BrowsingContext extends EventEmitter<{
return;
}
const request = new Request(this, event);
const request = Request.from(this, event);
this.#requests.set(request.id, request);
this.emit('request', {request});
});
session.on('log.entryAdded', entry => {
sessionEmitter.on('log.entryAdded', entry => {
if (entry.source.context !== this.id) {
return;
}
@ -219,7 +241,7 @@ export class BrowsingContext extends EventEmitter<{
this.emit('log', {entry});
});
session.on('browsingContext.userPromptOpened', info => {
sessionEmitter.on('browsingContext.userPromptOpened', info => {
if (info.context !== this.id) {
return;
}
@ -236,6 +258,12 @@ export class BrowsingContext extends EventEmitter<{
get children(): Iterable<BrowsingContext> {
return this.#children.values();
}
get closed(): boolean {
return this.#reason !== undefined;
}
get disposed(): boolean {
return this.closed;
}
get realms(): Iterable<WindowRealm> {
return this.#realms.values();
}
@ -251,14 +279,26 @@ export class BrowsingContext extends EventEmitter<{
}
// keep-sorted end
@throwIfDisposed()
@inertIfDisposed
private dispose(reason?: string): void {
this.#reason = reason;
this[disposeSymbol]();
}
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async activate(): Promise<void> {
await this.#session.send('browsingContext.activate', {
context: this.id,
});
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async captureScreenshot(
options: CaptureScreenshotOptions = {}
): Promise<string> {
@ -271,7 +311,10 @@ export class BrowsingContext extends EventEmitter<{
return data;
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async close(promptUnload?: boolean): Promise<void> {
await Promise.all(
[...this.#children.values()].map(async child => {
@ -284,7 +327,10 @@ export class BrowsingContext extends EventEmitter<{
});
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async traverseHistory(delta: number): Promise<void> {
await this.#session.send('browsingContext.traverseHistory', {
context: this.id,
@ -292,7 +338,10 @@ export class BrowsingContext extends EventEmitter<{
});
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async navigate(
url: string,
wait?: Bidi.BrowsingContext.ReadinessState
@ -309,7 +358,10 @@ export class BrowsingContext extends EventEmitter<{
});
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async reload(options: ReloadOptions = {}): Promise<Navigation> {
await this.#session.send('browsingContext.reload', {
context: this.id,
@ -322,7 +374,10 @@ export class BrowsingContext extends EventEmitter<{
});
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async print(options: PrintOptions = {}): Promise<string> {
const {
result: {data},
@ -333,7 +388,10 @@ export class BrowsingContext extends EventEmitter<{
return data;
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async handleUserPrompt(options: HandleUserPromptOptions = {}): Promise<void> {
await this.#session.send('browsingContext.handleUserPrompt', {
context: this.id,
@ -341,7 +399,10 @@ export class BrowsingContext extends EventEmitter<{
});
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async setViewport(options: SetViewportOptions = {}): Promise<void> {
await this.#session.send('browsingContext.setViewport', {
context: this.id,
@ -349,7 +410,10 @@ export class BrowsingContext extends EventEmitter<{
});
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async performActions(actions: Bidi.Input.SourceActions[]): Promise<void> {
await this.#session.send('input.performActions', {
context: this.id,
@ -357,19 +421,28 @@ export class BrowsingContext extends EventEmitter<{
});
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async releaseActions(): Promise<void> {
await this.#session.send('input.releaseActions', {
context: this.id,
});
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
createWindowRealm(sandbox: string): WindowRealm {
return WindowRealm.from(this, sandbox);
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async addPreloadScript(
functionDeclaration: string,
options: AddPreloadScriptOptions = {}
@ -383,8 +456,20 @@ export class BrowsingContext extends EventEmitter<{
);
}
@throwIfDisposed()
@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async removePreloadScript(script: string): Promise<void> {
await this.userContext.browser.removePreloadScript(script);
}
[disposeSymbol](): void {
this.#reason ??=
'Browsing context already closed, probably because the user context closed.';
this.emit('closed', {reason: this.#reason});
this.#disposables.dispose();
super[disposeSymbol]();
}
}

View File

@ -4,10 +4,10 @@
* 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} from '../../util/decorators.js';
import {Deferred} from '../../util/Deferred.js';
import {DisposableStack, disposeSymbol} from '../../util/disposable.js';
import type {BrowsingContext} from './BrowsingContext.js';
import type {Request} from './Request.js';
@ -24,44 +24,82 @@ export interface NavigationInfo {
* @internal
*/
export class Navigation extends EventEmitter<{
/** Emitted when navigation has a request associated with it. */
request: Request;
/** Emitted when fragment navigation occurred. */
fragment: NavigationInfo;
/** Emitted when navigation failed. */
failed: NavigationInfo;
/** Emitted when navigation was aborted. */
aborted: NavigationInfo;
}> {
static from(context: BrowsingContext, url: string): Navigation {
const navigation = new Navigation(context, url);
static from(context: BrowsingContext): Navigation {
const navigation = new Navigation(context);
navigation.#initialize();
return navigation;
}
// keep-sorted start
#context: BrowsingContext;
#id = new Deferred<string>();
#request: Request | undefined;
#url: string;
readonly #browsingContext: BrowsingContext;
readonly #disposables = new DisposableStack();
readonly #id = new Deferred<string>();
// keep-sorted end
private constructor(context: BrowsingContext, url: string) {
private constructor(context: BrowsingContext) {
super();
// keep-sorted start
this.#context = context;
this.#url = url;
this.#browsingContext = context;
// keep-sorted end
}
#initialize() {
// ///////////////////////
// Session listeners //
// ///////////////////////
const session = this.#session;
for (const [bidiEvent, event] of [
const browsingContextEmitter = this.#disposables.use(
new EventEmitter(this.#browsingContext)
);
browsingContextEmitter.once('closed', () => {
this.emit('failed', {
url: this.#browsingContext.url,
timestamp: new Date(),
});
this.dispose();
});
this.#browsingContext.on('request', ({request}) => {
if (request.navigation === this.#id.value()) {
this.#request = request;
this.emit('request', request);
}
});
const sessionEmitter = this.#disposables.use(
new EventEmitter(this.#session)
);
// To get the navigation ID if any.
for (const eventName of [
'browsingContext.domContentLoaded',
'browsingContext.load',
] as const) {
sessionEmitter.on(eventName, info => {
if (info.context !== this.#browsingContext.id) {
return;
}
if (!info.navigation) {
return;
}
if (!this.#id.resolved()) {
this.#id.resolve(info.navigation);
}
});
}
for (const [eventName, event] of [
['browsingContext.fragmentNavigated', 'fragment'],
['browsingContext.navigationFailed', 'failed'],
['browsingContext.navigationAborted', 'aborted'],
] as const) {
session.on(bidiEvent, (info: Bidi.BrowsingContext.NavigationInfo) => {
if (info.context !== this.#context.id) {
sessionEmitter.on(eventName, info => {
if (info.context !== this.#browsingContext.id) {
return;
}
if (!info.navigation) {
@ -73,33 +111,34 @@ export class Navigation extends EventEmitter<{
if (this.#id.value() !== info.navigation) {
return;
}
this.#url = info.url;
this.emit(event, {
url: this.#url,
url: info.url,
timestamp: new Date(info.timestamp),
});
this.dispose();
});
}
// ///////////////////
// Parent listeners //
// ///////////////////
this.#context.on('request', ({request}) => {
if (request.navigation === this.#id.value()) {
this.#request = request;
}
});
}
// keep-sorted start block=yes
get #session() {
return this.#context.userContext.browser.session;
return this.#browsingContext.userContext.browser.session;
}
get url(): string {
return this.#url;
get disposed(): boolean {
return this.#disposables.disposed;
}
request(): Request | undefined {
get request(): Request | undefined {
return this.#request;
}
// keep-sorted end
@inertIfDisposed
private dispose(): void {
this[disposeSymbol]();
}
[disposeSymbol](): void {
this.#disposables.dispose();
super[disposeSymbol]();
}
}

View File

@ -7,6 +7,8 @@
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 {BrowsingContext} from './BrowsingContext.js';
import type {Session} from './Session.js';
@ -32,34 +34,57 @@ export type EvaluateOptions = Omit<
*/
export abstract class Realm extends EventEmitter<{
/** Emitted when the realm is destroyed. */
destroyed: void;
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;
}> {
// keep-sorted start
#reason?: string;
protected readonly disposables = new DisposableStack();
readonly id: string;
readonly origin: string;
// keep-sorted end
protected constructor(id: string, origin: string) {
super();
// keep-sorted start
this.id = id;
this.origin = origin;
// keep-sorted end
}
protected initialize(): void {
this.session.on('script.realmDestroyed', info => {
if (info.realm === this.id) {
this.emit('destroyed', undefined);
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.');
});
}
// keep-sorted start block=yes
get disposed(): boolean {
return this.#reason !== undefined;
}
protected abstract get session(): Session;
protected get target(): Bidi.Script.Target {
return {realm: this.id};
}
// keep-sorted end
@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,
@ -67,6 +92,10 @@ export abstract class Realm extends EventEmitter<{
});
}
@throwIfDisposed<Realm>(realm => {
// SAFETY: Disposal implies this exists.
return realm.#reason!;
})
async callFunction(
functionDeclaration: string,
awaitPromise: boolean,
@ -81,6 +110,10 @@ export abstract class Realm extends EventEmitter<{
return result;
}
@throwIfDisposed<Realm>(realm => {
// SAFETY: Disposal implies this exists.
return realm.#reason!;
})
async evaluate(
expression: string,
awaitPromise: boolean,
@ -94,6 +127,15 @@ export abstract class Realm extends EventEmitter<{
});
return result;
}
[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]();
}
}
/**
@ -106,8 +148,10 @@ export class WindowRealm extends Realm {
return realm;
}
// keep-sorted start
readonly browsingContext: BrowsingContext;
readonly sandbox?: string;
// keep-sorted end
readonly #workers: {
dedicated: Map<string, DedicatedWorkerRealm>;
@ -117,53 +161,59 @@ export class WindowRealm extends Realm {
shared: new Map(),
};
constructor(context: BrowsingContext, sandbox?: string) {
private constructor(context: BrowsingContext, sandbox?: string) {
super('', '');
// keep-sorted start
this.browsingContext = context;
this.sandbox = sandbox;
// keep-sorted end
}
override initialize(): void {
super.initialize();
// ///////////////////////
// Session listeners //
// ///////////////////////
this.session.on('script.realmCreated', info => {
if (info.type === 'window') {
// SAFETY: This is the only time we allow mutations.
(this as any).id = info.realm;
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
sessionEmitter.on('script.realmCreated', info => {
if (info.type !== 'window') {
return;
}
(this as any).id = info.realm;
(this as any).origin = info.origin;
});
sessionEmitter.on('script.realmCreated', info => {
if (info.type !== 'dedicated-worker') {
return;
}
if (info.type === 'dedicated-worker') {
if (!info.owners.includes(this.id)) {
return;
}
const realm = DedicatedWorkerRealm.from(this, info.realm, info.origin);
realm.on('destroyed', () => {
this.#workers.dedicated.set(realm.id, realm);
const realmEmitter = this.disposables.use(new EventEmitter(realm));
realmEmitter.once('destroyed', () => {
realmEmitter.removeAllListeners();
this.#workers.dedicated.delete(realm.id);
});
this.#workers.dedicated.set(realm.id, realm);
this.emit('worker', realm);
}
});
// ///////////////////
// Parent listeners //
// ///////////////////
this.browsingContext.userContext.browser.on('sharedworker', ({realm}) => {
if (realm.owners.has(this)) {
realm.on('destroyed', () => {
this.#workers.shared.delete(realm.id);
});
if (!realm.owners.has(this)) {
return;
}
this.#workers.shared.set(realm.id, realm);
const realmEmitter = this.disposables.use(new EventEmitter(realm));
realmEmitter.once('destroyed', () => {
realmEmitter.removeAllListeners();
this.#workers.shared.delete(realm.id);
});
this.emit('sharedworker', realm);
}
});
}
@ -198,11 +248,16 @@ export class DedicatedWorkerRealm extends Realm {
return realm;
}
readonly owners: Set<DedicatedWorkerOwnerRealm>;
// keep-sorted start
readonly #workers = new Map<string, DedicatedWorkerRealm>();
readonly owners: Set<DedicatedWorkerOwnerRealm>;
// keep-sorted end
constructor(owner: DedicatedWorkerOwnerRealm, id: string, origin: string) {
private constructor(
owner: DedicatedWorkerOwnerRealm,
id: string,
origin: string
) {
super(id, origin);
this.owners = new Set([owner]);
}
@ -210,24 +265,24 @@ export class DedicatedWorkerRealm extends Realm {
override initialize(): void {
super.initialize();
// ///////////////////////
// Session listeners //
// ///////////////////////
this.session.on('script.realmCreated', info => {
if (info.type === 'dedicated-worker') {
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
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);
realm.on('destroyed', () => {
this.#workers.set(realm.id, realm);
const realmEmitter = this.disposables.use(new EventEmitter(realm));
realmEmitter.once('destroyed', () => {
this.#workers.delete(realm.id);
});
this.#workers.set(realm.id, realm);
this.emit('worker', realm);
}
});
}
@ -251,11 +306,12 @@ export class SharedWorkerRealm extends Realm {
return realm;
}
readonly owners: Set<WindowRealm>;
// keep-sorted start
readonly #workers = new Map<string, DedicatedWorkerRealm>();
readonly owners: Set<WindowRealm>;
// keep-sorted end
constructor(
private constructor(
owners: [WindowRealm, ...WindowRealm[]],
id: string,
origin: string
@ -267,24 +323,24 @@ export class SharedWorkerRealm extends Realm {
override initialize(): void {
super.initialize();
// ///////////////////////
// Session listeners //
// ///////////////////////
this.session.on('script.realmCreated', info => {
if (info.type === 'dedicated-worker') {
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
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);
realm.on('destroyed', () => {
this.#workers.set(realm.id, realm);
const realmEmitter = this.disposables.use(new EventEmitter(realm));
realmEmitter.once('destroyed', () => {
this.#workers.delete(realm.id);
});
this.#workers.set(realm.id, realm);
this.emit('worker', realm);
}
});
}

View File

@ -7,6 +7,8 @@
import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {EventEmitter} from '../../common/EventEmitter.js';
import {inertIfDisposed} from '../../util/decorators.js';
import {DisposableStack, disposeSymbol} from '../../util/disposable.js';
import type {BrowsingContext} from './BrowsingContext.js';
@ -14,44 +16,68 @@ import type {BrowsingContext} from './BrowsingContext.js';
* @internal
*/
export class Request extends EventEmitter<{
// Emitted whenever a redirect is received.
/** Emitted when the request is redirected. */
redirect: Request;
// Emitted when when the request succeeds.
/** Emitted when the request succeeds. */
success: Bidi.Network.ResponseData;
// Emitted when when the request errors.
/** Emitted when the request fails. */
error: string;
}> {
readonly #context: BrowsingContext;
readonly #event: Bidi.Network.BeforeRequestSentParameters;
static from(
browsingContext: BrowsingContext,
event: Bidi.Network.BeforeRequestSentParameters
): Request {
const request = new Request(browsingContext, event);
request.#initialize();
return request;
}
#response?: Bidi.Network.ResponseData;
#redirect?: Request;
// keep-sorted start
#error?: string;
#redirect?: Request;
#response?: Bidi.Network.ResponseData;
readonly #browsingContext: BrowsingContext;
readonly #disposables = new DisposableStack();
readonly #event: Bidi.Network.BeforeRequestSentParameters;
// keep-sorted end
constructor(
context: BrowsingContext,
private constructor(
browsingContext: BrowsingContext,
event: Bidi.Network.BeforeRequestSentParameters
) {
super();
this.#context = context;
// keep-sorted start
this.#browsingContext = browsingContext;
this.#event = event;
// keep-sorted end
}
const session = this.#session;
session.on('network.beforeRequestSent', event => {
if (event.context !== this.id) {
#initialize() {
const browsingContextEmitter = this.#disposables.use(
new EventEmitter(this.#browsingContext)
);
browsingContextEmitter.once('closed', ({reason}) => {
this.#error = reason;
this.emit('error', this.#error);
this.dispose();
});
const sessionEmitter = this.#disposables.use(
new EventEmitter(this.#session)
);
sessionEmitter.on('network.beforeRequestSent', event => {
if (event.context !== this.#browsingContext.id) {
return;
}
if (event.request.request !== this.id) {
return;
}
if (this.#redirect) {
return;
}
this.#redirect = new Request(this.#context, event);
this.#redirect = Request.from(this.#browsingContext, event);
this.emit('redirect', this.#redirect);
this.dispose();
});
session.on('network.fetchError', event => {
if (event.context !== this.#context.id) {
sessionEmitter.on('network.fetchError', event => {
if (event.context !== this.#browsingContext.id) {
return;
}
if (event.request.request !== this.id) {
@ -59,9 +85,10 @@ export class Request extends EventEmitter<{
}
this.#error = event.errorText;
this.emit('error', this.#error);
this.dispose();
});
session.on('network.responseCompleted', event => {
if (event.context !== this.#context.id) {
sessionEmitter.on('network.responseCompleted', event => {
if (event.context !== this.#browsingContext.id) {
return;
}
if (event.request.request !== this.id) {
@ -69,46 +96,53 @@ export class Request extends EventEmitter<{
}
this.#response = event.response;
this.emit('success', this.#response);
this.dispose();
});
}
// keep-sorted start block=yes
get #session() {
return this.#context.userContext.browser.session;
return this.#browsingContext.userContext.browser.session;
}
get id(): string {
return this.#event.request.request;
get disposed(): boolean {
return this.#disposables.disposed;
}
get url(): string {
return this.#event.request.url;
}
get initiator(): Bidi.Network.Initiator {
return this.#event.initiator;
}
get method(): string {
return this.#event.request.method;
}
get headers(): Bidi.Network.Header[] {
return this.#event.request.headers;
}
get navigation(): string | undefined {
return this.#event.navigation ?? undefined;
}
get redirect(): Request | undefined {
return this.redirect;
}
get response(): Bidi.Network.ResponseData | undefined {
return this.#response;
}
get error(): string | undefined {
return this.#error;
}
get headers(): Bidi.Network.Header[] {
return this.#event.request.headers;
}
get id(): string {
return this.#event.request.request;
}
get initiator(): Bidi.Network.Initiator {
return this.#event.initiator;
}
get method(): string {
return this.#event.request.method;
}
get navigation(): string | undefined {
return this.#event.navigation ?? undefined;
}
get redirect(): Request | undefined {
return this.redirect;
}
get response(): Bidi.Network.ResponseData | undefined {
return this.#response;
}
get url(): string {
return this.#event.request.url;
}
// keep-sorted end
@inertIfDisposed
private dispose(): void {
this[disposeSymbol]();
}
[disposeSymbol](): void {
this.#disposables.dispose();
super[disposeSymbol]();
}
}

View File

@ -8,7 +8,8 @@ import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {EventEmitter} from '../../common/EventEmitter.js';
import {debugError} from '../../common/util.js';
import {throwIfDisposed} from '../../util/decorators.js';
import {inertIfDisposed, throwIfDisposed} from '../../util/decorators.js';
import {DisposableStack, disposeSymbol} from '../../util/disposable.js';
import {Browser} from './Browser.js';
import type {BidiEvents, Commands, Connection} from './Connection.js';
@ -75,58 +76,53 @@ export class Session
return session;
}
readonly connection: Connection;
// keep-sorted start
#reason: string | undefined;
readonly #disposables = new DisposableStack();
readonly #info: Bidi.Session.NewResult;
readonly browser!: Browser;
#reason: string | undefined;
readonly connection: Connection;
// keep-sorted end
private constructor(connection: Connection, info: Bidi.Session.NewResult) {
super();
this.connection = connection;
// keep-sorted start
this.#info = info;
this.connection = connection;
// keep-sorted end
}
async #initialize(): Promise<void> {
// ///////////////////////
// Connection listeners //
// ///////////////////////
this.connection.pipeTo(this);
// //////////////////////////////
// Asynchronous initialization //
// //////////////////////////////
// SAFETY: We use `any` to allow assignment of the readonly property.
(this as any).browser = await Browser.from(this);
// //////////////////
// Child listeners //
// //////////////////
this.browser.once('closed', ({reason}) => {
const browserEmitter = this.#disposables.use(this.browser);
browserEmitter.once('closed', ({reason}) => {
this.dispose(reason);
});
}
get disposed(): boolean {
return this.#reason !== undefined;
}
get id(): string {
return this.#info.sessionId;
}
// keep-sorted start block=yes
get capabilities(): Bidi.Session.NewResult['capabilities'] {
return this.#info.capabilities;
}
dispose(reason?: string): void {
if (this.disposed) {
return;
get disposed(): boolean {
return this.ended;
}
this.#reason = reason ?? 'Session was disposed.';
this.emit('ended', {reason: this.#reason});
this.removeAllListeners();
get ended(): boolean {
return this.#reason !== undefined;
}
get id(): string {
return this.#info.sessionId;
}
// keep-sorted end
@inertIfDisposed
private dispose(reason?: string): void {
this.#reason = reason;
this[disposeSymbol]();
}
pipeTo<Events extends BidiEvents>(emitter: EventEmitter<Events>): void {
@ -140,7 +136,7 @@ export class Session
* object is used, so we implement this method here, although it's not defined
* in the spec.
*/
@throwIfDisposed((session: Session) => {
@throwIfDisposed<Session>(session => {
// SAFETY: By definition of `disposed`, `#reason` is defined.
return session.#reason!;
})
@ -151,7 +147,7 @@ export class Session
return await this.connection.send(method, params);
}
@throwIfDisposed((session: Session) => {
@throwIfDisposed<Session>(session => {
// SAFETY: By definition of `disposed`, `#reason` is defined.
return session.#reason!;
})
@ -161,7 +157,7 @@ export class Session
});
}
@throwIfDisposed((session: Session) => {
@throwIfDisposed<Session>(session => {
// SAFETY: By definition of `disposed`, `#reason` is defined.
return session.#reason!;
})
@ -169,7 +165,16 @@ export class Session
try {
await this.send('session.end', {});
} finally {
this.dispose(`Session (${this.id}) has already ended.`);
this.dispose(`Session already ended.`);
}
}
[disposeSymbol](): void {
this.#reason ??=
'Session already destroyed, probably because the connection broke.';
this.emit('ended', {reason: this.#reason});
this.#disposables.dispose();
super[disposeSymbol]();
}
}

View File

@ -8,7 +8,7 @@ import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {EventEmitter} from '../../common/EventEmitter.js';
import {assert} from '../../util/assert.js';
import {throwIfDisposed} from '../../util/decorators.js';
import {inertIfDisposed, throwIfDisposed} from '../../util/decorators.js';
import {DisposableStack, disposeSymbol} from '../../util/disposable.js';
import type {Browser} from './Browser.js';
@ -35,6 +35,13 @@ export class UserContext extends EventEmitter<{
/** The new browsing context. */
browsingContext: BrowsingContext;
};
/**
* Emitted when the user context is closed.
*/
closed: {
/** The reason the user context was closed. */
reason: string;
};
}> {
static create(browser: Browser, id: string): UserContext {
const context = new UserContext(browser, id);
@ -46,16 +53,15 @@ export class UserContext extends EventEmitter<{
#reason?: string;
// Note these are only top-level contexts.
readonly #browsingContexts = new Map<string, BrowsingContext>();
readonly #disposables = new DisposableStack();
// @ts-expect-error -- TODO: This will be used once the WebDriver BiDi
// protocol supports it.
readonly #id: string;
readonly #disposables = new DisposableStack();
readonly browser: Browser;
// keep-sorted end
private constructor(browser: Browser, id: string) {
super();
// keep-sorted start
this.#id = id;
this.browser = browser;
@ -63,9 +69,13 @@ export class UserContext extends EventEmitter<{
}
#initialize() {
// ////////////////////
// Session listeners //
// ////////////////////
const browserEmitter = this.#disposables.use(
new EventEmitter(this.browser)
);
browserEmitter.once('closed', ({reason}) => {
this.dispose(`User context already closed: ${reason}`);
});
const sessionEmitter = this.#disposables.use(
new EventEmitter(this.#session)
);
@ -85,7 +95,9 @@ export class UserContext extends EventEmitter<{
const browsingContextEmitter = this.#disposables.use(
new EventEmitter(browsingContext)
);
browsingContextEmitter.on('destroyed', () => {
browsingContextEmitter.on('closed', () => {
browsingContextEmitter.removeAllListeners();
this.#browsingContexts.delete(browsingContext.id);
});
@ -100,12 +112,16 @@ export class UserContext extends EventEmitter<{
get browsingContexts(): Iterable<BrowsingContext> {
return this.#browsingContexts.values();
}
get closed(): boolean {
return this.#reason !== undefined;
}
get disposed(): boolean {
return Boolean(this.#reason);
return this.closed;
}
// keep-sorted end
dispose(reason?: string): void {
@inertIfDisposed
private dispose(reason?: string): void {
this.#reason = reason;
this[disposeSymbol]();
}
@ -136,23 +152,28 @@ export class UserContext extends EventEmitter<{
return browsingContext;
}
@throwIfDisposed<UserContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async close(): Promise<void> {
try {
const promises = [];
for (const browsingContext of this.#browsingContexts.values()) {
promises.push(browsingContext.close());
}
await Promise.all(promises);
this.dispose('User context was closed.');
} finally {
this.dispose('User context already closed.');
}
}
[disposeSymbol](): void {
super[disposeSymbol]();
if (this.#reason === undefined) {
this.#reason =
'User context was destroyed, probably because browser disconnected/closed.';
}
this.#reason ??=
'User context already closed, probably because the browser disconnected/closed.';
this.emit('closed', {reason: this.#reason});
this.#disposables.dispose();
super[disposeSymbol]();
}
}

View File

@ -7,7 +7,7 @@
import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
import {EventEmitter} from '../../common/EventEmitter.js';
import {throwIfDisposed} from '../../util/decorators.js';
import {inertIfDisposed, throwIfDisposed} from '../../util/decorators.js';
import {DisposableStack, disposeSymbol} from '../../util/disposable.js';
import type {BrowsingContext} from './BrowsingContext.js';
@ -32,7 +32,13 @@ export type UserPromptResult = Omit<
* @internal
*/
export class UserPrompt extends EventEmitter<{
/** Emitted when the user prompt is handled. */
handled: UserPromptResult;
/** Emitted when the user prompt is closed. */
closed: {
/** The reason the user prompt was closed. */
reason: string;
};
}> {
static from(
browsingContext: BrowsingContext,
@ -56,17 +62,20 @@ export class UserPrompt extends EventEmitter<{
info: Bidi.BrowsingContext.UserPromptOpenedParameters
) {
super();
// keep-sorted start
this.info = info;
this.browsingContext = context;
this.info = info;
// keep-sorted end
}
#initialize() {
// ////////////////////
// Session listeners //
// ////////////////////
const browserContextEmitter = this.#disposables.use(
new EventEmitter(this.browsingContext)
);
browserContextEmitter.once('closed', ({reason}) => {
this.dispose(`User prompt already closed: ${reason}`);
});
const sessionEmitter = this.#disposables.use(
new EventEmitter(this.#session)
);
@ -76,7 +85,7 @@ export class UserPrompt extends EventEmitter<{
}
this.#result = parameters;
this.emit('handled', parameters);
this.dispose('User prompt was handled.');
this.dispose('User prompt already handled.');
});
}
@ -84,20 +93,27 @@ export class UserPrompt extends EventEmitter<{
get #session() {
return this.browsingContext.userContext.browser.session;
}
get closed(): boolean {
return this.#reason !== undefined;
}
get disposed(): boolean {
return Boolean(this.#reason);
return this.closed;
}
get handled(): boolean {
return this.#result !== undefined;
}
get result(): UserPromptResult | undefined {
return this.#result;
}
// keep-sorted end
dispose(reason?: string): void {
@inertIfDisposed
private dispose(reason?: string): void {
this.#reason = reason;
this[disposeSymbol]();
}
@throwIfDisposed((prompt: UserPrompt) => {
@throwIfDisposed<UserPrompt>(prompt => {
// SAFETY: Disposal implies this exists.
return prompt.#reason!;
})
@ -111,13 +127,11 @@ export class UserPrompt extends EventEmitter<{
}
[disposeSymbol](): void {
super[disposeSymbol]();
if (this.#reason === undefined) {
this.#reason =
'User prompt was destroyed, probably because the associated browsing context was destroyed.';
}
this.#reason ??=
'User prompt already closed, probably because the associated browsing context was destroyed.';
this.emit('closed', {reason: this.#reason});
this.#disposables.dispose();
super[disposeSymbol]();
}
}

View File

@ -63,6 +63,18 @@ export function throwIfDisposed<This extends Disposed>(
};
}
export function inertIfDisposed<This extends Disposed>(
target: (this: This, ...args: any[]) => any,
_: unknown
) {
return function (this: This, ...args: any[]): any {
if (this.disposed) {
return;
}
return target.call(this, ...args);
};
}
/**
* The decorator only invokes the target if the target has not been invoked with
* the same arguments before. The decorated method throws an error if it's