Skip to content

feat(webview2): Add document scripts and messaging - #25592

Draft
morning4coffe-dev wants to merge 3 commits into
masterfrom
dev/doti/webview2-document-scripts
Draft

morning4coffe-dev wants to merge 3 commits into
masterfrom
dev/doti/webview2-document-scripts

Conversation

@morning4coffe-dev

Copy link
Copy Markdown
Member

Part of #25395 (the WebView2 members that BlazorWebView depends on). Carved out of the older draft #23754 as a small change on top of current master.

What this adds

CoreWebView2 members that were [NotImplemented] on every platform:

  • AddScriptToExecuteOnDocumentCreatedAsync / RemoveScriptToExecuteOnDocumentCreated
  • PostWebMessageAsString / PostWebMessageAsJson
  • the DOMContentLoaded event (and its args class)
  • CoreWebView2WebMessageReceivedEventArgs.Source

through three new internal capability interfaces (ISupportsDocumentCreatedScripts, ISupportsPostWebMessage, ISupportsDOMContentLoaded) next to the existing ones, so INativeWebView does not change. A platform without a capability behaves as before (the generated stubs' NotImplementedException or not-implemented report). Scripts added before the native view exists are queued and registered before its first navigation.

Win32 implements all three by forwarding to the real WebView2 (AddScriptToExecuteOnDocumentCreated, PostWebMessage*, add_DOMContentLoaded, get_Source). Other platforms are not changed here; a follow-up adds a chrome.webview bridge for the WebKit/Android/X11 hosts.

Verification

  • New unit tests (11) pass on macOS. The unit-test project needs the macOS ICU libraries on the library path to start (DYLD_LIBRARY_PATH=~/.nuget/packages/uno.icu-macos/<version>/runtimes/osx/native), independent of this change.
  • SamplesApp (desktop head, which builds the Win32, macOS and X11 runtimes and the runtime tests), Uno.UI.Runtime.Android and Uno.UI.Runtime.AppleUIKit build.
  • Not run: the new Win32 runtime tests (Given_WebView2_DocumentScripts, SkiaWin32 and NativeWinUI) compile but were not executed, and nothing ran on X11, macOS, Android or iOS. The Win32 behaviour relies on this PR's CI.

Notes

  • DOMContentLoaded.NavigationId uses Uno's own navigation counter (the one NavigationStarting/NavigationCompleted report), not WebView2's native id.
  • On a platform without the capability, a script added before the native view exists now fails asynchronously with NotImplementedException once the view is created; previously it threw synchronously.
  • Scripts already registered on a native view are not replayed if the template is re-applied; only queued ones are carried over.
  • The docs page has a capability matrix.

Implement AddScriptToExecuteOnDocumentCreatedAsync, RemoveScriptToExecuteOnDocumentCreated, PostWebMessageAsString/Json, DOMContentLoaded and WebMessageReceivedEventArgs.Source on CoreWebView2 behind optional native capabilities. Scripts added before the native WebView exists are queued and registered ahead of its first navigation. Platforms without the capabilities keep the generated stub behavior.
Forward document-created scripts, PostWebMessage, DOMContentLoaded and the web message source to the WebView2 COM interfaces.
Add unit tests using a fake native WebView, Win32/WinUI runtime tests, and document the new members with a per-platform matrix.
@unodevops

Copy link
Copy Markdown
Contributor

[TestInitialize]
public void Initialize()
{
_contentFolder = Path.Combine(Path.GetTempPath(), "UnoWebView2DocumentScripts", Guid.NewGuid().ToString("N"));

private void Navigate(WebView2 webView, string fileName, string html)
{
File.WriteAllText(Path.Combine(_contentFolder, fileName), html);
Comment on lines +206 to +209
catch (Exception e)
{
pending.Completion.TrySetException(e);
}
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 240312 found UI Test snapshots differences: skia-linux-screenshots: 118, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 118 changed over 2458

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • CompositionEffectBrush.png-dark
    • CompositionEffectBrush.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • Buttons.png-dark
    • Buttons.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ClipboardTests.png-dark
    • DisplayInformation.png-dark
    • Gamepad_CurrentReading.png-dark
    • Gamepad_Enumeration.png-dark
    • ImageBrushAlignmentXY.png-dark
    • ImageBrushAlignmentXY.png
    • DoubleImageBrushInList.png-dark
    • DoubleImageBrushInList.png
    • Focus_FocusVisual_Properties.png-dark
    • Focus_FocusVisual_Properties.png
  • skia-windows-screenshots: 2458 changed over 2488

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AndroidWindowInsets.png-dark
    • AnimatedVisualsTestbedPage.png-dark
    • AppBar_KeyBoard.png-dark
    • ApplicationViewSpanningRectsPage.png-dark
    • AppWindowPositionAndSize.png-dark
    • AppXamlDefinedResources.png
    • Attributed_text_Simple.png
    • AutoBorderStretchwithbottommargin.png
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • AutoSizedPathCentered.png
    • AutoSizedTopLeft.png-dark
    • AutoSuggestBox_SoftKeboard.png-dark
    • AutoSuggestBox_SoftKeboard.png
    • Basics_Automated.png-dark
    • Basics_Automated.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • BitmapIcon_Sizing.png-dark
    • BitmapIcon_Sizing.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/automation Categorizes an issue or PR as relevant to project automation kind/documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants