|
18 | 18 | // </copyright> |
19 | 19 |
|
20 | 20 | using System; |
| 21 | +using System.Collections.Concurrent; |
| 22 | +using System.Text.Json; |
| 23 | +using System.Text.Json.Serialization; |
21 | 24 | using System.Threading; |
22 | 25 | using System.Threading.Tasks; |
23 | 26 | using OpenQA.Selenium.BiDi.Communication; |
| 27 | +using OpenQA.Selenium.BiDi.Communication.Json; |
| 28 | +using OpenQA.Selenium.BiDi.Communication.Json.Converters; |
24 | 29 |
|
25 | 30 | namespace OpenQA.Selenium.BiDi; |
26 | 31 |
|
27 | 32 | public sealed class BiDi : IAsyncDisposable |
28 | 33 | { |
29 | 34 | private readonly Broker _broker; |
| 35 | + private readonly JsonSerializerOptions _jsonOptions; |
| 36 | + private readonly BiDiJsonSerializerContext _jsonContext; |
30 | 37 |
|
31 | | - private Session.SessionModule? _sessionModule; |
32 | | - private BrowsingContext.BrowsingContextModule? _browsingContextModule; |
33 | | - private Browser.BrowserModule? _browserModule; |
34 | | - private Network.NetworkModule? _networkModule; |
35 | | - private Input.InputModule? _inputModule; |
36 | | - private Script.ScriptModule? _scriptModule; |
37 | | - private Log.LogModule? _logModule; |
38 | | - private Storage.StorageModule? _storageModule; |
39 | | - private WebExtension.WebExtensionModule? _webExtensionModule; |
40 | | - private Emulation.EmulationModule? _emulationModule; |
41 | | - |
42 | | - private readonly object _moduleLock = new(); |
| 38 | + private readonly ConcurrentDictionary<Type, Module> _modules = []; |
43 | 39 |
|
44 | 40 | private BiDi(string url) |
45 | 41 | { |
46 | 42 | var uri = new Uri(url); |
47 | 43 |
|
48 | | - _broker = new Broker(this, uri); |
49 | | - } |
50 | | - |
51 | | - internal Session.SessionModule SessionModule |
52 | | - { |
53 | | - get |
| 44 | + _jsonOptions = new JsonSerializerOptions |
54 | 45 | { |
55 | | - if (_sessionModule is not null) return _sessionModule; |
56 | | - lock (_moduleLock) |
| 46 | + PropertyNameCaseInsensitive = true, |
| 47 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 48 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 49 | + |
| 50 | + // BiDi returns special numbers such as "NaN" as strings |
| 51 | + // Additionally, -0 is returned as a string "-0" |
| 52 | + NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals | JsonNumberHandling.AllowReadingFromString, |
| 53 | + Converters = |
57 | 54 | { |
58 | | - _sessionModule ??= new Session.SessionModule(_broker); |
| 55 | + new BrowsingContextConverter(this), |
| 56 | + new BrowserUserContextConverter(this), |
| 57 | + new CollectorConverter(this), |
| 58 | + new InterceptConverter(this), |
| 59 | + new HandleConverter(this), |
| 60 | + new InternalIdConverter(this), |
| 61 | + new PreloadScriptConverter(this), |
| 62 | + new RealmConverter(this), |
| 63 | + new DateTimeOffsetConverter(), |
| 64 | + new WebExtensionConverter(this), |
59 | 65 | } |
60 | | - return _sessionModule; |
61 | | - } |
62 | | - } |
| 66 | + }; |
63 | 67 |
|
64 | | - public BrowsingContext.BrowsingContextModule BrowsingContext |
65 | | - { |
66 | | - get |
67 | | - { |
68 | | - if (_browsingContextModule is not null) return _browsingContextModule; |
69 | | - lock (_moduleLock) |
70 | | - { |
71 | | - _browsingContextModule ??= new BrowsingContext.BrowsingContextModule(_broker); |
72 | | - } |
73 | | - return _browsingContextModule; |
74 | | - } |
75 | | - } |
| 68 | + _jsonContext = new BiDiJsonSerializerContext(_jsonOptions); |
76 | 69 |
|
77 | | - public Browser.BrowserModule Browser |
78 | | - { |
79 | | - get |
80 | | - { |
81 | | - if (_browserModule is not null) return _browserModule; |
82 | | - lock (_moduleLock) |
83 | | - { |
84 | | - _browserModule ??= new Browser.BrowserModule(_broker); |
85 | | - } |
86 | | - return _browserModule; |
87 | | - } |
| 70 | + _broker = new Broker(this, uri, _jsonOptions); |
88 | 71 | } |
89 | 72 |
|
90 | | - public Network.NetworkModule Network |
91 | | - { |
92 | | - get |
93 | | - { |
94 | | - if (_networkModule is not null) return _networkModule; |
95 | | - lock (_moduleLock) |
96 | | - { |
97 | | - _networkModule ??= new Network.NetworkModule(_broker); |
98 | | - } |
99 | | - return _networkModule; |
100 | | - } |
101 | | - } |
| 73 | + internal Session.SessionModule SessionModule => AsModule<Session.SessionModule>(); |
102 | 74 |
|
103 | | - internal Input.InputModule InputModule |
104 | | - { |
105 | | - get |
106 | | - { |
107 | | - if (_inputModule is not null) return _inputModule; |
108 | | - lock (_moduleLock) |
109 | | - { |
110 | | - _inputModule ??= new Input.InputModule(_broker); |
111 | | - } |
112 | | - return _inputModule; |
113 | | - } |
114 | | - } |
| 75 | + public BrowsingContext.BrowsingContextModule BrowsingContext => AsModule<BrowsingContext.BrowsingContextModule>(); |
115 | 76 |
|
116 | | - public Script.ScriptModule Script |
117 | | - { |
118 | | - get |
119 | | - { |
120 | | - if (_scriptModule is not null) return _scriptModule; |
121 | | - lock (_moduleLock) |
122 | | - { |
123 | | - _scriptModule ??= new Script.ScriptModule(_broker); |
124 | | - } |
125 | | - return _scriptModule; |
126 | | - } |
127 | | - } |
| 77 | + public Browser.BrowserModule Browser => AsModule<Browser.BrowserModule>(); |
128 | 78 |
|
129 | | - public Log.LogModule Log |
130 | | - { |
131 | | - get |
132 | | - { |
133 | | - if (_logModule is not null) return _logModule; |
134 | | - lock (_moduleLock) |
135 | | - { |
136 | | - _logModule ??= new Log.LogModule(_broker); |
137 | | - } |
138 | | - return _logModule; |
139 | | - } |
140 | | - } |
| 79 | + public Network.NetworkModule Network => AsModule<Network.NetworkModule>(); |
141 | 80 |
|
142 | | - public Storage.StorageModule Storage |
143 | | - { |
144 | | - get |
145 | | - { |
146 | | - if (_storageModule is not null) return _storageModule; |
147 | | - lock (_moduleLock) |
148 | | - { |
149 | | - _storageModule ??= new Storage.StorageModule(_broker); |
150 | | - } |
151 | | - return _storageModule; |
152 | | - } |
153 | | - } |
| 81 | + internal Input.InputModule InputModule => AsModule<Input.InputModule>(); |
154 | 82 |
|
155 | | - public WebExtension.WebExtensionModule WebExtension |
156 | | - { |
157 | | - get |
158 | | - { |
159 | | - if (_webExtensionModule is not null) return _webExtensionModule; |
160 | | - lock (_moduleLock) |
161 | | - { |
162 | | - _webExtensionModule ??= new WebExtension.WebExtensionModule(_broker); |
163 | | - } |
164 | | - return _webExtensionModule; |
165 | | - } |
166 | | - } |
| 83 | + public Script.ScriptModule Script => AsModule<Script.ScriptModule>(); |
| 84 | + |
| 85 | + public Log.LogModule Log => AsModule<Log.LogModule>(); |
167 | 86 |
|
168 | | - public Emulation.EmulationModule Emulation |
| 87 | + public Storage.StorageModule Storage => AsModule<Storage.StorageModule>(); |
| 88 | + |
| 89 | + public WebExtension.WebExtensionModule WebExtension => AsModule<WebExtension.WebExtensionModule>(); |
| 90 | + |
| 91 | + public Emulation.EmulationModule Emulation => AsModule<Emulation.EmulationModule>(); |
| 92 | + |
| 93 | + public TModule AsModule<TModule>() where TModule : Module, new() |
169 | 94 | { |
170 | | - get |
171 | | - { |
172 | | - if (_emulationModule is not null) return _emulationModule; |
173 | | - lock (_moduleLock) |
174 | | - { |
175 | | - _emulationModule ??= new Emulation.EmulationModule(_broker); |
176 | | - } |
177 | | - return _emulationModule; |
178 | | - } |
| 95 | + return (TModule)_modules.GetOrAdd(typeof(TModule), _ => Module.Create<TModule>(this, _broker, _jsonOptions, _jsonContext)); |
179 | 96 | } |
180 | 97 |
|
181 | 98 | public Task<Session.StatusResult> StatusAsync() |
|
0 commit comments