1919
2020using System ;
2121using System . Collections . Generic ;
22- using System . Reflection ;
22+
23+ #nullable enable
2324
2425namespace OpenQA . Selenium . DevTools
2526{
@@ -30,17 +31,26 @@ public abstract class DevToolsDomains
3031 {
3132 // By default, we will look for a supported version within this
3233 // number of versions, as that will most likely still work.
33- private static readonly int DefaultVersionRange = 5 ;
34+ private const int DefaultVersionRange = 5 ;
3435
3536 // This is the list of known supported DevTools version implementation.
3637 // When new versions are implemented for support, new types must be
37- // added to this dictionary.
38- private static readonly Dictionary < int , Type > SupportedDevToolsVersions = new Dictionary < int , Type > ( )
38+ // added to this array and to the method below.
39+ private static int [ ] SupportedDevToolsVersions =>
40+ [
41+ 130 ,
42+ 132 ,
43+ 131 ,
44+ 85
45+ ] ;
46+
47+ private static DevToolsDomains ? CreateDevToolsDomain ( int protocolVersion , DevToolsSession session ) => protocolVersion switch
3948 {
40- { 130 , typeof ( V130 . V130Domains ) } ,
41- { 132 , typeof ( V132 . V132Domains ) } ,
42- { 131 , typeof ( V131 . V131Domains ) } ,
43- { 85 , typeof ( V85 . V85Domains ) }
49+ 130 => new V130 . V130Domains ( session ) ,
50+ 132 => new V132 . V132Domains ( session ) ,
51+ 131 => new V131 . V131Domains ( session ) ,
52+ 85 => new V85 . V85Domains ( session ) ,
53+ _ => null
4454 } ;
4555
4656 /// <summary>
@@ -74,6 +84,8 @@ public abstract class DevToolsDomains
7484 /// <param name="protocolVersion">The version of the DevTools Protocol to use.</param>
7585 /// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param>
7686 /// <returns>The <see cref="DevToolsDomains"/> object containing the version-specific domains.</returns>
87+ /// <exception cref="ArgumentException">If <paramref name="protocolVersion"/> is negative.</exception>
88+ /// <exception cref="WebDriverException">If the desired protocol version is not supported.</exception>
7789 public static DevToolsDomains InitializeDomains ( int protocolVersion , DevToolsSession session )
7890 {
7991 return InitializeDomains ( protocolVersion , session , DefaultVersionRange ) ;
@@ -86,34 +98,29 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes
8698 /// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param>
8799 /// <param name="versionRange">The range of versions within which to match the provided version number. Defaults to 5 versions.</param>
88100 /// <returns>The <see cref="DevToolsDomains"/> object containing the version-specific domains.</returns>
101+ /// <exception cref="ArgumentException">If <paramref name="protocolVersion"/> is negative.</exception>
102+ /// <exception cref="WebDriverException">If the desired protocol version is not in the supported range.</exception>
89103 public static DevToolsDomains InitializeDomains ( int protocolVersion , DevToolsSession session , int versionRange )
90104 {
91105 if ( versionRange < 0 )
92106 {
93107 throw new ArgumentException ( "Version range must be positive" , nameof ( versionRange ) ) ;
94108 }
95109
96- DevToolsDomains domains = null ;
97- Type domainType = MatchDomainsVersion ( protocolVersion , versionRange ) ;
98- ConstructorInfo constructor = domainType . GetConstructor ( new Type [ ] { typeof ( DevToolsSession ) } ) ;
99- if ( constructor != null )
110+ // Return fast on an exact match
111+ DevToolsDomains ? domains = CreateDevToolsDomain ( protocolVersion , session ) ;
112+ if ( domains is not null )
100113 {
101- domains = constructor . Invoke ( new object [ ] { session } ) as DevToolsDomains ;
114+ return domains ;
102115 }
103116
104- return domains ;
117+ return CreateFallbackDomain ( protocolVersion , session , versionRange ) ;
105118 }
106119
107- private static Type MatchDomainsVersion ( int desiredVersion , int versionRange )
120+ private static DevToolsDomains CreateFallbackDomain ( int desiredVersion , DevToolsSession session , int versionRange )
108121 {
109- // Return fast on an exact match
110- if ( SupportedDevToolsVersions . ContainsKey ( desiredVersion ) )
111- {
112- return SupportedDevToolsVersions [ desiredVersion ] ;
113- }
114-
115122 // Get the list of supported versions and sort descending
116- List < int > supportedVersions = new List < int > ( SupportedDevToolsVersions . Keys ) ;
123+ List < int > supportedVersions = new List < int > ( SupportedDevToolsVersions ) ;
117124 supportedVersions . Sort ( ( first , second ) => second . CompareTo ( first ) ) ;
118125
119126 foreach ( int supportedVersion in supportedVersions )
@@ -123,7 +130,7 @@ private static Type MatchDomainsVersion(int desiredVersion, int versionRange)
123130 // (that is, closest without going over).
124131 if ( desiredVersion >= supportedVersion && desiredVersion - supportedVersion < versionRange )
125132 {
126- return SupportedDevToolsVersions [ supportedVersion ] ;
133+ return CreateDevToolsDomain ( supportedVersion , session ) ! ;
127134 }
128135 }
129136
0 commit comments