Skip to content

Commit 0cbe3f2

Browse files
Merge branch 'trunk' into devtools-event-args
2 parents a817d4d + 57f541a commit 0cbe3f2

16 files changed

+249
-29
lines changed

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ internal Broker(BiDi bidi, ITransport transport)
7070
{
7171
new BrowsingContextConverter(_bidi),
7272
new BrowserUserContextConverter(bidi),
73+
new BrowserClientWindowConverter(),
7374
new NavigationConverter(),
7475
new InterceptConverter(_bidi),
7576
new RequestConverter(_bidi),
@@ -97,6 +98,7 @@ internal Broker(BiDi bidi, ITransport transport)
9798
new Json.Converters.Enumerable.LocateNodesResultConverter(),
9899
new Json.Converters.Enumerable.InputSourceActionsConverter(),
99100
new Json.Converters.Enumerable.GetUserContextsResultConverter(),
101+
new Json.Converters.Enumerable.GetClientWindowsResultConverter(),
100102
new Json.Converters.Enumerable.GetRealmsResultConverter(),
101103
}
102104
};
@@ -109,15 +111,17 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
109111
await _transport.ConnectAsync(cancellationToken).ConfigureAwait(false);
110112

111113
_receiveMessagesCancellationTokenSource = new CancellationTokenSource();
112-
_receivingMessageTask = _myTaskFactory.StartNew(async () => await ReceiveMessagesAsync(_receiveMessagesCancellationTokenSource.Token), TaskCreationOptions.LongRunning).Unwrap();
113-
_eventEmitterTask = _myTaskFactory.StartNew(async () => await ProcessEventsAwaiterAsync(), TaskCreationOptions.LongRunning).Unwrap();
114+
_receivingMessageTask = _myTaskFactory.StartNew(async () => await ReceiveMessagesAsync(_receiveMessagesCancellationTokenSource.Token)).Unwrap();
115+
_eventEmitterTask = _myTaskFactory.StartNew(ProcessEventsAwaiterAsync).Unwrap();
114116
}
115117

116118
private async Task ReceiveMessagesAsync(CancellationToken cancellationToken)
117119
{
118120
while (!cancellationToken.IsCancellationRequested)
119121
{
120-
var message = await _transport.ReceiveAsJsonAsync<Message>(_jsonSerializerContext, cancellationToken);
122+
var data = await _transport.ReceiveAsync(cancellationToken).ConfigureAwait(false);
123+
124+
var message = JsonSerializer.Deserialize(new ReadOnlySpan<byte>(data), _jsonSerializerContext.Message);
121125

122126
switch (message)
123127
{
@@ -177,21 +181,21 @@ private async Task ProcessEventsAwaiterAsync()
177181
}
178182

179183
public async Task<TResult> ExecuteCommandAsync<TCommand, TResult>(TCommand command, CommandOptions? options)
180-
where TCommand: Command
184+
where TCommand : Command
181185
{
182186
var jsonElement = await ExecuteCommandCoreAsync(command, options).ConfigureAwait(false);
183187

184188
return (TResult)jsonElement.Deserialize(typeof(TResult), _jsonSerializerContext)!;
185189
}
186190

187191
public async Task ExecuteCommandAsync<TCommand>(TCommand command, CommandOptions? options)
188-
where TCommand: Command
192+
where TCommand : Command
189193
{
190194
await ExecuteCommandCoreAsync(command, options).ConfigureAwait(false);
191195
}
192196

193197
private async Task<JsonElement> ExecuteCommandCoreAsync<TCommand>(TCommand command, CommandOptions? options)
194-
where TCommand: Command
198+
where TCommand : Command
195199
{
196200
command.Id = Interlocked.Increment(ref _currentCommandId);
197201

@@ -205,7 +209,9 @@ private async Task<JsonElement> ExecuteCommandCoreAsync<TCommand>(TCommand comma
205209

206210
_pendingCommands[command.Id] = tcs;
207211

208-
await _transport.SendAsJsonAsync(command, _jsonSerializerContext, cts.Token).ConfigureAwait(false);
212+
var data = JsonSerializer.SerializeToUtf8Bytes(command, typeof(TCommand), _jsonSerializerContext);
213+
214+
await _transport.SendAsync(data, cts.Token).ConfigureAwait(false);
209215

210216
return await tcs.Task.ConfigureAwait(false);
211217
}

dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
8686
[JsonSerializable(typeof(Modules.Browser.GetUserContextsCommand))]
8787
[JsonSerializable(typeof(Modules.Browser.GetUserContextsResult))]
8888
[JsonSerializable(typeof(Modules.Browser.RemoveUserContextCommand))]
89+
[JsonSerializable(typeof(Modules.Browser.GetClientWindowsCommand))]
90+
[JsonSerializable(typeof(Modules.Browser.GetClientWindowsResult))]
8991
[JsonSerializable(typeof(Modules.Browser.UserContextInfo))]
9092
[JsonSerializable(typeof(IReadOnlyList<Modules.Browser.UserContextInfo>))]
93+
[JsonSerializable(typeof(IReadOnlyList<Modules.Browser.ClientWindowInfo>))]
9194

9295

9396
[JsonSerializable(typeof(Modules.BrowsingContext.ActivateCommand))]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// <copyright file="BrowserClientWindowConverter.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using OpenQA.Selenium.BiDi.Modules.Browser;
21+
using System;
22+
using System.Text.Json;
23+
using System.Text.Json.Serialization;
24+
25+
#nullable enable
26+
27+
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters;
28+
29+
internal class BrowserClientWindowConverter : JsonConverter<ClientWindow>
30+
{
31+
public override ClientWindow? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
32+
{
33+
var id = reader.GetString();
34+
35+
return new ClientWindow(id!);
36+
}
37+
38+
public override void Write(Utf8JsonWriter writer, ClientWindow value, JsonSerializerOptions options)
39+
{
40+
writer.WriteStringValue(value.Id);
41+
}
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// <copyright file="GetClientWindowsResultConverter.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using OpenQA.Selenium.BiDi.Modules.Browser;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Text.Json;
24+
using System.Text.Json.Serialization;
25+
26+
#nullable enable
27+
28+
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters.Enumerable;
29+
30+
internal class GetClientWindowsResultConverter : JsonConverter<GetClientWindowsResult>
31+
{
32+
public override GetClientWindowsResult Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
33+
{
34+
using var doc = JsonDocument.ParseValue(ref reader);
35+
var clientWindows = doc.RootElement.GetProperty("clientWindows").Deserialize<IReadOnlyList<ClientWindowInfo>>(options);
36+
37+
return new GetClientWindowsResult(clientWindows!);
38+
}
39+
40+
public override void Write(Utf8JsonWriter writer, GetClientWindowsResult value, JsonSerializerOptions options)
41+
{
42+
throw new NotImplementedException();
43+
}
44+
}

dotnet/src/webdriver/BiDi/Communication/Json/Converters/Enumerable/GetCookiesResultConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal class GetCookiesResultConverter : JsonConverter<GetCookiesResult>
3131
{
3232
public override GetCookiesResult Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
3333
{
34-
var doc = JsonDocument.ParseValue(ref reader);
34+
using var doc = JsonDocument.ParseValue(ref reader);
3535
var cookies = doc.RootElement.GetProperty("cookies").Deserialize<IReadOnlyList<Modules.Network.Cookie>>(options);
3636
var partitionKey = doc.RootElement.GetProperty("partitionKey").Deserialize<PartitionKey>(options);
3737

dotnet/src/webdriver/BiDi/Communication/Json/Converters/Enumerable/GetRealmsResultConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal class GetRealmsResultConverter : JsonConverter<GetRealmsResult>
3131
{
3232
public override GetRealmsResult Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
3333
{
34-
var doc = JsonDocument.ParseValue(ref reader);
34+
using var doc = JsonDocument.ParseValue(ref reader);
3535
var realms = doc.RootElement.GetProperty("realms").Deserialize<IReadOnlyList<RealmInfo>>(options);
3636

3737
return new GetRealmsResult(realms!);

dotnet/src/webdriver/BiDi/Communication/Json/Converters/Enumerable/GetUserContextsResultConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal class GetUserContextsResultConverter : JsonConverter<GetUserContextsRes
3131
{
3232
public override GetUserContextsResult Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
3333
{
34-
var doc = JsonDocument.ParseValue(ref reader);
34+
using var doc = JsonDocument.ParseValue(ref reader);
3535
var userContexts = doc.RootElement.GetProperty("userContexts").Deserialize<IReadOnlyList<UserContextInfo>>(options);
3636

3737
return new GetUserContextsResult(userContexts!);

dotnet/src/webdriver/BiDi/Communication/Json/Converters/Enumerable/LocateNodesResultConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal class LocateNodesResultConverter : JsonConverter<LocateNodesResult>
3232
{
3333
public override LocateNodesResult Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
3434
{
35-
var doc = JsonDocument.ParseValue(ref reader);
35+
using var doc = JsonDocument.ParseValue(ref reader);
3636
var nodes = doc.RootElement.GetProperty("nodes").Deserialize<IReadOnlyList<RemoteValue.Node>>(options);
3737

3838
return new LocateNodesResult(nodes!);

dotnet/src/webdriver/BiDi/Communication/Transport/ITransport.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Threading.Tasks;
2121
using System.Threading;
2222
using System;
23-
using System.Text.Json.Serialization;
2423

2524
#nullable enable
2625

@@ -30,8 +29,7 @@ interface ITransport : IDisposable
3029
{
3130
Task ConnectAsync(CancellationToken cancellationToken);
3231

33-
Task<T> ReceiveAsJsonAsync<T>(JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken);
32+
Task<byte[]> ReceiveAsync(CancellationToken cancellationToken);
3433

35-
Task SendAsJsonAsync<TCommand>(TCommand command, JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken)
36-
where TCommand : Command;
34+
Task SendAsync(byte[] data, CancellationToken cancellationToken);
3735
}

dotnet/src/webdriver/BiDi/Communication/Transport/WebSocketTransport.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
using System.Net.WebSockets;
2323
using System.Threading.Tasks;
2424
using System.Threading;
25-
using System.Text.Json;
2625
using System.Text;
2726
using OpenQA.Selenium.Internal.Logging;
28-
using System.Text.Json.Serialization;
2927

3028
#nullable enable
3129

@@ -45,7 +43,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
4543
await _webSocket.ConnectAsync(_uri, cancellationToken).ConfigureAwait(false);
4644
}
4745

48-
public async Task<T> ReceiveAsJsonAsync<T>(JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken)
46+
public async Task<byte[]> ReceiveAsync(CancellationToken cancellationToken)
4947
{
5048
using var ms = new MemoryStream();
5149

@@ -61,31 +59,28 @@ public async Task<T> ReceiveAsJsonAsync<T>(JsonSerializerContext jsonSerializerC
6159

6260
ms.Seek(0, SeekOrigin.Begin);
6361

62+
byte[] data = ms.ToArray();
63+
6464
if (_logger.IsEnabled(LogEventLevel.Trace))
6565
{
66-
_logger.Trace($"BiDi RCV <-- {Encoding.UTF8.GetString(ms.ToArray())}");
66+
_logger.Trace($"BiDi RCV <-- {Encoding.UTF8.GetString(data)}");
6767
}
6868

69-
var res = await JsonSerializer.DeserializeAsync(ms, typeof(T), jsonSerializerContext, cancellationToken).ConfigureAwait(false);
70-
71-
return (T)res!;
69+
return data;
7270
}
7371

74-
public async Task SendAsJsonAsync<TCommand>(TCommand command, JsonSerializerContext jsonSerializerContext, CancellationToken cancellationToken)
75-
where TCommand : Command
72+
public async Task SendAsync(byte[] data, CancellationToken cancellationToken)
7673
{
77-
var buffer = JsonSerializer.SerializeToUtf8Bytes(command, typeof(TCommand), jsonSerializerContext);
78-
7974
await _socketSendSemaphoreSlim.WaitAsync(cancellationToken);
8075

8176
try
8277
{
8378
if (_logger.IsEnabled(LogEventLevel.Trace))
8479
{
85-
_logger.Trace($"BiDi SND --> {Encoding.UTF8.GetString(buffer)}");
80+
_logger.Trace($"BiDi SND --> {Encoding.UTF8.GetString(data)}");
8681
}
8782

88-
await _webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, cancellationToken).ConfigureAwait(false);
83+
await _webSocket.SendAsync(new ArraySegment<byte>(data), WebSocketMessageType.Text, true, cancellationToken).ConfigureAwait(false);
8984
}
9085
finally
9186
{

0 commit comments

Comments
 (0)