Skip to content

Commit 269623e

Browse files
authored
[dotnet] [bidi] JsonSerializerContext instance per module (#16649)
1 parent efcb5e7 commit 269623e

File tree

15 files changed

+209
-207
lines changed

15 files changed

+209
-207
lines changed

dotnet/src/webdriver/BiDi/BiDi.cs

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

20-
using OpenQA.Selenium.BiDi.Json;
2120
using OpenQA.Selenium.BiDi.Json.Converters;
2221
using System;
22+
using System.Collections.Concurrent;
2323
using System.Text.Json;
2424
using System.Text.Json.Serialization;
2525
using System.Threading;
@@ -29,56 +29,24 @@ namespace OpenQA.Selenium.BiDi;
2929

3030
public sealed class BiDi : IAsyncDisposable
3131
{
32-
internal Broker Broker { get; }
33-
internal JsonSerializerOptions JsonOptions { get; }
34-
private readonly BiDiJsonSerializerContext _jsonContext;
35-
36-
public JsonSerializerOptions DefaultBiDiOptions()
37-
{
38-
return new JsonSerializerOptions
39-
{
40-
PropertyNameCaseInsensitive = true,
41-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
42-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
43-
44-
// BiDi returns special numbers such as "NaN" as strings
45-
// Additionally, -0 is returned as a string "-0"
46-
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals | JsonNumberHandling.AllowReadingFromString,
47-
Converters =
48-
{
49-
new BrowsingContextConverter(this),
50-
new BrowserUserContextConverter(this),
51-
new CollectorConverter(this),
52-
new InterceptConverter(this),
53-
new HandleConverter(this),
54-
new InternalIdConverter(this),
55-
new PreloadScriptConverter(this),
56-
new RealmConverter(this),
57-
new DateTimeOffsetConverter(),
58-
new WebExtensionConverter(this),
59-
}
60-
};
61-
}
32+
private readonly ConcurrentDictionary<Type, Module> _modules = new();
6233

6334
private BiDi(string url)
6435
{
6536
var uri = new Uri(url);
6637

67-
JsonOptions = DefaultBiDiOptions();
68-
69-
_jsonContext = new BiDiJsonSerializerContext(JsonOptions);
70-
71-
Broker = new Broker(this, uri, JsonOptions);
72-
SessionModule = Module.Create<Session.SessionModule>(this, JsonOptions, _jsonContext);
73-
BrowsingContext = Module.Create<BrowsingContext.BrowsingContextModule>(this, JsonOptions, _jsonContext);
74-
Browser = Module.Create<Browser.BrowserModule>(this, JsonOptions, _jsonContext);
75-
Network = Module.Create<Network.NetworkModule>(this, JsonOptions, _jsonContext);
76-
InputModule = Module.Create<Input.InputModule>(this, JsonOptions, _jsonContext);
77-
Script = Module.Create<Script.ScriptModule>(this, JsonOptions, _jsonContext);
78-
Log = Module.Create<Log.LogModule>(this, JsonOptions, _jsonContext);
79-
Storage = Module.Create<Storage.StorageModule>(this, JsonOptions, _jsonContext);
80-
WebExtension = Module.Create<WebExtension.WebExtensionModule>(this, JsonOptions, _jsonContext);
81-
Emulation = Module.Create<Emulation.EmulationModule>(this, JsonOptions, _jsonContext);
38+
Broker = new Broker(this, uri);
39+
40+
SessionModule = AsModule<Session.SessionModule>();
41+
BrowsingContext = AsModule<BrowsingContext.BrowsingContextModule>();
42+
Browser = AsModule<Browser.BrowserModule>();
43+
Network = AsModule<Network.NetworkModule>();
44+
InputModule = AsModule<Input.InputModule>();
45+
Script = AsModule<Script.ScriptModule>();
46+
Log = AsModule<Log.LogModule>();
47+
Storage = AsModule<Storage.StorageModule>();
48+
WebExtension = AsModule<WebExtension.WebExtensionModule>();
49+
Emulation = AsModule<Emulation.EmulationModule>();
8250
}
8351

8452
internal Session.SessionModule SessionModule { get; }
@@ -125,4 +93,38 @@ public async ValueTask DisposeAsync()
12593
await Broker.DisposeAsync().ConfigureAwait(false);
12694
GC.SuppressFinalize(this);
12795
}
96+
97+
public T AsModule<T>() where T : Module, new()
98+
{
99+
return (T)_modules.GetOrAdd(typeof(T), Module.Create<T>(this, Broker, GetJsonOptions()));
100+
}
101+
102+
private Broker Broker { get; }
103+
104+
private JsonSerializerOptions GetJsonOptions()
105+
{
106+
return new JsonSerializerOptions
107+
{
108+
PropertyNameCaseInsensitive = true,
109+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
110+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
111+
112+
// BiDi returns special numbers such as "NaN" as strings
113+
// Additionally, -0 is returned as a string "-0"
114+
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals | JsonNumberHandling.AllowReadingFromString,
115+
Converters =
116+
{
117+
new BrowsingContextConverter(this),
118+
new BrowserUserContextConverter(this),
119+
new CollectorConverter(this),
120+
new InterceptConverter(this),
121+
new HandleConverter(this),
122+
new InternalIdConverter(this),
123+
new PreloadScriptConverter(this),
124+
new RealmConverter(this),
125+
new DateTimeOffsetConverter(),
126+
new WebExtensionConverter(this),
127+
}
128+
};
129+
}
128130
}

dotnet/src/webdriver/BiDi/Broker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public sealed class Broker : IAsyncDisposable
5050
private Task? _eventEmitterTask;
5151
private CancellationTokenSource? _receiveMessagesCancellationTokenSource;
5252

53-
internal Broker(BiDi bidi, Uri url, JsonSerializerOptions jsonOptions)
53+
internal Broker(BiDi bidi, Uri url)
5454
{
5555
_bidi = bidi;
5656
_transport = new WebSocketTransport(url);

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,67 @@
1919

2020
using OpenQA.Selenium.BiDi.Json;
2121
using System.Text.Json;
22-
using System.Text.Json.Serialization;
2322
using System.Threading.Tasks;
2423

2524
namespace OpenQA.Selenium.BiDi.Browser;
2625

2726
public sealed class BrowserModule : Module
2827
{
29-
internal new BiDiJsonSerializerContext JsonContext => (BiDiJsonSerializerContext)base.JsonContext;
28+
private BiDiJsonSerializerContext _jsonContext = null!;
3029

3130
public async Task<CloseResult> CloseAsync(CloseOptions? options = null)
3231
{
33-
return await Broker.ExecuteCommandAsync(new CloseCommand(), options, JsonContext.Browser_CloseCommand, JsonContext.Browser_CloseResult).ConfigureAwait(false);
32+
return await Broker.ExecuteCommandAsync(new CloseCommand(), options, _jsonContext.Browser_CloseCommand, _jsonContext.Browser_CloseResult).ConfigureAwait(false);
3433
}
3534

3635
public async Task<CreateUserContextResult> CreateUserContextAsync(CreateUserContextOptions? options = null)
3736
{
3837
var @params = new CreateUserContextParameters(options?.AcceptInsecureCerts, options?.Proxy, options?.UnhandledPromptBehavior);
3938

40-
return await Broker.ExecuteCommandAsync(new CreateUserContextCommand(@params), options, JsonContext.CreateUserContextCommand, JsonContext.CreateUserContextResult).ConfigureAwait(false);
39+
return await Broker.ExecuteCommandAsync(new CreateUserContextCommand(@params), options, _jsonContext.CreateUserContextCommand, _jsonContext.CreateUserContextResult).ConfigureAwait(false);
4140
}
4241

4342
public async Task<GetUserContextsResult> GetUserContextsAsync(GetUserContextsOptions? options = null)
4443
{
45-
return await Broker.ExecuteCommandAsync(new GetUserContextsCommand(), options, JsonContext.GetUserContextsCommand, JsonContext.GetUserContextsResult).ConfigureAwait(false);
44+
return await Broker.ExecuteCommandAsync(new GetUserContextsCommand(), options, _jsonContext.GetUserContextsCommand, _jsonContext.GetUserContextsResult).ConfigureAwait(false);
4645
}
4746

4847
public async Task<RemoveUserContextResult> RemoveUserContextAsync(UserContext userContext, RemoveUserContextOptions? options = null)
4948
{
5049
var @params = new RemoveUserContextParameters(userContext);
5150

52-
return await Broker.ExecuteCommandAsync(new RemoveUserContextCommand(@params), options, JsonContext.RemoveUserContextCommand, JsonContext.RemoveUserContextResult).ConfigureAwait(false);
51+
return await Broker.ExecuteCommandAsync(new RemoveUserContextCommand(@params), options, _jsonContext.RemoveUserContextCommand, _jsonContext.RemoveUserContextResult).ConfigureAwait(false);
5352
}
5453

5554
public async Task<GetClientWindowsResult> GetClientWindowsAsync(GetClientWindowsOptions? options = null)
5655
{
57-
return await Broker.ExecuteCommandAsync(new(), options, JsonContext.GetClientWindowsCommand, JsonContext.GetClientWindowsResult
56+
return await Broker.ExecuteCommandAsync(new(), options, _jsonContext.GetClientWindowsCommand, _jsonContext.GetClientWindowsResult
5857
).ConfigureAwait(false);
5958
}
6059

6160
public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorAllowedAsync(string destinationFolder, SetDownloadBehaviorOptions? options = null)
6261
{
6362
var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorAllowed(destinationFolder), options?.UserContexts);
6463

65-
return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, JsonContext.SetDownloadBehaviorCommand, JsonContext.SetDownloadBehaviorResult).ConfigureAwait(false);
64+
return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult).ConfigureAwait(false);
6665
}
6766

6867
public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorAllowedAsync(SetDownloadBehaviorOptions? options = null)
6968
{
7069
var @params = new SetDownloadBehaviorParameters(null, options?.UserContexts);
7170

72-
return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, JsonContext.SetDownloadBehaviorCommand, JsonContext.SetDownloadBehaviorResult).ConfigureAwait(false);
71+
return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult).ConfigureAwait(false);
7372
}
7473

7574
public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorDeniedAsync(SetDownloadBehaviorOptions? options = null)
7675
{
7776
var @params = new SetDownloadBehaviorParameters(new DownloadBehaviorDenied(), options?.UserContexts);
7877

79-
return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, JsonContext.SetDownloadBehaviorCommand, JsonContext.SetDownloadBehaviorResult).ConfigureAwait(false);
78+
return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, _jsonContext.SetDownloadBehaviorCommand, _jsonContext.SetDownloadBehaviorResult).ConfigureAwait(false);
8079
}
81-
protected override JsonSerializerContext CreateJsonContext(JsonSerializerOptions options)
80+
81+
protected override void Initialize(JsonSerializerOptions options)
8282
{
83-
return new BiDiJsonSerializerContext(options);
83+
_jsonContext = new BiDiJsonSerializerContext(options);
8484
}
8585
}

0 commit comments

Comments
 (0)