|
18 | 18 | // </copyright> |
19 | 19 |
|
20 | 20 | using System; |
| 21 | +using System.Text.Json; |
| 22 | +using System.Text.Json.Serialization; |
21 | 23 | using System.Threading; |
22 | 24 | using System.Threading.Tasks; |
23 | 25 | using OpenQA.Selenium.BiDi.Communication; |
| 26 | +using OpenQA.Selenium.BiDi.Communication.Json; |
| 27 | +using OpenQA.Selenium.BiDi.Communication.Json.Converters; |
24 | 28 |
|
25 | 29 | namespace OpenQA.Selenium.BiDi; |
26 | 30 |
|
27 | 31 | public sealed class BiDi : IAsyncDisposable |
28 | 32 | { |
29 | 33 | private readonly Broker _broker; |
30 | | - |
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(); |
| 34 | + private readonly JsonSerializerOptions _jsonOptions; |
| 35 | + private readonly BiDiJsonSerializerContext _jsonContext; |
43 | 36 |
|
44 | 37 | private BiDi(string url) |
45 | 38 | { |
46 | 39 | var uri = new Uri(url); |
47 | 40 |
|
48 | | - _broker = new Broker(this, uri); |
49 | | - } |
50 | | - |
51 | | - internal Session.SessionModule SessionModule |
52 | | - { |
53 | | - get |
| 41 | + _jsonOptions = new JsonSerializerOptions |
54 | 42 | { |
55 | | - if (_sessionModule is not null) return _sessionModule; |
56 | | - lock (_moduleLock) |
| 43 | + PropertyNameCaseInsensitive = true, |
| 44 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 45 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 46 | + |
| 47 | + // BiDi returns special numbers such as "NaN" as strings |
| 48 | + // Additionally, -0 is returned as a string "-0" |
| 49 | + NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals | JsonNumberHandling.AllowReadingFromString, |
| 50 | + Converters = |
57 | 51 | { |
58 | | - _sessionModule ??= new Session.SessionModule(_broker); |
| 52 | + new BrowsingContextConverter(this), |
| 53 | + new BrowserUserContextConverter(this), |
| 54 | + new CollectorConverter(this), |
| 55 | + new InterceptConverter(this), |
| 56 | + new HandleConverter(this), |
| 57 | + new InternalIdConverter(this), |
| 58 | + new PreloadScriptConverter(this), |
| 59 | + new RealmConverter(this), |
| 60 | + new DateTimeOffsetConverter(), |
| 61 | + new WebExtensionConverter(this), |
59 | 62 | } |
60 | | - return _sessionModule; |
61 | | - } |
| 63 | + }; |
| 64 | + |
| 65 | + _jsonContext = new BiDiJsonSerializerContext(_jsonOptions); |
| 66 | + |
| 67 | + _broker = new Broker(this, uri, _jsonOptions); |
| 68 | + SessionModule = Module.Create<Session.SessionModule>(this, _broker, _jsonOptions, _jsonContext); |
| 69 | + BrowsingContext = Module.Create<BrowsingContext.BrowsingContextModule>(this, _broker, _jsonOptions, _jsonContext); |
| 70 | + Browser = Module.Create<Browser.BrowserModule>(this, _broker, _jsonOptions, _jsonContext); |
| 71 | + Network = Module.Create<Network.NetworkModule>(this, _broker, _jsonOptions, _jsonContext); |
| 72 | + InputModule = Module.Create<Input.InputModule>(this, _broker, _jsonOptions, _jsonContext); |
| 73 | + Script = Module.Create<Script.ScriptModule>(this, _broker, _jsonOptions, _jsonContext); |
| 74 | + Log = Module.Create<Log.LogModule>(this, _broker, _jsonOptions, _jsonContext); |
| 75 | + Storage = Module.Create<Storage.StorageModule>(this, _broker, _jsonOptions, _jsonContext); |
| 76 | + WebExtension = Module.Create<WebExtension.WebExtensionModule>(this, _broker, _jsonOptions, _jsonContext); |
| 77 | + Emulation = Module.Create<Emulation.EmulationModule>(this, _broker, _jsonOptions, _jsonContext); |
62 | 78 | } |
63 | 79 |
|
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 | | - } |
| 80 | + internal Session.SessionModule SessionModule { get; } |
76 | 81 |
|
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 | | - } |
88 | | - } |
| 82 | + public BrowsingContext.BrowsingContextModule BrowsingContext { get; } |
89 | 83 |
|
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 | | - } |
| 84 | + public Browser.BrowserModule Browser { get; } |
102 | 85 |
|
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 | | - } |
| 86 | + public Network.NetworkModule Network { get; } |
115 | 87 |
|
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 | | - } |
| 88 | + internal Input.InputModule InputModule { get; } |
128 | 89 |
|
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 | | - } |
| 90 | + public Script.ScriptModule Script { get; } |
141 | 91 |
|
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 | | - } |
| 92 | + public Log.LogModule Log { get; } |
154 | 93 |
|
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 | | - } |
| 94 | + public Storage.StorageModule Storage { get; } |
167 | 95 |
|
168 | | - public Emulation.EmulationModule Emulation |
169 | | - { |
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 | | - } |
179 | | - } |
| 96 | + public WebExtension.WebExtensionModule WebExtension { get; } |
| 97 | + |
| 98 | + public Emulation.EmulationModule Emulation { get; } |
180 | 99 |
|
181 | 100 | public Task<Session.StatusResult> StatusAsync() |
182 | 101 | { |
|
0 commit comments