Skip to content

Commit

Permalink
Simplify mobile HttpClientHandler source files (#57766)
Browse files Browse the repository at this point in the history
They're 99% identical so we can just use one .cs file with ifdefs instead. This reduces the chance of missing one of the files when a new method is added.
  • Loading branch information
akoeplinger authored and Mitchell Hwang committed Sep 3, 2021
1 parent 3a1bd2b commit e478b66
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 417 deletions.
18 changes: 14 additions & 4 deletions src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
<PropertyGroup Condition="'$(TargetsAndroid)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetsMacCatalyst)' == 'true' or '$(TargetsTVOS)' == 'true'">
<DefineConstants>$(DefineConstants);TARGET_MOBILE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetsAndroid)' == 'true'">
<DefineConstants>$(DefineConstants);TARGET_ANDROID</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetsiOS)' == 'true'">
<DefineConstants>$(DefineConstants);TARGET_IOS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetsMacCatalyst)' == 'true'">
<DefineConstants>$(DefineConstants);TARGET_MACCATALYST</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetsTVOS)' == 'true'">
<DefineConstants>$(DefineConstants);TARGET_TVOS</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetsBrowser)' == 'true'">
<DefineConstants>$(DefineConstants);TARGET_BROWSER</DefineConstants>
</PropertyGroup>
Expand Down Expand Up @@ -41,10 +53,8 @@
<Compile Include="System\Net\Http\HttpClient.cs" />
<Compile Condition="'$(TargetsAndroid)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetsMacCatalyst)' == 'true' or '$(TargetsTVOS)' == 'true'"
Include="System\Net\Http\HttpClientHandler.AnyMobile.cs" />
<Compile Condition="'$(TargetsiOS)' == 'true'" Include="System\Net\Http\HttpClientHandler.iOS.cs" />
<Compile Condition="'$(TargetsMacCatalyst)' == 'true'" Include="System\Net\Http\HttpClientHandler.MacCatalyst.cs" />
<Compile Condition="'$(TargetsTVOS)' == 'true'" Include="System\Net\Http\HttpClientHandler.tvOS.cs" />
<Compile Condition="'$(TargetsAndroid)' == 'true'" Include="System\Net\Http\HttpClientHandler.Android.cs" />
<Compile Condition="'$(TargetsAndroid)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetsMacCatalyst)' == 'true' or '$(TargetsTVOS)' == 'true'"
Include="System\Net\Http\HttpClientHandler.AnyMobile.InvokeNativeHandler.cs" />
<Compile Condition="'$(TargetsAndroid)' != 'true' and '$(TargetsiOS)' != 'true' and '$(TargetsMacCatalyst)' != 'true' and '$(TargetsTVOS)' != 'true'"
Include="System\Net\Http\HttpClientHandler.cs" />
<Compile Include="System\Net\Http\HttpCompletionOption.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,25 @@ public partial class HttpClientHandler : HttpMessageHandler
{
private static MethodInfo? _nativeHandlerMethod;

#if TARGET_ANDROID
private const string NativeHandlerType = "Xamarin.Android.Net.AndroidMessageHandler";
private const string AssemblyName = "Mono.Android";
private const string GetHttpMessageHandlerType = "Android.Runtime.AndroidEnvironment, Mono.Android";
#elif TARGET_IOS
private const string NativeHandlerType = "System.Net.Http.NSUrlSessionHandler";
private const string AssemblyName = "Xamarin.iOS";
private const string GetHttpMessageHandlerType = "ObjCRuntime.RuntimeOptions, Xamarin.iOS";
#elif TARGET_MACCATALYST
private const string NativeHandlerType = "System.Net.Http.NSUrlSessionHandler";
private const string AssemblyName = "Xamarin.MacCatalyst";
private const string GetHttpMessageHandlerType = "ObjCRuntime.RuntimeOptions, Xamarin.MacCatalyst";
#elif TARGET_TVOS
private const string NativeHandlerType = "System.Net.Http.NSUrlSessionHandler";
private const string AssemblyName = "Xamarin.TVOS";
private const string GetHttpMessageHandlerType = "ObjCRuntime.RuntimeOptions, Xamarin.TVOS";
#else
#error Unknown target
#endif

[DynamicDependency("get_DefaultProxyCredentials", NativeHandlerType, AssemblyName)]
private ICredentials? GetDefaultProxyCredentials() => (ICredentials?)InvokeNativeHandlerMethod("get_DefaultProxyCredentials");
Expand Down Expand Up @@ -133,7 +150,7 @@ private HttpMessageHandler CreateNativeHandler()
{
if (_nativeHandlerMethod == null)
{
Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.MacCatalyst");
Type? runtimeOptions = Type.GetType(GetHttpMessageHandlerType);
_nativeHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
}

Expand Down
Loading

0 comments on commit e478b66

Please sign in to comment.