chrome-devtools-frontend
Version:
Chrome DevTools UI
139 lines (121 loc) • 3.17 kB
text/typescript
/**
* @license
* Copyright 2025 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
import type * as Bidi from 'webdriver-bidi-protocol';
import {
DeviceRequestPrompt,
type DeviceRequestPromptDevice,
} from '../api/DeviceRequestPrompt.js';
import {UnsupportedOperation} from '../common/Errors.js';
import {Deferred} from '../util/Deferred.js';
import type {Session} from './core/Session.js';
/**
* @internal
*/
export class BidiDeviceRequestPromptManager {
readonly
readonly
constructor(contextId: string, session: Session) {
this.
this.
}
async
if (!this.
this.
await this.
['bluetooth.requestDevicePromptUpdated'],
[],
);
}
}
async waitForDevicePrompt(
timeout: number,
signal: AbortSignal | undefined,
): Promise<DeviceRequestPrompt> {
const deferred = Deferred.create<DeviceRequestPrompt>({
message: `Waiting for \`DeviceRequestPrompt\` failed: ${timeout}ms exceeded`,
timeout,
});
const onRequestDevicePromptUpdated = (
params: Bidi.Bluetooth.RequestDevicePromptUpdatedParameters,
) => {
if (params.context === this.
deferred.resolve(
new BidiDeviceRequestPrompt(
this.
params.prompt,
this.
params.devices,
),
);
this.
'bluetooth.requestDevicePromptUpdated',
onRequestDevicePromptUpdated,
);
}
};
this.
'bluetooth.requestDevicePromptUpdated',
onRequestDevicePromptUpdated,
);
if (signal) {
signal.addEventListener(
'abort',
() => {
deferred.reject(signal.reason);
},
{once: true},
);
}
await this.
return await deferred.valueOrThrow();
}
}
/**
* @internal
*/
export class BidiDeviceRequestPrompt extends DeviceRequestPrompt {
readonly
constructor(
contextId: string,
promptId: string,
session: Session,
devices: Bidi.Bluetooth.RequestDeviceInfo[],
) {
super();
this.
this.
this.
this.devices.push(
...devices.map(d => {
return {
id: d.id,
name: d.name ?? 'UNKNOWN',
};
}),
);
}
async cancel(): Promise<void> {
await this.
context: this.
prompt: this.
accept: false,
});
}
async select(device: DeviceRequestPromptDevice): Promise<void> {
await this.
context: this.
prompt: this.
accept: true,
device: device.id,
});
}
waitForDevice(): never {
throw new UnsupportedOperation();
}
}