Skip to content

webview: isolate WKWebsiteDataStore and add async script message handler on macOS - #8

Merged
lanewei120 merged 1 commit into
bambulab:masterfrom
XinZhangBambu:fix/webview-webkit-data-store-isolation-clean
Sep 11, 2026
Merged

webview: isolate WKWebsiteDataStore and add async script message handler on macOS#8
lanewei120 merged 1 commit into
bambulab:masterfrom
XinZhangBambu:fix/webview-webkit-data-store-isolation-clean

Conversation

@XinZhangBambu

Copy link
Copy Markdown
Collaborator

Adds two macOS/WebKit-only capabilities to wxWebView, both opt-in and no-ops on other backends. Rebased onto current master, so this branch contains only the three commits below and touches only the macOS WebView files.

1. Non-persistent website data store (macOS)

New virtuals on wxWebView:

  • SetNonPersistentWebsiteDataStore(bool enable = true)
  • IsNonPersistentWebsiteDataStore() const

On the macOS/WebKit backend this makes the WKWebView use WKWebsiteDataStore.nonPersistentDataStore, so cookies, local storage and caches are isolated per view and never written to disk. Must be called before Create(). The default implementations are no-ops, so GTK and Edge are unaffected.

The macOS backend also honours SetUserDataPathOption() to isolate the on-disk data store when a persistent store is used.

2. Async AddScriptMessageHandler overload

New virtual overload:

  • AddScriptMessageHandler(const wxString& name, bool runScriptSync)

The default implementation ignores the flag and forwards to the existing single-argument overload, so GTK and Edge behave exactly as before. Only the macOS/WebKit backend honours runScriptSync == false.

Motivation: on macOS the handler bootstrap script is injected with a synchronous RunScriptSync() that spins wxYield() waiting for evaluateJavaScript. If the WKWebView is off-screen its WebContent process gets throttled, the callback never fires, and the main thread hangs. Passing false injects the alias asynchronously instead.

Commits

  • webview: add runScriptSync overload for macOS AddScriptMessageHandler
  • webview: isolate WKWebsiteDataStore via SetUserDataPathOption on macOS
  • webview: add SetNonPersistentWebsiteDataStore for macOS WKWebView

Files

  • include/wx/webview.h, interface/wx/webview.h — new virtuals + docs
  • include/wx/osx/webview_webkit.h, src/osx/webview_webkit.mm — macOS implementation

Note on overlap with #4

#4 solves the same AddScriptMessageHandler hang by changing the signature to AddScriptMessageHandler(const wxString& name, bool runScriptSync = true) across all backends. This branch instead adds a separate virtual overload so existing backend signatures stay untouched. The two approaches conflict — only one should be merged.

Supersedes #7 (same diff, but that branch carried three already-merged commits from my fork's master in its commit list).

Add two opt-in, macOS/WebKit-only capabilities to wxWebView.

SetNonPersistentWebsiteDataStore() makes the WKWebView use
WKWebsiteDataStore.nonPersistentDataStore, so that cookies, local storage
and caches are isolated per view and never written to disk. It must be
called before Create(). The macOS backend also honours
SetUserDataPathOption() to isolate the on-disk store when a persistent one
is used.

The new AddScriptMessageHandler(name, runScriptSync) overload allows
injecting the handler bootstrap script asynchronously. The synchronous
injection spins wxYield() waiting for evaluateJavaScript, which never
completes when the WKWebView is off-screen and its WebContent process is
throttled, hanging the main thread.

The default implementations of all new virtuals are no-ops or forward to
the existing single-argument overload, so wxGTK and wxMSW are unchanged.
@XinZhangBambu
XinZhangBambu force-pushed the fix/webview-webkit-data-store-isolation-clean branch from b1b125b to 5d3a57b Compare September 10, 2026 10:03
@lanewei120
lanewei120 merged commit 7c74234 into bambulab:master Sep 11, 2026
0 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants