2121using System . Collections . Generic ;
2222using System . Collections . ObjectModel ;
2323
24+ #nullable enable
25+
2426namespace OpenQA . Selenium
2527{
2628 /// <summary>
2729 /// Defines an interface allowing the user to manipulate cookies on the current page.
2830 /// </summary>
29- internal class CookieJar : ICookieJar
31+ internal sealed class CookieJar : ICookieJar
3032 {
31- private WebDriver driver ;
33+ private readonly WebDriver driver ;
3234
3335 /// <summary>
3436 /// Initializes a new instance of the <see cref="CookieJar"/> class.
3537 /// </summary>
3638 /// <param name="driver">The driver that is currently in use</param>
39+ /// <exception cref="ArgumentNullException">If <paramref name="driver"/> is <see langword="null"/>.</exception>
3740 public CookieJar ( WebDriver driver )
3841 {
39- this . driver = driver ;
42+ this . driver = driver ?? throw new ArgumentNullException ( nameof ( driver ) ) ;
4043 }
4144
4245 /// <summary>
4346 /// Gets all cookies defined for the current page.
4447 /// </summary>
4548 public ReadOnlyCollection < Cookie > AllCookies
4649 {
47- get { return this . GetAllCookies ( ) ; }
50+ get
51+ {
52+ object returned = this . driver . InternalExecute ( DriverCommand . GetAllCookies , new Dictionary < string , object > ( ) ) . Value ;
53+
54+ try
55+ {
56+ List < Cookie > toReturn = new List < Cookie > ( ) ;
57+ if ( returned is object ? [ ] cookies )
58+ {
59+ foreach ( object ? rawCookie in cookies )
60+ {
61+ if ( rawCookie != null )
62+ {
63+ Cookie newCookie = Cookie . FromDictionary ( ( Dictionary < string , object ? > ) rawCookie ) ;
64+ toReturn . Add ( newCookie ) ;
65+ }
66+ }
67+ }
68+
69+ return new ReadOnlyCollection < Cookie > ( toReturn ) ;
70+ }
71+ catch ( Exception e )
72+ {
73+ throw new WebDriverException ( "Unexpected problem getting cookies" , e ) ;
74+ }
75+ }
4876 }
4977
5078 /// <summary>
5179 /// Method for creating a cookie in the browser
5280 /// </summary>
5381 /// <param name="cookie"><see cref="Cookie"/> that represents a cookie in the browser</param>
82+ /// <exception cref="ArgumentNullException">If <paramref name="cookie"/> is <see langword="null"/>.</exception>
5483 public void AddCookie ( Cookie cookie )
5584 {
85+ if ( cookie is null )
86+ {
87+ throw new ArgumentNullException ( nameof ( cookie ) ) ;
88+ }
89+
5690 Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
5791 parameters . Add ( "cookie" , cookie ) ;
5892 this . driver . InternalExecute ( DriverCommand . AddCookie , parameters ) ;
@@ -62,18 +96,21 @@ public void AddCookie(Cookie cookie)
6296 /// Delete the cookie by passing in the name of the cookie
6397 /// </summary>
6498 /// <param name="name">The name of the cookie that is in the browser</param>
65- public void DeleteCookieNamed ( string name )
99+ public void DeleteCookieNamed ( string ? name )
66100 {
67- Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
68- parameters . Add ( "name" , name ) ;
69- this . driver . InternalExecute ( DriverCommand . DeleteCookie , parameters ) ;
101+ if ( name is not null )
102+ {
103+ Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
104+ parameters . Add ( "name" , name ) ;
105+ this . driver . InternalExecute ( DriverCommand . DeleteCookie , parameters ) ;
106+ }
70107 }
71108
72109 /// <summary>
73110 /// Delete a cookie in the browser by passing in a copy of a cookie
74111 /// </summary>
75112 /// <param name="cookie">An object that represents a copy of the cookie that needs to be deleted</param>
76- public void DeleteCookie ( Cookie cookie )
113+ public void DeleteCookie ( Cookie ? cookie )
77114 {
78115 if ( cookie != null )
79116 {
@@ -94,10 +131,10 @@ public void DeleteAllCookies()
94131 /// </summary>
95132 /// <param name="name">name of the cookie that needs to be returned</param>
96133 /// <returns>A Cookie from the name</returns>
97- public Cookie GetCookieNamed ( string name )
134+ public Cookie ? GetCookieNamed ( string ? name )
98135 {
99- Cookie cookieToReturn = null ;
100- if ( name != null )
136+ Cookie ? cookieToReturn = null ;
137+ if ( name is not null )
101138 {
102139 ReadOnlyCollection < Cookie > allCookies = this . AllCookies ;
103140 foreach ( Cookie currentCookie in allCookies )
@@ -112,37 +149,5 @@ public Cookie GetCookieNamed(string name)
112149
113150 return cookieToReturn ;
114151 }
115-
116- /// <summary>
117- /// Method for getting a Collection of Cookies that are present in the browser
118- /// </summary>
119- /// <returns>ReadOnlyCollection of Cookies in the browser</returns>
120- private ReadOnlyCollection < Cookie > GetAllCookies ( )
121- {
122- List < Cookie > toReturn = new List < Cookie > ( ) ;
123- object returned = this . driver . InternalExecute ( DriverCommand . GetAllCookies , new Dictionary < string , object > ( ) ) . Value ;
124-
125- try
126- {
127- object [ ] cookies = returned as object [ ] ;
128- if ( cookies != null )
129- {
130- foreach ( object rawCookie in cookies )
131- {
132- Dictionary < string , object > cookieDictionary = rawCookie as Dictionary < string , object > ;
133- if ( rawCookie != null )
134- {
135- toReturn . Add ( Cookie . FromDictionary ( cookieDictionary ) ) ;
136- }
137- }
138- }
139-
140- return new ReadOnlyCollection < Cookie > ( toReturn ) ;
141- }
142- catch ( Exception e )
143- {
144- throw new WebDriverException ( "Unexpected problem getting cookies" , e ) ;
145- }
146- }
147152 }
148153}
0 commit comments