Skip to content

Commit

Permalink
Restore & obsolete Windows MauiWebView ctor (#15541)
Browse files Browse the repository at this point in the history
* Restore & obsolete Windows MauiWebView ctor

* Reinstate functionality for obsoleted ctor
  • Loading branch information
jfversluis authored Jun 12, 2023
1 parent 81d2406 commit c34957a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
47 changes: 30 additions & 17 deletions src/Core/src/Platform/Windows/MauiWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,20 @@ public class MauiWebView : WebView2, IWebViewDelegate
{
readonly WeakReference<WebViewHandler> _handler;

[Obsolete("Constructor is no longer used, please use an overloaded version.")]
#pragma warning disable CS8618
public MauiWebView()
{
SetupPlatformEvents();
}
#pragma warning restore CS8618

public MauiWebView(WebViewHandler handler)
{
ArgumentNullException.ThrowIfNull(handler, nameof(handler));
_handler = new WeakReference<WebViewHandler>(handler);

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);
}
};
SetupPlatformEvents();
}

WebView2? _internalWebView;
Expand Down Expand Up @@ -132,7 +125,7 @@ public async void LoadUrl(string? url)
uri = new Uri(LocalScheme + url, UriKind.RelativeOrAbsolute);
}

if (_handler.TryGetTarget(out var handler))
if (_handler?.TryGetTarget(out var handler) ?? false)
await handler.SyncPlatformCookies(uri.AbsoluteUri);

try
Expand All @@ -144,5 +137,25 @@ public async void LoadUrl(string? url)
Debug.WriteLine(nameof(MauiWebView), $"Failed to load: {uri} {exc}");
}
}

void SetupPlatformEvents()
{
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);
}
};
}
}
}
3 changes: 1 addition & 2 deletions src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ static Microsoft.Maui.PropertyMapperExtensions.ModifyMapping<TVirtualView, TView
static Microsoft.Maui.PropertyMapperExtensions.PrependToMapping<TVirtualView, TViewHandler>(this Microsoft.Maui.IPropertyMapper<Microsoft.Maui.IElement!, Microsoft.Maui.IElementHandler!>! propertyMapper, string! key, System.Action<TViewHandler, TVirtualView>! method) -> void
static Microsoft.Maui.PropertyMapperExtensions.ReplaceMapping<TVirtualView, TViewHandler>(this Microsoft.Maui.IPropertyMapper<Microsoft.Maui.IElement!, Microsoft.Maui.IElementHandler!>! propertyMapper, string! key, System.Action<TViewHandler, TVirtualView>! method) -> void
static Microsoft.Maui.SizeRequest.operator !=(Microsoft.Maui.SizeRequest left, Microsoft.Maui.SizeRequest right) -> bool
static Microsoft.Maui.SizeRequest.operator ==(Microsoft.Maui.SizeRequest left, Microsoft.Maui.SizeRequest right) -> bool
*REMOVED*Microsoft.Maui.Platform.MauiWebView.MauiWebView() -> void
static Microsoft.Maui.SizeRequest.operator ==(Microsoft.Maui.SizeRequest left, Microsoft.Maui.SizeRequest right) -> bool

0 comments on commit c34957a

Please sign in to comment.