1717// under the License.
1818// </copyright>
1919
20- using OpenQA . Selenium . BiDi . Json ;
2120using OpenQA . Selenium . BiDi . Json . Converters ;
2221using System ;
22+ using System . Collections . Concurrent ;
2323using System . Text . Json ;
2424using System . Text . Json . Serialization ;
2525using System . Threading ;
@@ -29,56 +29,24 @@ namespace OpenQA.Selenium.BiDi;
2929
3030public 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}
0 commit comments