Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Make the devtools SendCommand method AOT-safe #15159

Open
wants to merge 12 commits into
base: trunk
Choose a base branch
from
9 changes: 7 additions & 2 deletions dotnet/src/webdriver/DevTools/DevToolsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -284,7 +285,7 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains

LogTrace("Sending {0} {1}: {2}", message.CommandId, message.CommandName, commandParameters.ToString());

string contents = JsonSerializer.Serialize(message);
string contents = JsonSerializer.Serialize(message, DevToolsSessionSerializerContext.Default.DevToolsCommandData);
this.pendingCommands.TryAdd(message.CommandId, message);
await this.connection.SendData(contents).ConfigureAwait(false);

Expand Down Expand Up @@ -413,7 +414,7 @@ private async Task<int> InitializeProtocol(int requestedProtocolVersion)
rawVersionInfo = await client.GetStringAsync("/json/version").ConfigureAwait(false);
}

var versionInfo = JsonSerializer.Deserialize<DevToolsVersionInfo>(rawVersionInfo) ?? throw new JsonException("/json/version endpoint returned null response");
var versionInfo = JsonSerializer.Deserialize(rawVersionInfo, DevToolsSessionSerializerContext.Default.DevToolsVersionInfo) ?? throw new JsonException("/json/version endpoint returned null response");
this.EndpointAddress = versionInfo.WebSocketDebuggerUrl;

if (requestedProtocolVersion == AutoDetectDevToolsProtocolVersion)
Expand Down Expand Up @@ -664,4 +665,8 @@ private void LogError(string message, params object?[] args)
}
}
}

[JsonSerializable(typeof(DevToolsCommandData))]
[JsonSerializable(typeof(DevToolsVersionInfo))]
internal sealed partial class DevToolsSessionSerializerContext : JsonSerializerContext;
}
6 changes: 5 additions & 1 deletion dotnet/src/webdriver/JavaScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

#nullable enable
Expand Down Expand Up @@ -425,7 +426,7 @@ private void OnScriptBindingCalled(object? sender, BindingCalledEventArgs e)
{
if (e.Name == MonitorBindingName)
{
DomMutationData valueChangeData = JsonSerializer.Deserialize<DomMutationData>(e.Payload) ?? throw new JsonException("DomMutationData returned null");
DomMutationData valueChangeData = JsonSerializer.Deserialize(e.Payload, JsonEngineSerializerContext.Default.DomMutationData) ?? throw new JsonException("DomMutationData returned null");
var locator = By.CssSelector($"*[data-__webdriver_id='{valueChangeData.TargetId}']");
valueChangeData.Element = driver.FindElements(locator).FirstOrDefault();

Expand Down Expand Up @@ -461,4 +462,7 @@ private void OnConsoleApiCalled(object? sender, ConsoleApiCalledEventArgs e)
}
}
}

[JsonSerializable(typeof(DomMutationData))]
internal sealed partial class JsonEngineSerializerContext : JsonSerializerContext;
}
Loading