Bug 229139

Summary: document.hasFocus() returns true for unfocused pages
Product: WebKit Reporter: Carlos Garcia Campos <cgarcia>
Component: WebCore Misc.Assignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: aperez, bugs-noreply, cdumez, esprehn+autocc, ews-watchlist, kangil.han, webkit-bug-importer, youennf
Priority: P2 Keywords: Gtk, InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Test
none
Patch
aperez: review+, ews-feeder: commit-queue-
Patch
none
Patch for landing
ews-feeder: commit-queue-
Patch for landing
ews-feeder: commit-queue-
Patch for landing none

Description Carlos Garcia Campos 2021-08-16 07:33:20 PDT
Created attachment 435599 [details]
Test

This can be reproduced with the attached test, the web view title shows whether the document has the focus or not at any time. Move the focus to a new tab in MiniBrowser to see that it remains true for the hidden tab. The problem is that we are always claiming to have the focus when the web view is in the active window. This is not happening in safari, but I don't understand why. I noticed this because ikea.com website stores the active widget in localstorage using document.hasFocus(). When opening multiple tabs it starts writing local storage like crazy making the network and web processes consume 100% cpu. It doesn't happen when using multiple windows instead of tabs, because in that case only the tab in the active window returns true for document.hasFocus().
Comment 1 Carlos Garcia Campos 2021-08-16 07:36:20 PDT
Created attachment 435600 [details]
Patch
Comment 2 Adrian Perez 2021-08-16 07:45:42 PDT
Comment on attachment 435600 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=435600&action=review

> Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitWebView.cpp:801
> +static void testWebViewDocumentFocus(WebViewTest* test, gconstpointer)

Nice test, by the way :)
Comment 3 Carlos Garcia Campos 2021-08-16 23:59:29 PDT
The gtk unit test is failing under xvfb, I'll fix it. But I need help with the cocoa unit tests. WebAuthenticationPanel test looks unrelated, but maybe the test assumes the view has the focus while the panel is open or something like that?
Comment 4 Carlos Garcia Campos 2021-08-17 00:16:24 PDT
In the case of gtk I think the problem is that the toplevel window focus is given to the window by the window manager, so under xvfb we never set the WindowIsActive flag because there's no a window manager.
Comment 5 Carlos Garcia Campos 2021-08-17 00:27:50 PDT
Created attachment 435668 [details]
Patch
Comment 6 Carlos Garcia Campos 2021-08-18 02:28:55 PDT
The cocoa tests failures are because the credential container requires the document to be focused and the page isn't actually focused, now caught by document.hasFocus(). We just need to focus the view in all the tests.
Comment 7 Carlos Garcia Campos 2021-08-18 02:29:24 PDT
Created attachment 435755 [details]
Patch for landing
Comment 8 Carlos Garcia Campos 2021-08-18 03:31:27 PDT
Created attachment 435758 [details]
Patch for landing
Comment 9 EWS 2021-08-18 23:31:22 PDT
ChangeLog entry in Tools/ChangeLog contains OOPS!.
Comment 10 Carlos Garcia Campos 2021-08-18 23:37:27 PDT
Created attachment 435847 [details]
Patch for landing
Comment 11 EWS 2021-08-19 01:46:12 PDT
Committed r281228 (240665@main): <https://commits.webkit.org/240665@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 435847 [details].
Comment 12 Radar WebKit Bug Importer 2021-08-19 01:47:18 PDT
<rdar://problem/82115580>