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] Source-generate CDP command and response serialization #15172

Closed
11 changes: 11 additions & 0 deletions dotnet/src/webdriver/DevTools/DevToolsDomains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace OpenQA.Selenium.DevTools
{
Expand Down Expand Up @@ -68,6 +70,15 @@ public abstract class DevToolsDomains
/// </summary>
public abstract Log Log { get; }

internal abstract JsonNode SerializeToNode<TCommand>(TCommand command)
where TCommand : ICommand;

internal abstract ICommandResponse<TCommand> DeserializeCommandResponse<TCommand>(JsonElement responseJson)
where TCommand : ICommand;

internal abstract TCommandResponse Deserialize<TCommandResponse>(JsonElement responseJson)
where TCommandResponse : ICommandResponse;

/// <summary>
/// Initializes the supplied DevTools session's domains for the specified browser version.
/// </summary>
Expand Down
29 changes: 13 additions & 16 deletions dotnet/src/webdriver/DevTools/DevToolsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// </copyright>

using OpenQA.Selenium.Internal.Logging;
using OpenQA.Selenium.DevTools.Json;
using System;
using System.Collections.Concurrent;
using System.Globalization;
Expand Down Expand Up @@ -156,19 +157,16 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
throw new ArgumentNullException(nameof(command));
}

var result = await SendCommand(command.CommandName, JsonSerializer.SerializeToNode(command), cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived).ConfigureAwait(false);
JsonNode serializedCommand = this.domains.SerializeToNode(command);

var result = await SendCommand(command.CommandName, serializedCommand, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived).ConfigureAwait(false);

if (result == null)
{
return null;
}

if (!this.domains.VersionSpecificDomains.ResponseTypeMap.TryGetCommandResponseType<TCommand>(out Type commandResponseType))
{
throw new InvalidOperationException($"Type {command.GetType()} does not correspond to a known command response type.");
}

return result.Value.Deserialize(commandResponseType) as ICommandResponse<TCommand>;
return this.domains.DeserializeCommandResponse<TCommand>(result.Value);
}

/// <summary>
Expand All @@ -189,19 +187,16 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
throw new ArgumentNullException(nameof(command));
}

var result = await SendCommand(command.CommandName, sessionId, JsonSerializer.SerializeToNode(command), cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived).ConfigureAwait(false);
JsonNode serializedCommand = this.domains.SerializeToNode(command);

var result = await SendCommand(command.CommandName, sessionId, serializedCommand, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived).ConfigureAwait(false);

if (result == null)
{
return null;
}

if (!this.domains.VersionSpecificDomains.ResponseTypeMap.TryGetCommandResponseType(command, out Type commandResponseType))
{
throw new InvalidOperationException($"Type {typeof(TCommand)} does not correspond to a known command response type.");
}

return result.Value.Deserialize(commandResponseType) as ICommandResponse<TCommand>;
return this.domains.DeserializeCommandResponse<TCommand>(result.Value);
}

/// <summary>
Expand All @@ -223,14 +218,16 @@ public T GetVersionSpecificDomains<T>() where T : DevToolsSessionDomains
throw new ArgumentNullException(nameof(command));
}

var result = await SendCommand(command.CommandName, JsonSerializer.SerializeToNode(command), cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived).ConfigureAwait(false);
JsonNode serializedCommand = this.domains.SerializeToNode(command);

var result = await SendCommand(command.CommandName, serializedCommand, cancellationToken, millisecondsTimeout, throwExceptionIfResponseNotReceived).ConfigureAwait(false);

if (result == null)
{
return default(TCommandResponse);
}

return result.Value.Deserialize<TCommandResponse>();
return this.domains.Deserialize<TCommandResponse>(result.Value);
}

/// <summary>
Expand Down
28 changes: 28 additions & 0 deletions dotnet/src/webdriver/DevTools/IAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// <copyright file="IAdapter.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>

using System.Text.Json.Nodes;

namespace OpenQA.Selenium.DevTools;

internal interface IAdapter
{
JsonNode SerializeToNode<TCommand>(TCommand command)
where TCommand : ICommand;
}
28 changes: 28 additions & 0 deletions dotnet/src/webdriver/DevTools/v130/DevToolsSerializationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// <copyright file="DevToolsSerializationContext.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>

using System.Text.Json.Serialization;

namespace OpenQA.Selenium.DevTools.V130;

[JsonSerializable(typeof(ICommand))]
internal sealed partial class V130RequestSerializationContext : JsonSerializerContext;

[JsonSerializable(typeof(ICommand))]
internal sealed partial class V130ResponseSerializationContext : JsonSerializerContext;
34 changes: 34 additions & 0 deletions dotnet/src/webdriver/DevTools/v130/V130Domains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@
// under the License.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace OpenQA.Selenium.DevTools.V130
{
/// <summary>
/// Class containing the domain implementation for version 130 of the DevTools Protocol.
/// </summary>
public class V130Domains : DevToolsDomains
{
private static readonly JsonSerializerOptions jsonRequestSerializerOptions = new()
{
TypeInfoResolver = V130RequestSerializationContext.Default
};

private static readonly JsonSerializerOptions jsonResponseSerializerOptions = new()
{
TypeInfoResolver = V130ResponseSerializationContext.Default
};

private DevToolsSessionDomains domains;

/// <summary>
Expand Down Expand Up @@ -64,5 +78,25 @@ public V130Domains(DevToolsSession session)
/// Gets the object used for manipulating the browser's logs.
/// </summary>
public override DevTools.Log Log => new V130Log(domains.Log);

internal override JsonNode SerializeToNode<TCommand>(TCommand command)
{
return JsonSerializer.SerializeToNode(command, jsonRequestSerializerOptions);
}

internal override ICommandResponse<TCommand> DeserializeCommandResponse<TCommand>(JsonElement responseJson)
{
if (!this.VersionSpecificDomains.ResponseTypeMap.TryGetCommandResponseType<TCommand>(out Type commandResponseType))
{
throw new InvalidOperationException($"Type {typeof(TCommand)} does not correspond to a known command response type.");
}

return (ICommandResponse<TCommand>)responseJson.Deserialize(commandResponseType, jsonResponseSerializerOptions);
}

internal override TCommandResponse Deserialize<TCommandResponse>(JsonElement responseJson)
{
return responseJson.Deserialize<TCommandResponse>(jsonResponseSerializerOptions);
}
}
}
28 changes: 28 additions & 0 deletions dotnet/src/webdriver/DevTools/v131/DevToolsSerializationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// <copyright file="DevToolsSerializationContext.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>

using System.Text.Json.Serialization;

namespace OpenQA.Selenium.DevTools.V131;

[JsonSerializable(typeof(ICommand))]
internal sealed partial class V131RequestSerializationContext : JsonSerializerContext;

[JsonSerializable(typeof(ICommand))]
internal sealed partial class V131ResponseSerializationContext : JsonSerializerContext;
34 changes: 34 additions & 0 deletions dotnet/src/webdriver/DevTools/v131/V131Domains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@
// under the License.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace OpenQA.Selenium.DevTools.V131
{
/// <summary>
/// Class containing the domain implementation for version 131 of the DevTools Protocol.
/// </summary>
public class V131Domains : DevToolsDomains
{
private static readonly JsonSerializerOptions jsonRequestSerializerOptions = new()
{
TypeInfoResolver = V131RequestSerializationContext.Default
};

private static readonly JsonSerializerOptions jsonResponseSerializerOptions = new()
{
TypeInfoResolver = V131ResponseSerializationContext.Default
};

private DevToolsSessionDomains domains;

/// <summary>
Expand Down Expand Up @@ -64,5 +78,25 @@ public V131Domains(DevToolsSession session)
/// Gets the object used for manipulating the browser's logs.
/// </summary>
public override DevTools.Log Log => new V131Log(domains.Log);

internal override JsonNode SerializeToNode<TCommand>(TCommand command)
{
return JsonSerializer.SerializeToNode(command, jsonRequestSerializerOptions);
}

internal override ICommandResponse<TCommand> DeserializeCommandResponse<TCommand>(JsonElement responseJson)
{
if (!this.VersionSpecificDomains.ResponseTypeMap.TryGetCommandResponseType<TCommand>(out Type commandResponseType))
{
throw new InvalidOperationException($"Type {typeof(TCommand)} does not correspond to a known command response type.");
}

return (ICommandResponse<TCommand>)responseJson.Deserialize(commandResponseType, jsonResponseSerializerOptions);
}

internal override TCommandResponse Deserialize<TCommandResponse>(JsonElement responseJson)
{
return responseJson.Deserialize<TCommandResponse>(jsonResponseSerializerOptions);
}
}
}
28 changes: 28 additions & 0 deletions dotnet/src/webdriver/DevTools/v132/DevToolsSerializationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// <copyright file="DevToolsSerializationContext.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>

using System.Text.Json.Serialization;

namespace OpenQA.Selenium.DevTools.V132;

[JsonSerializable(typeof(ICommand))]
internal sealed partial class V132RequestSerializationContext : JsonSerializerContext;

[JsonSerializable(typeof(ICommand))]
internal sealed partial class V132ResponseSerializationContext : JsonSerializerContext;
34 changes: 34 additions & 0 deletions dotnet/src/webdriver/DevTools/v132/V132Domains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@
// under the License.
// </copyright>

using System;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace OpenQA.Selenium.DevTools.V132
{
/// <summary>
/// Class containing the domain implementation for version 132 of the DevTools Protocol.
/// </summary>
public class V132Domains : DevToolsDomains
{
private static readonly JsonSerializerOptions jsonRequestSerializerOptions = new()
{
TypeInfoResolver = V132RequestSerializationContext.Default
};

private static readonly JsonSerializerOptions jsonResponseSerializerOptions = new()
{
TypeInfoResolver = V132ResponseSerializationContext.Default
};

private DevToolsSessionDomains domains;

/// <summary>
Expand Down Expand Up @@ -64,5 +78,25 @@ public V132Domains(DevToolsSession session)
/// Gets the object used for manipulating the browser's logs.
/// </summary>
public override DevTools.Log Log => new V132Log(domains.Log);

internal override JsonNode SerializeToNode<TCommand>(TCommand command)
{
return JsonSerializer.SerializeToNode(command, jsonRequestSerializerOptions);
}

internal override ICommandResponse<TCommand> DeserializeCommandResponse<TCommand>(JsonElement responseJson)
{
if (!this.VersionSpecificDomains.ResponseTypeMap.TryGetCommandResponseType<TCommand>(out Type commandResponseType))
{
throw new InvalidOperationException($"Type {typeof(TCommand)} does not correspond to a known command response type.");
}

return (ICommandResponse<TCommand>)responseJson.Deserialize(commandResponseType, jsonResponseSerializerOptions);
}

internal override TCommandResponse Deserialize<TCommandResponse>(JsonElement responseJson)
{
return responseJson.Deserialize<TCommandResponse>(jsonResponseSerializerOptions);
}
}
}
Loading
Loading