Repository navigation
feat(win32): WebView2Aot backend for <WebView2/> - #23621
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a NativeAOT-compatible WebView2 backend for the Win32 Skia runtime by integrating WebView2Aot (NET10+) and selecting it by default on .NET 10+, while keeping the existing Microsoft.Web.WebView2 backend as a fallback via UNO_WEBVIEW2_BACKEND.
Changes:
- Add a shared Win32 HWND/WndProc hosting base (
Win32NativeWebViewBase) and refactor the existing WebView2 implementation to use it. - Introduce a new NET10+
Win32NativeAotWebViewbackend based on WebView2Aot, with backend selection viaUNO_WEBVIEW2_BACKEND. - Update packaging/docs and add banned API analyzer configuration for unsafe
PWSTR.From(string)usage.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Uno.UI.Runtime.Skia.Win32/Uno.UI.Runtime.Skia.Win32.csproj | Conditionally references WebView2Aot for net10+ and enables banned API analyzer with BannedSymbols.txt. |
| src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebViewBase.cs | New base type factoring Win32 window creation, WndProc, and resize forwarding for WebView hosting. |
| src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebView.cs | Adds backend selection logic and refactors existing Microsoft.Web.WebView2 implementation to derive from the new base. |
| src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeAotWebView.cs | New NET10+ WebView2Aot-based backend implementation. |
| src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeAotWebView.Request.cs | AOT wrappers for WebResourceRequested request/headers iteration against WebView2Aot COM interfaces. |
| src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeAotWebView.Response.cs | AOT wrappers for WebResourceRequested response/headers against WebView2Aot COM interfaces. |
| src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeAotWebView.Helpers.cs | Stream conversion helpers and a generated COM IStream implementation for request/response bodies. |
| src/Uno.UI.Runtime.Skia.Win32/BannedSymbols.txt | Bans DirectN.PWSTR.From(string) with guidance for a fixed-pointer alternative. |
| src/Directory.Build.targets | Pins WebView2Aot package version repository-wide. |
| doc/articles/controls/WebView.md | Documents the two Windows WebView2 backends and the UNO_WEBVIEW2_BACKEND override. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23621/wasm-skia-net9/index.html |
|
|
ajpinedam
left a comment
There was a problem hiding this comment.
A few valid comments from Copilot but aside from that it looks good
Fixes: #23142 Context: MicrosoftEdge/WebView2Feedback#5238 Context: https://github.com/smourier/WebView2Aot Context: smourier/DirectNAot#9 On Windows, the <WebView2/> XAML control is implemented in terms of the [Microsoft.Web.WebView2 NuGet package][0]. The problem: Microsoft.Web.WebView2, when used for "normal" .NET, uses COM Interop. Native AOT does not support COM interop. The result is that things fail, a'la MicrosoftEdge/WebView2Feedback#5238 (which is for WinForms, but the same fundamental idea). In the case of #23142, if we build Uno.Gallery for Native AOT: git clone https://github.com/unoplatform/Uno.Gallery.git cd Uno.Gallery git checkout 54a08b1587aa020a5651d61eb3a3023a79e89d72 sed -i '' 's/"Uno.Sdk": ".*"/"Uno.Sdk": "6.6.0-dev.216"/g' global.json dotnet publish -c Release -r win-arm64 -f net10.0-desktop -p:TargetFrameworkOverride=net10.0-desktop ` -bl Uno.Gallery/Uno.Gallery.csproj -p:SelfContained=true -p:UseSkiaRendering=true ` -p:SkiaPublishAot=true -p:IsAotCompatible=true Run it: .\Uno.Gallery\bin\Release\net10.0-desktop\win-arm64\publish\Uno.Gallery.exe | Out-Default and access the **UI components > WebView** entry in the left tree, then the console contains the following output: fail: Uno.UI.Dispatching.NativeDispatcher[0] NativeDispatcher unhandled exception System.NotSupportedException: COM Interop requires ComWrapper instance registered for marshalling. at System.Runtime.InteropServices.ComWrappers.ComInterfaceForObject(Object) + 0xa4 at System.Runtime.InteropServices.ComWrappers.ComInterfaceForObject(Object, Guid) + 0x24 at Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateCoreWebView2EnvironmentWithOptions(String, String, ICoreWebView2EnvironmentOptions, ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler) + 0xdc at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateAsync>d__102.MoveNext() + 0xe4 --- End of stack trace from previous location --- at Uno.UI.Runtime.Skia.Win32.Win32NativeWebView.<>c__DisplayClass15_0.<<-ctor>b__0>d.MoveNext() + 0x60 --- End of stack trace from previous location --- at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__124_0(Object state) + 0x1c at Uno.UI.Dispatching.NativeDispatcher.RunAction(NativeDispatcher, Action) + 0x8c The Microsoft.Web.WebView2 package *does* support Native AOT, but *only if* you're building for WinRT. (Uno is not built on WinRT.) A way to use Microsoft.Web.WebView2 while supporting Native AOT in a "normal" (non-WinRT) Windows environment is to instead use smourier/WebView2Aot, in the [WebView2Aot NuGet package][1]. WebView2Aot uses the [DirectN AOT generator][2] to emit Native AOT- compatible COM interop infrastrucure. Update `Uno.UI.Runtime.Skia.Win32` as follows: 1. Split out the "native" bits of `Win32NativeWebView` into `Win32NativeWebViewBase.cs`. (Anything dealing with a Windows WndProc, etc.) 2. Update `Win32NativeWebView` to inherit `Win32NativeWebViewBase`. 3. Add a new `Win32NativeAotWebView` type, which also inherits from `Win32NativeWebViewBase`. This implements `<WebView2/>` support in terms of WebView2Aot. However, WebView2Aot requires .NET 10. Consequently, `Uno.UI.Runtime.Skia.Win32.csproj` should only reference WebView2Aot when targeting .NET 10 and later, and `Win32NativeAotWebView` can only exist in .NET 10 and later. Additionally, this is a potential significant change. We want WebView2Aot to be the default in all Windows configurations for consistency -- nobody wants to debug a scenario of "WebView2 works when I'm not building for Native AOT!" -- but there should be a way to use the previous Microsoft.Web.WebView2 implementation in case WebView2Aot breaks things, or for testing, or… Update `Win32NativeWebViewProvider` as follows: 1. When built for .NET 9, only Win32NativerWebView / the Microsoft.Web.WebView2 backend is supported. 2. When built for .NET 10+, the new WebView2Aot backend is used by default. This can be overridden by setting the `UNO_WEBVIEW2_BACKEND` environment variable to `microsoft.web.webview2`. `UNO_WEBVIEW2_BACKEND` can also be set to `webview2aot` to be explicit. The `UNO_WEBVIEW2_BACKEND` environment variable is always read, and will log an error message when using an unsupported value, e.g. when `UNO_WEBVIEW2_BACKEND=webview2aot` on .NET 9. Aside: add BannedApiAnalyzers to `Uno.UI.Runtime.Skia.Win32.csproj` which make it an error to use `PWSTR.From(string)`. See also smourier/DirectNAot#9. [0]: https://www.nuget.org/packages/Microsoft.Web.WebView2 [1]: https://www.nuget.org/packages/WebView2Aot [2]: https://github.com/smourier/DirectNAot
07b81aa to
89e6d00
Compare
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23621/wasm-skia-net9/index.html |
|
Tick the box to add this pull request to the merge queue (same as
|
Context: #23621 Context: #23603 PR #23621 added a WebView2Aot backend for `<WebView2/>` within `Uno.UI.Runtime.Skia.Win32`, and added support for a `UNO_WEBVIEW2_BACKEND` environment variable to switch between the backends. For Uno 7, remove support for the original Microsoft.Web.WebView2 backend. We'll stick with WebView2Aot going forward, which will reduce assembly size (only one backend), simplify maintenance (only one backend), and has support for Native AOT. Additionally, as the feature/breakingchanges branch is now on .NET 11 (#23603), which drops support for .NET 9, we no longer need to make the WebView2Aot NuGet reference conditional on .NET 10+. Cleanup `Uno.UI.Runtime.Skia.Win32` accordingly.
Context: #23621 Context: #23603 PR #23621 added a WebView2Aot backend for `<WebView2/>` within `Uno.UI.Runtime.Skia.Win32`, and added support for a `UNO_WEBVIEW2_BACKEND` environment variable to switch between the backends. For Uno 7, remove support for the original Microsoft.Web.WebView2 backend. We'll stick with WebView2Aot going forward, which will reduce assembly size (only one backend), simplify maintenance (only one backend), and has support for Native AOT. Additionally, as the feature/breakingchanges branch is now on .NET 11 (#23603), which drops support for .NET 9, we no longer need to make the WebView2Aot NuGet reference conditional on .NET 10+. Cleanup `Uno.UI.Runtime.Skia.Win32` accordingly.
Fixes: #23142
Context: MicrosoftEdge/WebView2Feedback#5238
Context: https://github.com/smourier/WebView2Aot
Context: smourier/DirectNAot#9
On Windows, the XAML control is implemented in terms of the Microsoft.Web.WebView2 NuGet package.
The problem: Microsoft.Web.WebView2, when used for "normal" .NET, uses COM Interop. Native AOT does not support COM interop. The result is that things fail, a'la
MicrosoftEdge/WebView2Feedback#5238 (which is for WinForms, but the same fundamental idea).
In the case of #23142, if we build Uno.Gallery for Native AOT:
Run it:
and access the UI components > WebView entry in the left tree, then the console contains the following output:
The Microsoft.Web.WebView2 package does support Native AOT, but only if you're building for WinRT. (Uno is not built on WinRT.)
A way to use Microsoft.Web.WebView2 while supporting Native AOT in a "normal" (non-WinRT) Windows environment is to instead use smourier/WebView2Aot, in the WebView2Aot NuGet package. WebView2Aot uses the DirectN AOT generator to emit Native AOT- compatible COM interop infrastrucure.
Update
Uno.UI.Runtime.Skia.Win32as follows:Split out the "native" bits of
Win32NativeWebViewintoWin32NativeWebViewBase.cs. (Anything dealing with a Windows WndProc, etc.)Update
Win32NativeWebViewto inheritWin32NativeWebViewBase.Add a new
Win32NativeAotWebViewtype, which also inherits fromWin32NativeWebViewBase. This implements<WebView2/>support in terms of WebView2Aot.However, WebView2Aot requires .NET 10. Consequently,
Uno.UI.Runtime.Skia.Win32.csprojshould only reference WebView2Aot when targeting .NET 10 and later, andWin32NativeAotWebViewcan only exist in .NET 10 and later.Additionally, this is a potential significant change. We want WebView2Aot to be the default in all Windows configurations for consistency -- nobody wants to debug a scenario of "WebView2 works when I'm not building for Native AOT!" -- but there should be a way to use the previous Microsoft.Web.WebView2 implementation in case WebView2Aot breaks things, or for testing, or…
Update
Win32NativeWebViewProvideras follows:When built for .NET 9, only Win32NativerWebView / the Microsoft.Web.WebView2 backend is supported.
When built for .NET 10+, the new WebView2Aot backend is used by default. This can be overridden by setting the
UNO_WEBVIEW2_BACKENDenvironment variable tomicrosoft.web.webview2.UNO_WEBVIEW2_BACKENDcan also be set towebview2aotto be explicit.The
UNO_WEBVIEW2_BACKENDenvironment variable is always read, and will log an error message when using an unsupported value, e.g. whenUNO_WEBVIEW2_BACKEND=webview2aoton .NET 9.Aside: add BannedApiAnalyzers to
Uno.UI.Runtime.Skia.Win32.csprojwhich make it an error to usePWSTR.From(string). See also smourier/DirectNAot#9.GitHub Issue: closes #
PR Type:
What changed? 🚀
PR Checklist ✅
Screenshots Compare Test Runresults.