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

Restore & obsolete Windows MauiWebView ctor #15541

Merged
merged 2 commits into from
Jun 12, 2023
Merged
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
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);
}
};
}
}
}
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