Repository navigation
Conversation
|
|
| } | ||
|
|
||
| PWSTR title = default; | ||
| _nativeWebView.CoreWebView2.get_DocumentTitle(out title).ThrowOnError(false); |
There was a problem hiding this comment.
@Copilot: why are you using .ThrowOnError(false)? (Related: why does .ThrowOnError(false) exist?)
.ThrowOnError(false) is basically a no-op: https://github.com/smourier/DirectNAot/blob/7d5862a3f1294c1a61afad2d5320ddfc34ddd748/DirectN/Partials/HRESULT.partial.cs#L20-L39
I can understand not wanting to throw an exception, as UpdateDocumentTitle() is called from async void NativeWebView_CoreWebView2InitializationCompleted(), and as NativeWebView_CoreWebView2InitializationCompleted() is async void, any thrown exception may tear down the process.
So not calling .ThrowOnError() is defensible. But as .ThrowOnError(false) does nothing, why call it?
Instead, if the HRESULT value is an error, the error should be logged:
var hr = _nativeWebView.CoreWebView2.get_DocumentTitle(out title);
if (hr.IsError)
{
this.Log().LogError("Unable to obtain DocumentTitle: {hr}");
}DO NOT USE .ThrowOnError(false). ThrowOnError() is acceptable, when exceptions are acceptable.
There was a problem hiding this comment.
Updated in commit 5e70f04: I removed .ThrowOnError(false) and replaced it with explicit HRESULT checks + logging in WpfNativeWebView (including UpdateDocumentTitle()). I also removed the other .ThrowOnError(false) usages in the same file for consistency.
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using System.Reflection; |
There was a problem hiding this comment.
@Copilot: why was using System.Reflection added? I don't see any Reflection use here.
There was a problem hiding this comment.
using System.Reflection is required in this file for Assembly.GetEntryAssembly() in TryInitializeCoreWebView2() (line 599 in the current file), used by WebView2Utilities.Initialize(...). So it isn’t unused.
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Web.WebView2" Aliases="WpfWebView" PrivateAssets="all" /> | ||
| <PackageReference Include="Microsoft.Web.WebView2" Aliases="WpfWebView" PrivateAssets="all" Condition="'$(TargetFramework)' == 'net9.0'" /> | ||
| <PackageReference Include="WebView2Aot" PrivateAssets="all" Condition="'$(TargetFramework)' == 'net10.0'" /> |
There was a problem hiding this comment.
@Copilot: this Condition attribute value will complicate future migrations to .NET 11/12/etc.
This should instead use [MSBuild]::IsTargetFrameworkCompatible(targetFrameworkTarget, targetFrameworkCandidate), from Property functions:
<PackageReference Include="WebView2Aot" PrivateAssets="all" Condition=" $([MSBuild]::IsTargetFrameworkCompatible($(TargetFramework), 'net10.0')) " />There was a problem hiding this comment.
Updated in 1d601f77.
I changed the WebView2Aot package condition to use IsTargetFrameworkCompatible:
Condition="$([MSBuild]::IsTargetFrameworkCompatible($(TargetFramework), 'net10.0'))"
This keeps it migration-friendly for future .NET TFMs.
Screenshot (no UI change; project file diff only): https://github.com/user-attachments/assets/96000b03-89ce-4fc6-9fea-e812a02ddd61
| private void ProcessNavigationCore(HttpRequestMessage httpRequestMessage) | ||
| { | ||
| #if NET10_0_OR_GREATER | ||
| _nativeWebView.NavigateWithWebResourceRequest(httpRequestMessage); |
There was a problem hiding this comment.
@Copilot: why have you omitted most of the previous logic? The !NET10_0_OR_GREATER codepath looks at httpRequestMessage.Headers, and calls CreateWebResourceRequest.
WebView2Aot also binds CreateWebResourceRequest: https://github.com/smourier/WebView2Aot/blob/bf7e1def857da101793099a288a5285f802d4b2c/WebView2/Generated/WebView2/ICoreWebView2Environment2.cs#L9
Why is this not reusing most of the existing code?
There was a problem hiding this comment.
Updated in c1e4e6d2.
I refactored this so both paths now reuse the same header-building logic (WebResourceRequestHelpers.BuildHeaders(...)), and the net10 path continues to call ICoreWebView2Environment2.CreateWebResourceRequest(...) before NavigateWithWebResourceRequest(...).
I also included content headers for parity when building the request headers.
Screenshot (no UI change; code diff only): https://github.com/user-attachments/assets/96000b03-89ce-4fc6-9fea-e812a02ddd61
| } | ||
|
|
||
| #if NET10_0_OR_GREATER | ||
| internal sealed class WpfWebView2 : HwndHost |
There was a problem hiding this comment.
@Copilot: I don't like the introduction of this type, for multiple reasons:
- It's the same name as the
!NET10_0_OR_GREATERalias for thepublictypeMicrosoft.Web.WebView2.Wpf.WebView2inMicrosoft.Web.WebView2.Wpf.dll. - As such, adding this type implicitly changes the signature of the
WpfNativeWebView(WpfWebView2, CoreWebView2)constructor, and this change isn't obvious in review. - This in turn changes the implicit "contract" of
WpfNativeWebViewProvider.CreateNativeWebView().
(3) is concerning and makes me want to rethink the architecture of this PR. Consider the current implementation of WpfNativeWebViewProvider.CreateNativeWebView():
partial class WpfNativeWebViewProvider {
public INativeWebView CreateNativeWebView(ContentPresenter contentPresenter)
{
var content = contentPresenter.Content as WpfWebView.Microsoft.Web.WebView2.Wpf.WebView2;
if (content is null)
{
content = new WpfWebView.Microsoft.Web.WebView2.Wpf.WebView2();
contentPresenter.Content = content;
}
return new WpfNativeWebView(content, _owner);
}
}If (when? how? just assume it's possible…) contentPresenter.Content is already set to a Microsoft.Web.WebView2.Wpf.WebView2 instance, the current semantic is that the instance will be reused.
With this PR, the .NET 10 version would not reuse the Microsoft.Web.WebView2.Wpf.WebView2 instance! Code that previously worked would no longer work the same.
Given (3), I no longer think that WpfNativeWebView should be updated to use WebView2Aot.
Instead, do the following:
- Rename
WpfNativeWebViewtoWindowsNativeWebView - Add
WpfNativeWebViewas a subclass ofWindowsNativeWebView - Abstract over all logic within
WindowsNativeWebViewthat relies onMicrosoft.Web.WebView2.Wpf.WebView2into methods overridden byWpfNativeWebView. - Add a new
NativeAotWebViewtype which inheritsWindowsNativeWebView, which uses WebView2Aot.
Having multiple INativeWebView implementations will allow WpfNativeWebViewProvider.CreateNativeWebView() to intelligently use both:
partial class WpfNativeWebViewProvider {
public INativeWebView CreateNativeWebView(ContentPresenter contentPresenter)
{
if (contentPresenter.Content is WpfWebView.Microsoft.Web.WebView2.Wpf.WebView2 content)
{
return new WpfNativeWebView(content, _owner);
}
#if NET10_0_OR_GREATER
if (contentPresenter.Content is WebView2.ICoreWebView2 aotContent)
{
return new NativeAotWebView(aotContent, _owner);
}
aotContent = NativeAotWebView.CreateCoreWebView2();
contentPresenter.Content = aotContent;
return new NativeAotWebView(aotContent, _owner);
#else
content = new WpfWebView.Microsoft.Web.WebView2.Wpf.WebView2();
contentPresenter.Content = content;
return new WpfNativeWebView(content, _owner);
#endif
}
}This would preserve existing semantics: if contentPresenter.Content is (somehow) already a Microsoft.Web.WebView2.Wpf.WebView2 instance, behavior is unchanged.
If contentPresenter.Content is null, behavior depends upon NET10_0_OR_GREATER: in .NET 10+, it's set to a WebView2.ICoreWebView2 instance. In .NET 9, it's set to a Microsoft.Web.WebView2.Wpf.WebView2 instance, which is the current behavior.
Not provided here is the implementation of NativeAotWebView.CreateCoreWebView2(). For a possible starting point, see the HelloWebView2 sample: https://github.com/smourier/WebView2Aot/blob/bf7e1def857da101793099a288a5285f802d4b2c/HelloWebView2/WebViewWindow.cs#L23-L37
There was a problem hiding this comment.
Updated in 3ac08507.
I split the implementations so WpfNativeWebView remains the Microsoft.Web.WebView2.Wpf.WebView2 path, and added a separate NativeAotWebView/NativeAotWebViewHost path for WebView2Aot on net10.
WpfNativeWebViewProvider.CreateNativeWebView() now preserves reuse semantics by first reusing an existing Microsoft.Web.WebView2.Wpf.WebView2 instance when present, and otherwise creating/using the AOT host on net10.
I also removed the WpfWebView2 naming collision and made constructor contracts explicit again.
Screenshot (no UI change; code diff only): https://github.com/user-attachments/assets/96000b03-89ce-4fc6-9fea-e812a02ddd61
|
@jonpryor Sorry, I missed this one - WPF target is no longer supported, is gallery using WPF host? If yes, it should probably be transitioned to Win32 instead. I would ideally prefer to drop WPF target completely in next big update, but overall it is likely completely broken now anyway |
|
Closing as this is modifying the wrong assembly. (Doh!) |
PR Type:
🐞 Bugfix
What changed? 🚀
Native AOT desktop runs of Gallery could fail when opening WebView, because the previous WPF host path depended on
Microsoft.Web.WebView2COM interop behavior that is not AOT-safe. This PR adds an AOT-compatible net10 hosting path while preserving existing WPF WebView reuse semantics.Runtime host split by implementation
WpfNativeWebViewnow remains theMicrosoft.Web.WebView2.Wpf.WebView2implementation.NativeAotWebView+NativeAotWebViewHostfor the net10 WebView2Aot path.Provider behavior and compatibility
WpfNativeWebViewProvider.CreateNativeWebView()now:Microsoft.Web.WebView2.Wpf.WebView2content when present (preserving previous behavior).NativeAotWebViewHost/NativeAotWebViewon net10 when WPF content is not already present.WpfWebView2naming collision that could obscure constructor signature/contract changes in review.Package wiring
Uno.UI.Runtime.Skia.Wpf.csprojreferences:Microsoft.Web.WebView2with alias for net9-compatible TFMs (IsTargetFrameworkCompatible(..., 'net9.0')).WebView2Aotfor net10-compatible TFMs (IsTargetFrameworkCompatible(..., 'net10.0')).WebView2Aotversion insrc/Directory.Build.targets.Navigation/request parity updates
CreateWebResourceRequest(...)headers.Hostheader filtering for request and content headers.Review feedback follow-up
.ThrowOnError(false)usages introduced in this PR.HRESULTchecks/logging where exceptions are not desired.using System.ReflectionwhereAssembly.GetEntryAssembly()is required for WebView2Aot initialization.PR Checklist ✅
Screenshots Compare Test Runresults.