Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add User agent modes #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions HybridWebView/Droid/HybridWebViewRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand All @@ -22,6 +22,8 @@ namespace Plugin.HybridWebView.Droid
/// </summary>
public class HybridWebViewRenderer : ViewRenderer<HybridWebViewControl, Android.Webkit.WebView>
{
private static string defaultUserAgent;

public static string MimeType = "text/html";

public static string EncodingType = "UTF-8";
Expand Down Expand Up @@ -133,6 +135,7 @@ private void SetupControl()

HybridWebViewControl.CallbackAdded += OnCallbackAdded;
SetNativeControl(webView);
defaultUserAgent = Control.Settings.UserAgentString;
SetUserAgent();
OnControlChanged?.Invoke(this, webView);
}
Expand Down Expand Up @@ -200,7 +203,7 @@ private Task OnClearCookiesRequest()
cookieSyncMngr.StopSync();
cookieSyncMngr.Sync();
}

return Task.CompletedTask;
}

Expand Down Expand Up @@ -461,7 +464,18 @@ private void SetUserAgent(object sender = null, EventArgs e = null)
{
if (Control != null && Element.UserAgent != null && Element.UserAgent.Length > 0)
{
Control.Settings.UserAgentString = Element.UserAgent;
switch (Element.UserAgentMode)
{
case UserAgentMode.Replace:
Control.Settings.UserAgentString = Element.UserAgent;
break;
case UserAgentMode.Append:
Control.Settings.UserAgentString = $"{defaultUserAgent} {Element.UserAgent}";
break;
case UserAgentMode.Prepend:
Control.Settings.UserAgentString = $"{Element.UserAgent} {defaultUserAgent}";
break;
}
}
}
}
Expand Down
30 changes: 21 additions & 9 deletions HybridWebView/HybridWebView.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<!--Work around so the conditions work below-->
<TargetFrameworks></TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid81;uap10.0.18362;Xamarin.Mac20</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid81;uap10.0.18362;net6.0-windows10.0.19041;Xamarin.Mac20</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid81;Xamarin.Mac20</TargetFrameworks>
<!--Feel free to add as many targets as you need below
netstandard1.0;netstandard2.0;MonoAndroid81;Xamarin.iOS10;uap10.0.16299;Xamarin.TVOS10;Xamarin.WatchOS10;Xamarin.Mac20;Tizen40
Expand All @@ -12,7 +12,7 @@
<AssemblyName>Plugin.HybridWebView</AssemblyName>
<RootNamespace>Plugin.HybridWebView</RootNamespace>
<PackageId>Plugin.HybridWebView</PackageId>

<Product>$(AssemblyName) ($(TargetFramework))</Product>
<AssemblyVersion>1.0.0.2</AssemblyVersion>
<AssemblyFileVersion>1.0.0.2</AssemblyFileVersion>
Expand All @@ -22,26 +22,26 @@
<NeutralLanguage>en</NeutralLanguage>
<LangVersion>default</LangVersion>
<DefineConstants>$(DefineConstants);</DefineConstants>

<UseFullSemVerForNuGet>false</UseFullSemVerForNuGet>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>

<LangVersion>latest</LangVersion>
<DebugType>portable</DebugType>

<!--TODO: Fill these in-->
<license>https://github.com/devsmadeofsteel/Plugin.HybridWebView/blob/master/LICENCE.md</license>
<PackageProjectUrl>https://github.com/devsmadeofsteel/Plugin.HybridWebView</PackageProjectUrl>
<RepositoryUrl>https://github.com/devsmadeofsteel/Plugin.HybridWebView</RepositoryUrl>
<PackageReleaseNotes>RELEASE NOTES</PackageReleaseNotes>
<PackageIconUrl>ICON URL</PackageIconUrl>
<PackageTags>xamarin, windows, ios, android, xamarin.forms, plugin, HybridWebView, webview</PackageTags>

<Title>HybridWebView Plugin for Xamarin and Windows</Title>
<Summary>Xamarin Plugin for a HybridWebView</Summary>
<Description>Lightweight cross platform WebView designed to leverage the native WebView components in Android, iOS, and Windows to provide enhanced functionality over the base control.</Description>


<Owners>Vinicius Dutra</Owners>
<Authors>Ryan Dixon, Vinicius Dutra</Authors>
<Copyright>Copyright 2018</Copyright>
Expand Down Expand Up @@ -76,6 +76,18 @@
-->
</ItemGroup>

<PropertyGroup Condition=" $(TargetFramework.Contains('-windows10')) ">
<UseMaui>true</UseMaui>
<ImplicitUsings>enable</ImplicitUsings>

<SupportedOSPlatformVersion>10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>
<ItemGroup Condition=" $(TargetFramework.Contains('-windows10')) ">
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0" />
<Compile Include="Windows\**\*.cs" />
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('MonoAndroid')) ">
<Compile Include="Droid\**\*.cs" />
</ItemGroup>
Expand Down Expand Up @@ -103,11 +115,11 @@




<ItemGroup Condition=" $(TargetFramework.StartsWith('netcoreapp')) ">
<Compile Include="**\*.netcore.cs" />
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('Tizen')) ">
<Compile Include="**\*.tizen.cs" />
<PackageReference Include="Tizen.NET" Version="4.0.0" />
Expand Down
13 changes: 13 additions & 0 deletions HybridWebView/Shared/Enumerations/UserAgentMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Plugin.HybridWebView.Shared.Enumerations
{
public enum UserAgentMode
{
Replace = 0,
Append = 1,
Prepend = 2,
}
}
18 changes: 17 additions & 1 deletion HybridWebView/Shared/HybridWebViewControl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
Expand Down Expand Up @@ -201,6 +201,22 @@ public string UserAgent
}
}

/// <summary>
/// Bindable property to control the user agent mode
/// </summary>
public UserAgentMode UserAgentMode
{
get => (UserAgentMode)GetValue(UserAgentModeProperty);
set
{
if (UserAgentMode == value)
return;

SetValue(UserAgentModeProperty, value);
OnUserAgentChanged?.Invoke(this, EventArgs.Empty);
}
}

public HybridWebViewControl()
{
HorizontalOptions = VerticalOptions = LayoutOptions.FillAndExpand;
Expand Down
8 changes: 7 additions & 1 deletion HybridWebView/Shared/HybridWebViewControl.static.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using Plugin.HybridWebView.Shared.Enumerations;
using Xamarin.Forms;
Expand Down Expand Up @@ -76,6 +76,12 @@ public partial class HybridWebViewControl
public static readonly BindableProperty UserAgentProperty =
BindableProperty.Create(nameof(UserAgent), typeof(string), typeof(HybridWebViewControl), "");

/// <summary>
/// A bindable property for the UserAgentMode property.
/// </summary>
public static readonly BindableProperty UserAgentModeProperty =
BindableProperty.Create(nameof(UserAgentMode), typeof(UserAgentMode), typeof(HybridWebViewControl), Shared.Enumerations.UserAgentMode.Replace);

/// <summary>
/// A dictionary used to add headers which are used throughout all instances of FormsWebView.
/// </summary>
Expand Down
21 changes: 20 additions & 1 deletion HybridWebView/UWP/HybridWebViewRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Plugin.HybridWebView.Shared;
using Plugin.HybridWebView.Shared;
using Plugin.HybridWebView.Shared.Enumerations;
using Plugin.HybridWebView.UWP;
using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Windows.Security.Cryptography.Certificates;
using Windows.UI.Xaml.Controls;
Expand All @@ -26,6 +28,9 @@ public class HybridWebViewRenderer : ViewRenderer<Shared.HybridWebViewControl, W
public static string BaseUrl { get; set; } = "ms-appx:///";
private LocalFileStreamResolver _resolver;

[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
private static extern int UrlMkGetSessionOption(int dwOption, StringBuilder pBuffer, int dwBufferLength, out int pdwBufferLength, int dwReserved);

public static void Initialize()
{
// ReSharper disable once UnusedVariable
Expand Down Expand Up @@ -154,6 +159,20 @@ private void NavigateWithCustomUserAgent(WebViewNavigationStartingEventArgs args
{
// Create new request with custom user agent
var requestMsg = new Windows.Web.Http.HttpRequestMessage(HttpMethod.Get, args.Uri);
if (Element.UserAgentMode != UserAgentMode.Replace)
{
var userAgentBuilder = new StringBuilder(256);
UrlMkGetSessionOption(0x10000001, userAgentBuilder, userAgentBuilder.Capacity - 1, out var length, 0);
switch (Element.UserAgentMode)
{
case UserAgentMode.Append:
userAgent = $"{userAgentBuilder} {Element.UserAgent}";
break;
case UserAgentMode.Prepend:
userAgent = $"{Element.UserAgent} {userAgentBuilder}";
break;
}
}
requestMsg.Headers.Add("User-Agent", userAgent);
Control.NavigateWithHttpRequestMessage(requestMsg);
}
Expand Down
Loading