Skip to content

feat(webview2): Add host bridge and virtual host mapping - #25594

Draft
morning4coffe-dev wants to merge 5 commits into
dev/doti/webview2-stackfrom
dev/doti/webview2-bridge-core
Draft

morning4coffe-dev wants to merge 5 commits into
dev/doti/webview2-stackfrom
dev/doti/webview2-bridge-core

Conversation

@morning4coffe-dev

Copy link
Copy Markdown
Member

Part of #25395. Stacked on #25593 and #25592 (base branch dev/doti/webview2-stack is master plus those two PRs; this PR will be retargeted to master once they merge, so only its own five commits are in the diff).

Shared, platform-neutral pieces for web views that have no native chrome.webview and, on WebKit, cannot intercept https: macOS, iOS, Linux X11 and Android. No platform uses it yet; the follow-up PRs (one per platform) wire each native view to it. Nothing changes on any platform until then.

What this adds (internal)

  • WebViewMessageBridge: builds the JavaScript that gives a page window.chrome.webview (postMessage, addEventListener/removeEventListener, a hidden host entry point for PostWebMessageAsString/Json). It does nothing if chrome.webview already exists, and reports DOMContentLoaded once, from the top frame, through a reserved message. The outbound channel is a parameter.
  • ScriptBridgeWebViewCapabilities: implements ISupportsDocumentCreatedScripts, ISupportsPostWebMessage and ISupportsDOMContentLoaded (from feat(webview2): Add document scripts and messaging #25592) on top of two platform primitives (evaluate a script; apply the document-start script set), with three injection modes (DocumentStart, BeforeParse, Late).
  • WebViewVirtualHostMap + ISupportsCustomSchemeVirtualHosts: translates https://<virtual host>/... to a custom scheme (uno-app for now, one constant) and back, for hosts that cannot intercept https. CoreWebView2 applies it only when the native view opts in, at the points where URLs cross the boundary: Source, Navigate, NavigationStarting, NavigationCompleted, NewWindowRequested, WebMessageReceived.Source, the WebResourceRequested request URI and its filters. Without the capability behavior is unchanged. By default the non-routable 0.0.0.0/8 range (what Blazor's WebView uses) counts as virtual.
  • WebResourceResponseSnapshot: copies any response (managed or native) into status, reason, headers and body bytes.

Verification

  • 121 WebView2 unit tests pass on macOS (91 new). 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.
  • The generated bridge JavaScript is checked by a small Node script (WebViewMessageBridge.node.js, 8 checks pass). The repo has no JavaScript engine in the unit-test host, so it is a committed one-off and not part of the test run.
  • The desktop SamplesApp head builds (Win32, macOS and X11 runtimes included).
  • Not verified: nothing ran in a real WebKit or Android web view; the Android and iOS runtime projects were not compiled.

Open questions for review

  1. Scheme name (uno-app is one constant, WebViewVirtualHostMap.NativeScheme).
  2. Should the default 0.0.0.0/8 rule apply only to Blazor, rather than to every app using such URLs on an opted-in platform?
  3. Origin/Referer request headers keep the native uno-app:// form.
  4. The bridge is installed in every frame while WebView2 delivers only top-frame messages to CoreWebView2; frame filtering is left to each platform.
  5. Custom-scheme pages are not secure contexts on every WebKit version; to be checked in the macOS/iOS PR.

Shared script and capability base that give native views without WebView2 (WebKit, Android, WebKitGTK) chrome.webview messaging, document-created scripts and DOMContentLoaded. No platform uses it yet.
Views that cannot intercept https can opt in with ISupportsCustomSchemeVirtualHosts; CoreWebView2 then translates uno-app:// URLs to https:// at its boundary. Without the capability nothing changes.
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.

1 participant