[dotnet] [bidi] Statically declare commands#17330
[dotnet] [bidi] Statically declare commands#17330nvborisenko merged 12 commits intoSeleniumHQ:trunkfrom
Conversation
Review Summary by Qodo[dotnet] [bidi] Refactor to statically declare commands using CommandDescriptor
WalkthroughsDescription• Replace concrete command classes with static CommandDescriptor instances for cleaner command definition • Refactor ExecuteCommandAsync to ExecuteAsync method accepting CommandDescriptor and parameters • Update JSON serialization to use parameter types instead of command wrapper classes • Change module visibility from public to internal for better encapsulation • Simplify command execution by removing intermediate command object allocation File Changes1. dotnet/src/webdriver/BiDi/Command.cs
|
Code Review by Qodo
|
There was a problem hiding this comment.
Pull request overview
Refactors the .NET BiDi implementation to define commands statically (via CommandDescriptor) and execute them through a single Broker.ExecuteAsync pathway, reducing per-call allocations and simplifying JSON source-generation requirements.
Changes:
- Introduces
CommandDescriptor<TParameters, TResult>and updates command execution to serialize{ id, method, params }directly inBroker. - Replaces numerous concrete
*Commandclasses with staticCommandDescriptorfields + parameter records across modules. - Updates JSON source-generation attributes to reference parameter/result types and narrows visibility of several module implementations.
Reviewed changes
Copilot reviewed 82 out of 82 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| dotnet/test/webdriver/BiDi/Session/SessionTests.cs | Updates custom module test to use CommandDescriptor + ExecuteAsync. |
| dotnet/src/webdriver/BiDi/Broker.cs | Implements ExecuteAsync and manual request JSON serialization using descriptors. |
| dotnet/src/webdriver/BiDi/Command.cs | Replaces Command hierarchy with CommandDescriptor; keeps Parameters/EmptyResult. |
| dotnet/src/webdriver/BiDi/Module.cs | Switches base module execution helper to ExecuteAsync(descriptor, params, ...). |
| dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs | Converts browser commands to static descriptors; changes module implementation visibility. |
| dotnet/src/webdriver/BiDi/Browser/Close.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Browser/CreateUserContext.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Browser/GetClientWindows.cs | Removes concrete command class, leaving options/result types. |
| dotnet/src/webdriver/BiDi/Browser/GetUserContexts.cs | Removes concrete command class, leaving options/result types. |
| dotnet/src/webdriver/BiDi/Browser/RemoveUserContext.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Browser/SetDownloadBehavior.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs | Converts browsingContext commands to static descriptors; updates subscribe contexts. |
| dotnet/src/webdriver/BiDi/BrowsingContext/Activate.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/CaptureScreenshot.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/Close.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/Create.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/GetTree.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/HandleUserPrompt.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/LocateNodes.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/Navigate.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/Print.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/Reload.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/SetViewport.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/TraverseHistory.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextInputModule.cs | Narrows wrapper module visibility while keeping interface-based surface. |
| dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs | Narrows wrapper module visibility while keeping interface-based surface. |
| dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextNetworkModule.cs | Narrows wrapper module visibility while keeping interface-based surface. |
| dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextScriptModule.cs | Narrows wrapper module visibility while keeping interface-based surface. |
| dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextStorageModule.cs | Narrows wrapper module visibility while keeping interface-based surface. |
| dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs | Converts emulation commands to static descriptors; updates source-gen types. |
| dotnet/src/webdriver/BiDi/Emulation/SetForcedColorsModeThemeOverride.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Emulation/SetGeolocationOverride.cs | Removes concrete command class, leaving polymorphic params/result types. |
| dotnet/src/webdriver/BiDi/Emulation/SetLocaleOverride.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Emulation/SetNetworkConditions.cs | Removes concrete command class, leaving polymorphic params/result types. |
| dotnet/src/webdriver/BiDi/Emulation/SetScreenOrientationOverride.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Emulation/SetScreenSettingsOverride.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Emulation/SetScriptingEnabled.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Emulation/SetScrollbarTypeOverride.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Emulation/SetTimezoneOverride.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Emulation/SetTouchOverride.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Emulation/SetUserAgentOverride.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Input/InputModule.cs | Converts input commands to static descriptors; updates subscribe contexts. |
| dotnet/src/webdriver/BiDi/Input/PerformActions.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Input/ReleaseActions.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Input/SetFiles.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Log/LogModule.cs | Updates subscription serialization references and narrows module visibility. |
| dotnet/src/webdriver/BiDi/Network/NetworkModule.cs | Converts network commands to static descriptors; updates subscribe contexts. |
| dotnet/src/webdriver/BiDi/Network/AddDataCollector.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Network/AddIntercept.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Network/ContinueRequest.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Network/ContinueResponse.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Network/ContinueWithAuth.cs | Removes concrete command class, leaving polymorphic params/result types. |
| dotnet/src/webdriver/BiDi/Network/FailRequest.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Network/GetData.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Network/ProvideResponse.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Network/RemoveDataCollector.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Network/RemoveIntercept.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Network/SetCacheBehavior.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Network/SetExtraHeaders.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Permissions/PermissionsModule.cs | Converts permissions command to a static descriptor; updates source-gen types. |
| dotnet/src/webdriver/BiDi/Permissions/SetPermission.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Script/ScriptModule.cs | Converts script commands to static descriptors; updates subscribe contexts and source-gen. |
| dotnet/src/webdriver/BiDi/Script/AddPreloadScript.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Script/CallFunction.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Script/Disown.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Script/Evaluate.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Script/GetRealms.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Script/RemovePreloadScript.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Session/SessionModule.cs | Converts session commands to static descriptors; updates source-gen types. |
| dotnet/src/webdriver/BiDi/Session/End.cs | Removes concrete command class, leaving options/result types. |
| dotnet/src/webdriver/BiDi/Session/New.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Session/Status.cs | Removes concrete command class, leaving options/result types. |
| dotnet/src/webdriver/BiDi/Session/Subscribe.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Session/Unsubscribe.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Speculation/SpeculationModule.cs | Updates subscription serialization references and narrows module visibility. |
| dotnet/src/webdriver/BiDi/Storage/StorageModule.cs | Converts storage commands to static descriptors; updates source-gen types. |
| dotnet/src/webdriver/BiDi/Storage/DeleteCookies.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Storage/GetCookies.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/Storage/SetCookie.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/WebExtension/WebExtensionModule.cs | Converts webExtension commands to static descriptors; updates source-gen types. |
| dotnet/src/webdriver/BiDi/WebExtension/Install.cs | Removes concrete command class, leaving params/result types. |
| dotnet/src/webdriver/BiDi/WebExtension/Uninstall.cs | Removes concrete command class, leaving params/result types. |
Commands declaration without allocation.
🔗 Related Issues
Contributes to #16095
💥 What does this PR do?
This pull request refactors the BiDi (Bi-Directional) WebDriver command execution pipeline to simplify command handling, improve type safety, and streamline serialization logic. The main changes include replacing the generic
ExecuteCommandAsyncmethod with a more type-safeExecuteAsyncmethod, centralizing command definitions, and cleaning up command class structure. Additionally, the handling of pending commands and serialization is improved for reliability and maintainability.Refactoring of command execution and handling:
ExecuteCommandAsyncmethod with a more type-safeExecuteAsyncmethod inBroker.cs, which now uses aCommand<TParameters, TResult>descriptor and parameters, improving clarity and reducing boilerplate. (dotnet/src/webdriver/BiDi/Broker.cs, [1] [2] [3] [4]TryRemoveinstead ofTryGetValue, ensuring commands are removed as soon as they are processed, preventing potential memory leaks or double-removal. (dotnet/src/webdriver/BiDi/Broker.cs, [1] [2] [3]Centralization and simplification of command definitions:
CloseCommand,CreateUserContextCommand) and replaced them with static command descriptors inBrowserModule.cs, reducing duplication and centralizing command logic. (dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs, [1];dotnet/src/webdriver/BiDi/Browser/Close.cs, [2];dotnet/src/webdriver/BiDi/Browser/CreateUserContext.cs, [3];dotnet/src/webdriver/BiDi/Browser/GetUserContexts.cs, [4];dotnet/src/webdriver/BiDi/Browser/GetClientWindows.cs, [5];dotnet/src/webdriver/BiDi/Browser/RemoveUserContext.cs, [6];dotnet/src/webdriver/BiDi/Browser/SetDownloadBehavior.cs, [7];dotnet/src/webdriver/BiDi/BrowsingContext/Activate.cs, [8]Module and visibility adjustments:
public sealedtointernal sealedto restrict their visibility and clarify their intended usage within the assembly. (dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs, [1];dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextInputModule.cs, [2];dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextLogModule.cs, [3]These changes collectively improve maintainability, type safety, and the overall structure of the BiDi WebDriver implementation.
🔄 Types of changes