Skip to content

feat(webview2): Create detached CoreWebView2Environment - #25593

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

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

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

MAUI's BlazorWebView (WinUI handler) starts with CoreWebView2Environment.CreateWithOptionsAsync(...), uses the resulting environment only to call CreateWebResourceResponse(...), and then calls the parameterless EnsureCoreWebView2Async(). Both environment members threw NotImplementedException on every platform, which faults the handler's start task silently.

  • CoreWebView2Environment.CreateAsync() / CreateWithOptionsAsync(...): return a detached environment that records the browser folder, user data folder and options. Where the platform can answer (Win32), the browser is probed first so a missing WebView2 runtime fails with FileNotFoundException, as on Windows. UserDataFolder / BrowserVersionString answer from the recorded inputs; members that need a live WebView keep throwing NotImplementedException.
  • CoreWebView2Environment.CreateWebResourceResponse(...): returns a platform-neutral managed response (ordered, case-insensitive header list, lenient Name: Value\r\n parsing).
  • CoreWebView2EnvironmentOptions: all members are stored as plain data (inert unless a platform reads them).
  • WebView2.EnsureCoreWebView2Async(environment[, controllerOptions]): accepts the environment, so CoreWebView2.Environment returns it; the options are ignored by the platforms.
  • CoreWebView2Settings: AreDevToolsEnabled, AreDefaultContextMenusEnabled and IsStatusBarEnabled are stored (platform behavior unchanged; Win32 still reads DevTools from FeatureConfiguration).
  • Win32: WebResourceRequested accepts a response that is not a native one by building it with the WebView's ICoreWebView2Environment.CreateWebResourceResponse.

Android and Apple still drop managed responses; follow-up PRs add them.

Verification

  • New unit tests (19, including the null-check and environment-adoption tests, since WebView2 can be constructed in the unit-test host) 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.
  • Skia desktop runtime tests filtered to Given_CoreWebView2Environment: 4 passed, 5 skipped (the Win32-only ones).
  • SamplesApp desktop head builds, which compiles the Win32, macOS and X11 runtimes.
  • Not run: the Win32 response conversion compiles but was not run (no end-to-end test of serving a managed response on Windows), and nothing ran on X11, WASM, Android or iOS.

Notes

  • Defaults of CoreWebView2EnvironmentOptions follow the WebView2 documentation from memory and should be double-checked: EnableTrackingPrevention true, ReleaseChannels all four, ChannelSearchKind MostStable; TargetCompatibleBrowserVersion defaults to an empty string (WebView2 defaults it to the SDK's version).
  • GetHeader on a response header that is not present throws ArgumentException (believed to match WebView2's E_INVALIDARG).
  • A second, different environment passed to EnsureCoreWebView2Async is ignored (the first one wins).
  • No new public members beyond WinUI's.

Implement CreateAsync/CreateWithOptionsAsync, CreateWebResourceResponse with a managed response, CoreWebView2EnvironmentOptions as stored data, EnsureCoreWebView2Async(environment) and stored settings flags. Win32 translates managed responses into native ones.
@unodevops

Copy link
Copy Markdown
Contributor

[PlatformCondition(ConditionMode.Include, RuntimeTestPlatforms.Skia)]
public async Task When_CreateWithOptionsAsync_Records_User_Data_Folder()
{
var userDataFolder = Path.Combine(Path.GetTempPath(), "uno-webview2-data-" + Guid.NewGuid().ToString("N"));
[PlatformCondition(ConditionMode.Include, RuntimeTestPlatforms.SkiaWin32)]
public async Task When_CreateWithOptionsAsync_Missing_Browser_Folder_Throws()
{
var missingFolder = Path.Combine(Path.GetTempPath(), "uno-webview2-missing-" + Guid.NewGuid().ToString("N"));
@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 240314 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) 🚨🚨
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • Buttons.png
    • ClipboardTests.png-dark
    • CompositionEffectBrush.png-dark
    • CompositionEffectBrush.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • DisplayInformation.png-dark
    • DynamicFont.png-dark
    • DynamicFont.png
    • DoubleImageBrushInList.png-dark
    • DoubleImageBrushInList.png
    • Focus_FocusVisual_Properties.png-dark
    • Focus_FocusVisual_Properties.png
    • DropDownButtonPage.png-dark
    • ElementLevelTheme.png-dark
  • skia-windows-screenshots: 2458 changed over 2488

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedIconPage.png
    • AppBarButtonTest.png-dark
    • AppBarButtonTest.png
    • AppBarButtonWithIconTest.png-dark
    • AppBarToggleButtonTest.png-dark
    • ApplicationViewSizing.png
    • AppWindowClosing.png
    • AppWindowTitleBar Properties.png-dark
    • Attributed_text_FontSize_Changing.png-dark
    • Attributed_text_FontSize_Changing.png
    • AutoBorderStretchwithleftmargin.png
    • AutoBorderStretchwithrightmargin.png-dark
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • Battery.png-dark
    • Battery.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • BitmapIcon_Monochromatic.png-dark
    • BitmapIcon_Monochromatic.png

@unodevops

Copy link
Copy Markdown
Contributor

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

@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 246704 found UI Test snapshots differences: skia-linux-screenshots: 250, skia-windows-screenshots: 341

Details
  • skia-linux-screenshots: 250 changed over 2496

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedVisualsTestbedPage.png
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • ComboBox_MaxDropdownHeight.png-dark
    • ComboBox_MaxDropdownHeight.png
    • AnimatedVisualsTestbedPage.png-dark
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • BasicAcrylicBrushTest2.png-dark
    • BasicAcrylicBrushTest2.png
    • CompositionNineGridBrush_Source_Changes.png-dark
    • CompositionNineGridBrush_Source_Changes.png
    • Accessibility_Showcase.png
    • AutomationProperties_StructureAndProps.png-dark
    • AutomationProperties_StructureAndProps.png
    • BasicAcrylicBrushTest.png-dark
    • BasicAcrylicBrushTest.png
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • BreadcrumbBarPage.png-dark
  • skia-windows-screenshots: 341 changed over 2496

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • BasicAcrylicBrushTest2.png-dark
    • BasicAcrylicBrushTest2.png
    • Border_CornerRadius_BorderThickness.png-dark
    • Border_CornerRadius_BorderThickness.png
    • AutomationProperties_StructureAndProps.png-dark
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • Border_CornerRadius_Binding.png-dark
    • Border_CornerRadius_Binding.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • CenteredGridinGridwithtwofixedsizechildren.png-dark
    • CenteredGridinGridwithtwofixedsizechildren.png
    • Basics.png-dark
    • Basics.png
    • AnimatedBackVisualSourceSamplePage.png
    • AnimatedBackVisualSourceSamplePage.png-dark
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.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