Skip to content
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

feat: webExtension module #3012

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -26,6 +26,7 @@ import type {
Storage,
Permissions,
Bluetooth,
WebExtension,
} from '../protocol/protocol.js';

import type {BidiCommandParameterParser} from './BidiParser.js';
Expand Down Expand Up @@ -232,4 +233,14 @@ export class BidiNoOpParser implements BidiCommandParameterParser {
return params as Storage.SetCookieParameters;
}
// keep-sorted end

// WebExtenstion module
// keep-sorted start block=yes
parseInstallParams(params: unknown): WebExtension.InstallParameters {
return params as WebExtension.InstallParameters;
}
parseUninstallParams(params: unknown): WebExtension.UninstallParameters {
return params as WebExtension.UninstallParameters;
}
// keep-sorted end
}
7 changes: 7 additions & 0 deletions src/bidiMapper/BidiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
Script,
Session,
Storage,
WebExtension,
} from '../protocol/protocol.js';

export interface BidiCommandParameterParser {
Expand Down Expand Up @@ -146,4 +147,10 @@ export interface BidiCommandParameterParser {
parseGetCookiesParams(params: unknown): Storage.GetCookiesParameters;
parseSetCookieParams(params: unknown): Storage.SetCookieParameters;
// keep-sorted end

// WebExtenstion module
// keep-sorted start block=yes
parseInstallParams(params: unknown): WebExtension.InstallParameters;
parseUninstallParams(params: unknown): WebExtension.UninstallParameters;
// keep-sorted end
}
7 changes: 5 additions & 2 deletions src/bidiMapper/CommandProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {ScriptProcessor} from './modules/script/ScriptProcessor.js';
import type {EventManager} from './modules/session/EventManager.js';
import {SessionProcessor} from './modules/session/SessionProcessor.js';
import {StorageProcessor} from './modules/storage/StorageProcessor.js';
import {WebExtensionProcessor} from './modules/webExtension/WebExtensionProcessor.js';
import {OutgoingMessage} from './OutgoingMessage.js';

export const enum CommandProcessorEvents {
Expand All @@ -71,6 +72,7 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
#scriptProcessor: ScriptProcessor;
#sessionProcessor: SessionProcessor;
#storageProcessor: StorageProcessor;
#webExtensionProcessor: WebExtensionProcessor;
// keep-sorted end

#parser: BidiCommandParameterParser;
Expand Down Expand Up @@ -134,6 +136,7 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
browsingContextStorage,
logger,
);
this.#webExtensionProcessor = new WebExtensionProcessor(browserCdpClient);
// keep-sorted end
}

Expand Down Expand Up @@ -402,8 +405,8 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
// WebExtension module
// keep-sorted start block=yes
case 'webExtension.install':
throw new UnknownErrorException(
`Method ${command.method} is not implemented.`,
return await this.#webExtensionProcessor.install(
this.#parser.parseInstallParams(command.params),
);
case 'webExtension.uninstall':
throw new UnknownErrorException(
Expand Down
68 changes: 68 additions & 0 deletions src/bidiMapper/modules/webExtension/WebExtensionProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* 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.
*/

/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

license is included twice.

* Copyright 2023 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 {
InvalidWebExtensionException,
type WebExtension,
} from '../../../protocol/protocol.js';

/**
* Responsible for handling the `storage` module.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Responsible for handling the `storage` module.
* Responsible for handling the `webExtension` module.

or delete the comment

*/
export class WebExtensionProcessor {
readonly #browserCdpClient: CdpClient;

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

async install(
params: WebExtension.InstallParameters,
): Promise<WebExtension.InstallResult> {
try {
const response = await this.#browserCdpClient.sendCommand(
'Extensions.loadUnpacked',
{
path: (params.extensionData as WebExtension.ExtensionArchivePath)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for other types, we should throw an Unsupported exception as per spec https://w3c.github.io/webdriver-bidi/#expand-a-web-extension-data-spec

you can use a switch statement to cover all possible types of extensionData.

.path,
},
);
return {
extension: response.id,
};
} catch (err: any) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (err: any) {
} catch (err) {

instead cast the (error as Error) for type-safety.

throw new InvalidWebExtensionException(err.toString());
}
}
}
11 changes: 11 additions & 0 deletions src/bidiTab/BidiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
Script,
Session,
Storage,
WebExtension,
} from '../protocol/protocol.js';
import * as Parser from '../protocol-parser/protocol-parser.js';

Expand Down Expand Up @@ -231,4 +232,14 @@ export class BidiParser implements BidiCommandParameterParser {
return Parser.Storage.parseSetCookieParams(params);
}
// keep-sorted end

// WebExtenstion module
// keep-sorted start block=yes
parseInstallParams(params: unknown): WebExtension.InstallParameters {
return Parser.WebModule.parseInstallParams(params);
}
parseUninstallParams(params: unknown): WebExtension.UninstallParameters {
return Parser.WebModule.parseUninstallParams(params);
}
// keep-sorted end
}
20 changes: 20 additions & 0 deletions src/protocol-parser/protocol-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,23 @@ export namespace Bluetooth {
) as Protocol.Bluetooth.SimulatePreconnectedPeripheralParameters;
}
}

/** @see https://w3c.github.io/webdriver-bidi/#module-webExtension */
export namespace WebModule {
export function parseInstallParams(
params: unknown,
): Protocol.WebExtension.InstallParameters {
return parseObject(
params,
WebDriverBidi.WebExtension.InstallParametersSchema,
);
}
export function parseUninstallParams(
params: unknown,
): Protocol.WebExtension.UninstallParameters {
return parseObject(
params,
WebDriverBidi.WebExtension.UninstallParametersSchema,
);
}
}
Loading