From 5ce4f12960bf12981ba92cd85c31763bdb07a0aa Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Mon, 6 May 2024 13:22:11 +0200 Subject: [PATCH] refactor: move createCdpHandle to IsolatedWorld (#12397) --- .../src/cdp/ExecutionContext.ts | 17 ++--------------- .../puppeteer-core/src/cdp/IsolatedWorld.ts | 19 +++++++++++++++++-- packages/puppeteer-core/src/cdp/Page.ts | 10 ++++------ 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/packages/puppeteer-core/src/cdp/ExecutionContext.ts b/packages/puppeteer-core/src/cdp/ExecutionContext.ts index 6efdf8ac76b..5fcdc1a57aa 100644 --- a/packages/puppeteer-core/src/cdp/ExecutionContext.ts +++ b/packages/puppeteer-core/src/cdp/ExecutionContext.ts @@ -268,7 +268,7 @@ export class ExecutionContext { return returnByValue ? valueFromRemoteObject(remoteObject) - : createCdpHandle(this._world, remoteObject); + : this._world.createCdpHandle(remoteObject); } const functionDeclaration = stringifyFunction(pageFunction); @@ -305,7 +305,7 @@ export class ExecutionContext { } return returnByValue ? valueFromRemoteObject(remoteObject) - : createCdpHandle(this._world, remoteObject); + : this._world.createCdpHandle(remoteObject); async function convertArgument( this: ExecutionContext, @@ -377,16 +377,3 @@ const rewriteError = (error: Error): Protocol.Runtime.EvaluateResponse => { } throw error; }; - -/** - * @internal - */ -export function createCdpHandle( - realm: IsolatedWorld, - remoteObject: Protocol.Runtime.RemoteObject -): JSHandle | ElementHandle { - if (remoteObject.subtype === 'node') { - return new CdpElementHandle(realm, remoteObject); - } - return new CdpJSHandle(realm, remoteObject); -} diff --git a/packages/puppeteer-core/src/cdp/IsolatedWorld.ts b/packages/puppeteer-core/src/cdp/IsolatedWorld.ts index 5846ef36527..f4f73624ad1 100644 --- a/packages/puppeteer-core/src/cdp/IsolatedWorld.ts +++ b/packages/puppeteer-core/src/cdp/IsolatedWorld.ts @@ -7,6 +7,7 @@ import type {Protocol} from 'devtools-protocol'; import type {CDPSession} from '../api/CDPSession.js'; +import type {ElementHandle} from '../api/ElementHandle.js'; import type {JSHandle} from '../api/JSHandle.js'; import {Realm} from '../api/Realm.js'; import type {TimeoutSettings} from '../common/TimeoutSettings.js'; @@ -17,9 +18,11 @@ import {disposeSymbol} from '../util/disposable.js'; import {Mutex} from '../util/Mutex.js'; import type {Binding} from './Binding.js'; -import {ExecutionContext, createCdpHandle} from './ExecutionContext.js'; +import {CdpElementHandle} from './ElementHandle.js'; +import {ExecutionContext} from './ExecutionContext.js'; import type {CdpFrame} from './Frame.js'; import type {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js'; +import {CdpJSHandle} from './JSHandle.js'; import {addPageBinding} from './utils.js'; import type {CdpWebWorker} from './WebWorker.js'; @@ -231,7 +234,7 @@ export class IsolatedWorld extends Realm { backendNodeId: backendNodeId, executionContextId: executionContext._contextId, }); - return createCdpHandle(this, object) as JSHandle; + return this.createCdpHandle(object) as JSHandle; } async adoptHandle>(handle: T): Promise { @@ -266,6 +269,18 @@ export class IsolatedWorld extends Realm { return newHandle; } + /** + * @internal + */ + createCdpHandle( + remoteObject: Protocol.Runtime.RemoteObject + ): JSHandle | ElementHandle { + if (remoteObject.subtype === 'node') { + return new CdpElementHandle(this, remoteObject); + } + return new CdpJSHandle(this, remoteObject); + } + [disposeSymbol](): void { super[disposeSymbol](); this.client.off('Runtime.bindingCalled', this.#onBindingCalled); diff --git a/packages/puppeteer-core/src/cdp/Page.ts b/packages/puppeteer-core/src/cdp/Page.ts index d551a15040f..0ca97c74b7c 100644 --- a/packages/puppeteer-core/src/cdp/Page.ts +++ b/packages/puppeteer-core/src/cdp/Page.ts @@ -64,7 +64,6 @@ import {Coverage} from './Coverage.js'; import type {DeviceRequestPrompt} from './DeviceRequestPrompt.js'; import {CdpDialog} from './Dialog.js'; import {EmulationManager} from './EmulationManager.js'; -import {createCdpHandle} from './ExecutionContext.js'; import {FirefoxTargetManager} from './FirefoxTargetManager.js'; import type {CdpFrame} from './Frame.js'; import {FrameManager} from './FrameManager.js'; @@ -576,10 +575,9 @@ export class CdpPage extends Page { prototypeObjectId: prototypeHandle.id, } ); - return createCdpHandle( - this.mainFrame().mainRealm(), - response.objects - ) as HandleFor; + return this.mainFrame() + .mainRealm() + .createCdpHandle(response.objects) as HandleFor; } override async cookies(...urls: string[]): Promise { @@ -814,7 +812,7 @@ export class CdpPage extends Page { return; } const values = event.args.map(arg => { - return createCdpHandle(context._world, arg); + return context._world.createCdpHandle(arg); }); this.#addConsoleMessage( convertConsoleMessageLevel(event.type),