Fix race condition in window key status detection
Moderate
Commit Hash:
d1224ed9f606406acf98603c0ce88be73d084609
Commit Time: 2025-10-31 00:54:42+00:00
Impact Level: Moderate
Generated By: webview2-upstream-sentry
Upstream Review:
View Upstream Review 🔗
📋 Summary
This commit fixes a race condition in the windowDidResignKey: method in render_widget_host_view_cocoa.mm. The condition check was changed from [NSApp keyWindow] == [self window] to [self window].isKeyWindow.
Problem scenario: When an app has multiple windows and a user clicks a notification, the window receives a windowDidResignKey notification. However, at that moment, [NSApp keyWindow] may temporarily still point to the current window, causing the condition to incorrectly evaluate to true, making the method return early without notifying the host. This leaves WebContents and Blink layer thinking they still have focus, causing JavaScript's document.hasFocus() to incorrectly return true.
Solution: Use the window's isKeyWindow property for the check. This property is updated before windowDidResignKey is called, so it correctly detects when the window has actually resigned key status.
Problem scenario: When an app has multiple windows and a user clicks a notification, the window receives a windowDidResignKey notification. However, at that moment, [NSApp keyWindow] may temporarily still point to the current window, causing the condition to incorrectly evaluate to true, making the method return early without notifying the host. This leaves WebContents and Blink layer thinking they still have focus, causing JavaScript's document.hasFocus() to incorrectly return true.
Solution: Use the window's isKeyWindow property for the check. This property is updated before windowDidResignKey is called, so it correctly detects when the window has actually resigned key status.
🎯 Impact Analysis
This change has a **moderate positive impact** on WebView2Mac. The fix addresses window focus state synchronization issues in multi-window scenarios, directly affecting WebView2Mac's core functionality.
Detailed impact analysis:
1. **Focus Management Improvement**: As an embedded web control, WebView2Mac is often used in applications with multiple windows. This fix ensures that when a window loses key status, WebContents is correctly notified and can properly update its internal focus state.
2. **JavaScript API Correctness**: The fix ensures that JavaScript APIs like document.hasFocus() return the correct focus state, which is crucial for web applications that rely on focus state to control their behavior.
3. **Notification Scenario Compatibility**: Specifically improves focus handling when users click notifications, which is a common scenario in macOS applications. For applications using WebView2Mac, when users interact with system notifications, the embedded web content can correctly perceive focus changes.
4. **Remote Cocoa Dependency**: Since WebView2Mac manages NSView through Remote Cocoa, and this change is in render_widget_host_view_cocoa.mm (a core Remote Cocoa file), it directly affects WebView2Mac's focus management mechanism.
5. **Compatibility**: This fix is backward compatible and doesn't break existing functionality; it only corrects behavior in edge cases.
**Recommendation**: The WebView2Mac team should verify this fix's behavior in multi-window scenarios and notification interaction scenarios to ensure focus state synchronization is correct. The fix can be validated by testing the return value of document.hasFocus().
Detailed impact analysis:
1. **Focus Management Improvement**: As an embedded web control, WebView2Mac is often used in applications with multiple windows. This fix ensures that when a window loses key status, WebContents is correctly notified and can properly update its internal focus state.
2. **JavaScript API Correctness**: The fix ensures that JavaScript APIs like document.hasFocus() return the correct focus state, which is crucial for web applications that rely on focus state to control their behavior.
3. **Notification Scenario Compatibility**: Specifically improves focus handling when users click notifications, which is a common scenario in macOS applications. For applications using WebView2Mac, when users interact with system notifications, the embedded web content can correctly perceive focus changes.
4. **Remote Cocoa Dependency**: Since WebView2Mac manages NSView through Remote Cocoa, and this change is in render_widget_host_view_cocoa.mm (a core Remote Cocoa file), it directly affects WebView2Mac's focus management mechanism.
5. **Compatibility**: This fix is backward compatible and doesn't break existing functionality; it only corrects behavior in edge cases.
**Recommendation**: The WebView2Mac team should verify this fix's behavior in multi-window scenarios and notification interaction scenarios to ensure focus state synchronization is correct. The fix can be validated by testing the return value of document.hasFocus().
Impacted Classes:
RenderWidgetHostViewCocoa
remote_cocoa::mojom::RenderWidgetHostNSViewHost