Skip to content

[dotnet] Add nullability annotations to devtools domains #15143

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

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions dotnet/src/webdriver/DevTools/DevToolsDomains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using System.Collections.Generic;
using System.Reflection;

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
Expand All @@ -30,7 +32,7 @@ public abstract class DevToolsDomains
{
// By default, we will look for a supported version within this
// number of versions, as that will most likely still work.
private static readonly int DefaultVersionRange = 5;
private const int DefaultVersionRange = 5;

// This is the list of known supported DevTools version implementation.
// When new versions are implemented for support, new types must be
Expand Down Expand Up @@ -93,23 +95,18 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes
throw new ArgumentException("Version range must be positive", nameof(versionRange));
}

DevToolsDomains domains = null;
Type domainType = MatchDomainsVersion(protocolVersion, versionRange);
ConstructorInfo constructor = domainType.GetConstructor(new Type[] { typeof(DevToolsSession) });
if (constructor != null)
{
domains = constructor.Invoke(new object[] { session }) as DevToolsDomains;
}

return domains;
ConstructorInfo constructor = domainType.GetConstructor(new Type[] { typeof(DevToolsSession) })!;
return (DevToolsDomains)constructor.Invoke(new object[] { session });
}

private static Type MatchDomainsVersion(int desiredVersion, int versionRange)
{
// Return fast on an exact match
if (SupportedDevToolsVersions.ContainsKey(desiredVersion))
if (SupportedDevToolsVersions.TryGetValue(desiredVersion, out Type? exactMatch))
{
return SupportedDevToolsVersions[desiredVersion];
return exactMatch;
}

// Get the list of supported versions and sort descending
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/DevTools/DevToolsExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/DevTools/DevToolsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions dotnet/src/webdriver/DevTools/DevToolsSessionDomains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
/// Provides an abstract base class for version-specific domain implementations.
/// </summary>
public abstract class DevToolsSessionDomains
{
private CommandResponseTypeMap responseTypeMap = new CommandResponseTypeMap();

/// <summary>
/// Initializes a new instance of the <see cref="DevToolsSessionDomains"/> class.
/// </summary>
Expand All @@ -37,7 +37,7 @@ protected DevToolsSessionDomains()
/// <summary>
/// Gets the <see cref="CommandResponseTypeMap"/> containing information about the types returned by DevTools Protocol commands.,
/// </summary>
internal CommandResponseTypeMap ResponseTypeMap => this.responseTypeMap;
internal CommandResponseTypeMap ResponseTypeMap { get; } = new CommandResponseTypeMap();

/// <summary>
/// Populates the command response type map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using System.Text.Json;
using System.Text.Json.Nodes;

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
Expand All @@ -44,16 +46,16 @@ public DevToolsEventReceivedEventArgs(string domainName, string eventName, JsonE
/// <summary>
/// Gets the domain on which the event is to be raised.
/// </summary>
public string DomainName { get; private set; }
public string DomainName { get; }

/// <summary>
/// Gets the name of the event to be raised.
/// </summary>
public string EventName { get; private set; }
public string EventName { get; }

/// <summary>
/// Gets the data with which the event is to be raised.
/// </summary>
public JsonElement EventData { get; private set; }
public JsonElement EventData { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System;

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
Expand Down
17 changes: 15 additions & 2 deletions dotnet/src/webdriver/DevTools/ExceptionThrownEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,34 @@

using System;

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
/// Provides data for events relating to JavaScript exception handling.
/// </summary>
public class ExceptionThrownEventArgs : EventArgs
{
/// <summary>
/// Initializes new instance of the <see cref="ExceptionThrownEventArgs"/> type.
/// </summary>
/// <param name="timestamp">The time stamp of the exception.</param>
/// <param name="message">The text of the exception.</param>
public ExceptionThrownEventArgs(DateTime timestamp, string message)
{
Timestamp = timestamp;
Message = message;
}

/// <summary>
/// Gets the time stamp of the exception.
/// </summary>
public DateTime Timestamp { get; internal set; }
public DateTime Timestamp { get; }

/// <summary>
/// Gets the text of the exception.
/// </summary>
public string Message { get; internal set; }
public string Message { get; }
}
}
15 changes: 11 additions & 4 deletions dotnet/src/webdriver/DevTools/Json/JsonEnumMemberConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,28 @@
using System.Text.Json;
using System.Text.Json.Serialization;

#nullable enable

namespace OpenQA.Selenium.DevTools.Json
{
internal class JsonEnumMemberConverter<TEnum> : JsonConverter<TEnum> where TEnum : Enum
internal sealed class JsonEnumMemberConverter<TEnum>
: JsonConverter<TEnum> where TEnum : struct, Enum
{
private readonly Dictionary<TEnum, string> _enumToString = new Dictionary<TEnum, string>();
private readonly Dictionary<string, TEnum> _stringToEnum = new Dictionary<string, TEnum>();

public JsonEnumMemberConverter()
{
var type = typeof(TEnum);
var values = Enum.GetValues(type);
#if NET8_0_OR_GREATER
TEnum[] values = Enum.GetValues<TEnum>();
#else
Array values = Enum.GetValues(type);
#endif

foreach (var value in values)
{
var enumMember = type.GetMember(value.ToString())[0];
var enumMember = type.GetField(value.ToString())!;
var attr = enumMember.GetCustomAttributes(typeof(EnumMemberAttribute), false)
.Cast<EnumMemberAttribute>()
.FirstOrDefault();
Expand All @@ -59,7 +66,7 @@ public JsonEnumMemberConverter()

public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var stringValue = reader.GetString();
var stringValue = reader.GetString() ?? throw new JsonException("Cannot read an enum string from \"null\"");

if (_stringToEnum.TryGetValue(stringValue, out var enumValue))
{
Expand Down
2 changes: 2 additions & 0 deletions dotnet/src/webdriver/DevTools/ResponsePausedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using System;

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions dotnet/src/webdriver/DevTools/v130/V130JavaScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ private void OnRuntimeBindingCalled(object sender, Runtime.BindingCalledEventArg
private void OnRuntimeExceptionThrown(object sender, Runtime.ExceptionThrownEventArgs e)
{
// TODO: Collect stack trace elements
var wrapped = new ExceptionThrownEventArgs()
{
Timestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
Message = e.ExceptionDetails.Text
};
var wrapped = new ExceptionThrownEventArgs
(
timestamp: new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
message: e.ExceptionDetails.Text
);

this.OnExceptionThrown(wrapped);
}
Expand Down
10 changes: 5 additions & 5 deletions dotnet/src/webdriver/DevTools/v131/V131JavaScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ private void OnRuntimeBindingCalled(object sender, Runtime.BindingCalledEventArg
private void OnRuntimeExceptionThrown(object sender, Runtime.ExceptionThrownEventArgs e)
{
// TODO: Collect stack trace elements
var wrapped = new ExceptionThrownEventArgs()
{
Timestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
Message = e.ExceptionDetails.Text
};
var wrapped = new ExceptionThrownEventArgs
(
timestamp: new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
message: e.ExceptionDetails.Text
);

this.OnExceptionThrown(wrapped);
}
Expand Down
10 changes: 5 additions & 5 deletions dotnet/src/webdriver/DevTools/v132/V132JavaScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ private void OnRuntimeBindingCalled(object sender, Runtime.BindingCalledEventArg
private void OnRuntimeExceptionThrown(object sender, Runtime.ExceptionThrownEventArgs e)
{
// TODO: Collect stack trace elements
var wrapped = new ExceptionThrownEventArgs()
{
Timestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
Message = e.ExceptionDetails.Text
};
var wrapped = new ExceptionThrownEventArgs
(
timestamp: new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
message: e.ExceptionDetails.Text
);

this.OnExceptionThrown(wrapped);
}
Expand Down
10 changes: 5 additions & 5 deletions dotnet/src/webdriver/DevTools/v85/V85JavaScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ private void OnRuntimeBindingCalled(object sender, Runtime.BindingCalledEventArg
private void OnRuntimeExceptionThrown(object sender, Runtime.ExceptionThrownEventArgs e)
{
// TODO: Collect stack trace elements
var wrapped = new ExceptionThrownEventArgs()
{
Timestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
Message = e.ExceptionDetails.Text
};
var wrapped = new ExceptionThrownEventArgs
(
timestamp: new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
message: e.ExceptionDetails.Text
);

this.OnExceptionThrown(wrapped);
}
Expand Down
Loading