Skip to content

Autofill commands #3276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/bidiMapper/BidiNoOpParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,22 @@ import type {
Permissions,
Bluetooth,
WebExtension,
Autofill,
} from '../protocol/protocol.js';

import type {BidiCommandParameterParser} from './BidiParser.js';

export class BidiNoOpParser implements BidiCommandParameterParser {
// Autofill module
// keep-sorted start block=yes
parseAutofillTriggerParams(params: unknown): Autofill.TriggerParameters {
return params as Autofill.TriggerParameters;
}
parseAutofillSetAddressParams(params: unknown) {
return params as Autofill.AddressParameters;
}
// keep-sorted end

// Bluetooth module
// keep-sorted start block=yes
parseHandleRequestDevicePromptParams(
Expand Down
7 changes: 7 additions & 0 deletions src/bidiMapper/BidiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import type {
Autofill,
Bluetooth,
Browser,
BrowsingContext,
Expand All @@ -30,6 +31,12 @@ import type {
} from '../protocol/protocol.js';

export interface BidiCommandParameterParser {
// Autofill module
// keep-sorted start block=yes
parseAutofillTriggerParams(params: unknown): Autofill.TriggerParameters;
parseAutofillSetAddressParams(params: unknown): Autofill.AddressParameters;
// keep-sorted end

// Bluetooth module
// keep-sorted start block=yes
parseHandleRequestDevicePromptParams(
Expand Down
15 changes: 15 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 type {MapperOptions} from './BidiServer.js';
import {AutofillProcessor} from './modules/autofill/AutofillProcessor.js';
import type {BluetoothProcessor} from './modules/bluetooth/BluetoothProcessor.js';
import {BrowserProcessor} from './modules/browser/BrowserProcessor.js';
import type {UserContextStorage} from './modules/browser/UserContextStorage.js';
Expand Down Expand Up @@ -63,6 +64,7 @@ interface CommandProcessorEventsMap extends Record<string | symbol, unknown> {

export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
// keep-sorted start
#autofillProcessor: AutofillProcessor;
#bluetoothProcessor: BluetoothProcessor;
#browserProcessor: BrowserProcessor;
#browsingContextProcessor: BrowsingContextProcessor;
Expand Down Expand Up @@ -100,6 +102,7 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
this.#bluetoothProcessor = bluetoothProcessor;

// keep-sorted start block=yes
this.#autofillProcessor = new AutofillProcessor(browserCdpClient);
this.#browserProcessor = new BrowserProcessor(
browserCdpClient,
browsingContextStorage,
Expand Down Expand Up @@ -148,6 +151,18 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
command: ChromiumBidi.Command,
): Promise<ChromiumBidi.ResultData> {
switch (command.method) {
// Autofill module
// keep-sorted start block=yes
case 'autofill.trigger':
return await this.#autofillProcessor.triggerById(
this.#parser.parseAutofillTriggerParams(command.params),
);
case 'autofill.setAddress':
return await this.#autofillProcessor.setAddresses(
this.#parser.parseAutofillSetAddressParams(command.params),
);
// keep-sorted end

// Bluetooth module
// keep-sorted start block=yes
case 'bluetooth.handleRequestDevicePrompt':
Expand Down
117 changes: 117 additions & 0 deletions src/bidiMapper/modules/autofill/AutofillProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* Copyright 2025 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 {CdpClient} from '../../../cdp/CdpClient.js';
import {
type Autofill,
type EmptyResult,
UnsupportedOperationException,
} from '../../../protocol/protocol.js';

/**
* Responsible for handling the `autofill` module.
*/
export class AutofillProcessor {
readonly #browserCdpClient: CdpClient;

constructor(browserCdpClient: CdpClient) {
this.#browserCdpClient = browserCdpClient;
}

/**
* Triggers autofill for a specific element with the provided field data.
*
* @param params Parameters for the autofill.trigger command
* @returns An empty result
*/
async triggerById(params: Autofill.TriggerParameters): Promise<EmptyResult> {
try {
// await this.#browserCdpClient.sendCommand('Autofill.trigger', {
// fieldId: Number(params.element.sharedId),
// frameId: undefined,
// card: {
// /**
// * 16-digit credit card number.
// */
// number: '1111222233334444',
// /**
// * Name of the credit card owner.
// */
// name: 'string',
// /**
// * 2-digit expiry month.
// */
// expiryMonth: '01',
// /**
// * 4-digit expiry year.
// */
// expiryYear: '30',
// /**
// * 3-digit card verification code.
// */
// cvc: '123',
// },
// });
await this.#browserCdpClient.sendCommand('Autofill.triggerById' as any, {
field_id: 1, //params.element.sharedId,
frame_id: 'top',
});

return {};
} catch (err) {
if ((err as Error).message.includes('command was not found')) {
throw new UnsupportedOperationException(
'Autofill triggering is not supported by this browser',
);
}
throw err;
}
}

/**
* Triggers autofill for a specific element with the provided field data.
*
* @param params Parameters for the autofill.trigger command
* @returns An empty result
*/
async setAddresses(params: Autofill.AddressParameters): Promise<EmptyResult> {
try {
await this.#browserCdpClient.sendCommand('Autofill.setAddresses', {
addresses: [
{
fields: params.fields,
// fields: [
// {
// name: 'name',
// value: 'John Doe',
// },
// ],
},
],
});

return {};
} catch (err) {
if ((err as Error).message.includes('command was not found')) {
throw new UnsupportedOperationException(
'Autofill setting addresses is not supported by this browser',
);
}
throw err;
}
}
}
11 changes: 11 additions & 0 deletions src/bidiTab/BidiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
import type {BidiCommandParameterParser} from '../bidiMapper/BidiMapper.js';
import type {
Autofill,
Bluetooth,
Browser,
BrowsingContext,
Expand All @@ -31,6 +32,16 @@ import type {
import * as Parser from '../protocol-parser/protocol-parser.js';

export class BidiParser implements BidiCommandParameterParser {
// Autofill module
// keep-sorted start block=yes
parseAutofillTriggerParams(params: unknown): Autofill.TriggerParameters {
return Parser.Autofill.parseTriggerRequest(params);
}
parseAutofillSetAddressParams(params: unknown): Autofill.AddressParameters {
return Parser.Autofill.parseSetAddressRequest(params);
}
// keep-sorted end

// Bluetooth module
// keep-sorted start block=yes
parseHandleRequestDevicePromptParams(
Expand Down
Loading