Skip to content

Commit d178006

Browse files
authored
[dotnet] [bidi] Dedicated json context for all modules (#16652)
1 parent 67fae55 commit d178006

File tree

12 files changed

+280
-258
lines changed

12 files changed

+280
-258
lines changed

dotnet/src/webdriver/BiDi/Broker.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,23 @@ private void ProcessReceivedMessage(byte[]? data)
272272
case "success":
273273
if (id is null) throw new JsonException("The remote end responded with 'success' message type, but missed required 'id' property.");
274274

275-
if (_pendingCommands.TryGetValue(id.Value, out var successCommand))
275+
if (_pendingCommands.TryGetValue(id.Value, out var command))
276276
{
277-
successCommand.TaskCompletionSource.SetResult((EmptyResult)JsonSerializer.Deserialize(ref resultReader, successCommand.JsonResultTypeInfo)!);
278-
_pendingCommands.TryRemove(id.Value, out _);
277+
try
278+
{
279+
var commandResult = JsonSerializer.Deserialize(ref resultReader, command.JsonResultTypeInfo)
280+
?? throw new JsonException("Remote end returned null command result in the 'result' property.");
281+
282+
command.TaskCompletionSource.SetResult((EmptyResult)commandResult);
283+
}
284+
catch (Exception ex)
285+
{
286+
command.TaskCompletionSource.SetException(ex);
287+
}
288+
finally
289+
{
290+
_pendingCommands.TryRemove(id.Value, out _);
291+
}
279292
}
280293
else
281294
{

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
// under the License.
1818
// </copyright>
1919

20-
using OpenQA.Selenium.BiDi.Json;
2120
using System.Text.Json;
21+
using System.Text.Json.Serialization;
2222
using System.Threading.Tasks;
2323

2424
namespace OpenQA.Selenium.BiDi.Browser;
2525

2626
public sealed class BrowserModule : Module
2727
{
28-
private BiDiJsonSerializerContext _jsonContext = null!;
28+
private BrowserJsonSerializerContext _jsonContext = null!;
2929

3030
public async Task<CloseResult> CloseAsync(CloseOptions? options = null)
3131
{
32-
return await Broker.ExecuteCommandAsync(new CloseCommand(), options, _jsonContext.Browser_CloseCommand, _jsonContext.Browser_CloseResult).ConfigureAwait(false);
32+
return await Broker.ExecuteCommandAsync(new CloseCommand(), options, _jsonContext.CloseCommand, _jsonContext.CloseResult).ConfigureAwait(false);
3333
}
3434

3535
public async Task<CreateUserContextResult> CreateUserContextAsync(CreateUserContextOptions? options = null)
@@ -80,6 +80,20 @@ public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorDeniedAsync(SetD
8080

8181
protected override void Initialize(JsonSerializerOptions options)
8282
{
83-
_jsonContext = new BiDiJsonSerializerContext(options);
83+
_jsonContext = new BrowserJsonSerializerContext(options);
8484
}
8585
}
86+
87+
[JsonSerializable(typeof(CloseCommand))]
88+
[JsonSerializable(typeof(CloseResult))]
89+
[JsonSerializable(typeof(CreateUserContextCommand))]
90+
[JsonSerializable(typeof(CreateUserContextResult))]
91+
[JsonSerializable(typeof(GetUserContextsCommand))]
92+
[JsonSerializable(typeof(GetUserContextsResult))]
93+
[JsonSerializable(typeof(RemoveUserContextCommand))]
94+
[JsonSerializable(typeof(RemoveUserContextResult))]
95+
[JsonSerializable(typeof(GetClientWindowsCommand))]
96+
[JsonSerializable(typeof(GetClientWindowsResult))]
97+
[JsonSerializable(typeof(SetDownloadBehaviorCommand))]
98+
[JsonSerializable(typeof(SetDownloadBehaviorResult))]
99+
internal partial class BrowserJsonSerializerContext : JsonSerializerContext;

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
// under the License.
1818
// </copyright>
1919

20-
using OpenQA.Selenium.BiDi.Json;
2120
using System;
2221
using System.Text.Json;
22+
using System.Text.Json.Serialization;
2323
using System.Threading.Tasks;
2424

2525
namespace OpenQA.Selenium.BiDi.BrowsingContext;
2626

2727
public sealed class BrowsingContextModule : Module
2828
{
29-
private BiDiJsonSerializerContext _jsonContext = null!;
29+
private BrowsingContextJsonSerializerContext _jsonContext = null!;
3030

3131
public async Task<CreateResult> CreateAsync(ContextType type, CreateOptions? options = null)
3232
{
@@ -67,7 +67,7 @@ public async Task<CloseResult> CloseAsync(BrowsingContext context, CloseOptions?
6767
{
6868
var @params = new CloseParameters(context, options?.PromptUnload);
6969

70-
return await Broker.ExecuteCommandAsync(new CloseCommand(@params), options, _jsonContext.BrowsingContext_CloseCommand, _jsonContext.BrowsingContext_CloseResult).ConfigureAwait(false);
70+
return await Broker.ExecuteCommandAsync(new CloseCommand(@params), options, _jsonContext.CloseCommand, _jsonContext.CloseResult).ConfigureAwait(false);
7171
}
7272

7373
public async Task<TraverseHistoryResult> TraverseHistoryAsync(BrowsingContext context, int delta, TraverseHistoryOptions? options = null)
@@ -254,6 +254,42 @@ public async Task<Subscription> OnUserPromptClosedAsync(Action<UserPromptClosedE
254254

255255
protected override void Initialize(JsonSerializerOptions options)
256256
{
257-
_jsonContext = new BiDiJsonSerializerContext(options);
257+
_jsonContext = new BrowsingContextJsonSerializerContext(options);
258258
}
259259
}
260+
261+
[JsonSerializable(typeof(ActivateCommand))]
262+
[JsonSerializable(typeof(ActivateResult))]
263+
[JsonSerializable(typeof(CaptureScreenshotCommand))]
264+
[JsonSerializable(typeof(CaptureScreenshotResult))]
265+
[JsonSerializable(typeof(CloseCommand))]
266+
[JsonSerializable(typeof(CloseResult))]
267+
[JsonSerializable(typeof(CreateCommand))]
268+
[JsonSerializable(typeof(CreateResult))]
269+
[JsonSerializable(typeof(GetTreeCommand))]
270+
[JsonSerializable(typeof(GetTreeResult))]
271+
[JsonSerializable(typeof(HandleUserPromptCommand))]
272+
[JsonSerializable(typeof(HandleUserPromptResult))]
273+
[JsonSerializable(typeof(LocateNodesCommand))]
274+
[JsonSerializable(typeof(LocateNodesResult))]
275+
[JsonSerializable(typeof(NavigateCommand))]
276+
[JsonSerializable(typeof(NavigateResult))]
277+
[JsonSerializable(typeof(PrintCommand))]
278+
[JsonSerializable(typeof(PrintResult))]
279+
[JsonSerializable(typeof(ReloadCommand))]
280+
[JsonSerializable(typeof(ReloadResult))]
281+
[JsonSerializable(typeof(SetViewportCommand))]
282+
[JsonSerializable(typeof(SetViewportResult))]
283+
[JsonSerializable(typeof(TraverseHistoryCommand))]
284+
[JsonSerializable(typeof(TraverseHistoryResult))]
285+
286+
[JsonSerializable(typeof(BrowsingContextInfo))]
287+
[JsonSerializable(typeof(DownloadWillBeginEventArgs))]
288+
[JsonSerializable(typeof(DownloadEndEventArgs))]
289+
[JsonSerializable(typeof(DownloadCanceledEventArgs))]
290+
[JsonSerializable(typeof(DownloadCompleteEventArgs))]
291+
[JsonSerializable(typeof(HistoryUpdatedEventArgs))]
292+
[JsonSerializable(typeof(NavigationInfo))]
293+
[JsonSerializable(typeof(UserPromptOpenedEventArgs))]
294+
[JsonSerializable(typeof(UserPromptClosedEventArgs))]
295+
internal partial class BrowsingContextJsonSerializerContext : JsonSerializerContext;

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
// under the License.
1818
// </copyright>
1919

20-
using OpenQA.Selenium.BiDi.Json;
2120
using System.Text.Json;
21+
using System.Text.Json.Serialization;
2222
using System.Threading.Tasks;
2323

2424
namespace OpenQA.Selenium.BiDi.Emulation;
2525

2626
public sealed class EmulationModule : Module
2727
{
28-
private BiDiJsonSerializerContext _jsonContext = null!;
28+
private EmulationJsonSerializerContext _jsonContext = null!;
2929

3030
public async Task<SetTimezoneOverrideResult> SetTimezoneOverrideAsync(string? timezone, SetTimezoneOverrideOptions? options = null)
3131
{
@@ -94,6 +94,22 @@ public async Task<SetGeolocationOverrideResult> SetGeolocationPositionErrorOverr
9494

9595
protected override void Initialize(JsonSerializerOptions options)
9696
{
97-
_jsonContext = new BiDiJsonSerializerContext(options);
97+
_jsonContext = new EmulationJsonSerializerContext(options);
9898
}
9999
}
100+
101+
[JsonSerializable(typeof(SetTimezoneOverrideCommand))]
102+
[JsonSerializable(typeof(SetTimezoneOverrideResult))]
103+
[JsonSerializable(typeof(SetUserAgentOverrideCommand))]
104+
[JsonSerializable(typeof(SetUserAgentOverrideResult))]
105+
[JsonSerializable(typeof(SetLocaleOverrideCommand))]
106+
[JsonSerializable(typeof(SetLocaleOverrideResult))]
107+
[JsonSerializable(typeof(SetForcedColorsModeThemeOverrideCommand))]
108+
[JsonSerializable(typeof(SetForcedColorsModeThemeOverrideResult))]
109+
[JsonSerializable(typeof(SetScriptingEnabledCommand))]
110+
[JsonSerializable(typeof(SetScriptingEnabledResult))]
111+
[JsonSerializable(typeof(SetScreenOrientationOverrideCommand))]
112+
[JsonSerializable(typeof(SetScreenOrientationOverrideResult))]
113+
[JsonSerializable(typeof(SetGeolocationOverrideCommand))]
114+
[JsonSerializable(typeof(SetGeolocationOverrideResult))]
115+
internal partial class EmulationJsonSerializerContext : JsonSerializerContext;

dotnet/src/webdriver/BiDi/Input/InputModule.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
// under the License.
1818
// </copyright>
1919

20-
using OpenQA.Selenium.BiDi.Json;
2120
using System.Collections.Generic;
2221
using System.Text.Json;
22+
using System.Text.Json.Serialization;
2323
using System.Threading.Tasks;
2424

2525
namespace OpenQA.Selenium.BiDi.Input;
2626

2727
public sealed class InputModule : Module
2828
{
29-
private BiDiJsonSerializerContext _jsonContext = null!;
29+
private InputJsonSerializerContext _jsonContext = null!;
3030

3131
public async Task<PerformActionsResult> PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable<SourceActions> actions, PerformActionsOptions? options = null)
3232
{
@@ -51,6 +51,18 @@ public async Task<SetFilesResult> SetFilesAsync(BrowsingContext.BrowsingContext
5151

5252
protected override void Initialize(JsonSerializerOptions options)
5353
{
54-
_jsonContext = new BiDiJsonSerializerContext(options);
54+
_jsonContext = new InputJsonSerializerContext(options);
5555
}
5656
}
57+
58+
[JsonSerializable(typeof(PerformActionsCommand))]
59+
[JsonSerializable(typeof(PerformActionsResult))]
60+
[JsonSerializable(typeof(ReleaseActionsCommand))]
61+
[JsonSerializable(typeof(ReleaseActionsResult))]
62+
[JsonSerializable(typeof(SetFilesCommand))]
63+
[JsonSerializable(typeof(SetFilesResult))]
64+
[JsonSerializable(typeof(IEnumerable<IPointerSourceAction>))]
65+
[JsonSerializable(typeof(IEnumerable<IKeySourceAction>))]
66+
[JsonSerializable(typeof(IEnumerable<INoneSourceAction>))]
67+
[JsonSerializable(typeof(IEnumerable<IWheelSourceAction>))]
68+
internal partial class InputJsonSerializerContext : JsonSerializerContext;

0 commit comments

Comments
 (0)