Skip to content

Commit 9706f56

Browse files
authored
[dotnet] [bidi] Provide type info immediately when serializing (#16397)
1 parent 90a1030 commit 9706f56

File tree

10 files changed

+66
-72
lines changed

10 files changed

+66
-72
lines changed

dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,52 @@ public sealed class BrowserModule : Module
2626
{
2727
public async Task<EmptyResult> CloseAsync(CloseOptions? options = null)
2828
{
29-
return await Broker.ExecuteCommandAsync<CloseCommand, EmptyResult>(new CloseCommand(), options, JsonContext).ConfigureAwait(false);
29+
return await Broker.ExecuteCommandAsync(new CloseCommand(), options, JsonContext.Browser_CloseCommand, JsonContext.EmptyResult).ConfigureAwait(false);
3030
}
3131

3232
public async Task<UserContextInfo> CreateUserContextAsync(CreateUserContextOptions? options = null)
3333
{
3434
var @params = new CreateUserContextParameters(options?.AcceptInsecureCerts, options?.Proxy, options?.UnhandledPromptBehavior);
3535

36-
return await Broker.ExecuteCommandAsync<CreateUserContextCommand, UserContextInfo>(new CreateUserContextCommand(@params), options, JsonContext).ConfigureAwait(false);
36+
return await Broker.ExecuteCommandAsync(new CreateUserContextCommand(@params), options, JsonContext.CreateUserContextCommand, JsonContext.UserContextInfo).ConfigureAwait(false);
3737
}
3838

3939
public async Task<GetUserContextsResult> GetUserContextsAsync(GetUserContextsOptions? options = null)
4040
{
41-
return await Broker.ExecuteCommandAsync<GetUserContextsCommand, GetUserContextsResult>(new GetUserContextsCommand(), options, JsonContext).ConfigureAwait(false);
41+
return await Broker.ExecuteCommandAsync(new GetUserContextsCommand(), options, JsonContext.GetUserContextsCommand, JsonContext.GetUserContextsResult).ConfigureAwait(false);
4242
}
4343

4444
public async Task<EmptyResult> RemoveUserContextAsync(UserContext userContext, RemoveUserContextOptions? options = null)
4545
{
4646
var @params = new RemoveUserContextParameters(userContext);
4747

48-
return await Broker.ExecuteCommandAsync<RemoveUserContextCommand, EmptyResult>(new RemoveUserContextCommand(@params), options, JsonContext).ConfigureAwait(false);
48+
return await Broker.ExecuteCommandAsync(new RemoveUserContextCommand(@params), options, JsonContext.RemoveUserContextCommand, JsonContext.EmptyResult).ConfigureAwait(false);
4949
}
5050

5151
public async Task<GetClientWindowsResult> GetClientWindowsAsync(GetClientWindowsOptions? options = null)
5252
{
53-
return await Broker.ExecuteCommandAsync<GetClientWindowsCommand, GetClientWindowsResult>(new(), options, JsonContext).ConfigureAwait(false);
53+
return await Broker.ExecuteCommandAsync(new(), options, JsonContext.GetClientWindowsCommand, JsonContext.GetClientWindowsResult
54+
).ConfigureAwait(false);
5455
}
5556

5657
public async Task<EmptyResult> SetDownloadBehaviorAllowedAsync(string destinationFolder, SetDownloadBehaviorOptions? options = null)
5758
{
5859
var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorAllowed(destinationFolder), options?.UserContexts);
5960

60-
return await Broker.ExecuteCommandAsync<SetDownloadBehaviorCommand, EmptyResult>(new SetDownloadBehaviorCommand(@params), options, JsonContext).ConfigureAwait(false);
61+
return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, JsonContext.SetDownloadBehaviorCommand, JsonContext.EmptyResult).ConfigureAwait(false);
6162
}
6263

6364
public async Task<EmptyResult> SetDownloadBehaviorAllowedAsync(SetDownloadBehaviorOptions? options = null)
6465
{
6566
var @params = new SetDownloadBehaviorParameters(null, options?.UserContexts);
6667

67-
return await Broker.ExecuteCommandAsync<SetDownloadBehaviorCommand, EmptyResult>(new SetDownloadBehaviorCommand(@params), options, JsonContext).ConfigureAwait(false);
68+
return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, JsonContext.SetDownloadBehaviorCommand, JsonContext.EmptyResult).ConfigureAwait(false);
6869
}
6970

7071
public async Task<EmptyResult> SetDownloadBehaviorDeniedAsync(SetDownloadBehaviorOptions? options = null)
7172
{
7273
var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorDenied(), options?.UserContexts);
7374

74-
return await Broker.ExecuteCommandAsync<SetDownloadBehaviorCommand, EmptyResult>(new SetDownloadBehaviorCommand(@params), options, JsonContext).ConfigureAwait(false);
75+
return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, JsonContext.SetDownloadBehaviorCommand, JsonContext.EmptyResult).ConfigureAwait(false);
7576
}
7677
}

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task<BrowsingContext> CreateAsync(ContextType type, CreateOptions?
2929
{
3030
var @params = new CreateParameters(type, options?.ReferenceContext, options?.Background, options?.UserContext);
3131

32-
var createResult = await Broker.ExecuteCommandAsync<CreateCommand, CreateResult>(new CreateCommand(@params), options, JsonContext).ConfigureAwait(false);
32+
var createResult = await Broker.ExecuteCommandAsync(new CreateCommand(@params), options, JsonContext.CreateCommand, JsonContext.CreateResult).ConfigureAwait(false);
3333

3434
return createResult.Context;
3535
}
@@ -38,77 +38,77 @@ public async Task<NavigateResult> NavigateAsync(BrowsingContext context, string
3838
{
3939
var @params = new NavigateParameters(context, url, options?.Wait);
4040

41-
return await Broker.ExecuteCommandAsync<NavigateCommand, NavigateResult>(new NavigateCommand(@params), options, JsonContext).ConfigureAwait(false);
41+
return await Broker.ExecuteCommandAsync(new NavigateCommand(@params), options, JsonContext.NavigateCommand, JsonContext.NavigateResult).ConfigureAwait(false);
4242
}
4343

4444
public async Task<EmptyResult> ActivateAsync(BrowsingContext context, ActivateOptions? options = null)
4545
{
4646
var @params = new ActivateParameters(context);
4747

48-
return await Broker.ExecuteCommandAsync<ActivateCommand, EmptyResult>(new ActivateCommand(@params), options, JsonContext).ConfigureAwait(false);
48+
return await Broker.ExecuteCommandAsync(new ActivateCommand(@params), options, JsonContext.ActivateCommand, JsonContext.EmptyResult).ConfigureAwait(false);
4949
}
5050

5151
public async Task<LocateNodesResult> LocateNodesAsync(BrowsingContext context, Locator locator, LocateNodesOptions? options = null)
5252
{
5353
var @params = new LocateNodesParameters(context, locator, options?.MaxNodeCount, options?.SerializationOptions, options?.StartNodes);
5454

55-
return await Broker.ExecuteCommandAsync<LocateNodesCommand, LocateNodesResult>(new LocateNodesCommand(@params), options, JsonContext).ConfigureAwait(false);
55+
return await Broker.ExecuteCommandAsync(new LocateNodesCommand(@params), options, JsonContext.LocateNodesCommand, JsonContext.LocateNodesResult).ConfigureAwait(false);
5656
}
5757

5858
public async Task<CaptureScreenshotResult> CaptureScreenshotAsync(BrowsingContext context, CaptureScreenshotOptions? options = null)
5959
{
6060
var @params = new CaptureScreenshotParameters(context, options?.Origin, options?.Format, options?.Clip);
6161

62-
return await Broker.ExecuteCommandAsync<CaptureScreenshotCommand, CaptureScreenshotResult>(new CaptureScreenshotCommand(@params), options, JsonContext).ConfigureAwait(false);
62+
return await Broker.ExecuteCommandAsync(new CaptureScreenshotCommand(@params), options, JsonContext.CaptureScreenshotCommand, JsonContext.CaptureScreenshotResult).ConfigureAwait(false);
6363
}
6464

6565
public async Task<EmptyResult> CloseAsync(BrowsingContext context, CloseOptions? options = null)
6666
{
6767
var @params = new CloseParameters(context, options?.PromptUnload);
6868

69-
return await Broker.ExecuteCommandAsync<CloseCommand, EmptyResult>(new CloseCommand(@params), options, JsonContext).ConfigureAwait(false);
69+
return await Broker.ExecuteCommandAsync(new CloseCommand(@params), options, JsonContext.BrowsingContext_CloseCommand, JsonContext.EmptyResult).ConfigureAwait(false);
7070
}
7171

7272
public async Task<TraverseHistoryResult> TraverseHistoryAsync(BrowsingContext context, int delta, TraverseHistoryOptions? options = null)
7373
{
7474
var @params = new TraverseHistoryParameters(context, delta);
7575

76-
return await Broker.ExecuteCommandAsync<TraverseHistoryCommand, TraverseHistoryResult>(new TraverseHistoryCommand(@params), options, JsonContext).ConfigureAwait(false);
76+
return await Broker.ExecuteCommandAsync(new TraverseHistoryCommand(@params), options, JsonContext.TraverseHistoryCommand, JsonContext.TraverseHistoryResult).ConfigureAwait(false);
7777
}
7878

7979
public async Task<NavigateResult> ReloadAsync(BrowsingContext context, ReloadOptions? options = null)
8080
{
8181
var @params = new ReloadParameters(context, options?.IgnoreCache, options?.Wait);
8282

83-
return await Broker.ExecuteCommandAsync<ReloadCommand, NavigateResult>(new ReloadCommand(@params), options, JsonContext).ConfigureAwait(false);
83+
return await Broker.ExecuteCommandAsync(new ReloadCommand(@params), options, JsonContext.ReloadCommand, JsonContext.NavigateResult).ConfigureAwait(false);
8484
}
8585

8686
public async Task<EmptyResult> SetViewportAsync(BrowsingContext context, SetViewportOptions? options = null)
8787
{
8888
var @params = new SetViewportParameters(context, options?.Viewport, options?.DevicePixelRatio);
8989

90-
return await Broker.ExecuteCommandAsync<SetViewportCommand, EmptyResult>(new SetViewportCommand(@params), options, JsonContext).ConfigureAwait(false);
90+
return await Broker.ExecuteCommandAsync(new SetViewportCommand(@params), options, JsonContext.SetViewportCommand, JsonContext.EmptyResult).ConfigureAwait(false);
9191
}
9292

9393
public async Task<GetTreeResult> GetTreeAsync(GetTreeOptions? options = null)
9494
{
9595
var @params = new GetTreeParameters(options?.MaxDepth, options?.Root);
9696

97-
return await Broker.ExecuteCommandAsync<GetTreeCommand, GetTreeResult>(new GetTreeCommand(@params), options, JsonContext).ConfigureAwait(false);
97+
return await Broker.ExecuteCommandAsync(new GetTreeCommand(@params), options, JsonContext.GetTreeCommand, JsonContext.GetTreeResult).ConfigureAwait(false);
9898
}
9999

100100
public async Task<PrintResult> PrintAsync(BrowsingContext context, PrintOptions? options = null)
101101
{
102102
var @params = new PrintParameters(context, options?.Background, options?.Margin, options?.Orientation, options?.Page, options?.PageRanges, options?.Scale, options?.ShrinkToFit);
103103

104-
return await Broker.ExecuteCommandAsync<PrintCommand, PrintResult>(new PrintCommand(@params), options, JsonContext).ConfigureAwait(false);
104+
return await Broker.ExecuteCommandAsync(new PrintCommand(@params), options, JsonContext.PrintCommand, JsonContext.PrintResult).ConfigureAwait(false);
105105
}
106106

107107
public async Task<EmptyResult> HandleUserPromptAsync(BrowsingContext context, HandleUserPromptOptions? options = null)
108108
{
109109
var @params = new HandleUserPromptParameters(context, options?.Accept, options?.UserText);
110110

111-
return await Broker.ExecuteCommandAsync<HandleUserPromptCommand, EmptyResult>(new HandleUserPromptCommand(@params), options, JsonContext).ConfigureAwait(false);
111+
return await Broker.ExecuteCommandAsync(new HandleUserPromptCommand(@params), options, JsonContext.HandleUserPromptCommand, JsonContext.EmptyResult).ConfigureAwait(false);
112112
}
113113

114114
public async Task<Subscription> OnNavigationStartedAsync(Func<NavigationInfo, Task> handler, BrowsingContextsSubscriptionOptions? options = null)

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Linq;
2626
using System.Text.Json;
2727
using System.Text.Json.Serialization;
28+
using System.Text.Json.Serialization.Metadata;
2829
using System.Threading;
2930
using System.Threading.Tasks;
3031

@@ -138,17 +139,9 @@ private async Task ProcessEventsAwaiterAsync()
138139
}
139140
}
140141

141-
public async Task<TResult> ExecuteCommandAsync<TCommand, TResult>(TCommand command, CommandOptions? options, JsonSerializerContext jsonContext)
142+
public async Task<TResult> ExecuteCommandAsync<TCommand, TResult>(TCommand command, CommandOptions? options, JsonTypeInfo<TCommand> jsonCommandTypeInfo, JsonTypeInfo<TResult> jsonResultTypeInfo)
142143
where TCommand : Command
143144
where TResult : EmptyResult
144-
{
145-
var result = await ExecuteCommandCoreAsync(command, options, jsonContext).ConfigureAwait(false);
146-
147-
return (TResult)result;
148-
}
149-
150-
private async Task<EmptyResult> ExecuteCommandCoreAsync<TCommand>(TCommand command, CommandOptions? options, JsonSerializerContext jsonContext)
151-
where TCommand : Command
152145
{
153146
command.Id = Interlocked.Increment(ref _currentCommandId);
154147
var tcs = new TaskCompletionSource<JsonElement>(TaskCreationOptions.RunContinuationsAsynchronously);
@@ -157,11 +150,11 @@ private async Task<EmptyResult> ExecuteCommandCoreAsync<TCommand>(TCommand comma
157150
cts.Token.Register(() => tcs.TrySetCanceled(cts.Token));
158151
var commandInfo = new CommandInfo(command.Id, command.ResultType, tcs);
159152
_pendingCommands[command.Id] = commandInfo;
160-
var data = JsonSerializer.SerializeToUtf8Bytes(command, typeof(TCommand), jsonContext);
153+
var data = JsonSerializer.SerializeToUtf8Bytes(command, jsonCommandTypeInfo);
161154

162155
await _transport.SendAsync(data, cts.Token).ConfigureAwait(false);
163156
var resultJson = await tcs.Task.ConfigureAwait(false);
164-
return (EmptyResult)JsonSerializer.Deserialize(resultJson, commandInfo.ResultType, jsonContext)!;
157+
return JsonSerializer.Deserialize(resultJson, jsonResultTypeInfo)!;
165158
}
166159

167160
public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Action<TEventArgs> action, SubscriptionOptions? options, JsonSerializerContext jsonContext)

dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,42 @@ public async Task<EmptyResult> SetTimezoneOverrideAsync(string? timezone, SetTim
2828
{
2929
var @params = new SetTimezoneOverrideParameters(timezone, options?.Contexts, options?.UserContexts);
3030

31-
return await Broker.ExecuteCommandAsync<SetTimezoneOverrideCommand, EmptyResult>(new SetTimezoneOverrideCommand(@params), options, JsonContext).ConfigureAwait(false);
31+
return await Broker.ExecuteCommandAsync(new SetTimezoneOverrideCommand(@params), options, JsonContext.SetTimezoneOverrideCommand, JsonContext.EmptyResult).ConfigureAwait(false);
3232
}
3333

3434
public async Task<EmptyResult> SetUserAgentOverrideAsync(string? userAgent, SetUserAgentOverrideOptions? options = null)
3535
{
3636
var @params = new SetUserAgentOverrideParameters(userAgent, options?.Contexts, options?.UserContexts);
3737

38-
return await Broker.ExecuteCommandAsync<SetUserAgentOverrideCommand, EmptyResult>(new SetUserAgentOverrideCommand(@params), options, JsonContext).ConfigureAwait(false);
38+
return await Broker.ExecuteCommandAsync(new SetUserAgentOverrideCommand(@params), options, JsonContext.SetUserAgentOverrideCommand, JsonContext.EmptyResult).ConfigureAwait(false);
3939
}
4040

4141
public async Task<EmptyResult> SetLocaleOverrideAsync(string? locale, SetLocaleOverrideOptions? options = null)
4242
{
4343
var @params = new SetLocaleOverrideParameters(locale, options?.Contexts, options?.UserContexts);
4444

45-
return await Broker.ExecuteCommandAsync<SetLocaleOverrideCommand, EmptyResult>(new SetLocaleOverrideCommand(@params), options, JsonContext).ConfigureAwait(false);
45+
return await Broker.ExecuteCommandAsync(new SetLocaleOverrideCommand(@params), options, JsonContext.SetLocaleOverrideCommand, JsonContext.EmptyResult).ConfigureAwait(false);
4646
}
4747

4848
public async Task<EmptyResult> SetForcedColorsModeThemeOverrideAsync(ForcedColorsModeTheme? theme, SetForcedColorsModeThemeOverrideOptions? options = null)
4949
{
5050
var @params = new SetForcedColorsModeThemeOverrideParameters(theme, options?.Contexts, options?.UserContexts);
5151

52-
return await Broker.ExecuteCommandAsync<SetForcedColorsModeThemeOverrideCommand, EmptyResult>(new SetForcedColorsModeThemeOverrideCommand(@params), options, JsonContext).ConfigureAwait(false);
52+
return await Broker.ExecuteCommandAsync(new SetForcedColorsModeThemeOverrideCommand(@params), options, JsonContext.SetForcedColorsModeThemeOverrideCommand, JsonContext.EmptyResult).ConfigureAwait(false);
5353
}
5454

5555
public async Task<EmptyResult> SetScriptingEnabledAsync(bool? enabled, SetScriptingEnabledOptions? options = null)
5656
{
5757
var @params = new SetScriptingEnabledParameters(enabled, options?.Contexts, options?.UserContexts);
5858

59-
return await Broker.ExecuteCommandAsync<SetScriptingEnabledCommand, EmptyResult>(new SetScriptingEnabledCommand(@params), options, JsonContext).ConfigureAwait(false);
59+
return await Broker.ExecuteCommandAsync(new SetScriptingEnabledCommand(@params), options, JsonContext.SetScriptingEnabledCommand, JsonContext.EmptyResult).ConfigureAwait(false);
6060
}
6161

6262
public async Task<EmptyResult> SetScreenOrientationOverrideAsync(ScreenOrientation? screenOrientation, SetScreenOrientationOverrideOptions? options = null)
6363
{
6464
var @params = new SetScreenOrientationOverrideParameters(screenOrientation, options?.Contexts, options?.UserContexts);
6565

66-
return await Broker.ExecuteCommandAsync<SetScreenOrientationOverrideCommand, EmptyResult>(new SetScreenOrientationOverrideCommand(@params), options, JsonContext).ConfigureAwait(false);
66+
return await Broker.ExecuteCommandAsync(new SetScreenOrientationOverrideCommand(@params), options, JsonContext.SetScreenOrientationOverrideCommand, JsonContext.EmptyResult).ConfigureAwait(false);
6767
}
6868

6969
public async Task<EmptyResult> SetGeolocationCoordinatesOverrideAsync(double latitude, double longitude, SetGeolocationCoordinatesOverrideOptions? options = null)
@@ -72,20 +72,20 @@ public async Task<EmptyResult> SetGeolocationCoordinatesOverrideAsync(double lat
7272

7373
var @params = new SetGeolocationOverrideCoordinatesParameters(coordinates, options?.Contexts, options?.UserContexts);
7474

75-
return await Broker.ExecuteCommandAsync<SetGeolocationOverrideCommand, EmptyResult>(new SetGeolocationOverrideCommand(@params), options, JsonContext).ConfigureAwait(false);
75+
return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, JsonContext.SetGeolocationOverrideCommand, JsonContext.EmptyResult).ConfigureAwait(false);
7676
}
7777

7878
public async Task<EmptyResult> SetGeolocationCoordinatesOverrideAsync(SetGeolocationOverrideOptions? options = null)
7979
{
8080
var @params = new SetGeolocationOverrideCoordinatesParameters(null, options?.Contexts, options?.UserContexts);
8181

82-
return await Broker.ExecuteCommandAsync<SetGeolocationOverrideCommand, EmptyResult>(new SetGeolocationOverrideCommand(@params), options, JsonContext).ConfigureAwait(false);
82+
return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, JsonContext.SetGeolocationOverrideCommand, JsonContext.EmptyResult).ConfigureAwait(false);
8383
}
8484

8585
public async Task<EmptyResult> SetGeolocationPositionErrorOverrideAsync(SetGeolocationPositionErrorOverrideOptions? options = null)
8686
{
8787
var @params = new SetGeolocationOverridePositionErrorParameters(new GeolocationPositionError(), options?.Contexts, options?.UserContexts);
8888

89-
return await Broker.ExecuteCommandAsync<SetGeolocationOverrideCommand, EmptyResult>(new SetGeolocationOverrideCommand(@params), options, JsonContext).ConfigureAwait(false);
89+
return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, JsonContext.SetGeolocationOverrideCommand, JsonContext.EmptyResult).ConfigureAwait(false);
9090
}
9191
}

0 commit comments

Comments
 (0)