From 740daed9dd7126e0374164b0c61b652d2487f23b Mon Sep 17 00:00:00 2001 From: Conor Breen Date: Wed, 1 Jun 2022 18:30:06 +0100 Subject: [PATCH 1/4] Fix for #5523 MauiWebView not loading local files on Windows --- src/Core/src/Platform/Windows/MauiWebView.cs | 31 ++++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Core/src/Platform/Windows/MauiWebView.cs b/src/Core/src/Platform/Windows/MauiWebView.cs index 2880166c15d9..58b7fa2a4da5 100644 --- a/src/Core/src/Platform/Windows/MauiWebView.cs +++ b/src/Core/src/Platform/Windows/MauiWebView.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Text.RegularExpressions; using Microsoft.UI.Xaml.Controls; +using Windows.ApplicationModel; namespace Microsoft.Maui.Platform { @@ -9,7 +10,9 @@ public class MauiWebView : WebView2, IWebViewDelegate { WebView2? _internalWebView; - const string LocalScheme = "ms-appx-web:///"; + // Arbitrary local host name for virtual folder mapping + const string LocalHostName = "appxpackage"; + const string LocalScheme = $"https://{LocalHostName}/"; // Script to insert a tag into an HTML document const string BaseInsertionScript = @" @@ -21,9 +24,12 @@ public class MauiWebView : WebView2, IWebViewDelegate public async void LoadHtml(string? html, string? baseUrl) { + var mapAppxFolder = false; + if (string.IsNullOrEmpty(baseUrl)) { baseUrl = LocalScheme; + mapAppxFolder = true; } // Generate a base tag for the document @@ -36,7 +42,7 @@ public async void LoadHtml(string? html, string? baseUrl) _internalWebView = new WebView2(); // TODO: For now, the CoreWebView2 won't be created without either setting Source or - // calling EnsureCoreWebView2Async(). + // calling EnsureCoreWebView2Async(). await _internalWebView.EnsureCoreWebView2Async(); // When the 'navigation' to the original HTML string is done, we can modify it to include our tag @@ -55,6 +61,16 @@ public async void LoadHtml(string? html, string? baseUrl) await EnsureCoreWebView2Async(); + if (mapAppxFolder) + { + var appxFolder = Package.Current.InstalledLocation.Path; + + CoreWebView2.SetVirtualHostNameToFolderMapping( + LocalHostName, + appxFolder, + Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow); + } + // Set the HTML for the 'real' WebView to the updated HTML NavigateToString(!string.IsNullOrEmpty(htmlWithBaseTag) ? htmlWithBaseTag : html); @@ -66,12 +82,21 @@ public async void LoadHtml(string? html, string? baseUrl) _internalWebView.NavigateToString(html); } - public void LoadUrl(string? url) + public async void LoadUrl(string? url) { Uri uri = new Uri(url ?? string.Empty, UriKind.RelativeOrAbsolute); if (!uri.IsAbsoluteUri) { + await EnsureCoreWebView2Async(); + + var appxFolder = Package.Current.InstalledLocation.Path; + + CoreWebView2.SetVirtualHostNameToFolderMapping( + LocalHostName, + appxFolder, + Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow); + uri = new Uri(LocalScheme + url, UriKind.RelativeOrAbsolute); } From 0f9e4c4074a569566318ffce76731dc62fb0b845 Mon Sep 17 00:00:00 2001 From: Conor Breen Date: Sun, 19 Jun 2022 10:54:21 +0100 Subject: [PATCH 2/4] Unpackaged app support for local HTML files. --- src/Core/src/Platform/Windows/MauiWebView.cs | 22 +++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Core/src/Platform/Windows/MauiWebView.cs b/src/Core/src/Platform/Windows/MauiWebView.cs index 58b7fa2a4da5..41f0bc02f618 100644 --- a/src/Core/src/Platform/Windows/MauiWebView.cs +++ b/src/Core/src/Platform/Windows/MauiWebView.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.Text.RegularExpressions; +using Microsoft.Maui.ApplicationModel; using Microsoft.UI.Xaml.Controls; using Windows.ApplicationModel; @@ -11,7 +12,7 @@ public class MauiWebView : WebView2, IWebViewDelegate WebView2? _internalWebView; // Arbitrary local host name for virtual folder mapping - const string LocalHostName = "appxpackage"; + const string LocalHostName = "appdir"; const string LocalScheme = $"https://{LocalHostName}/"; // Script to insert a tag into an HTML document @@ -22,14 +23,19 @@ public class MauiWebView : WebView2, IWebViewDelegate head.innerHTML = 'baseTag' + head.innerHTML; }"; + // Allow for packaged/unpackaged app support + string ApplicationPath = AppInfoUtils.IsPackagedApp + ? Package.Current.InstalledLocation.Path + : AppContext.BaseDirectory; + public async void LoadHtml(string? html, string? baseUrl) { - var mapAppxFolder = false; + var mapBaseDirectory = false; if (string.IsNullOrEmpty(baseUrl)) { baseUrl = LocalScheme; - mapAppxFolder = true; + mapBaseDirectory = true; } // Generate a base tag for the document @@ -61,13 +67,11 @@ public async void LoadHtml(string? html, string? baseUrl) await EnsureCoreWebView2Async(); - if (mapAppxFolder) + if (mapBaseDirectory) { - var appxFolder = Package.Current.InstalledLocation.Path; - CoreWebView2.SetVirtualHostNameToFolderMapping( LocalHostName, - appxFolder, + ApplicationPath, Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow); } @@ -90,11 +94,9 @@ public async void LoadUrl(string? url) { await EnsureCoreWebView2Async(); - var appxFolder = Package.Current.InstalledLocation.Path; - CoreWebView2.SetVirtualHostNameToFolderMapping( LocalHostName, - appxFolder, + ApplicationPath, Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow); uri = new Uri(LocalScheme + url, UriKind.RelativeOrAbsolute); From 5d76df33c8aa4ff377da515f651b6989b59904fb Mon Sep 17 00:00:00 2001 From: Conor Breen Date: Sun, 19 Jun 2022 11:05:21 +0100 Subject: [PATCH 3/4] Use property for path --- src/Core/src/Platform/Windows/MauiWebView.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/src/Platform/Windows/MauiWebView.cs b/src/Core/src/Platform/Windows/MauiWebView.cs index 41f0bc02f618..e9ef0d36699e 100644 --- a/src/Core/src/Platform/Windows/MauiWebView.cs +++ b/src/Core/src/Platform/Windows/MauiWebView.cs @@ -24,7 +24,7 @@ public class MauiWebView : WebView2, IWebViewDelegate }"; // Allow for packaged/unpackaged app support - string ApplicationPath = AppInfoUtils.IsPackagedApp + string ApplicationPath => AppInfoUtils.IsPackagedApp ? Package.Current.InstalledLocation.Path : AppContext.BaseDirectory; From 8dc5d97aa1fd270a2cdc12eb93b4b2c7ab229f84 Mon Sep 17 00:00:00 2001 From: Conor Breen Date: Mon, 20 Jun 2022 21:00:44 +0100 Subject: [PATCH 4/4] Auto map/unmap virtual host for app dir when navigating to safe/unsafe URIs --- src/Core/src/Platform/Windows/MauiWebView.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Core/src/Platform/Windows/MauiWebView.cs b/src/Core/src/Platform/Windows/MauiWebView.cs index e9ef0d36699e..a4028f258c39 100644 --- a/src/Core/src/Platform/Windows/MauiWebView.cs +++ b/src/Core/src/Platform/Windows/MauiWebView.cs @@ -9,6 +9,26 @@ namespace Microsoft.Maui.Platform { public class MauiWebView : WebView2, IWebViewDelegate { + public MauiWebView() + { + NavigationStarting += (sender, args) => + { + // Auto map local virtual app dir host, e.g. if navigating back to local site from a link to an external site + if (args?.Uri?.ToLowerInvariant().StartsWith(LocalScheme.TrimEnd('/').ToLowerInvariant()) == true) + { + CoreWebView2.SetVirtualHostNameToFolderMapping( + LocalHostName, + ApplicationPath, + Web.WebView2.Core.CoreWebView2HostResourceAccessKind.Allow); + } + // Auto unmap local virtual app dir host if navigating to any other potentially unsafe domain + else + { + CoreWebView2.ClearVirtualHostNameToFolderMapping(LocalHostName); + } + }; + } + WebView2? _internalWebView; // Arbitrary local host name for virtual folder mapping