Repository navigation
feat(webview2): Improve API parity across platforms - #23754
morning4coffe-dev wants to merge 17 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR expands Uno’s WebView2 / CoreWebView2 implementation to better match WebView2 API behavior across platforms (Skia Win32/macOS, iOS, Android, WebAssembly, X11), including explicit initialization, lifecycle events, settings (User-Agent / JS / zoom), cookie management, printing, and postMessage support, plus new SamplesApp manual-test pages.
Changes:
- Adds explicit
WebView2.EnsureCoreWebView2Async(...)initialization (with optional environment/controller options) plusWebView2.Close()behavior and initialization error reporting. - Introduces cross-platform “capability” interfaces (
ISupportsCookieManager,ISupportsPrint,ISupportsUserAgent, etc.) and wires them into native providers (Win32, macOS, iOS, Android, WASM, X11). - Updates WASM interop/TypeScript glue and adds SamplesApp manual tests for the new API surface.
Reviewed changes
Copilot reviewed 54 out of 70 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Uno.UI/UI/Xaml/Controls/WebView/WebView2/WebView2.Properties.cs | Guards Source updates when closed; triggers implicit init on Source changes. |
| src/Uno.UI/UI/Xaml/Controls/WebView/WebView2/WebView2.cs | Adds explicit init overloads, close/dispose behavior, and initialization event/error propagation. |
| src/Uno.UI/UI/Xaml/Controls/WebView/WebView2/CoreWebView2InitializedEventArgs.cs | Adds internal ctor to carry initialization exceptions. |
| src/Uno.UI/UI/Xaml/Controls/WebView/WebView1/WebView.cs | Implements new IWebView.RequiresExplicitInitialization contract for legacy WebView. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.wasm.cs | Adds capability-interface implementations and improved navigation/message lifecycle for WASM iframe host. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.Interop.wasm.cs | Adds JSImport entries for postMessage, print UI, and close. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/ISupportsZoomControl.cs | New capability interface (zoom enable/disable). |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/ISupportsUserAgent.cs | New capability interface (User-Agent override). |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/ISupportsScriptEnabled.cs | New capability interface (JS enable/disable). |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/ISupportsPrint.cs | New capability interface (print UI / PDF stream). |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/ISupportsPostWebMessage.cs | New capability interface (native host-to-page messaging). |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/ISupportsDocumentCreatedScripts.cs | New capability interface (document-start script injection). |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/ISupportsCookieManager.cs | New capability interface (cookie store operations). |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/ISupportsClose.cs | New capability interface (explicit native close). |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/iOSmacOS/UnoWKWebView.iOS.cs | Adds iOS/macOS WebKit-backed implementations for settings, cookies, scripts, and printing. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/Android/NativeWebViewWrapper.Android.cs | Adds Android implementations for settings, cookies, scripts, and printing (UI only). |
| src/Uno.UI/UI/Xaml/Controls/WebView/Native/Android/InternalWebClient.Android.cs | Injects document-created scripts at navigation start on Android. |
| src/Uno.UI/UI/Xaml/Controls/WebView/IWebView.cs | Adds RequiresExplicitInitialization to control native creation timing. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2Settings.cs | Adds UserAgent, IsScriptEnabled, IsZoomControlEnabled with change notifications. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2PrintSettings.cs | Introduces a managed CoreWebView2PrintSettings surface + validation. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2EnvironmentOptions.cs | Introduces managed environment options surface + default-value tracking. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2Environment.cs | Introduces managed environment surface + factory methods. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2DOMContentLoadedEventArgs.cs | Adds managed DOMContentLoaded event args. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2CookieManager.cs | Adds managed cookie manager surface backed by capability interface. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2Cookie.cs | Adds managed cookie model surface. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2ControllerOptions.cs | Adds managed controller options surface. |
| src/Uno.UI/UI/Xaml/Controls/WebView/Core/CoreWebView2ContentLoadingEventArgs.cs | Adds managed ContentLoading event args. |
| src/Uno.UI/ts/Windows/UI/Xaml/Controls/WebView.ts | Updates WASM host glue for JSON-serialized script results, postMessage, printing, and close. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2Settings.cs | Adjusts generated stubs to defer to newly implemented managed members. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2ServerCertificateErrorDetectedEventArgs.cs | Replaces not-implemented stub with usable managed shape. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2PrintSettings.cs | Adjusts generated stubs to defer to newly implemented managed members. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2PermissionRequestedEventArgs.cs | Replaces not-implemented stub with usable managed shape. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2FrameCreatedEventArgs.cs | Replaces not-implemented stub with usable managed shape. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2EnvironmentOptions.cs | Adjusts generated stubs to defer to newly implemented managed members. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2Environment.cs | Adjusts generated stubs to defer to newly implemented managed members. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2DownloadStartingEventArgs.cs | Replaces not-implemented stub with usable managed shape. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2DOMContentLoadedEventArgs.cs | Adjusts generated stubs to defer to newly implemented managed members. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2CookieManager.cs | Adjusts generated stubs to defer to newly implemented managed members. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2Cookie.cs | Adjusts generated stubs to defer to newly implemented managed members. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2ControllerOptions.cs | Adjusts generated stubs to defer to newly implemented managed members. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2ContextMenuRequestedEventArgs.cs | Replaces not-implemented stub with usable managed shape. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2ContentLoadingEventArgs.cs | Adjusts generated stubs to defer to newly implemented managed members. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.Web.WebView2.Core/CoreWebView2.cs | Adjusts generated stubs to defer to newly implemented managed members/events/methods. |
| src/Uno.UI/Generated/3.0.0.0/Microsoft.UI.Xaml.Controls/WebView2.cs | Adjusts generated stubs to defer to newly implemented WebView2 members. |
| src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Controls/WebView/Win32NativeWebView.cs | Adds Win32 implementations for cookies, printing, UA/JS/zoom, messaging, doc-created scripts, lifecycle events, and env/controller options. |
| src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWebView.h | Extends native macOS bridge API for lifecycle callbacks, printing, cookies, scripts, UA/JS, postMessage, and private mode creation. |
| src/Uno.UI.Runtime.Skia.MacOS/UI/Xaml/Controls/WebView/MacOSNativeWebViewProvider.cs | Registers lifecycle + capability callbacks for macOS native webview. |
| src/Uno.UI.Runtime.Skia.MacOS/UI/Xaml/Controls/WebView/MacOSNativeWebView.cs | Adds close/unregister logic, private mode creation option, and lifecycle callback wiring. |
| src/Uno.UI.Runtime.Skia.MacOS/UI/Xaml/Controls/WebView/MacOSNativeWebView.Capabilities.cs | Implements macOS capabilities for UA/JS/zoom, messaging, scripts, cookies, and printing using native bridge callbacks. |
| src/Uno.UI.Runtime.Skia.MacOS/Native/NativeUno.cs | Adds P/Invoke signatures for new native macOS webview bridge APIs. |
| src/SamplesApp/UITests.Shared/UITests.Shared.projitems | Registers new manual SamplesApp WebView2 test pages. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_UserAgent.xaml.cs | Adds manual sample for CoreWebView2Settings.UserAgent. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_UserAgent.xaml | XAML for User-Agent sample page. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_Print.xaml.cs | Adds manual sample for PDF printing + print UI. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_Print.xaml | XAML for printing sample page. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_PostWebMessage.xaml.cs | Adds manual sample for host-to-page messaging. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_PostWebMessage.xaml | XAML for messaging sample page. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_LifecycleEvents.xaml.cs | Adds manual sample for lifecycle event ordering/logging. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_LifecycleEvents.xaml | XAML for lifecycle sample page. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_EnvironmentOptions.xaml.cs | Adds manual sample for env/controller options initialization. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_EnvironmentOptions.xaml | XAML for env/controller options sample page. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_DocumentCreatedScripts.xaml.cs | Adds manual sample for document-created scripts. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_DocumentCreatedScripts.xaml | XAML for document-created scripts sample page. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_CookieManager.xaml.cs | Adds manual sample for cookie manager operations. |
| src/SamplesApp/UITests.Shared/Microsoft_UI_Xaml_Controls/WebView2Tests/WebView2_CookieManager.xaml | XAML for cookie manager sample page. |
| src/AddIns/Uno.UI.WebView.Skia.X11/X11NativeWebView.cs | Adds capability implementations for X11 WebKitGTK provider (UA/JS/zoom/scripts/print; cookies remain unsupported). |
| src/.gitignore | Ignores macOS runtime native output folder. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
5709802 to
83c8c1b
Compare
|
Claude finished @morning4coffe-dev's task in 5m 42s —— View job PR Review: feat(webview2): Improve API parity across platformsTasks:
SummaryVerdict: fix-first — the PR is a solid, well-structured feature with good coverage across platforms and tests. A few issues should be addressed before it can land cleanly. Issues by severity
Cross-cutting notesNull URI in
Capability-interface design — the Test coverage — runtime tests are well-scoped to macOS and WASM, cover the intended scenarios (lifecycle ordering, cookie round-trip, init idempotency, environment rejection), and use PR draft state — the description calls this out explicitly (rebase needed, platform-wide validation pending). The inline comments above apply to the code as-is; the overall shape of the implementation is sound. |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-23754/wasm-skia-net9/index.html |
|
The build 222223 found UI Test snapshots differences: Details
|
|
Claude finished @morning4coffe-dev's task in 4m 31s —— View job PR Review: feat(webview2): Improve API parity across platformsTasks:
SummaryVerdict: fix-first — the overall shape is sound and most of the issues from the prior review pass have been addressed. Three items remain before this can land cleanly. Fixes confirmed since the prior review pass
Remaining issues
Cross-cutting notesCapability interface decomposition — the
Test coverage — well-scoped to macOS and WASM using |
|
|
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
Claude finished @morning4coffe-dev's task in 24s —— View job PR Review (latest push
|
| Severity | Count |
|---|---|
| Concern | 1 |
The one finding is posted inline. 41a72440 narrows the catch in the async void handlers OnLoaded and OnListClick of WebView2_CookieManager.xaml.cs to ArgumentException, InvalidOperationException and NotSupportedException. Any other exception, such as a COMException from a native cookie store, would escape the async void and crash the sample app. This contradicts the repo's async void try/catch rule. It is sample-only code and a small fix.
I didn't re-audit the earlier native, X11, macOS or iOS changes. They were covered and confirmed fixed in the prior passes.
| } | ||
| OutputText.Text = sb.ToString(); | ||
| } | ||
| catch (Exception ex) when (ex is ArgumentException or InvalidOperationException or NotSupportedException) |
There was a problem hiding this comment.
This catch is inside an async void handler (OnListClick, same for OnLoaded at line 25). Narrowing it to three exception types means anything else — e.g. COMException/IOException from the native cookie store, or OperationCanceledException — escapes the async void and tears the app down. The repo rule is that async void needs a wrapping try/catch. Either keep the broad catch in these handlers (the previous revision, with its comment) or add a final catch-all that reports to OutputText.
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-23754/docs/index.html |
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
|
The build 241181 found UI Test snapshots differences: Details
|
|
|
|
Claude finished @morning4coffe-dev's task in 39s —— View job PR Review: feat(webview2): Improve API parity across platformsVerdict: ship after a minor fix. I scoped this pass to the commits since the last review: the
Cross-cutting notes
|
| } | ||
| OutputText.Text = sb.ToString(); | ||
| } | ||
| catch (Exception ex) when (ex is ArgumentException or InvalidOperationException or NotSupportedException) |
There was a problem hiding this comment.
Narrowing this catch inside an async void handler means any other exception (e.g. IOException, COMException, OperationCanceledException from the native layer) now escapes and tears down the app. The removed comment was the reason the broad catch existed. Either keep the broad catch for async void handlers, or add COMException/IOException/OperationCanceledException to the filter. This also applies to the init handler at line ~25.
|
Tick the box to add this pull request to the merge queue (same as
|
| foreach (var remaining in _documentCreatedScripts.Values) | ||
| { | ||
| using var script = new WebKit.UserScript( | ||
| remaining, | ||
| WebKit.UserContentInjectedFrames.AllFrames, | ||
| WebKit.UserScriptInjectionTime.Start, | ||
| null, | ||
| null); | ||
| _webview.UserContentManager.AddScript(script); | ||
| } |
| foreach (var cookie in cookies) | ||
| { | ||
| if (string.Equals(cookie.Name, name, System.StringComparison.Ordinal)) | ||
| { | ||
| await RunCookieMutation(handle => NativeUno.uno_webview_delete_cookies(GetCapabilityHandle(), handle, cookie.Name, cookie.Domain, cookie.Path)); | ||
| } | ||
| } |
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-23754/docs/index.html |
|
The build 246706 found UI Test snapshots differences: Details
|
GitHub Issue: closes #12644, closes #17711, closes #22415
PR Type:
✨ Feature
What changed? 🚀
EnsureCoreWebView2Async(...)initialization with environment/controller options, initialization error reporting, and terminalClose()behavior.CoreWebView2with cookie management, printing, user-agent overrides, JavaScript and zoom settings, document-created scripts, host-to-page messaging, and content lifecycle events.WebView2Aotbackend.Close, preserves host-only cookie scope and native navigation flags, and installs supported messaging bridges before application callbacks.Current platform support
WebView2Aotand managed SDK)NavigateToStringbridge onlyNavigateToString; URI documents are not synthesized at load completionUnsupported platform requests fail explicitly instead of being silently ignored. Android's existing page-to-host channel is retained. Browser URI documents, including same-origin URI navigation, do not advertise the
NavigateToStringmessaging bridge.Validation
WebView2Aotcases passed, including cookie update/delete identity, native lifecycle behavior, and strict object-versus-JSON-looking-string message types.ContentLoading, lifecycle ordering, URI rejection, close behavior and strict message types.Exact-head CI build 234924 now passes all reported checks (64 successful, one neutral), including managed binaries, Linux runtime tests and the Release
net11.0-iossimulator build. The iOS log confirms that assembly optimization/linking ran and completed with zero errors, so the IL2037 verification gate is satisfied. This does not imply unrecorded manual device or screen-reader testing.CI follow-up: build 234907 exposed Apple
CS8625delegate-wrapper assignments before linking and stale Linux double-JSON expectations. Nullable native weak-delegate properties fix the compile boundary without suppression; the Linux assertions now require the exact WebView2 JSON type without changing production serialization or allowing alternative results. A real Apple-reference binding probe reproduces the compile errors before the correction and passes afterward; CI 234924 verifies the full managed/iOS build and Linux runtime paths.PR Checklist ✅
Screenshots Compare Test Runresults.