Skip to content

Commit

Permalink
chore: use getContext
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Aug 13, 2024
1 parent 72fbfc9 commit d7ac42f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 175 deletions.
8 changes: 8 additions & 0 deletions src/bidiMapper/BidiServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -55,6 +56,7 @@ export class BidiServer extends EventEmitter<BidiServerEvent> {
#browsingContextStorage = new BrowsingContextStorage();
#realmStorage = new RealmStorage();
#preloadScriptStorage = new PreloadScriptStorage();
#bluetoothProcessor: BluetoothProcessor;

#logger?: LoggerFn;

Expand Down Expand Up @@ -98,6 +100,10 @@ export class BidiServer extends EventEmitter<BidiServerEvent> {
browserCdpClient,
logger
);
this.#bluetoothProcessor = new BluetoothProcessor(
this.#eventManager,
this.#browsingContextStorage
);
this.#commandProcessor = new CommandProcessor(
cdpConnection,
browserCdpClient,
Expand All @@ -106,6 +112,7 @@ export class BidiServer extends EventEmitter<BidiServerEvent> {
this.#realmStorage,
this.#preloadScriptStorage,
networkStorage,
this.#bluetoothProcessor,
parser,
async (options: MapperOptions) => {
// This is required to ignore certificate errors when service worker is fetched.
Expand All @@ -123,6 +130,7 @@ export class BidiServer extends EventEmitter<BidiServerEvent> {
this.#browsingContextStorage,
this.#realmStorage,
networkStorage,
this.#bluetoothProcessor,
this.#preloadScriptStorage,
defaultUserContextId,
options?.unhandledPromptBehavior,
Expand Down
8 changes: 3 additions & 5 deletions src/bidiMapper/CommandProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
realmStorage: RealmStorage,
preloadScriptStorage: PreloadScriptStorage,
networkStorage: NetworkStorage,
bluetoothProcessor: BluetoothProcessor,
parser: BidiCommandParameterParser = new BidiNoOpParser(),
initConnection: (options: MapperOptions) => Promise<void>,
logger?: LoggerFn
Expand All @@ -92,16 +93,13 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
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(
Expand Down
14 changes: 4 additions & 10 deletions src/bidiMapper/modules/bluetooth/BluetoothProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -56,10 +52,8 @@ export class BluetoothProcessor {
async handleRequestDevicePrompt(
params: Bluetooth.HandleRequestDevicePromptParameters
): Promise<EmptyResult> {
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',
Expand Down
5 changes: 5 additions & 0 deletions src/bidiMapper/modules/cdp/CdpTargetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -49,6 +50,7 @@ export class CdpTargetManager {

readonly #browsingContextStorage: BrowsingContextStorage;
readonly #networkStorage: NetworkStorage;
readonly #bluetoothProcessor: BluetoothProcessor;
readonly #preloadScriptStorage: PreloadScriptStorage;
readonly #realmStorage: RealmStorage;

Expand All @@ -64,6 +66,7 @@ export class CdpTargetManager {
browsingContextStorage: BrowsingContextStorage,
realmStorage: RealmStorage,
networkStorage: NetworkStorage,
bluetoothProcessor: BluetoothProcessor,
preloadScriptStorage: PreloadScriptStorage,
defaultUserContextId: Browser.UserContext,
unhandledPromptBehavior?: Session.UserPromptHandler,
Expand All @@ -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;
Expand Down Expand Up @@ -288,6 +292,7 @@ export class CdpTargetManager {
);

this.#networkStorage.onCdpTargetCreated(target);
this.#bluetoothProcessor.onCdpTargetCreated(target);

return target;
}
Expand Down
160 changes: 0 additions & 160 deletions src/bidiMapper/modules/context/BrowsingContextProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, Realm>();
#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();
}
Expand Down

0 comments on commit d7ac42f

Please sign in to comment.