mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: handle RealmCreated properly (#10834)
This commit is contained in:
parent
38b190afc8
commit
28564b36e1
@ -147,7 +147,7 @@ export class BrowsingContext extends Realm {
|
|||||||
info: Bidi.BrowsingContext.Info,
|
info: Bidi.BrowsingContext.Info,
|
||||||
browserName: string
|
browserName: string
|
||||||
) {
|
) {
|
||||||
super(connection, info.context);
|
super(connection);
|
||||||
this.#id = info.context;
|
this.#id = info.context;
|
||||||
this.#url = info.url;
|
this.#url = info.url;
|
||||||
this.#parent = info.parent;
|
this.#parent = info.parent;
|
||||||
@ -167,7 +167,7 @@ export class BrowsingContext extends Realm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
createRealmForSandbox(): Realm {
|
createRealmForSandbox(): Realm {
|
||||||
return new Realm(this.connection, this.#id);
|
return new Realm(this.connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
get url(): string {
|
get url(): string {
|
||||||
|
@ -7,7 +7,6 @@ import {scriptInjector} from '../ScriptInjector.js';
|
|||||||
import {EvaluateFunc, HandleFor} from '../types.js';
|
import {EvaluateFunc, HandleFor} from '../types.js';
|
||||||
import {
|
import {
|
||||||
PuppeteerURL,
|
PuppeteerURL,
|
||||||
debugError,
|
|
||||||
getSourcePuppeteerURLIfAvailable,
|
getSourcePuppeteerURLIfAvailable,
|
||||||
isString,
|
isString,
|
||||||
} from '../util.js';
|
} from '../util.js';
|
||||||
@ -27,43 +26,52 @@ export const getSourceUrlComment = (url: string): string => {
|
|||||||
|
|
||||||
export class Realm extends EventEmitter {
|
export class Realm extends EventEmitter {
|
||||||
readonly connection: Connection;
|
readonly connection: Connection;
|
||||||
readonly #id: string;
|
|
||||||
|
|
||||||
|
#id!: string;
|
||||||
#sandbox!: Sandbox;
|
#sandbox!: Sandbox;
|
||||||
|
|
||||||
constructor(connection: Connection, id: string) {
|
constructor(connection: Connection) {
|
||||||
super();
|
super();
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
this.#id = id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get target(): Bidi.Script.Target {
|
get target(): Bidi.Script.Target {
|
||||||
return {
|
return {
|
||||||
context: this.#id,
|
context: this.#sandbox.environment._id,
|
||||||
sandbox: this.#sandbox.name,
|
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 {
|
setSandbox(sandbox: Sandbox): void {
|
||||||
this.#sandbox = sandbox;
|
this.#sandbox = sandbox;
|
||||||
|
this.connection.on(
|
||||||
// TODO: Tack correct realm similar to BrowsingContexts
|
Bidi.ChromiumBidi.Script.EventNames.RealmCreated,
|
||||||
this.connection.on(Bidi.ChromiumBidi.Script.EventNames.RealmCreated, () => {
|
this.handleRealmCreated
|
||||||
void this.#sandbox.taskManager.rerunAll();
|
);
|
||||||
});
|
|
||||||
// TODO(jrandolf): We should try to find a less brute-force way of doing
|
|
||||||
// this.
|
|
||||||
this.connection.on(
|
this.connection.on(
|
||||||
Bidi.ChromiumBidi.Script.EventNames.RealmDestroyed,
|
Bidi.ChromiumBidi.Script.EventNames.RealmDestroyed,
|
||||||
async () => {
|
this.handleRealmDestroyed
|
||||||
const promise = this.internalPuppeteerUtil;
|
|
||||||
this.internalPuppeteerUtil = undefined;
|
|
||||||
try {
|
|
||||||
await (await promise)?.dispose();
|
|
||||||
} catch (error) {
|
|
||||||
debugError(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,6 +197,17 @@ export class Realm extends EventEmitter {
|
|||||||
? BidiSerializer.deserialize(result.result)
|
? BidiSerializer.deserialize(result.result)
|
||||||
: createBidiHandle(sandbox, 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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user