Skip to content

Commit

Permalink
[CoreFoundation] Make P/Invokes in CFNetwork have blittable signature…
Browse files Browse the repository at this point in the history
…s. (#20161)

Contributes towards #15684.
  • Loading branch information
rolfbjarne committed Feb 22, 2024
1 parent 70368f2 commit fe695d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 48 deletions.
77 changes: 32 additions & 45 deletions src/CoreFoundation/CFProxySupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

using System;
using System.Net;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -499,14 +500,17 @@ public string? ProxyAutoConfigURLString {
#endif
public static partial class CFNetwork {
[DllImport (Constants.CFNetworkLibrary)]
extern static /* CFArrayRef __nullable */ IntPtr CFNetworkCopyProxiesForAutoConfigurationScript (
unsafe extern static /* CFArrayRef __nullable */ IntPtr CFNetworkCopyProxiesForAutoConfigurationScript (
/* CFStringRef __nonnull */ IntPtr proxyAutoConfigurationScript,
/* CFURLRef __nonnull */ IntPtr targetURL, /* CFErrorRef __nullable * __nullable */ out IntPtr error);
/* CFURLRef __nonnull */ IntPtr targetURL, /* CFErrorRef __nullable * __nullable */ IntPtr* error);

static NSArray? CopyProxiesForAutoConfigurationScript (NSString proxyAutoConfigurationScript, NSUrl targetURL)
{
IntPtr err;
IntPtr native = CFNetworkCopyProxiesForAutoConfigurationScript (proxyAutoConfigurationScript.Handle, targetURL.Handle, out err);
IntPtr native;
unsafe {
native = CFNetworkCopyProxiesForAutoConfigurationScript (proxyAutoConfigurationScript.Handle, targetURL.Handle, &err);
}
return native == IntPtr.Zero ? null : new NSArray (native);
}

Expand Down Expand Up @@ -806,21 +810,16 @@ await Task.Run (() => {
}
}

#if NET
[DllImport (Constants.CFNetworkLibrary)]
extern unsafe static /* CFRunLoopSourceRef __nonnull */ IntPtr CFNetworkExecuteProxyAutoConfigurationScript (
/* CFStringRef __nonnull */ IntPtr proxyAutoConfigurationScript,
/* CFURLRef __nonnull */ IntPtr targetURL,
#if NET
/* CFProxyAutoConfigurationResultCallback __nonnull */ delegate* unmanaged<IntPtr, IntPtr, IntPtr, void> cb,
/* CFStreamClientContext * __nonnull */ ref CFStreamClientContext clientContext);
#else
[DllImport (Constants.CFNetworkLibrary)]
extern static /* CFRunLoopSourceRef __nonnull */ IntPtr CFNetworkExecuteProxyAutoConfigurationScript (
/* CFStringRef __nonnull */ IntPtr proxyAutoConfigurationScript,
/* CFURLRef __nonnull */ IntPtr targetURL,
/* CFProxyAutoConfigurationResultCallback __nonnull */ CFProxyAutoConfigurationResultCallbackInternal cb,
/* CFStreamClientContext * __nonnull */ ref CFStreamClientContext clientContext);
#endif
/* CFStreamClientContext * __nonnull */ CFStreamClientContext* clientContext);

public static CFProxy []? ExecuteProxyAutoConfigurationScript (string proxyAutoConfigurationScript, Uri targetUrl, out NSError? outError)
{
Expand All @@ -833,20 +832,17 @@ await Task.Run (() => {

using (var pacScript = new NSString (proxyAutoConfigurationScript))
using (var url = new NSUrl (targetUrl.AbsoluteUri)) {
#if NET
CreatePACCFRunLoopSource factory;
unsafe {
#if NET
factory = delegate (delegate* unmanaged<IntPtr, IntPtr, IntPtr, void> cb, ref CFStreamClientContext context)
#else
factory = delegate (CFProxyAutoConfigurationResultCallbackInternal cb, ref CFStreamClientContext context)
#endif
{
return CFNetworkExecuteProxyAutoConfigurationScript (pacScript.Handle, url.Handle, cb, ref context);
return CFNetworkExecuteProxyAutoConfigurationScript (pacScript.Handle, url.Handle, cb, (CFStreamClientContext*) Unsafe.AsPointer<CFStreamClientContext> (ref context));
};
}
#else
CreatePACCFRunLoopSource factory = delegate (CFProxyAutoConfigurationResultCallbackInternal cb, ref CFStreamClientContext context)
{
return CFNetworkExecuteProxyAutoConfigurationScript (pacScript.Handle, url.Handle, cb, ref context);
};
#endif
return ExecutePacCFRunLoopSourceBlocking (factory, out outError);
}
}
Expand All @@ -861,20 +857,17 @@ await Task.Run (() => {

using (var pacScript = new NSString (proxyAutoConfigurationScript))
using (var url = new NSUrl (targetUrl.AbsoluteUri)) {
#if NET
CreatePACCFRunLoopSource factory;
unsafe {
#if NET
factory = delegate (delegate* unmanaged<IntPtr, IntPtr, IntPtr, void> cb, ref CFStreamClientContext context)
#else
factory = delegate (CFProxyAutoConfigurationResultCallbackInternal cb, ref CFStreamClientContext context)
#endif
{
return CFNetworkExecuteProxyAutoConfigurationScript (pacScript.Handle, url.Handle, cb, ref context);
return CFNetworkExecuteProxyAutoConfigurationScript (pacScript.Handle, url.Handle, cb, (CFStreamClientContext*) Unsafe.AsPointer<CFStreamClientContext> (ref context));
};
}
#else
CreatePACCFRunLoopSource factory = delegate (CFProxyAutoConfigurationResultCallbackInternal cb, ref CFStreamClientContext context)
{
return CFNetworkExecuteProxyAutoConfigurationScript (pacScript.Handle, url.Handle, cb, ref context);
};
#endif
// use the helper task with a factory for this method
return await ExecutePacCFRunLoopSourceAsync (factory, cancellationToken).ConfigureAwait (false);
}
Expand All @@ -886,14 +879,14 @@ await Task.Run (() => {
/* CFURLRef __nonnull */ IntPtr proxyAutoConfigurationURL,
/* CFURLRef __nonnull */ IntPtr targetURL,
/* CFProxyAutoConfigurationResultCallback __nonnull */ delegate* unmanaged<IntPtr, IntPtr, IntPtr, void> cb,
/* CFStreamClientContext * __nonnull */ ref CFStreamClientContext clientContext);
/* CFStreamClientContext * __nonnull */ CFStreamClientContext* clientContext);
#else
[DllImport (Constants.CFNetworkLibrary)]
extern static /* CFRunLoopSourceRef __nonnull */ IntPtr CFNetworkExecuteProxyAutoConfigurationURL (
extern unsafe static /* CFRunLoopSourceRef __nonnull */ IntPtr CFNetworkExecuteProxyAutoConfigurationURL (
/* CFURLRef __nonnull */ IntPtr proxyAutoConfigurationURL,
/* CFURLRef __nonnull */ IntPtr targetURL,
/* CFProxyAutoConfigurationResultCallback __nonnull */ CFProxyAutoConfigurationResultCallbackInternal cb,
/* CFStreamClientContext * __nonnull */ ref CFStreamClientContext clientContext);
/* CFStreamClientContext * __nonnull */ CFStreamClientContext* clientContext);
#endif

public static CFProxy []? ExecuteProxyAutoConfigurationUrl (Uri proxyAutoConfigurationUrl, Uri targetUrl, out NSError? outError)
Expand All @@ -907,20 +900,17 @@ await Task.Run (() => {

using (var pacUrl = new NSUrl (proxyAutoConfigurationUrl.AbsoluteUri)) // toll free bridge to CFUrl
using (var url = new NSUrl (targetUrl.AbsoluteUri)) {
#if NET
CreatePACCFRunLoopSource factory;
unsafe {
#if NET
factory = delegate (delegate* unmanaged<IntPtr, IntPtr, IntPtr, void> cb, ref CFStreamClientContext context)
#else
factory = delegate (CFProxyAutoConfigurationResultCallbackInternal cb, ref CFStreamClientContext context)
#endif
{
return CFNetworkExecuteProxyAutoConfigurationURL (pacUrl.Handle, url.Handle, cb, ref context);
return CFNetworkExecuteProxyAutoConfigurationURL (pacUrl.Handle, url.Handle, cb, (CFStreamClientContext*) Unsafe.AsPointer<CFStreamClientContext> (ref context));
};
}
#else
CreatePACCFRunLoopSource factory = delegate (CFProxyAutoConfigurationResultCallbackInternal cb, ref CFStreamClientContext context)
{
return CFNetworkExecuteProxyAutoConfigurationURL (pacUrl.Handle, url.Handle, cb, ref context);
};
#endif
return ExecutePacCFRunLoopSourceBlocking (factory, out outError);
}
}
Expand All @@ -936,20 +926,17 @@ await Task.Run (() => {

using (var pacUrl = new NSUrl (proxyAutoConfigurationUrl.AbsoluteUri)) // toll free bridge to CFUrl
using (var url = new NSUrl (targetUrl.AbsoluteUri)) {
#if NET
CreatePACCFRunLoopSource factory;
unsafe {
#if NET
factory = delegate (delegate* unmanaged<IntPtr, IntPtr, IntPtr, void> cb, ref CFStreamClientContext context)
#else
factory = delegate (CFProxyAutoConfigurationResultCallbackInternal cb, ref CFStreamClientContext context)
#endif
{
return CFNetworkExecuteProxyAutoConfigurationURL (pacUrl.Handle, url.Handle, cb, ref context);
return CFNetworkExecuteProxyAutoConfigurationURL (pacUrl.Handle, url.Handle, cb, (CFStreamClientContext*) Unsafe.AsPointer<CFStreamClientContext> (ref context));
};
}
#else
CreatePACCFRunLoopSource factory = delegate (CFProxyAutoConfigurationResultCallbackInternal cb, ref CFStreamClientContext context)
{
return CFNetworkExecuteProxyAutoConfigurationURL (pacUrl.Handle, url.Handle, cb, ref context);
};
#endif
// use the helper task with a factory for this method
return await ExecutePacCFRunLoopSourceAsync (factory, cancellationToken).ConfigureAwait (false);
}
Expand Down
3 changes: 0 additions & 3 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,6 @@ public partial class BlittablePInvokes {
"System.Int32 Security.SslContext::SSLSetSessionTicketsEnabled(System.IntPtr,System.Boolean)",
"System.Int32 SystemConfiguration.NetworkReachability::SCNetworkReachabilityGetFlags(System.IntPtr,SystemConfiguration.NetworkReachabilityFlags&)",
"System.IntPtr AudioUnit.AudioComponent::AudioComponentFindNext(System.IntPtr,AudioUnit.AudioComponentDescription&)",
"System.IntPtr CoreFoundation.CFNetwork::CFNetworkCopyProxiesForAutoConfigurationScript(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.IntPtr CoreFoundation.CFNetwork::CFNetworkExecuteProxyAutoConfigurationScript(System.IntPtr,System.IntPtr,method System.Void *(System.IntPtr,System.IntPtr,System.IntPtr),CoreFoundation.CFStreamClientContext&)",
"System.IntPtr CoreFoundation.CFNetwork::CFNetworkExecuteProxyAutoConfigurationURL(System.IntPtr,System.IntPtr,method System.Void *(System.IntPtr,System.IntPtr,System.IntPtr),CoreFoundation.CFStreamClientContext&)",
"System.IntPtr CoreFoundation.CFPropertyList::CFPropertyListCreateData(System.IntPtr,System.IntPtr,System.IntPtr,System.UIntPtr,System.IntPtr&)",
"System.IntPtr CoreFoundation.CFPropertyList::CFPropertyListCreateWithData(System.IntPtr,System.IntPtr,System.UIntPtr,System.IntPtr&,System.IntPtr&)",
"System.IntPtr CoreFoundation.CFRunLoopSourceCustom::CFRunLoopSourceCreate(System.IntPtr,System.IntPtr,CoreFoundation.CFRunLoopSourceContext&)",
Expand Down

8 comments on commit fe695d3

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.