Skip to content

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

Closed
XinZhangBambu wants to merge 6 commits into
bambulab:masterfrom
XinZhangBambu:fix/webview-webkit-data-store-isolation
Closed

webview: isolate WKWebsiteDataStore and add async script message handler on macOS#7
XinZhangBambu wants to merge 6 commits into
bambulab:masterfrom
XinZhangBambu:fix/webview-webkit-data-store-isolation

Conversation

@XinZhangBambu

Copy link
Copy Markdown
Collaborator

Adds two macOS/WebKit-only capabilities to wxWebView, both opt-in and no-ops on other backends.

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 not written to disk. Must be called before Create(). 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.

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.

XinZhangBambu and others added 6 commits March 28, 2025 11:22
On macOS, AddScriptMessageHandler() injected its window.<name> alias into
the current document via synchronous RunScript() -> RunScriptSync(), which
busy-waits with while(!done) wxYield() for evaluateJavaScript's completion
handler. When the WKWebView is off-screen / in a background tab, macOS can
throttle or suspend its WebContent process so the handler never fires,
hanging the main thread.

Add a two-argument overload AddScriptMessageHandler(name, runScriptSync).
Only the macOS/WebKit backend overrides it; the base default ignores the
flag and falls back to the existing single-argument API, so GTK and Edge
need no changes. When runScriptSync is false on macOS the alias is injected
asynchronously via evaluateJavaScript.
Map SetUserDataPathOption to a per-instance WKWebsiteDataStore so dual
Studio processes do not share the default WebKit store. macOS 14+ uses
dataStoreForIdentifier with a UUID persisted under the lock path; older
OS keeps slot 0 on defaultDataStore and uses nonPersistent for others.

Co-authored-by: Cursor <cursoragent@cursor.com>
Expose an explicit API to force WKWebsiteDataStore.nonPersistentDataStore
before Create(). When enabled it takes precedence over SetUserDataPathOption
path/slot selection.

Co-authored-by: Cursor <cursoragent@cursor.com>
@XinZhangBambu

Copy link
Copy Markdown
Collaborator Author

Superseded by #8, which has the same diff but a clean commit history (rebased onto current master, only the three macOS WebView commits).

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.

1 participant