From b78a93982e4fa8db2fb33d1613c5cbb2197be9eb Mon Sep 17 00:00:00 2001 From: Tanay Parikh Date: Mon, 14 Feb 2022 14:47:23 -0800 Subject: [PATCH 1/2] Blazor Windows Open Links in Browser with Configurability Windows portion of #4338 --- .../Windows/BlazorWebViewHandler.Windows.cs | 9 ++++- .../src/Maui/Windows/WinUIWebViewManager.cs | 40 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/BlazorWebView/src/Maui/Windows/BlazorWebViewHandler.Windows.cs b/src/BlazorWebView/src/Maui/Windows/BlazorWebViewHandler.Windows.cs index 66e44a550b81..818e97aac615 100644 --- a/src/BlazorWebView/src/Maui/Windows/BlazorWebViewHandler.Windows.cs +++ b/src/BlazorWebView/src/Maui/Windows/BlazorWebViewHandler.Windows.cs @@ -58,7 +58,14 @@ private void StartWebViewCoreIfPossible() var fileProvider = VirtualView.CreateFileProvider(contentRootDir); - _webviewManager = new WinUIWebViewManager(NativeView, Services!, ComponentsDispatcher, fileProvider, VirtualView.JSComponents, hostPageRelativePath, contentRootDir); + _webviewManager = new WinUIWebViewManager(NativeView, + Services!, + ComponentsDispatcher, + fileProvider, + VirtualView.JSComponents, + hostPageRelativePath, + contentRootDir, + ExternalLinkMode); if (RootComponents != null) { diff --git a/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs b/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs index 9a1378614013..c813a1bc2edd 100644 --- a/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs +++ b/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs @@ -8,6 +8,7 @@ using Microsoft.Web.WebView2.Core; using Windows.ApplicationModel; using Windows.Storage.Streams; +using Launcher = Windows.System.Launcher; using WebView2Control = Microsoft.UI.Xaml.Controls.WebView2; namespace Microsoft.AspNetCore.Components.WebView.Maui @@ -21,13 +22,25 @@ public class WinUIWebViewManager : WebView2WebViewManager private readonly WebView2Control _webview; private readonly string _hostPageRelativePath; private readonly string _contentRootDir; + private readonly ExternalLinkMode _externalLinkMode; - public WinUIWebViewManager(WebView2Control webview, IServiceProvider services, Dispatcher dispatcher, IFileProvider fileProvider, JSComponentConfigurationStore jsComponents, string hostPageRelativePath, string contentRootDir) + public WinUIWebViewManager( + WebView2Control webview, + IServiceProvider services, + Dispatcher dispatcher, + IFileProvider fileProvider, + JSComponentConfigurationStore jsComponents, + string hostPageRelativePath, + string contentRootDir, + ExternalLinkMode externalLinkMode) : base(webview, services, dispatcher, fileProvider, jsComponents, hostPageRelativePath) { _webview = webview; _hostPageRelativePath = hostPageRelativePath; _contentRootDir = contentRootDir; + _externalLinkMode = externalLinkMode; + + _webview.CoreWebView2Initialized += Webview_CoreWebView2Initialized; } protected override async Task HandleWebResourceRequest(CoreWebView2WebResourceRequestedEventArgs eventArgs) @@ -102,5 +115,30 @@ protected override void QueueBlazorStart() "); }; } + + private void Webview_CoreWebView2Initialized(WebView2Control sender, UI.Xaml.Controls.CoreWebView2InitializedEventArgs args) + { + _webview.CoreWebView2.NavigationStarting += CoreWebView2_NavigationStarting; + _webview.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested; + } + + private void CoreWebView2_NavigationStarting(CoreWebView2 sender, CoreWebView2NavigationStartingEventArgs args) + { + var uri = new Uri(args.Uri); + if (uri.Host != "0.0.0.0" && _externalLinkMode == ExternalLinkMode.OpenInExternalBrowser) + { + _ = Launcher.LaunchUriAsync(uri); + args.Cancel = true; + } + } + + private void CoreWebView2_NewWindowRequested(CoreWebView2 sender, CoreWebView2NewWindowRequestedEventArgs args) + { + // Intercept _blank target tags to always open in device browser + // regardless of ExternalLinkMode.OpenInWebview + var uri = new Uri(args.Uri); + _ = Launcher.LaunchUriAsync(uri); + args.Handled = true; + } } } From 9639b8c0c6614a6f70b6c947708434318fa87c17 Mon Sep 17 00:00:00 2001 From: Tanay Parikh Date: Mon, 14 Feb 2022 19:36:19 -0800 Subject: [PATCH 2/2] TryCreate URI --- .../src/Maui/Windows/WinUIWebViewManager.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs b/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs index c813a1bc2edd..f4a948ef0ec3 100644 --- a/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs +++ b/src/BlazorWebView/src/Maui/Windows/WinUIWebViewManager.cs @@ -25,7 +25,7 @@ public class WinUIWebViewManager : WebView2WebViewManager private readonly ExternalLinkMode _externalLinkMode; public WinUIWebViewManager( - WebView2Control webview, + WebView2Control webview!!, IServiceProvider services, Dispatcher dispatcher, IFileProvider fileProvider, @@ -124,8 +124,9 @@ private void Webview_CoreWebView2Initialized(WebView2Control sender, UI.Xaml.Con private void CoreWebView2_NavigationStarting(CoreWebView2 sender, CoreWebView2NavigationStartingEventArgs args) { - var uri = new Uri(args.Uri); - if (uri.Host != "0.0.0.0" && _externalLinkMode == ExternalLinkMode.OpenInExternalBrowser) + if (Uri.TryCreate(args.Uri, UriKind.RelativeOrAbsolute, out var uri) && + uri.Host != "0.0.0.0" && + _externalLinkMode == ExternalLinkMode.OpenInExternalBrowser) { _ = Launcher.LaunchUriAsync(uri); args.Cancel = true; @@ -136,9 +137,11 @@ private void CoreWebView2_NewWindowRequested(CoreWebView2 sender, CoreWebView2Ne { // Intercept _blank target tags to always open in device browser // regardless of ExternalLinkMode.OpenInWebview - var uri = new Uri(args.Uri); - _ = Launcher.LaunchUriAsync(uri); - args.Handled = true; + if (Uri.TryCreate(args.Uri, UriKind.RelativeOrAbsolute, out var uri)) + { + _ = Launcher.LaunchUriAsync(uri); + args.Handled = true; + } } } }