Skip to content

Commit e2d1a5e

Browse files
authored
[Foundation] Improve and simplify the manually bound constructors for NSHttpCookie slightly. (#22740)
Improve and simplify the manually bound constructors for NSHttpCookie by using the same pattern we use elsewhere for manually bound constructors.
1 parent ebe3cc1 commit e2d1a5e

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

src/Foundation/NSHttpCookie.cs

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,67 +23,68 @@
2323
using System;
2424
using System.Net;
2525

26-
// Disable until we get around to enable + fix any issues.
27-
#nullable disable
26+
#nullable enable
2827

2928
namespace Foundation {
3029
public partial class NSHttpCookie {
3130
// same order as System.Net.Cookie
3231
// http://msdn.microsoft.com/en-us/library/a18ka3h2.aspx
32+
/// <summary>Create a new cookie with the supplied name and value.</summary>
3333
/// <param name="name">Cookie's name. Cannot be null.</param>
34-
/// <param name="value">Cookie's value. Cannot be null.</param>
35-
/// <summary>Create a new cookie with the supplied name and value.</summary>
36-
/// <remarks>A default Path and Domain will be used to ensure a valid instance is created.</remarks>
37-
public NSHttpCookie (string name, string value) : this (name, value, null, null)
34+
/// <param name="value">Cookie's value. Cannot be null.</param>
35+
/// <remarks>A default Path and Domain will be used to ensure a valid instance is created.</remarks>
36+
public NSHttpCookie (string name, string value)
37+
: this (name, value, null, null)
3838
{
39-
CreateCookie (name, value, null, null, null, null, null, null, null, null, null, null);
4039
}
4140

41+
/// <summary>Create a new cookie with the supplied name, value and path.</summary>
4242
/// <param name="name">Cookie's name. Cannot be null.</param>
43-
/// <param name="value">Cookie's value. Cannot be null.</param>
44-
/// <param name="path">Path where the cookie will be applied on the domain. Using "/" will send the cookie to every URL on the domain.</param>
45-
/// <summary>Create a new cookie with the supplied name, value and path.</summary>
46-
/// <remarks>A default Domain will be used to ensure a valid instance is created</remarks>
47-
public NSHttpCookie (string name, string value, string path) : this (name, value, path, null)
43+
/// <param name="value">Cookie's value. Cannot be null.</param>
44+
/// <param name="path">Path where the cookie will be applied on the domain. Using "/" will send the cookie to every URL on the domain.</param>
45+
/// <remarks>A default Domain will be used to ensure a valid instance is created</remarks>
46+
public NSHttpCookie (string name, string value, string? path)
47+
: this (name, value, path, null)
4848
{
49-
CreateCookie (name, value, path, null, null, null, null, null, null, null, null, null);
5049
}
5150

51+
/// <summary>Create a new cookie with the supplied name, value, path and domain.</summary>
5252
/// <param name="name">Cookie's name. Cannot be null.</param>
53-
/// <param name="value">Cookie's value. Cannot be null.</param>
54-
/// <param name="path">Path where the cookie will be applied on the domain. Using "/" will send the cookie to every URL on the domain.</param>
55-
/// <param name="domain">Domain (e.g. xamarin.com) related to the cookie</param>
56-
/// <summary>Create a new cookie with the supplied name, value, path and domain.</summary>
57-
/// <remarks>An ArgumentNullException will be thrown if either `name` or `value` are null.</remarks>
58-
public NSHttpCookie (string name, string value, string path, string domain)
53+
/// <param name="value">Cookie's value. Cannot be null.</param>
54+
/// <param name="path">Path where the cookie will be applied on the domain. Using "/" will send the cookie to every URL on the domain.</param>
55+
/// <param name="domain">Domain (e.g. microsoft.com) related to the cookie</param>
56+
/// <remarks>An <see cref="ArgumentNullException" /> will be thrown if either <paramref name="name" /> or <paramref name="value" /> are null.</remarks>
57+
public NSHttpCookie (string name, string value, string? path, string? domain)
58+
: base (NSObjectFlag.Empty)
5959
{
6060
CreateCookie (name, value, path, domain, null, null, null, null, null, null, null, null);
6161
}
6262

6363
// FIXME: should we expose more complex/long ctor or point people to use a Cookie ?
6464

65-
/// <param name="cookie">An existing Cookie from the .NET framework</param>
66-
/// <summary>Create a new cookie from the supplied System.Net.Cookie instance properties</summary>
67-
/// <remarks>This constructor will throw an ArgumentNullException if `cookie` is null</remarks>
65+
/// <summary>Create a new cookie from the supplied <see cref="System.Net.Cookie" /> instance's properties.</summary>
66+
/// <param name="cookie">An existing <see cref="System.Net.Cookie" /> from the .NET framework</param>
67+
/// <remarks>This constructor will throw an <see cref="ArgumentNullException" /> if <paramref name="cookie" /> is null</remarks>
6868
public NSHttpCookie (Cookie cookie)
69+
: base (NSObjectFlag.Empty)
6970
{
7071
if (cookie is null)
71-
throw new ArgumentNullException ("cookie");
72+
throw new ArgumentNullException (nameof (cookie));
7273

73-
string commentUrl = cookie.CommentUri is not null ? cookie.CommentUri.ToString () : null;
74+
var commentUrl = cookie.CommentUri?.ToString ();
7475
bool? discard = null;
7576
if (cookie.Discard)
7677
discard = true;
7778
CreateCookie (cookie.Name, cookie.Value, cookie.Path, cookie.Domain, cookie.Comment, commentUrl, discard, cookie.Expires, null, cookie.Port, cookie.Secure, cookie.Version);
7879
}
7980

80-
void CreateCookie (string name, string value, string path, string domain, string comment, string commentUrl, bool? discard, DateTime? expires, int? maximumAge, string ports, bool? secure, int? version)
81+
void CreateCookie (string name, string value, string? path, string? domain, string? comment, string? commentUrl, bool? discard, DateTime? expires, int? maximumAge, string? ports, bool? secure, int? version)
8182
{
8283
// mandatory checks or defaults
8384
if (name is null)
84-
throw new ArgumentNullException ("name");
85+
throw new ArgumentNullException (nameof (name));
8586
if (value is null)
86-
throw new ArgumentNullException ("value");
87+
throw new ArgumentNullException (nameof (value));
8788
if (String.IsNullOrEmpty (path))
8889
path = "/"; // default in .net
8990
if (String.IsNullOrEmpty (domain))
@@ -115,10 +116,16 @@ void CreateCookie (string name, string value, string path, string domain, string
115116
if (version.HasValue)
116117
properties.Add (NSHttpCookie.KeyVersion, new NSString (version.Value.ToString ()));
117118

119+
#if NET10_0_OR_GREATER
120+
var throwOnInitFailure = true;
121+
#else
122+
var throwOnInitFailure = false;
123+
#endif
124+
118125
if (IsDirectBinding) {
119-
Handle = Messaging.IntPtr_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle ("initWithProperties:"), properties.Handle);
126+
InitializeHandle (Messaging.IntPtr_objc_msgSend_IntPtr (this.Handle, Selector.GetHandle ("initWithProperties:"), properties.Handle), "initWithProperties:", throwOnInitFailure);
120127
} else {
121-
Handle = Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithProperties:"), properties.Handle);
128+
InitializeHandle (Messaging.IntPtr_objc_msgSendSuper_IntPtr (this.SuperHandle, Selector.GetHandle ("initWithProperties:"), properties.Handle), "initWithProperties:", throwOnInitFailure);
122129
}
123130
}
124131
}

tests/cecil-tests/ConstructorTest.KnownFailures.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public partial class ConstructorTest {
1616
"Foundation.NSAppleEventDescriptor::.ctor(Foundation.NSAppleEventDescriptorType)",
1717
"Foundation.NSAsyncActionDispatcher::.ctor(System.Action)",
1818
"Foundation.NSAsyncSynchronizationContextDispatcher::.ctor(System.Threading.SendOrPostCallback,System.Object)",
19-
"Foundation.NSHttpCookie::.ctor(System.Net.Cookie)",
20-
"Foundation.NSHttpCookie::.ctor(System.String,System.String,System.String,System.String)",
2119
"Foundation.NSMutableArray`1::.ctor(TValue[])",
2220
"Foundation.NSObject::.ctor(Foundation.NSObjectFlag)",
2321
"Foundation.NSObject::.ctor(ObjCRuntime.NativeHandle,System.Boolean)",

tests/cecil-tests/SetHandleTest.KnownFailures.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public partial class SetHandleTest {
1313
"CoreFoundation.DispatchBlock::Retain()",
1414
"CoreFoundation.OSLog::.ctor(System.String,System.String)",
1515
"CoreGraphics.CGPattern::.ctor(CoreGraphics.CGRect,CoreGraphics.CGAffineTransform,System.Runtime.InteropServices.NFloat,System.Runtime.InteropServices.NFloat,CoreGraphics.CGPatternTiling,System.Boolean,CoreGraphics.CGPattern/DrawPattern)",
16-
"Foundation.NSHttpCookie::CreateCookie(System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable`1<System.Boolean>,System.Nullable`1<System.DateTime>,System.Nullable`1<System.Int32>,System.String,System.Nullable`1<System.Boolean>,System.Nullable`1<System.Int32>)",
1716
"Foundation.NSKeyedUnarchiver::.ctor(Foundation.NSData)",
1817
"Foundation.NSUuid::.ctor(System.Byte[])",
1918
"GameplayKit.GKPath::.ctor(System.Numerics.Vector2[],System.Single,System.Boolean)",

tests/monotouch-test/Foundation/CookieTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,13 @@ public void DotNetInteropMin ()
109109
{
110110
// an invalid NSDictionary returns null from Objective-C but that
111111
// results in an 'empty' instance inside MonoTouch
112+
#if NET10_0_OR_GREATER
113+
Assert.Throws<Exception> (() => { new NSHttpCookie (new Cookie ()); }, "Exception");
114+
#else
112115
using (var cookie = new NSHttpCookie (new Cookie ())) {
113116
Assert.That (cookie.Handle, Is.EqualTo (NativeHandle.Zero), "ctor");
114117
}
118+
#endif
115119
}
116120

117121
[Test]

0 commit comments

Comments
 (0)