chore: handle RealmCreated properly (#10834)

This commit is contained in:
jrandolf 2023-09-06 16:32:47 +02:00 committed by GitHub
parent 38b190afc8
commit 28564b36e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 23 deletions

View File

@ -147,7 +147,7 @@ export class BrowsingContext extends Realm {
info: Bidi.BrowsingContext.Info,
browserName: string
) {
super(connection, info.context);
super(connection);
this.#id = info.context;
this.#url = info.url;
this.#parent = info.parent;
@ -167,7 +167,7 @@ export class BrowsingContext extends Realm {
}
createRealmForSandbox(): Realm {
return new Realm(this.connection, this.#id);
return new Realm(this.connection);
}
get url(): string {

View File

@ -7,7 +7,6 @@ import {scriptInjector} from '../ScriptInjector.js';
import {EvaluateFunc, HandleFor} from '../types.js';
import {
PuppeteerURL,
debugError,
getSourcePuppeteerURLIfAvailable,
isString,
} from '../util.js';
@ -27,43 +26,52 @@ export const getSourceUrlComment = (url: string): string => {
export class Realm extends EventEmitter {
readonly connection: Connection;
readonly #id: string;
#id!: string;
#sandbox!: Sandbox;
constructor(connection: Connection, id: string) {
constructor(connection: Connection) {
super();
this.connection = connection;
this.#id = id;
}
get target(): Bidi.Script.Target {
return {
context: this.#id,
context: this.#sandbox.environment._id,
sandbox: this.#sandbox.name,
};
}
handleRealmDestroyed = async (
params: Bidi.Script.RealmDestroyed['params']
): Promise<void> => {
if (params.realm === this.#id) {
// Note: The Realm is destroyed, so in theory the handle should be as
// well.
this.internalPuppeteerUtil = undefined;
}
};
handleRealmCreated = (params: Bidi.Script.RealmCreated['params']): void => {
if (
params.type === 'window' &&
params.context === this.#sandbox.environment._id &&
params.sandbox === this.#sandbox.name
) {
this.#id = params.realm;
void this.#sandbox.taskManager.rerunAll();
}
};
setSandbox(sandbox: Sandbox): void {
this.#sandbox = sandbox;
// TODO: Tack correct realm similar to BrowsingContexts
this.connection.on(Bidi.ChromiumBidi.Script.EventNames.RealmCreated, () => {
void this.#sandbox.taskManager.rerunAll();
});
// TODO(jrandolf): We should try to find a less brute-force way of doing
// this.
this.connection.on(
Bidi.ChromiumBidi.Script.EventNames.RealmCreated,
this.handleRealmCreated
);
this.connection.on(
Bidi.ChromiumBidi.Script.EventNames.RealmDestroyed,
async () => {
const promise = this.internalPuppeteerUtil;
this.internalPuppeteerUtil = undefined;
try {
await (await promise)?.dispose();
} catch (error) {
debugError(error);
}
}
this.handleRealmDestroyed
);
}
@ -189,6 +197,17 @@ export class Realm extends EventEmitter {
? BidiSerializer.deserialize(result.result)
: createBidiHandle(sandbox, result.result);
}
[Symbol.dispose](): void {
this.connection.off(
Bidi.ChromiumBidi.Script.EventNames.RealmCreated,
this.handleRealmCreated
);
this.connection.off(
Bidi.ChromiumBidi.Script.EventNames.RealmDestroyed,
this.handleRealmDestroyed
);
}
}
/**