From 2b5e91a7566c78476681f4b62f240b698641ce35 Mon Sep 17 00:00:00 2001 From: Christoph Fink Date: Fri, 31 Dec 2021 13:00:23 +0100 Subject: [PATCH 1/3] add WinUI support --- HybridWebView/HybridWebView.csproj | 30 +- .../Windows/HybridWebViewRenderer.cs | 339 ++++++++++++++++++ 2 files changed, 360 insertions(+), 9 deletions(-) create mode 100644 HybridWebView/Windows/HybridWebViewRenderer.cs diff --git a/HybridWebView/HybridWebView.csproj b/HybridWebView/HybridWebView.csproj index 4ac6b0b..2a7d698 100644 --- a/HybridWebView/HybridWebView.csproj +++ b/HybridWebView/HybridWebView.csproj @@ -3,7 +3,7 @@ - netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid81;uap10.0.18362;Xamarin.Mac20 + netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid81;uap10.0.18362;net6.0-windows10.0.19041;Xamarin.Mac20 netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid81;Xamarin.Mac20 https://github.com/devsmadeofsteel/Plugin.HybridWebView/blob/master/LICENCE.md https://github.com/devsmadeofsteel/Plugin.HybridWebView @@ -36,12 +36,12 @@ RELEASE NOTES ICON URL xamarin, windows, ios, android, xamarin.forms, plugin, HybridWebView, webview - + HybridWebView Plugin for Xamarin and Windows Xamarin Plugin for a HybridWebView Lightweight cross platform WebView designed to leverage the native WebView components in Android, iOS, and Windows to provide enhanced functionality over the base control. - - + + Vinicius Dutra Ryan Dixon, Vinicius Dutra Copyright 2018 @@ -76,6 +76,18 @@ --> + + true + enable + + 10.0.17763.0 + 10.0.17763.0 + + + + + + @@ -103,11 +115,11 @@ - + - + diff --git a/HybridWebView/Windows/HybridWebViewRenderer.cs b/HybridWebView/Windows/HybridWebViewRenderer.cs new file mode 100644 index 0000000..2401d59 --- /dev/null +++ b/HybridWebView/Windows/HybridWebViewRenderer.cs @@ -0,0 +1,339 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Maui.Controls.Compatibility.Platform.UWP; +using Microsoft.Maui.Controls.Platform; +using Plugin.HybridWebView.Shared; +using Plugin.HybridWebView.Shared.Enumerations; +using Windows.Security.Cryptography.Certificates; +using Windows.Web.Http; +using Windows.Web.Http.Filters; +using Microsoft.UI.Xaml.Controls; +using Microsoft.Web.WebView2.Core; + +namespace Plugin.HybridWebView.Windows +{ + /// + /// Interface for HybridWebView + /// + public class HybridWebViewRenderer : ViewRenderer + { + public static event EventHandler OnControlChanged; + + public static string BaseUrl { get; set; } = "ms-appx:///"; + + public static void Initialize() + { + // ReSharper disable once UnusedVariable + var dt = DateTime.Now; + } + + protected override async void OnElementChanged(ElementChangedEventArgs e) + { + base.OnElementChanged(e); + + if (Control == null && Element != null) + await SetupControlAsync(); + + if (e.NewElement != null) + SetupNewElement(e.NewElement); + + if (e.OldElement != null) + DestroyOldElement(e.OldElement); + } + + private void SetupNewElement(HybridWebViewControl element) + { + element.PropertyChanged += OnWebViewPropertyChanged; + element.OnJavascriptInjectionRequest += OnJavascriptInjectionRequestAsync; + element.OnClearCookiesRequested += OnClearCookiesRequest; + element.OnGetAllCookiesRequestedAsync += OnGetAllCookieRequestAsync; + element.OnGetCookieRequestedAsync += OnGetCookieRequestAsync; + element.OnSetCookieRequestedAsync += OnSetCookieRequestAsync; + element.OnBackRequested += OnBackRequested; + element.OnForwardRequested += OnForwardRequested; + element.OnRefreshRequested += OnRefreshRequested; + element.OnUserAgentChanged += SetUserAgent; + + SetSource(); + } + + private void DestroyOldElement(HybridWebViewControl element) + { + element.PropertyChanged -= OnWebViewPropertyChanged; + element.OnJavascriptInjectionRequest -= OnJavascriptInjectionRequestAsync; + element.OnClearCookiesRequested -= OnClearCookiesRequest; + element.OnBackRequested -= OnBackRequested; + element.OnGetAllCookiesRequestedAsync -= OnGetAllCookieRequestAsync; + element.OnGetCookieRequestedAsync -= OnGetCookieRequestAsync; + element.OnSetCookieRequestedAsync -= OnSetCookieRequestAsync; + element.OnForwardRequested -= OnForwardRequested; + element.OnRefreshRequested -= OnRefreshRequested; + element.OnUserAgentChanged -= SetUserAgent; + + element.Dispose(); + } + + private async Task SetupControlAsync() + { + var control = new WebView2(); + + SetNativeControl(control); + + HybridWebViewControl.CallbackAdded += OnCallbackAdded; + Control.NavigationStarting += OnNavigationStarting; + Control.NavigationCompleted += OnNavigationCompleted; + Control.WebMessageReceived += OnWebMessageReceived; + Control.DefaultBackgroundColor = Microsoft.UI.Colors.Transparent; + + await Control.EnsureCoreWebView2Async(); + Control.CoreWebView2.WebResourceRequested += OnWebResourceRequested; + Control.CoreWebView2.DOMContentLoaded += OnDOMContentLoaded; + Control.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All); + SetUserAgent(); + + OnControlChanged?.Invoke(this, control); + } + + private void OnRefreshRequested(object sender, EventArgs e) => SetSource(); + + private void OnForwardRequested(object sender, EventArgs e) + { + if (Control == null) + return; + + if (Control.CanGoForward) + Control.GoForward(); + } + + private void OnBackRequested(object sender, EventArgs e) + { + if (Control == null) + return; + + if (Control.CanGoBack) + Control.GoBack(); + } + + private void OnWebViewPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) + { + switch (e.PropertyName) + { + case nameof(HybridWebViewControl.Source): + SetSource(); + break; + } + } + + private void OnNavigationStarting(WebView2 sender, CoreWebView2NavigationStartingEventArgs args) + { + if (Element == null) + return; + + Element.Navigating = true; + var handler = Element.HandleNavigationStartRequest(args.Uri ?? Element.Source); + args.Cancel = handler.Cancel; + + Device.BeginInvokeOnMainThread(() => Element.CurrentUrl = args.Uri); + } + + private void OnWebResourceRequested(CoreWebView2 sender, CoreWebView2WebResourceRequestedEventArgs args) + { + // Add Local Headers + foreach (var header in Element.LocalRegisteredHeaders) + { + if (!args.Request.Headers.Contains(header.Key)) + args.Request.Headers.SetHeader(header.Key, header.Value); + } + + // Add Global Headers + if (Element.EnableGlobalHeaders) + { + foreach (var header in HybridWebViewControl.GlobalRegisteredHeaders) + { + if (!args.Request.Headers.Contains(header.Key)) + args.Request.Headers.SetHeader(header.Key, header.Value); + } + } + } + + private void OnNavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args) + { + if (Element == null) + return; + + if (!args.IsSuccess) + Element.HandleNavigationError((int)args.WebErrorStatus); + + Element.CanGoBack = Control.CanGoBack; + Element.CanGoForward = Control.CanGoForward; + + Element.Navigating = false; + Element.HandleNavigationCompleted(Control.Source.ToString()); + } + + private async void OnDOMContentLoaded(CoreWebView2 sender, CoreWebView2DOMContentLoadedEventArgs args) + { + // Add Injection Function + await Control.ExecuteScriptAsync(HybridWebViewControl.InjectedFunction); + + // Add Global Callbacks + if (Element.EnableGlobalCallbacks) + { + foreach (var callback in HybridWebViewControl.GlobalRegisteredCallbacks) + await Control.ExecuteScriptAsync(HybridWebViewControl.GenerateFunctionScript(callback.Key)); + } + + // Add Local Callbacks + foreach (var callback in Element.LocalRegisteredCallbacks) + await Control.ExecuteScriptAsync(HybridWebViewControl.GenerateFunctionScript(callback.Key)); + + Element.HandleContentLoaded(); + } + + private async void OnCallbackAdded(object sender, string e) + { + if (Element == null || String.IsNullOrWhiteSpace(e)) + return; + + if ((sender == null && Element.EnableGlobalCallbacks) || sender != null) + await OnJavascriptInjectionRequestAsync(HybridWebViewControl.GenerateFunctionScript(e)); + } + + + private void OnWebMessageReceived(WebView2 sender, CoreWebView2WebMessageReceivedEventArgs args) + { + if (Element == null) + return; + Element.HandleScriptReceived(args.TryGetWebMessageAsString()); + } + + private Task OnClearCookiesRequest() + { + if (Control == null || Element == null) + return Task.CompletedTask; + + var url = new Uri(Element.Source); + var filter = new HttpBaseProtocolFilter(); + var cookieManager = filter.CookieManager; + var cookieCollection = cookieManager.GetCookies(url); + + foreach (var currentCookie in cookieCollection) + cookieManager.DeleteCookie(currentCookie); + + return Task.CompletedTask; + } + + private Task OnGetAllCookieRequestAsync() + { + if (Control == null || Element == null) + return Task.FromResult(String.Empty); + var domain = (new Uri(Element.Source)).Host; + var cookie = String.Empty; + var url = new Uri(Element.Source); + + var filter = new HttpBaseProtocolFilter(); + var cookieManager = filter.CookieManager; + var cookieCollection = cookieManager.GetCookies(url); + + foreach (var currentCookie in cookieCollection) + { + cookie += currentCookie.Name + "=" + currentCookie.Value + "; "; + } + + if (cookie.Length > 2) + { + cookie = cookie.Remove(cookie.Length - 2); + } + + return Task.FromResult(cookie); + } + + private Task OnGetCookieRequestAsync(string key) + { + if (Control == null || Element == null) + return Task.FromResult(String.Empty); + var url = new Uri(Element.Source); + var cookie = String.Empty; + + var filter = new HttpBaseProtocolFilter(); + var cookieManager = filter.CookieManager; + var cookieCollection = cookieManager.GetCookies(url); + + foreach (var currentCookie in cookieCollection) + { + if (key == currentCookie.Name) + { + cookie = currentCookie.Value; + break; + } + } + + return Task.FromResult(cookie); + } + + private Task OnSetCookieRequestAsync(Cookie cookie) + { + if (Control == null || Element == null) + return Task.FromResult(String.Empty); + var newCookie = new HttpCookie(cookie.Name, cookie.Domain, cookie.Path) { Value = cookie.Value, HttpOnly = cookie.HttpOnly, Secure = cookie.Secure, Expires = cookie.Expires }; + + var filter = new HttpBaseProtocolFilter(); + filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted); + filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired); + filter.CookieManager.SetCookie(newCookie); + return OnGetCookieRequestAsync(cookie.Name); + + } + + private async Task OnJavascriptInjectionRequestAsync(string js) + { + if (Control == null) + return String.Empty; + var result = await Control.ExecuteScriptAsync(js); + return result; + } + + private void SetSource() + { + if (Element == null || Control == null || String.IsNullOrWhiteSpace(Element.Source)) + return; + + switch (Element.ContentType) + { + case WebViewContentType.Internet: + LoadUrl(Element.Source); + break; + case WebViewContentType.StringData: + LoadStringData(Element.Source); + break; + case WebViewContentType.LocalFile: + LoadUrl(Element.Source); + break; + } + } + + private void LoadUrl(string url) + { + var uri = new Uri(url, UriKind.RelativeOrAbsolute); + if (!uri.IsAbsoluteUri) + uri = new Uri((Element?.BaseUrl ?? BaseUrl) + url, UriKind.RelativeOrAbsolute); + + Control.Source = uri; + } + + private void LoadStringData(string source) => Control.NavigateToString(source); + + private void SetUserAgent(object sender = null, EventArgs e = null) + { + if (Control != null && Element.UserAgent != null && Element.UserAgent.Length > 0) + { + Control.CoreWebView2.Settings.UserAgent = Element.UserAgent; + } + } + } +} \ No newline at end of file From 24360f000ba7b1507966a0e293442a497ea53889 Mon Sep 17 00:00:00 2001 From: Christoph Fink Date: Fri, 31 Dec 2021 13:25:29 +0100 Subject: [PATCH 2/3] add UserAgentMode --- HybridWebView/Droid/HybridWebViewRenderer.cs | 20 +++++++++++++++--- .../Shared/Enumerations/UserAgentMode.cs | 13 ++++++++++++ HybridWebView/Shared/HybridWebViewControl.cs | 18 +++++++++++++++- .../Shared/HybridWebViewControl.static.cs | 8 ++++++- HybridWebView/UWP/HybridWebViewRenderer.cs | 21 ++++++++++++++++++- .../Windows/HybridWebViewRenderer.cs | 16 +++++++++++++- HybridWebView/iOS/HybridWebViewRenderer.cs | 18 ++++++++++++++-- HybridWebView/macOS/HybridWebViewRenderer.cs | 18 ++++++++++++++-- 8 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 HybridWebView/Shared/Enumerations/UserAgentMode.cs diff --git a/HybridWebView/Droid/HybridWebViewRenderer.cs b/HybridWebView/Droid/HybridWebViewRenderer.cs index 56ae1a6..25a2dc8 100644 --- a/HybridWebView/Droid/HybridWebViewRenderer.cs +++ b/HybridWebView/Droid/HybridWebViewRenderer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -22,6 +22,8 @@ namespace Plugin.HybridWebView.Droid /// public class HybridWebViewRenderer : ViewRenderer { + private static string defaultUserAgent; + public static string MimeType = "text/html"; public static string EncodingType = "UTF-8"; @@ -133,6 +135,7 @@ private void SetupControl() HybridWebViewControl.CallbackAdded += OnCallbackAdded; SetNativeControl(webView); + defaultUserAgent = Control.Settings.UserAgentString; SetUserAgent(); OnControlChanged?.Invoke(this, webView); } @@ -200,7 +203,7 @@ private Task OnClearCookiesRequest() cookieSyncMngr.StopSync(); cookieSyncMngr.Sync(); } - + return Task.CompletedTask; } @@ -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; + } } } } diff --git a/HybridWebView/Shared/Enumerations/UserAgentMode.cs b/HybridWebView/Shared/Enumerations/UserAgentMode.cs new file mode 100644 index 0000000..be91913 --- /dev/null +++ b/HybridWebView/Shared/Enumerations/UserAgentMode.cs @@ -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, + } +} diff --git a/HybridWebView/Shared/HybridWebViewControl.cs b/HybridWebView/Shared/HybridWebViewControl.cs index f16e466..7aa84b4 100644 --- a/HybridWebView/Shared/HybridWebViewControl.cs +++ b/HybridWebView/Shared/HybridWebViewControl.cs @@ -1,4 +1,4 @@ -using Newtonsoft.Json; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net; @@ -201,6 +201,22 @@ public string UserAgent } } + /// + /// Bindable property to control the user agent mode + /// + 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; diff --git a/HybridWebView/Shared/HybridWebViewControl.static.cs b/HybridWebView/Shared/HybridWebViewControl.static.cs index 20404f0..1c090e8 100644 --- a/HybridWebView/Shared/HybridWebViewControl.static.cs +++ b/HybridWebView/Shared/HybridWebViewControl.static.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using Plugin.HybridWebView.Shared.Enumerations; using Xamarin.Forms; @@ -76,6 +76,12 @@ public partial class HybridWebViewControl public static readonly BindableProperty UserAgentProperty = BindableProperty.Create(nameof(UserAgent), typeof(string), typeof(HybridWebViewControl), ""); + /// + /// A bindable property for the UserAgentMode property. + /// + public static readonly BindableProperty UserAgentModeProperty = + BindableProperty.Create(nameof(UserAgentMode), typeof(UserAgentMode), typeof(HybridWebViewControl), Shared.Enumerations.UserAgentMode.Replace); + /// /// A dictionary used to add headers which are used throughout all instances of FormsWebView. /// diff --git a/HybridWebView/UWP/HybridWebViewRenderer.cs b/HybridWebView/UWP/HybridWebViewRenderer.cs index f327198..7c43792 100644 --- a/HybridWebView/UWP/HybridWebViewRenderer.cs +++ b/HybridWebView/UWP/HybridWebViewRenderer.cs @@ -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; @@ -26,6 +28,9 @@ public class HybridWebViewRenderer : ViewRenderer public class HybridWebViewRenderer : ViewRenderer { + private static string defaultUserAgent; + public static event EventHandler OnControlChanged; public static string BaseUrl { get; set; } = "ms-appx:///"; @@ -94,6 +96,7 @@ private async Task SetupControlAsync() Control.CoreWebView2.WebResourceRequested += OnWebResourceRequested; Control.CoreWebView2.DOMContentLoaded += OnDOMContentLoaded; Control.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All); + defaultUserAgent = Control.CoreWebView2.Settings.UserAgent; SetUserAgent(); OnControlChanged?.Invoke(this, control); @@ -332,7 +335,18 @@ private void SetUserAgent(object sender = null, EventArgs e = null) { if (Control != null && Element.UserAgent != null && Element.UserAgent.Length > 0) { - Control.CoreWebView2.Settings.UserAgent = Element.UserAgent; + switch (Element.UserAgentMode) + { + case UserAgentMode.Replace: + Control.CoreWebView2.Settings.UserAgent = Element.UserAgent; + break; + case UserAgentMode.Append: + Control.CoreWebView2.Settings.UserAgent = $"{defaultUserAgent} {Element.UserAgent}"; + break; + case UserAgentMode.Prepend: + Control.CoreWebView2.Settings.UserAgent = $"{Element.UserAgent} {defaultUserAgent}"; + break; + } } } } diff --git a/HybridWebView/iOS/HybridWebViewRenderer.cs b/HybridWebView/iOS/HybridWebViewRenderer.cs index 0fcc2f2..23cf375 100644 --- a/HybridWebView/iOS/HybridWebViewRenderer.cs +++ b/HybridWebView/iOS/HybridWebViewRenderer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.ComponentModel; using System.IO; using System.Threading.Tasks; @@ -20,6 +20,8 @@ namespace Plugin.HybridWebView.iOS /// public class HybridWebViewRenderer : ViewRenderer, IWKScriptMessageHandler, IWKUIDelegate { + private static string defaultUserAgent; + public static event EventHandler OnControlChanged; public static string BaseUrl { get; set; } = $"{NSBundle.MainBundle.BundlePath}/"; @@ -106,6 +108,7 @@ private void SetupControl() HybridWebViewControl.CallbackAdded += OnCallbackAdded; SetNativeControl(wkWebView); + defaultUserAgent = Control.CustomUserAgent; SetUserAgent(); OnControlChanged?.Invoke(this, wkWebView); } @@ -428,7 +431,18 @@ private void SetUserAgent(object sender = null, EventArgs e = null) { if (Control != null && Element.UserAgent != null && Element.UserAgent.Length > 0) { - Control.CustomUserAgent = Element.UserAgent; + switch (Element.UserAgentMode) + { + case UserAgentMode.Replace: + Control.CustomUserAgent = Element.UserAgent; + break; + case UserAgentMode.Append: + Control.CustomUserAgent = $"{defaultUserAgent} {Element.UserAgent}"; + break; + case UserAgentMode.Prepend: + Control.CustomUserAgent = $"{Element.UserAgent} {defaultUserAgent}"; + break; + } } } } diff --git a/HybridWebView/macOS/HybridWebViewRenderer.cs b/HybridWebView/macOS/HybridWebViewRenderer.cs index d51d17c..754940d 100644 --- a/HybridWebView/macOS/HybridWebViewRenderer.cs +++ b/HybridWebView/macOS/HybridWebViewRenderer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.ComponentModel; using System.IO; using System.Threading.Tasks; @@ -16,6 +16,8 @@ namespace Plugin.HybridWebView.macOS { public class HybridWebViewRenderer : ViewRenderer, IWKScriptMessageHandler, IWKUIDelegate { + private static string defaultUserAgent; + public static event EventHandler OnControlChanged; public static string BaseUrl { get; set; } = NSBundle.MainBundle.ResourcePath; @@ -98,6 +100,7 @@ private void SetupControl() HybridWebViewControl.CallbackAdded += OnCallbackAdded; SetNativeControl(wkWebView); + defaultUserAgent = Control.CustomUserAgent; SetUserAgent(); OnControlChanged?.Invoke(this, wkWebView); } @@ -351,7 +354,18 @@ private void SetUserAgent(object sender = null, EventArgs e = null) { if (Control != null && Element.UserAgent != null && Element.UserAgent.Length > 0) { - Control.CustomUserAgent = Element.UserAgent; + switch (Element.UserAgentMode) + { + case UserAgentMode.Replace: + Control.CustomUserAgent = Element.UserAgent; + break; + case UserAgentMode.Append: + Control.CustomUserAgent = $"{defaultUserAgent} {Element.UserAgent}"; + break; + case UserAgentMode.Prepend: + Control.CustomUserAgent = $"{Element.UserAgent} {defaultUserAgent}"; + break; + } } } } From ada0a1623682a94d16d9ab5a8fd6c216c028a2db Mon Sep 17 00:00:00 2001 From: Christoph Fink Date: Mon, 3 Jan 2022 14:10:36 +0100 Subject: [PATCH 3/3] use CoreWebView2CookieManager --- .../Windows/HybridWebViewRenderer.cs | 45 +++++++------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/HybridWebView/Windows/HybridWebViewRenderer.cs b/HybridWebView/Windows/HybridWebViewRenderer.cs index 1955f75..c46628f 100644 --- a/HybridWebView/Windows/HybridWebViewRenderer.cs +++ b/HybridWebView/Windows/HybridWebViewRenderer.cs @@ -220,13 +220,8 @@ private Task OnClearCookiesRequest() if (Control == null || Element == null) return Task.CompletedTask; - var url = new Uri(Element.Source); - var filter = new HttpBaseProtocolFilter(); - var cookieManager = filter.CookieManager; - var cookieCollection = cookieManager.GetCookies(url); - - foreach (var currentCookie in cookieCollection) - cookieManager.DeleteCookie(currentCookie); + var cookieManager = Control.CoreWebView2.CookieManager; + cookieManager.DeleteAllCookies(); return Task.CompletedTask; } @@ -235,15 +230,12 @@ private Task OnGetAllCookieRequestAsync() { if (Control == null || Element == null) return Task.FromResult(String.Empty); - var domain = (new Uri(Element.Source)).Host; - var cookie = String.Empty; - var url = new Uri(Element.Source); - var filter = new HttpBaseProtocolFilter(); - var cookieManager = filter.CookieManager; - var cookieCollection = cookieManager.GetCookies(url); + var cookieManager = Control.CoreWebView2.CookieManager; + var cookieList = await cookieManager.GetCookiesAsync(Element.Source); - foreach (var currentCookie in cookieCollection) + var cookie = String.Empty; + foreach (var currentCookie in cookieList) { cookie += currentCookie.Name + "=" + currentCookie.Value + "; "; } @@ -260,14 +252,12 @@ private Task OnGetCookieRequestAsync(string key) { if (Control == null || Element == null) return Task.FromResult(String.Empty); - var url = new Uri(Element.Source); - var cookie = String.Empty; - var filter = new HttpBaseProtocolFilter(); - var cookieManager = filter.CookieManager; - var cookieCollection = cookieManager.GetCookies(url); + var cookieManager = Control.CoreWebView2.CookieManager; + var cookieList = await cookieManager.GetCookiesAsync(Element.Source); - foreach (var currentCookie in cookieCollection) + var cookie = String.Empty; + foreach (var currentCookie in cookieList) { if (key == currentCookie.Name) { @@ -283,14 +273,13 @@ private Task OnSetCookieRequestAsync(Cookie cookie) { if (Control == null || Element == null) return Task.FromResult(String.Empty); - var newCookie = new HttpCookie(cookie.Name, cookie.Domain, cookie.Path) { Value = cookie.Value, HttpOnly = cookie.HttpOnly, Secure = cookie.Secure, Expires = cookie.Expires }; - - var filter = new HttpBaseProtocolFilter(); - filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted); - filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired); - filter.CookieManager.SetCookie(newCookie); - return OnGetCookieRequestAsync(cookie.Name); - + var cookieManager = Control.CoreWebView2.CookieManager; + var webViewCookie = cookieManager.CreateCookie(cookie.Name, cookie.Value, cookie.Domain, cookie.Path); + webViewCookie.IsHttpOnly = cookie.HttpOnly; + webViewCookie.IsSecure = cookie.Secure; + webViewCookie.Expires = new DateTimeOffset(cookie.Expires).ToUnixTimeSeconds(); + cookieManager.AddOrUpdateCookie(webViewCookie); + return OnGetCookieRequestAsync(webViewCookie.Name); } private async Task OnJavascriptInjectionRequestAsync(string js)