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] Simplify and modernize DevToolsDomains.InitializeDomains #15198

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Changes from all commits
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
53 changes: 30 additions & 23 deletions dotnet/src/webdriver/DevTools/DevToolsDomains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

using System;
using System.Collections.Generic;
using System.Reflection;

#nullable enable

namespace OpenQA.Selenium.DevTools
{
Expand All @@ -30,17 +31,26 @@ 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
// added to this dictionary.
private static readonly Dictionary<int, Type> SupportedDevToolsVersions = new Dictionary<int, Type>()
// added to this array and to the method below.
private static int[] SupportedDevToolsVersions =>
[
130,
132,
131,
85
];

private static DevToolsDomains? CreateDevToolsDomain(int protocolVersion, DevToolsSession session) => protocolVersion switch
{
{ 130, typeof(V130.V130Domains) },
{ 132, typeof(V132.V132Domains) },
{ 131, typeof(V131.V131Domains) },
{ 85, typeof(V85.V85Domains) }
130 => new V130.V130Domains(session),
132 => new V132.V132Domains(session),
131 => new V131.V131Domains(session),
85 => new V85.V85Domains(session),
_ => null
};

/// <summary>
Expand Down Expand Up @@ -74,6 +84,8 @@ public abstract class DevToolsDomains
/// <param name="protocolVersion">The version of the DevTools Protocol to use.</param>
/// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param>
/// <returns>The <see cref="DevToolsDomains"/> object containing the version-specific domains.</returns>
/// <exception cref="ArgumentException">If <paramref name="protocolVersion"/> is negative.</exception>
/// <exception cref="WebDriverException">If the desired protocol version is not supported.</exception>
public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session)
{
return InitializeDomains(protocolVersion, session, DefaultVersionRange);
Expand All @@ -86,34 +98,29 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes
/// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param>
/// <param name="versionRange">The range of versions within which to match the provided version number. Defaults to 5 versions.</param>
/// <returns>The <see cref="DevToolsDomains"/> object containing the version-specific domains.</returns>
/// <exception cref="ArgumentException">If <paramref name="protocolVersion"/> is negative.</exception>
/// <exception cref="WebDriverException">If the desired protocol version is not in the supported range.</exception>
public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session, int versionRange)
{
if (versionRange < 0)
{
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)
// Return fast on an exact match
DevToolsDomains? domains = CreateDevToolsDomain(protocolVersion, session);
if (domains is not null)
{
domains = constructor.Invoke(new object[] { session }) as DevToolsDomains;
return domains;
}

return domains;
return CreateFallbackDomain(protocolVersion, session, versionRange);
}

private static Type MatchDomainsVersion(int desiredVersion, int versionRange)
private static DevToolsDomains CreateFallbackDomain(int desiredVersion, DevToolsSession session, int versionRange)
{
// Return fast on an exact match
if (SupportedDevToolsVersions.ContainsKey(desiredVersion))
{
return SupportedDevToolsVersions[desiredVersion];
}

// Get the list of supported versions and sort descending
List<int> supportedVersions = new List<int>(SupportedDevToolsVersions.Keys);
List<int> supportedVersions = new List<int>(SupportedDevToolsVersions);
supportedVersions.Sort((first, second) => second.CompareTo(first));

foreach (int supportedVersion in supportedVersions)
Expand All @@ -123,7 +130,7 @@ private static Type MatchDomainsVersion(int desiredVersion, int versionRange)
// (that is, closest without going over).
if (desiredVersion >= supportedVersion && desiredVersion - supportedVersion < versionRange)
{
return SupportedDevToolsVersions[supportedVersion];
return CreateDevToolsDomain(supportedVersion, session)!;
}
}

Expand Down