chore: make execution context delegate public (#34894)

This commit is contained in:
Yury Semikhatsky 2025-02-21 18:27:24 -08:00 committed by GitHub
parent e38099ef13
commit e091baad79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 13 additions and 23 deletions

View File

@ -142,7 +142,6 @@ export class BidiPage implements PageDelegate {
} }
const delegate = new BidiExecutionContext(this._session, realmInfo); const delegate = new BidiExecutionContext(this._session, realmInfo);
const context = new dom.FrameExecutionContext(delegate, frame, worldName); const context = new dom.FrameExecutionContext(delegate, frame, worldName);
(context as any)[contextDelegateSymbol] = delegate;
frame._contextCreated(worldName, context); frame._contextCreated(worldName, context);
this._realmToContext.set(realmInfo.realm, context); this._realmToContext.set(realmInfo.realm, context);
} }
@ -579,7 +578,5 @@ function addMainBinding(callback: (arg: any) => void) {
} }
function toBidiExecutionContext(executionContext: dom.FrameExecutionContext): BidiExecutionContext { function toBidiExecutionContext(executionContext: dom.FrameExecutionContext): BidiExecutionContext {
return (executionContext as any)[contextDelegateSymbol] as BidiExecutionContext; return executionContext.delegate as BidiExecutionContext;
} }
const contextDelegateSymbol = Symbol('delegate');

View File

@ -675,7 +675,6 @@ class FrameSession {
else if (contextPayload.name === UTILITY_WORLD_NAME) else if (contextPayload.name === UTILITY_WORLD_NAME)
worldName = 'utility'; worldName = 'utility';
const context = new dom.FrameExecutionContext(delegate, frame, worldName); const context = new dom.FrameExecutionContext(delegate, frame, worldName);
(context as any)[contextDelegateSymbol] = delegate;
if (worldName) if (worldName)
frame._contextCreated(worldName, context); frame._contextCreated(worldName, context);
this._contextIdToContext.set(contextPayload.id, context); this._contextIdToContext.set(contextPayload.id, context);
@ -1163,7 +1162,7 @@ class FrameSession {
async _adoptBackendNodeId(backendNodeId: Protocol.DOM.BackendNodeId, to: dom.FrameExecutionContext): Promise<dom.ElementHandle> { async _adoptBackendNodeId(backendNodeId: Protocol.DOM.BackendNodeId, to: dom.FrameExecutionContext): Promise<dom.ElementHandle> {
const result = await this._client._sendMayFail('DOM.resolveNode', { const result = await this._client._sendMayFail('DOM.resolveNode', {
backendNodeId, backendNodeId,
executionContextId: ((to as any)[contextDelegateSymbol] as CRExecutionContext)._contextId, executionContextId: (to.delegate as CRExecutionContext)._contextId,
}); });
if (!result || result.object.subtype === 'null') if (!result || result.object.subtype === 'null')
throw new Error(dom.kUnableToAdoptErrorMessage); throw new Error(dom.kUnableToAdoptErrorMessage);
@ -1196,8 +1195,6 @@ async function emulateTimezone(session: CRSession, timezoneId: string) {
} }
} }
const contextDelegateSymbol = Symbol('delegate');
// Chromium reference: https://source.chromium.org/chromium/chromium/src/+/main:components/embedder_support/user_agent_utils.cc;l=434;drc=70a6711e08e9f9e0d8e4c48e9ba5cab62eb010c2 // Chromium reference: https://source.chromium.org/chromium/chromium/src/+/main:components/embedder_support/user_agent_utils.cc;l=434;drc=70a6711e08e9f9e0d8e4c48e9ba5cab62eb010c2
function calculateUserAgentMetadata(options: types.BrowserContextOptions) { function calculateUserAgentMetadata(options: types.BrowserContextOptions) {
const ua = options.userAgent; const ua = options.userAgent;

View File

@ -150,7 +150,6 @@ export class FFPage implements PageDelegate {
else if (!auxData.name) else if (!auxData.name)
worldName = 'main'; worldName = 'main';
const context = new dom.FrameExecutionContext(delegate, frame, worldName); const context = new dom.FrameExecutionContext(delegate, frame, worldName);
(context as any)[contextDelegateSymbol] = delegate;
if (worldName) if (worldName)
frame._contextCreated(worldName, context); frame._contextCreated(worldName, context);
this._contextIdToContext.set(executionContextId, context); this._contextIdToContext.set(executionContextId, context);
@ -527,7 +526,7 @@ export class FFPage implements PageDelegate {
const result = await this._session.send('Page.adoptNode', { const result = await this._session.send('Page.adoptNode', {
frameId: handle._context.frame._id, frameId: handle._context.frame._id,
objectId: handle._objectId, objectId: handle._objectId,
executionContextId: ((to as any)[contextDelegateSymbol] as FFExecutionContext)._executionContextId executionContextId: (to.delegate as FFExecutionContext)._executionContextId
}); });
if (!result.remoteObject) if (!result.remoteObject)
throw new Error(dom.kUnableToAdoptErrorMessage); throw new Error(dom.kUnableToAdoptErrorMessage);

View File

@ -56,7 +56,7 @@ export interface ExecutionContextDelegate {
} }
export class ExecutionContext extends SdkObject { export class ExecutionContext extends SdkObject {
private _delegate: ExecutionContextDelegate; readonly delegate: ExecutionContextDelegate;
private _utilityScriptPromise: Promise<JSHandle> | undefined; private _utilityScriptPromise: Promise<JSHandle> | undefined;
private _contextDestroyedScope = new LongStandingScope(); private _contextDestroyedScope = new LongStandingScope();
readonly worldNameForTest: string; readonly worldNameForTest: string;
@ -64,7 +64,7 @@ export class ExecutionContext extends SdkObject {
constructor(parent: SdkObject, delegate: ExecutionContextDelegate, worldNameForTest: string) { constructor(parent: SdkObject, delegate: ExecutionContextDelegate, worldNameForTest: string) {
super(parent, 'execution-context'); super(parent, 'execution-context');
this.worldNameForTest = worldNameForTest; this.worldNameForTest = worldNameForTest;
this._delegate = delegate; this.delegate = delegate;
} }
contextDestroyed(reason: string) { contextDestroyed(reason: string) {
@ -76,24 +76,24 @@ export class ExecutionContext extends SdkObject {
} }
rawEvaluateJSON(expression: string): Promise<any> { rawEvaluateJSON(expression: string): Promise<any> {
return this._raceAgainstContextDestroyed(this._delegate.rawEvaluateJSON(expression)); return this._raceAgainstContextDestroyed(this.delegate.rawEvaluateJSON(expression));
} }
rawEvaluateHandle(expression: string): Promise<ObjectId> { rawEvaluateHandle(expression: string): Promise<ObjectId> {
return this._raceAgainstContextDestroyed(this._delegate.rawEvaluateHandle(expression)); return this._raceAgainstContextDestroyed(this.delegate.rawEvaluateHandle(expression));
} }
async evaluateWithArguments(expression: string, returnByValue: boolean, values: any[], objectIds: ObjectId[]): Promise<any> { async evaluateWithArguments(expression: string, returnByValue: boolean, values: any[], objectIds: ObjectId[]): Promise<any> {
const utilityScript = await this._utilityScript(); const utilityScript = await this._utilityScript();
return this._raceAgainstContextDestroyed(this._delegate.evaluateWithArguments(expression, returnByValue, utilityScript, values, objectIds)); return this._raceAgainstContextDestroyed(this.delegate.evaluateWithArguments(expression, returnByValue, utilityScript, values, objectIds));
} }
getProperties(object: JSHandle): Promise<Map<string, JSHandle>> { getProperties(object: JSHandle): Promise<Map<string, JSHandle>> {
return this._raceAgainstContextDestroyed(this._delegate.getProperties(object)); return this._raceAgainstContextDestroyed(this.delegate.getProperties(object));
} }
releaseHandle(objectId: ObjectId): Promise<void> { releaseHandle(objectId: ObjectId): Promise<void> {
return this._delegate.releaseHandle(objectId); return this.delegate.releaseHandle(objectId);
} }
adoptIfNeeded(handle: JSHandle): Promise<JSHandle> | null { adoptIfNeeded(handle: JSHandle): Promise<JSHandle> | null {
@ -108,7 +108,7 @@ export class ExecutionContext extends SdkObject {
${utilityScriptSource.source} ${utilityScriptSource.source}
return new (module.exports.UtilityScript())(${isUnderTest()}); return new (module.exports.UtilityScript())(${isUnderTest()});
})();`; })();`;
this._utilityScriptPromise = this._raceAgainstContextDestroyed(this._delegate.rawEvaluateHandle(source).then(objectId => new JSHandle(this, 'object', 'UtilityScript', objectId))); this._utilityScriptPromise = this._raceAgainstContextDestroyed(this.delegate.rawEvaluateHandle(source).then(objectId => new JSHandle(this, 'object', 'UtilityScript', objectId)));
} }
return this._utilityScriptPromise; return this._utilityScriptPromise;
} }

View File

@ -496,7 +496,6 @@ export class WKPage implements PageDelegate {
else if (contextPayload.type === 'user' && contextPayload.name === UTILITY_WORLD_NAME) else if (contextPayload.type === 'user' && contextPayload.name === UTILITY_WORLD_NAME)
worldName = 'utility'; worldName = 'utility';
const context = new dom.FrameExecutionContext(delegate, frame, worldName); const context = new dom.FrameExecutionContext(delegate, frame, worldName);
(context as any)[contextDelegateSymbol] = delegate;
if (worldName) if (worldName)
frame._contextCreated(worldName, context); frame._contextCreated(worldName, context);
this._contextIdToContext.set(contextPayload.id, context); this._contextIdToContext.set(contextPayload.id, context);
@ -954,7 +953,7 @@ export class WKPage implements PageDelegate {
async adoptElementHandle<T extends Node>(handle: dom.ElementHandle<T>, to: dom.FrameExecutionContext): Promise<dom.ElementHandle<T>> { async adoptElementHandle<T extends Node>(handle: dom.ElementHandle<T>, to: dom.FrameExecutionContext): Promise<dom.ElementHandle<T>> {
const result = await this._session.sendMayFail('DOM.resolveNode', { const result = await this._session.sendMayFail('DOM.resolveNode', {
objectId: handle._objectId, objectId: handle._objectId,
executionContextId: ((to as any)[contextDelegateSymbol] as WKExecutionContext)._contextId executionContextId: (to.delegate as WKExecutionContext)._contextId
}); });
if (!result || result.object.subtype === 'null') if (!result || result.object.subtype === 'null')
throw new Error(dom.kUnableToAdoptErrorMessage); throw new Error(dom.kUnableToAdoptErrorMessage);
@ -978,7 +977,7 @@ export class WKPage implements PageDelegate {
const context = await parent._mainContext(); const context = await parent._mainContext();
const result = await this._session.send('DOM.resolveNode', { const result = await this._session.send('DOM.resolveNode', {
frameId: frame._id, frameId: frame._id,
executionContextId: ((context as any)[contextDelegateSymbol] as WKExecutionContext)._contextId executionContextId: (context.delegate as WKExecutionContext)._contextId
}); });
if (!result || result.object.subtype === 'null') if (!result || result.object.subtype === 'null')
throw new Error('Frame has been detached.'); throw new Error('Frame has been detached.');
@ -1253,5 +1252,3 @@ function isLoadedSecurely(url: string, timing: network.ResourceTiming) {
return true; return true;
} catch (_) {} } catch (_) {}
} }
const contextDelegateSymbol = Symbol('delegate');