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
3738 // added to this dictionary.
38- private static readonly Dictionary < int , Type > SupportedDevToolsVersions = new Dictionary < int , Type > ( )
39+ private static int [ ] SupportedProtocolVersions =>
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="ArgumentOutOfRangeException">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,48 +98,38 @@ 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="ArgumentOutOfRangeException">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 {
93- throw new ArgumentException ( "Version range must be positive" , nameof ( versionRange ) ) ;
94- }
95-
96- DevToolsDomains domains = null ;
97- Type domainType = MatchDomainsVersion ( protocolVersion , versionRange ) ;
98- ConstructorInfo constructor = domainType . GetConstructor ( new Type [ ] { typeof ( DevToolsSession ) } ) ;
99- if ( constructor != null )
100- {
101- domains = constructor . Invoke ( new object [ ] { session } ) as DevToolsDomains ;
107+ throw new ArgumentOutOfRangeException ( nameof ( versionRange ) , "Version range must not be negative" ) ;
102108 }
103109
104- return domains ;
105- }
106-
107- private static Type MatchDomainsVersion ( int desiredVersion , int versionRange )
108- {
109110 // Return fast on an exact match
110- if ( SupportedDevToolsVersions . ContainsKey ( desiredVersion ) )
111+ DevToolsDomains ? domains = CreateDevToolsDomain ( protocolVersion , session ) ;
112+ if ( domains is not null )
111113 {
112- return SupportedDevToolsVersions [ desiredVersion ] ;
114+ return domains ;
113115 }
114116
115117 // Get the list of supported versions and sort descending
116- List < int > supportedVersions = new List < int > ( SupportedDevToolsVersions . Keys ) ;
118+ List < int > supportedVersions = new List < int > ( SupportedProtocolVersions ) ;
117119 supportedVersions . Sort ( ( first , second ) => second . CompareTo ( first ) ) ;
118120
119121 foreach ( int supportedVersion in supportedVersions )
120122 {
121123 // Match the version with the desired version within the
122124 // version range, using "The Price Is Right" style matching
123125 // (that is, closest without going over).
124- if ( desiredVersion >= supportedVersion && desiredVersion - supportedVersion < versionRange )
126+ if ( protocolVersion >= supportedVersion && protocolVersion - supportedVersion < versionRange )
125127 {
126- return SupportedDevToolsVersions [ supportedVersion ] ;
128+ return CreateDevToolsDomain ( supportedVersion , session ) ! ;
127129 }
128130 }
129131
130- throw new WebDriverException ( $ "DevTools version is not in the supported range. Desired version={ desiredVersion } , range={ versionRange } . Supported versions: { string . Join ( ", " , supportedVersions ) } ") ;
132+ throw new WebDriverException ( $ "DevTools version is not in the supported range. Desired version={ protocolVersion } , range={ versionRange } . Supported versions: { string . Join ( ", " , supportedVersions ) } ") ;
131133 }
132134 }
133135}
0 commit comments