Skip to content

feat(webview2): Serve virtual host responses on Android - #25599

Draft
morning4coffe-dev wants to merge 4 commits into
dev/doti/webview2-bridge-corefrom
dev/doti/webview2-android-resources
Draft

morning4coffe-dev wants to merge 4 commits into
dev/doti/webview2-bridge-corefrom
dev/doti/webview2-android-resources

Conversation

@morning4coffe-dev

Copy link
Copy Markdown
Member

Part of #25395. Stacked on #25594 (and through it #25593 and #25592): the base branch is dev/doti/webview2-bridge-core, so only this PR's own commits are in the diff. This PR will be retargeted to master once those merge. Work in progress, opened as a draft.

Android's WebView already sees every request in ShouldInterceptRequest, so the virtual host stays https://0.0.0.1/ natively and nothing is rewritten (no ISupportsCustomSchemeVirtualHosts here). What was missing is that a response could only be a native one, the event ran synchronously on the IO thread, and GetDeferral did nothing.

What it does

  • Any INativeWebResourceResponse (including the managed one from CreateWebResourceResponse) is accepted through WebResourceResponseSnapshot and turned into a WebResourceResponse; requests nobody answers load normally.
  • A real deferral: the event is raised on the UI thread and the IO thread waits for the deferral, with a bounded timeout.
  • ResourceContext Document for main-frame navigations (helper with unit tests); request filters are honored.
  • Scripts and messaging on top of the shared bridge, without a new AndroidX.WebKit dependency: the bridge and user scripts are injected into intercepted text/html responses before the document is parsed (HtmlDocumentScriptInjector), and run on load for documents Uno does not intercept. This is isolated so that WebViewCompat.addDocumentStartJavaScript can replace it if the dependency is accepted (open question on WebView2 on Skia: WebResourceRequested responses and the APIs BlazorWebView depends on are not implemented #25395). DOMContentLoaded and PostWebMessage* work through the bridge and the existing unoWebView JavaScript interface.
  • WebMessageReceived.Source is the WebView's current URL at message time (a JavaScript interface has no origin) and only the main frame is forwarded as far as the API allows.

Verification

  • 159 WebView2 unit tests pass on macOS, including the new HtmlDocumentScriptInjector and WebResourceContextHelper tests.
  • Uno.UI.Runtime.Android builds for net10.0-android.
  • Not run: the new Android runtime tests (Given_WebView2_VirtualHostResponses) compile but have not been executed: the local emulator did not start (the only available AVD is not usable on this machine), so nothing has been run on an Android WebView yet.

Open

  • AndroidX.WebKit dependency (document-start script and origin-aware message listener).
  • addJavascriptInterface is visible to every frame; the limits are documented.
  • Not tested with a stock MAUI BlazorWebView.

Decode an intercepted document as a browser would, insert the bridge and user scripts ahead of the first page script, and re-encode it as UTF-8 with a byte order mark. Also infer a request's resource context from the main-frame flag and its headers.
Raise WebResourceRequested on the UI thread and block the IO thread until handlers and deferrals complete (bounded), accept managed responses, infer the resource context from the request, and add document-created scripts, chrome.webview messaging and DOMContentLoaded on top of the shared bridge (BeforeParse for served documents, Late otherwise). NavigationCompleted now follows onPageFinished.
@github-actions github-actions Bot added platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform area/skia ✏️ Categorizes an issue or PR as relevant to Skia area/automation Categorizes an issue or PR as relevant to project automation kind/documentation labels Oct 20, 2026
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 area/skia ✏️ Categorizes an issue or PR as relevant to Skia kind/documentation platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant