Skip to content

Commit

Permalink
feat: implement bluetooth event
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Mar 26, 2024
1 parent 47f3f87 commit 4fcf84b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/bidiMapper/CommandProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {Result} from '../utils/result.js';

import {BidiNoOpParser} from './BidiNoOpParser.js';
import type {BidiCommandParameterParser} from './BidiParser.js';
import {BluetoohProcessor} from './domains/bluetooth/BluetoothProcessor';
import {BrowserProcessor} from './domains/browser/BrowserProcessor.js';
import {CdpProcessor} from './domains/cdp/CdpProcessor.js';
import {BrowsingContextProcessor} from './domains/context/BrowsingContextProcessor.js';
Expand Down Expand Up @@ -60,6 +61,7 @@ type CommandProcessorEventsMap = {

export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
// keep-sorted start
#bluetoothProcessor: BluetoohProcessor;
#browserProcessor: BrowserProcessor;
#browsingContextProcessor: BrowsingContextProcessor;
#cdpProcessor: CdpProcessor;
Expand Down Expand Up @@ -98,6 +100,10 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
const preloadScriptStorage = new PreloadScriptStorage();

// keep-sorted start block=yes
this.#bluetoothProcessor = new BluetoohProcessor(
eventManager,
browsingContextStorage
);
this.#browserProcessor = new BrowserProcessor(browserCdpClient);
this.#browsingContextProcessor = new BrowsingContextProcessor(
cdpConnection,
Expand All @@ -106,6 +112,7 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
eventManager,
browsingContextStorage,
realmStorage,
this.#bluetoothProcessor,
networkStorage,
preloadScriptStorage,
acceptInsecureCerts,
Expand Down
50 changes: 50 additions & 0 deletions src/bidiMapper/domains/bluetooth/BluetoothProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2024 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type {BrowsingContextStorage} from '../context/BrowsingContextStorage.js';
import type {CdpTarget} from '../context/CdpTarget.js';
import type {EventManager} from '../session/EventManager.js';

export class BluetoohProcessor {
#eventManager: EventManager;
#browsingContextStorage: BrowsingContextStorage;

constructor(
eventManager: EventManager,
browsingContextStorage: BrowsingContextStorage
) {
this.#eventManager = eventManager;
this.#browsingContextStorage = browsingContextStorage;
}

onCdpTargetCreated(cdpTarget: CdpTarget) {
cdpTarget.cdpClient.on('DeviceAccess.deviceRequestPrompted', (event) => {
this.#eventManager.registerEvent(
{
type: 'event',
method: 'bluetooth.requestDevicePromptOpened',
params: {
context: cdpTarget.id,
prompt: event.id,
devices: event.devices,
},
},
cdpTarget.id
);
});
}
}
6 changes: 6 additions & 0 deletions src/bidiMapper/domains/context/BrowsingContextProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '../../../protocol/protocol.js';
import {CdpErrorConstants} from '../../../utils/CdpErrorConstants.js';
import {LogType, type LoggerFn} from '../../../utils/log.js';
import type {BluetoohProcessor} 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';
Expand All @@ -51,6 +52,8 @@ export class BrowsingContextProcessor {
readonly #selfTargetId: string;
readonly #eventManager: EventManager;

readonly #bluetoothProcessor: BluetoohProcessor;

readonly #browsingContextStorage: BrowsingContextStorage;
readonly #networkStorage: NetworkStorage;
readonly #acceptInsecureCerts: boolean;
Expand All @@ -67,13 +70,15 @@ export class BrowsingContextProcessor {
eventManager: EventManager,
browsingContextStorage: BrowsingContextStorage,
realmStorage: RealmStorage,
bluetoothProcessor: BluetoohProcessor,
networkStorage: NetworkStorage,
preloadScriptStorage: PreloadScriptStorage,
acceptInsecureCerts: boolean,
defaultUserContextId: Browser.UserContext,
logger?: LoggerFn
) {
this.#acceptInsecureCerts = acceptInsecureCerts;
this.#bluetoothProcessor = bluetoothProcessor;
this.#cdpConnection = cdpConnection;
this.#browserCdpClient = browserCdpClient;
this.#selfTargetId = selfTargetId;
Expand Down Expand Up @@ -499,6 +504,7 @@ export class BrowsingContextProcessor {
);

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

return target;
}
Expand Down
9 changes: 9 additions & 0 deletions src/bidiMapper/domains/context/CdpTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export class CdpTarget {
}),
this.#initAndEvaluatePreloadScripts(),
this.#cdpClient.sendCommand('Runtime.runIfWaitingForDebugger'),
this.#initDeviceAccessIfNeeded(),
]);
} catch (error: any) {
// The target might have been closed before the initialization finished.
Expand Down Expand Up @@ -226,6 +227,14 @@ export class CdpTarget {
}
}

async #initDeviceAccessIfNeeded(): Promise<void> {
const enabled = this.isSubscribedTo(BiDiModule.Bluetooth);
await this.#cdpClient.sendCommand(
enabled ? 'DeviceAccess.enable' : 'DeviceAccess.disable'
);
return;
}

#setEventListeners() {
this.#cdpClient.on('*', (event, params) => {
// We may encounter uses for EventEmitter other than CDP events,
Expand Down
5 changes: 5 additions & 0 deletions src/bidiMapper/domains/session/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ export class SubscriptionManager {
this.subscribe(specificEvent, contextId, channel)
);
return;
case ChromiumBidi.BiDiModule.Bluetooth:
Object.values(ChromiumBidi.Bluetooth.EventNames).map((specificEvent) =>
this.subscribe(specificEvent, contextId, channel)
);
return;
default:
// Intentionally left empty.
}
Expand Down
21 changes: 19 additions & 2 deletions src/protocol/chromium-bidi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/

import type * as Cdp from './cdp.js';
import type * as WebDriverBidiBluetooth from './generated/webdriver-bidi-bluetooth.js';
import type * as WebDriverBidiPermissions from './generated/webdriver-bidi-permissions.js';
import type * as WebDriverBidi from './generated/webdriver-bidi.js';

export type EventNames =
// keep-sorted start
| Cdp.EventNames
| `${BiDiModule}`
| `${Bluetooth.EventNames}`
| `${BrowsingContext.EventNames}`
| `${Log.EventNames}`
| `${Network.EventNames}`
Expand All @@ -31,6 +33,7 @@ export type EventNames =

export enum BiDiModule {
// keep-sorted start
Bluetooth = 'bluetooth',
Browser = 'browser',
BrowsingContext = 'browsingContext',
Cdp = 'cdp',
Expand Down Expand Up @@ -88,6 +91,13 @@ export namespace Network {
}
}

export namespace Bluetooth {
export enum EventNames {
RequestDevicePromptOpened = 'bluetooth.requestDevicePromptOpened',
RequestDevicePromptClosed = 'bluetooth.requestDevicePromptClosed',
}
}

export type Command = (
| WebDriverBidi.Command
| Cdp.Command
Expand All @@ -104,7 +114,14 @@ export type CommandResponse =
| WebDriverBidi.CommandResponse
| Cdp.CommandResponse;

export type Event = WebDriverBidi.Event | Cdp.Event;
export type BluetoothEvent = {
type: 'event';
} & (
| WebDriverBidiBluetooth.Bluetooth.RequestDevicePromptOpened
| (WebDriverBidiBluetooth.Bluetooth.RequestDevicePromptClosed &
WebDriverBidi.Extensible)
);
export type Event = WebDriverBidi.Event | Cdp.Event | BluetoothEvent;

export const EVENT_NAMES = new Set([
// keep-sorted start
Expand All @@ -120,6 +137,6 @@ export type ResultData = WebDriverBidi.ResultData | Cdp.ResultData;

export type BidiPlusChannel = string | null;

export type Message = (WebDriverBidi.Message | Cdp.Message) & {
export type Message = (WebDriverBidi.Message | Cdp.Message | BluetoothEvent) & {
channel?: BidiPlusChannel;
};
1 change: 1 addition & 0 deletions src/protocol/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * as ChromiumBidi from './chromium-bidi.js';
export * from './generated/webdriver-bidi.js';
export * from './ErrorResponse.js';
export * from './generated/webdriver-bidi-permissions.js';
export * from './generated/webdriver-bidi-bluetooth.js';

0 comments on commit 4fcf84b

Please sign in to comment.