19
19
20
20
using System ;
21
21
using System . Collections . Generic ;
22
- using System . Reflection ;
22
+
23
+ #nullable enable
23
24
24
25
namespace OpenQA . Selenium . DevTools
25
26
{
@@ -30,17 +31,26 @@ public abstract class DevToolsDomains
30
31
{
31
32
// By default, we will look for a supported version within this
32
33
// number of versions, as that will most likely still work.
33
- private static readonly int DefaultVersionRange = 5 ;
34
+ private const int DefaultVersionRange = 5 ;
34
35
35
36
// This is the list of known supported DevTools version implementation.
36
37
// 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
39
48
{
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
44
54
} ;
45
55
46
56
/// <summary>
@@ -74,6 +84,8 @@ public abstract class DevToolsDomains
74
84
/// <param name="protocolVersion">The version of the DevTools Protocol to use.</param>
75
85
/// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param>
76
86
/// <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>
77
89
public static DevToolsDomains InitializeDomains ( int protocolVersion , DevToolsSession session )
78
90
{
79
91
return InitializeDomains ( protocolVersion , session , DefaultVersionRange ) ;
@@ -86,34 +98,29 @@ public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSes
86
98
/// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param>
87
99
/// <param name="versionRange">The range of versions within which to match the provided version number. Defaults to 5 versions.</param>
88
100
/// <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>
89
103
public static DevToolsDomains InitializeDomains ( int protocolVersion , DevToolsSession session , int versionRange )
90
104
{
91
105
if ( versionRange < 0 )
92
106
{
93
107
throw new ArgumentException ( "Version range must be positive" , nameof ( versionRange ) ) ;
94
108
}
95
109
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 )
100
113
{
101
- domains = constructor . Invoke ( new object [ ] { session } ) as DevToolsDomains ;
114
+ return domains ;
102
115
}
103
116
104
- return domains ;
117
+ return CreateFallbackDomain ( protocolVersion , session , versionRange ) ;
105
118
}
106
119
107
- private static Type MatchDomainsVersion ( int desiredVersion , int versionRange )
120
+ private static DevToolsDomains CreateFallbackDomain ( int desiredVersion , DevToolsSession session , int versionRange )
108
121
{
109
- // Return fast on an exact match
110
- if ( SupportedDevToolsVersions . ContainsKey ( desiredVersion ) )
111
- {
112
- return SupportedDevToolsVersions [ desiredVersion ] ;
113
- }
114
-
115
122
// 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 ) ;
117
124
supportedVersions . Sort ( ( first , second ) => second . CompareTo ( first ) ) ;
118
125
119
126
foreach ( int supportedVersion in supportedVersions )
@@ -123,7 +130,7 @@ private static Type MatchDomainsVersion(int desiredVersion, int versionRange)
123
130
// (that is, closest without going over).
124
131
if ( desiredVersion >= supportedVersion && desiredVersion - supportedVersion < versionRange )
125
132
{
126
- return SupportedDevToolsVersions [ supportedVersion ] ;
133
+ return CreateDevToolsDomain ( supportedVersion , session ) ! ;
127
134
}
128
135
}
129
136
0 commit comments