From d7ac42fc021dce252c1ea03411fa27926905ceef Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Tue, 13 Aug 2024 18:34:24 +0200 Subject: [PATCH] chore: use getContext --- src/bidiMapper/BidiServer.ts | 8 + src/bidiMapper/CommandProcessor.ts | 8 +- .../modules/bluetooth/BluetoothProcessor.ts | 14 +- .../modules/cdp/CdpTargetManager.ts | 5 + .../context/BrowsingContextProcessor.ts | 160 ------------------ 5 files changed, 20 insertions(+), 175 deletions(-) diff --git a/src/bidiMapper/BidiServer.ts b/src/bidiMapper/BidiServer.ts index 0573268f15..b77b14437d 100644 --- a/src/bidiMapper/BidiServer.ts +++ b/src/bidiMapper/BidiServer.ts @@ -26,6 +26,7 @@ import type {Result} from '../utils/result.js'; import type {BidiCommandParameterParser} from './BidiParser.js'; import type {BidiTransport} from './BidiTransport.js'; import {CommandProcessor, CommandProcessorEvents} from './CommandProcessor.js'; +import { BluetoothProcessor } from './modules/bluetooth/BluetoothProcessor'; import {CdpTargetManager} from './modules/cdp/CdpTargetManager.js'; import {BrowsingContextStorage} from './modules/context/BrowsingContextStorage.js'; import {NetworkStorage} from './modules/network/NetworkStorage.js'; @@ -55,6 +56,7 @@ export class BidiServer extends EventEmitter { #browsingContextStorage = new BrowsingContextStorage(); #realmStorage = new RealmStorage(); #preloadScriptStorage = new PreloadScriptStorage(); + #bluetoothProcessor: BluetoothProcessor; #logger?: LoggerFn; @@ -98,6 +100,10 @@ export class BidiServer extends EventEmitter { browserCdpClient, logger ); + this.#bluetoothProcessor = new BluetoothProcessor( + this.#eventManager, + this.#browsingContextStorage + ); this.#commandProcessor = new CommandProcessor( cdpConnection, browserCdpClient, @@ -106,6 +112,7 @@ export class BidiServer extends EventEmitter { this.#realmStorage, this.#preloadScriptStorage, networkStorage, + this.#bluetoothProcessor, parser, async (options: MapperOptions) => { // This is required to ignore certificate errors when service worker is fetched. @@ -123,6 +130,7 @@ export class BidiServer extends EventEmitter { this.#browsingContextStorage, this.#realmStorage, networkStorage, + this.#bluetoothProcessor, this.#preloadScriptStorage, defaultUserContextId, options?.unhandledPromptBehavior, diff --git a/src/bidiMapper/CommandProcessor.ts b/src/bidiMapper/CommandProcessor.ts index e92f9c91bb..59f6e929e4 100644 --- a/src/bidiMapper/CommandProcessor.ts +++ b/src/bidiMapper/CommandProcessor.ts @@ -84,6 +84,7 @@ export class CommandProcessor extends EventEmitter { realmStorage: RealmStorage, preloadScriptStorage: PreloadScriptStorage, networkStorage: NetworkStorage, + bluetoothProcessor: BluetoothProcessor, parser: BidiCommandParameterParser = new BidiNoOpParser(), initConnection: (options: MapperOptions) => Promise, logger?: LoggerFn @@ -92,16 +93,13 @@ export class CommandProcessor extends EventEmitter { this.#parser = parser; this.#logger = logger; + this.#bluetoothProcessor = bluetoothProcessor; + // keep-sorted start block=yes - this.#bluetoothProcessor = new BluetoothProcessor( - eventManager, - browsingContextStorage - ); this.#browserProcessor = new BrowserProcessor(browserCdpClient); this.#browsingContextProcessor = new BrowsingContextProcessor( browserCdpClient, browsingContextStorage, - this.#bluetoothProcessor, eventManager ); this.#cdpProcessor = new CdpProcessor( diff --git a/src/bidiMapper/modules/bluetooth/BluetoothProcessor.ts b/src/bidiMapper/modules/bluetooth/BluetoothProcessor.ts index d26b0696f6..11eefe64ca 100644 --- a/src/bidiMapper/modules/bluetooth/BluetoothProcessor.ts +++ b/src/bidiMapper/modules/bluetooth/BluetoothProcessor.ts @@ -15,13 +15,9 @@ * limitations under the License. */ -import { - NoSuchFrameException, - type Bluetooth, - type EmptyResult, -} from '../../../protocol/protocol.js'; +import type {Bluetooth, EmptyResult} from '../../../protocol/protocol.js'; +import { CdpTarget } from '../cdp/CdpTarget.js'; import type {BrowsingContextStorage} from '../context/BrowsingContextStorage.js'; -import type {CdpTarget} from '../context/CdpTarget.js'; import type {EventManager} from '../session/EventManager.js'; export class BluetoothProcessor { @@ -56,10 +52,8 @@ export class BluetoothProcessor { async handleRequestDevicePrompt( params: Bluetooth.HandleRequestDevicePromptParameters ): Promise { - const context = this.#browsingContextStorage.findContext(params.context); - if (!context) { - throw new NoSuchFrameException('context not found'); - } + const context = this.#browsingContextStorage.getContext(params.context); + if (params.accept) { await context.cdpTarget.cdpClient.sendCommand( 'DeviceAccess.selectPrompt', diff --git a/src/bidiMapper/modules/cdp/CdpTargetManager.ts b/src/bidiMapper/modules/cdp/CdpTargetManager.ts index 1af49d1e1e..de68e72c31 100644 --- a/src/bidiMapper/modules/cdp/CdpTargetManager.ts +++ b/src/bidiMapper/modules/cdp/CdpTargetManager.ts @@ -33,6 +33,7 @@ import {WorkerRealm, type WorkerRealmType} from '../script/WorkerRealm.js'; import type {EventManager} from '../session/EventManager.js'; import {CdpTarget} from './CdpTarget.js'; +import { BluetoothProcessor } from '../bluetooth/BluetoothProcessor.js'; const cdpToBidiTargetTypes = { service_worker: 'service-worker', @@ -49,6 +50,7 @@ export class CdpTargetManager { readonly #browsingContextStorage: BrowsingContextStorage; readonly #networkStorage: NetworkStorage; + readonly #bluetoothProcessor: BluetoothProcessor; readonly #preloadScriptStorage: PreloadScriptStorage; readonly #realmStorage: RealmStorage; @@ -64,6 +66,7 @@ export class CdpTargetManager { browsingContextStorage: BrowsingContextStorage, realmStorage: RealmStorage, networkStorage: NetworkStorage, + bluetoothProcessor: BluetoothProcessor, preloadScriptStorage: PreloadScriptStorage, defaultUserContextId: Browser.UserContext, unhandledPromptBehavior?: Session.UserPromptHandler, @@ -77,6 +80,7 @@ export class CdpTargetManager { this.#browsingContextStorage = browsingContextStorage; this.#preloadScriptStorage = preloadScriptStorage; this.#networkStorage = networkStorage; + this.#bluetoothProcessor = bluetoothProcessor; this.#realmStorage = realmStorage; this.#defaultUserContextId = defaultUserContextId; this.#unhandledPromptBehavior = unhandledPromptBehavior; @@ -288,6 +292,7 @@ export class CdpTargetManager { ); this.#networkStorage.onCdpTargetCreated(target); + this.#bluetoothProcessor.onCdpTargetCreated(target); return target; } diff --git a/src/bidiMapper/modules/context/BrowsingContextProcessor.ts b/src/bidiMapper/modules/context/BrowsingContextProcessor.ts index a9035f9a5a..d5dbe75858 100644 --- a/src/bidiMapper/modules/context/BrowsingContextProcessor.ts +++ b/src/bidiMapper/modules/context/BrowsingContextProcessor.ts @@ -26,37 +26,22 @@ import { NoSuchAlertException, } from '../../../protocol/protocol.js'; import {CdpErrorConstants} from '../../../utils/CdpErrorConstants.js'; -<<<<<<< HEAD -======= -import {LogType, type LoggerFn} from '../../../utils/log.js'; -import type {BluetoothProcessor} from '../bluetooth/BluetoothProcessor.js'; -import type {NetworkStorage} from '../network/NetworkStorage.js'; -import type {PreloadScriptStorage} from '../script/PreloadScriptStorage.js'; -import type {Realm} from '../script/Realm.js'; -import type {RealmStorage} from '../script/RealmStorage.js'; -import {WorkerRealm, type WorkerRealmType} from '../script/WorkerRealm.js'; ->>>>>>> 91be1069 (feat: implement bluetooth event) import type {EventManager} from '../session/EventManager.js'; import type {BrowsingContextImpl} from './BrowsingContextImpl.js'; import type {BrowsingContextStorage} from './BrowsingContextStorage.js'; -import { BluetoothProcessor } from '../bluetooth/BluetoothProcessor.js'; export class BrowsingContextProcessor { readonly #browserCdpClient: CdpClient; readonly #browsingContextStorage: BrowsingContextStorage; readonly #eventManager: EventManager; - readonly #bluetoothProcessor: BluetoothProcessor; - constructor( browserCdpClient: CdpClient, browsingContextStorage: BrowsingContextStorage, - bluetoothProcessor: BluetoothProcessor, eventManager: EventManager ) { this.#browserCdpClient = browserCdpClient; this.#browsingContextStorage = browsingContextStorage; - this.#bluetoothProcessor = bluetoothProcessor; this.#eventManager = eventManager; this.#eventManager.addSubscribeHook( ChromiumBidi.BrowsingContext.EventNames.ContextCreated, @@ -327,151 +312,6 @@ export class BrowsingContextProcessor { }, context.id ); -<<<<<<< HEAD -======= - } - } - - #handleFrameDetachedEvent(params: Protocol.Page.FrameDetachedEvent) { - // In case of OOPiF no need in deleting BrowsingContext. - if (params.reason === 'swap') { - return; - } - this.#browsingContextStorage.findContext(params.frameId)?.dispose(); - } - - #handleAttachedToTargetEvent( - params: Protocol.Target.AttachedToTargetEvent, - parentSessionCdpClient: CdpClient - ) { - const {sessionId, targetInfo} = params; - const targetCdpClient = this.#cdpConnection.getCdpClient(sessionId); - - this.#logger?.( - LogType.debugInfo, - 'AttachedToTarget event received:', - params - ); - - switch (targetInfo.type) { - case 'page': - case 'iframe': { - if (targetInfo.targetId === this.#selfTargetId) { - break; - } - - const cdpTarget = this.#createCdpTarget(targetCdpClient, targetInfo); - const maybeContext = this.#browsingContextStorage.findContext( - targetInfo.targetId - ); - if (maybeContext) { - // OOPiF. - maybeContext.updateCdpTarget(cdpTarget); - } else { - // New context. - BrowsingContextImpl.create( - cdpTarget, - this.#realmStorage, - targetInfo.targetId, - null, - targetInfo.browserContextId && - targetInfo.browserContextId !== this.#defaultUserContextId - ? targetInfo.browserContextId - : 'default', - this.#eventManager, - this.#browsingContextStorage, - this.#logger - ); - } - return; - } - case 'service_worker': - case 'worker': { - const realm = this.#realmStorage.findRealm({ - cdpSessionId: parentSessionCdpClient.sessionId, - }); - // If there is no browsing context, this worker is already terminated. - if (!realm) { - break; - } - - const cdpTarget = this.#createCdpTarget(targetCdpClient, targetInfo); - this.#handleWorkerTarget( - cdpToBidiTargetTypes[targetInfo.type], - cdpTarget, - realm - ); - return; - } - // In CDP, we only emit shared workers on the browser and not the set of - // frames that use the shared worker. If we change this in the future to - // behave like service workers (emits on both browser and frame targets), - // we can remove this block and merge service workers with the above one. - case 'shared_worker': { - const cdpTarget = this.#createCdpTarget(targetCdpClient, targetInfo); - this.#handleWorkerTarget( - cdpToBidiTargetTypes[targetInfo.type], - cdpTarget - ); - return; - } - } - - // DevTools or some other not supported by BiDi target. Just release - // debugger and ignore them. - targetCdpClient - .sendCommand('Runtime.runIfWaitingForDebugger') - .then(() => - parentSessionCdpClient.sendCommand('Target.detachFromTarget', params) - ) - .catch((error) => this.#logger?.(LogType.debugError, error)); - } - - #createCdpTarget( - targetCdpClient: CdpClient, - targetInfo: Protocol.Target.TargetInfo - ) { - this.#setEventListeners(targetCdpClient); - - const target = CdpTarget.create( - targetInfo.targetId, - targetCdpClient, - this.#browserCdpClient, - this.#realmStorage, - this.#eventManager, - this.#preloadScriptStorage, - this.#networkStorage, - this.#acceptInsecureCerts, - this.#logger - ); - - this.#networkStorage.onCdpTargetCreated(target); - this.#bluetoothProcessor.onCdpTargetCreated(target); - - return target; - } - - #workers = new Map(); - #handleWorkerTarget( - realmType: WorkerRealmType, - cdpTarget: CdpTarget, - ownerRealm?: Realm - ) { - cdpTarget.cdpClient.on('Runtime.executionContextCreated', (params) => { - const {uniqueId, id, origin} = params.context; - const workerRealm = new WorkerRealm( - cdpTarget.cdpClient, - this.#eventManager, - id, - this.#logger, - serializeOrigin(origin), - ownerRealm ? [ownerRealm] : [], - uniqueId, - this.#realmStorage, - realmType - ); - this.#workers.set(cdpTarget.cdpSessionId, workerRealm); ->>>>>>> 91be1069 (feat: implement bluetooth event) }); return Promise.resolve(); }