Make headless mode visibility change notifications asynchronous on Mac

Commit: b7c93465 | 2025-10-20 16:38:27+00:00

← Back to List

Make headless mode visibility change notifications asynchronous on Mac

Minor
Commit Hash: b7c9346597b9c5ea671afd3366d0da782e8469d8
Commit Time: 2025-10-20 16:38:27+00:00
Impact Level: Minor
Generated By: webview2-upstream-sentry
Upstream Review: View Upstream Review 🔗

📋 Summary

This commit modifies the handling of visibility state changes in headless mode. In headless mode, the visibility state is now checked to determine if an actual change has occurred, and the OnVisibilityChanged notification is sent asynchronously only when the state truly changes, rather than being invoked synchronously. This makes the behavior in headless mode more similar to that in UI mode and addresses the issue where document.visibilityState cannot be reliably propagated when running in headless mode. Specific changes include: 1) adding logic to check for state changes; 2) changing the OnVisibilityChanged invocation from synchronous to asynchronous execution via PostTask; 3) using WeakPtr to ensure the object remains valid when the callback is executed.

🎯 Impact Analysis

The impact of this change on WebView2Mac is mainly reflected in the HostingNSWindowBridge class, as this class inherits from NativeWidgetNSWindowBridge and may override or depend on the SetVisibilityState method. The main impacts include: 1) If HostingNSWindowBridge overrides the SetVisibilityState method, compatibility with upstream changes must be ensured; 2) In headless mode, the timing of visibility state change notifications shifts from synchronous to asynchronous, which may affect downstream code that relies on receiving visibility change notifications immediately; 3) The asynchronous notification mechanism uses WeakPtr, enhancing the safety of object lifecycles, but also means that notifications may be dropped when objects are destroyed; 4) This change mainly affects behavior in headless mode, with minimal impact on normal window mode. WebView2Mac needs to test whether visibility state changes still work correctly in headless mode.

Impacted Classes:

NativeWidgetNSWindowBridge HostingNSWindowBridge