Skip to content

Commit c447d75

Browse files
committed
BiDi shared json context for built-in modules
1 parent d5d6877 commit c447d75

File tree

12 files changed

+16
-105
lines changed

12 files changed

+16
-105
lines changed

dotnet/src/webdriver/BiDi/BiDi.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System.Threading;
2525
using System.Threading.Tasks;
2626
using OpenQA.Selenium.BiDi.Communication;
27+
using OpenQA.Selenium.BiDi.Communication.Json;
2728
using OpenQA.Selenium.BiDi.Communication.Json.Converters;
2829

2930
namespace OpenQA.Selenium.BiDi;
@@ -32,6 +33,7 @@ public sealed class BiDi : IAsyncDisposable
3233
{
3334
private readonly Broker _broker;
3435
private readonly JsonSerializerOptions _jsonOptions;
36+
private readonly BiDiJsonSerializerContext _jsonContext;
3537

3638
private readonly ConcurrentDictionary<Type, Module> _modules = [];
3739

@@ -63,6 +65,8 @@ private BiDi(string url)
6365
}
6466
};
6567

68+
_jsonContext = new BiDiJsonSerializerContext(_jsonOptions);
69+
6670
_broker = new Broker(this, uri, _jsonOptions);
6771
}
6872

@@ -88,7 +92,7 @@ private BiDi(string url)
8892

8993
public TModule AsModule<TModule>() where TModule : Module, new()
9094
{
91-
return (TModule)_modules.GetOrAdd(typeof(TModule), _ => Module.Create<TModule>(this, _broker, _jsonOptions));
95+
return (TModule)_modules.GetOrAdd(typeof(TModule), _ => Module.Create<TModule>(this, _broker, _jsonOptions, _jsonContext));
9296
}
9397

9498
public Task<Session.StatusResult> StatusAsync()

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Communication;
21-
using System.Collections.Generic;
22-
using System.Text.Json;
23-
using System.Text.Json.Serialization;
2421
using System.Threading.Tasks;
2522

2623
namespace OpenQA.Selenium.BiDi.Browser;
@@ -76,25 +73,4 @@ public async Task<EmptyResult> SetDownloadBehaviorDeniedAsync(SetDownloadBehavio
7673

7774
return await Broker.ExecuteCommandAsync<SetDownloadBehaviorCommand, EmptyResult>(new SetDownloadBehaviorCommand(@params), options, JsonContext).ConfigureAwait(false);
7875
}
79-
80-
protected internal override JsonSerializerContext ConfigureJson(JsonSerializerOptions options)
81-
{
82-
return new BrowserModuleJsonSerializerContext(options);
83-
}
8476
}
85-
86-
[JsonSerializable(typeof(Command))]
87-
[JsonSerializable(typeof(EmptyResult))]
88-
89-
[JsonSerializable(typeof(CloseCommand))]
90-
[JsonSerializable(typeof(CreateUserContextCommand))]
91-
[JsonSerializable(typeof(GetUserContextsCommand))]
92-
[JsonSerializable(typeof(GetUserContextsResult))]
93-
[JsonSerializable(typeof(RemoveUserContextCommand))]
94-
[JsonSerializable(typeof(GetClientWindowsCommand))]
95-
[JsonSerializable(typeof(GetClientWindowsResult))]
96-
[JsonSerializable(typeof(SetDownloadBehaviorCommand))]
97-
[JsonSerializable(typeof(UserContextInfo))]
98-
[JsonSerializable(typeof(IReadOnlyList<UserContextInfo>))]
99-
[JsonSerializable(typeof(IReadOnlyList<ClientWindowInfo>))]
100-
internal partial class BrowserModuleJsonSerializerContext : JsonSerializerContext;

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
// </copyright>
1919

2020
using System;
21-
using System.Text.Json;
22-
using System.Text.Json.Serialization;
2321
using System.Threading.Tasks;
2422
using OpenQA.Selenium.BiDi.Communication;
25-
using OpenQA.Selenium.BiDi.Communication.Json;
2623

2724
namespace OpenQA.Selenium.BiDi.BrowsingContext;
2825

@@ -253,9 +250,4 @@ public async Task<Subscription> OnUserPromptClosedAsync(Action<UserPromptClosedE
253250
{
254251
return await Broker.SubscribeAsync("browsingContext.userPromptClosed", handler, options, JsonContext).ConfigureAwait(false);
255252
}
256-
257-
protected internal override JsonSerializerContext ConfigureJson(JsonSerializerOptions options)
258-
{
259-
return new BiDiJsonSerializerContext(options);
260-
}
261253
}

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

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

20-
using System;
21-
using System.Text.Json;
22-
using System.Text.Json.Serialization;
2320
using System.Threading.Tasks;
2421
using OpenQA.Selenium.BiDi.Communication;
25-
using OpenQA.Selenium.BiDi.Communication.Json;
2622

2723
namespace OpenQA.Selenium.BiDi.Emulation;
2824

@@ -92,9 +88,4 @@ public async Task<EmptyResult> SetGeolocationPositionErrorOverrideAsync(SetGeolo
9288

9389
return await Broker.ExecuteCommandAsync<SetGeolocationOverrideCommand, EmptyResult>(new SetGeolocationOverrideCommand(@params), options, JsonContext).ConfigureAwait(false);
9490
}
95-
96-
protected internal override JsonSerializerContext ConfigureJson(JsonSerializerOptions options)
97-
{
98-
return new BiDiJsonSerializerContext(options);
99-
}
10091
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Communication;
21-
using OpenQA.Selenium.BiDi.Communication.Json;
2221
using System.Collections.Generic;
23-
using System.Text.Json;
24-
using System.Text.Json.Serialization;
2522
using System.Threading.Tasks;
2623

2724
namespace OpenQA.Selenium.BiDi.Input;
@@ -48,9 +45,4 @@ public async Task<EmptyResult> SetFilesAsync(BrowsingContext.BrowsingContext con
4845

4946
return await Broker.ExecuteCommandAsync<SetFilesCommand, EmptyResult>(new SetFilesCommand(@params), options, JsonContext).ConfigureAwait(false);
5047
}
51-
52-
protected internal override JsonSerializerContext ConfigureJson(JsonSerializerOptions options)
53-
{
54-
return new BiDiJsonSerializerContext(options);
55-
}
5648
}

dotnet/src/webdriver/BiDi/Log/LogModule.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
using System.Threading.Tasks;
2121
using System;
2222
using OpenQA.Selenium.BiDi.Communication;
23-
using System.Text.Json;
24-
using OpenQA.Selenium.BiDi.Communication.Json;
25-
using System.Text.Json.Serialization;
2623

2724
namespace OpenQA.Selenium.BiDi.Log;
2825

@@ -37,9 +34,4 @@ public async Task<Subscription> OnEntryAddedAsync(Action<LogEntry> handler, Subs
3734
{
3835
return await Broker.SubscribeAsync("log.entryAdded", handler, options, JsonContext).ConfigureAwait(false);
3936
}
40-
41-
protected internal override JsonSerializerContext ConfigureJson(JsonSerializerOptions options)
42-
{
43-
return new BiDiJsonSerializerContext(options);
44-
}
4537
}

dotnet/src/webdriver/BiDi/Module.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,30 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Communication;
21+
using OpenQA.Selenium.BiDi.Communication.Json;
2122
using System.Text.Json;
22-
using System.Text.Json.Serialization;
2323

2424
namespace OpenQA.Selenium.BiDi;
2525

2626
public abstract class Module
2727
{
28-
protected JsonSerializerContext JsonContext { get; set; }
29-
3028
protected Broker Broker { get; private set; }
3129

32-
protected internal abstract JsonSerializerContext ConfigureJson(JsonSerializerOptions options);
30+
internal BiDiJsonSerializerContext JsonContext { get; set; }
31+
32+
protected virtual void Initialize(JsonSerializerOptions options) { }
3333

34-
public static TModule Create<TModule>(BiDi bidi, Broker broker, JsonSerializerOptions jsonOptions) where TModule : Module, new()
34+
internal static TModule Create<TModule>(BiDi bidi, Broker broker, JsonSerializerOptions jsonOptions, BiDiJsonSerializerContext context)
35+
where TModule : Module, new()
3536
{
3637
TModule module = new()
3738
{
38-
Broker = broker
39+
Broker = broker,
40+
JsonContext = context
3941
};
40-
module.JsonContext = module.ConfigureJson(jsonOptions);
42+
43+
module.Initialize(jsonOptions);
44+
4145
return module;
4246
}
4347
}

dotnet/src/webdriver/BiDi/Network/NetworkModule.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919

2020
using System;
2121
using System.Collections.Generic;
22-
using System.Text.Json;
23-
using System.Text.Json.Serialization;
2422
using System.Threading.Tasks;
2523
using OpenQA.Selenium.BiDi.Communication;
26-
using OpenQA.Selenium.BiDi.Communication.Json;
2724

2825
namespace OpenQA.Selenium.BiDi.Network;
2926

@@ -176,9 +173,4 @@ public async Task<Subscription> OnAuthRequiredAsync(Action<AuthRequiredEventArgs
176173
{
177174
return await Broker.SubscribeAsync("network.authRequired", handler, options, JsonContext).ConfigureAwait(false);
178175
}
179-
180-
protected internal override JsonSerializerContext ConfigureJson(JsonSerializerOptions options)
181-
{
182-
return new BiDiJsonSerializerContext(options);
183-
}
184176
}

dotnet/src/webdriver/BiDi/Script/ScriptModule.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Communication;
21-
using OpenQA.Selenium.BiDi.Communication.Json;
2221
using System;
23-
using System.Text.Json;
24-
using System.Text.Json.Serialization;
2522
using System.Threading.Tasks;
2623

2724
namespace OpenQA.Selenium.BiDi.Script;
@@ -108,9 +105,4 @@ public async Task<Subscription> OnRealmDestroyedAsync(Action<RealmDestroyedEvent
108105
{
109106
return await Broker.SubscribeAsync("script.realmDestroyed", handler, options, JsonContext).ConfigureAwait(false);
110107
}
111-
112-
protected internal override JsonSerializerContext ConfigureJson(JsonSerializerOptions options)
113-
{
114-
return new BiDiJsonSerializerContext(options);
115-
}
116108
}

dotnet/src/webdriver/BiDi/Session/SessionModule.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Communication;
21-
using OpenQA.Selenium.BiDi.Communication.Json;
2221
using System.Collections.Generic;
23-
using System.Text.Json;
24-
using System.Text.Json.Serialization;
2522
using System.Threading.Tasks;
2623

2724
namespace OpenQA.Selenium.BiDi.Session;
@@ -58,9 +55,4 @@ public async Task<EmptyResult> EndAsync(EndOptions? options = null)
5855
{
5956
return await Broker.ExecuteCommandAsync<EndCommand, EmptyResult>(new EndCommand(), options, JsonContext).ConfigureAwait(false);
6057
}
61-
62-
protected internal override JsonSerializerContext ConfigureJson(JsonSerializerOptions options)
63-
{
64-
return new BiDiJsonSerializerContext(options);
65-
}
6658
}

0 commit comments

Comments
 (0)