Closed Bug 1674743 Opened 4 years ago Closed 4 years ago

Calling window.focus() in OOP iframe fires focus event asynchronously

Categories

(Core :: DOM: UI Events & Focus Handling, defect, P3)

defect

Tracking

()

RESOLVED FIXED
84 Branch
Fission Milestone M7
Tracking Status
firefox84 --- fixed

People

(Reporter: edgar, Assigned: edgar)

References

(Blocks 1 open bug)

Details

Attachments

(2 files)

Attached file test.html (deleted) —

Fission

With fission enabled, the focus event is dispatched asynchronously after calling window.focus() in an OOP iframe.

onload
before calling window.focus()
after calling window.focus()
window.onfocus: target: [object HTMLDocument], eventPhase: 1
window.onfocus: target: [object Window], eventPhase: 2
window.onfocus: target: [object Window], eventPhase: 2

non-Fission

The focus event used to be dispatched synchronously while calling window.focus() in the iframe.

onload
before calling window.focus()
window.onfocus: target: [object HTMLDocument], eventPhase: 1
window.onfocus: target: [object Window], eventPhase: 2
window.onfocus: target: [object Window], eventPhase: 2
after calling window.focus()

window.focus() on a non-OOP iframe would call nsContentUtils::RequestFrameFocus synchronously, https://searchfox.org/mozilla-central/rev/7b07725fe9dc5d59a525990fc1dba78bc8b82af1/dom/base/nsGlobalWindowOuter.cpp#5143, and then dispatch focus event synchronously.

But window.focus() on an OOP iframe sends an IPC message to its parent frame instead, https://searchfox.org/mozilla-central/rev/7b07725fe9dc5d59a525990fc1dba78bc8b82af1/dom/base/nsGlobalWindowOuter.cpp#5131-5136, then wait for parent to send IPC back, the focus event is fired asynchronously due to the bounce of the IPC.

Henri, do you have any idea how should we deal with this?

Flags: needinfo?(hsivonen)
Blocks: 1660752

Looks like it's synchronous in Chrome. Ouch.

(In reply to Edgar Chen [:edgar] from comment #2)

Henri, do you have any idea how should we deal with this?

Unclear. One option would be to try to fire the event synchronously in the process containing the frame and then somehow suppress the event from firing when the framed content catches up. It would be nice to know what Chrome actually does.

Flags: needinfo?(hsivonen)

That is interesting, those Chrome results seem different from https://github.com/whatwg/html/issues/3506. Does that mean they had to change things here or are we missing something?

Severity: -- → S3
Fission Milestone: --- → M7
Priority: -- → P3

(In reply to Anne (:annevk) from comment #4)

That is interesting, those Chrome results seem different from https://github.com/whatwg/html/issues/3506.

This seems a different case.

  • This bug is filed for window.focus() called in iframe process, and Chrome handles and fires focus event synchronously in the iframe process, but we do it asynchronously.
  • The spec one is for iframe.contentWindow.focus() called in parent frame process, both Chrome and us handle focus in iframe process asynchronously.

I discussed this with Neha and the conclusion was to try a fix similar to https://hg.mozilla.org/mozilla-central/rev/efecc359f305 . That is, let's try firing the event synchronously and suppressing the async event that we currently fire.

Assignee: nobody → hsivonen
Status: NEW → ASSIGNED

I have a local patch that calls nsFocusManager::WindowRaised before https://searchfox.org/mozilla-central/rev/e75e8e5b980ef18f4596a783fbc8a36621de7d1e/dom/base/nsGlobalWindowOuter.cpp#5132, then the focus event would be dispatched synchronously, but we still get async event after we send the IPC to parent frame, I feel that it is somehow related to the bug 1674702 that parent frame tries to blur and focus the OOP frame when it receives the focus IPC.

(In reply to Henri Sivonen (:hsivonen) from comment #6)

I discussed this with Neha and the conclusion was to try a fix similar to https://hg.mozilla.org/mozilla-central/rev/efecc359f305 . That is, let's try firing the event synchronously and suppressing the async event that we currently fire.

Oops. I was commenting on a different issue than the one on file. I was commenting on how the focus event fires in the framer when the framer focuses an OOP iframe. We have differences from Chrome, but the difference may be the we don't fire the event at all.

(In reply to Henri Sivonen (:hsivonen) from comment #8)

Oops. I was commenting on a different issue than the one on file. I was commenting on how the focus event fires in the framer when the framer focuses an OOP iframe. We have differences from Chrome, but the difference may be the we don't fire the event at all.

Are you talking about something like https://codepen.io/edgarchen-the-decoder/pen/GRqGNmo?
I also noticed that chrome fires blur event synchronously in framer when the framer focuses an OOP iframe, and we don't fire blur event at all.

Along those lines, but I was thinking of iframe.onfocus when calling iframe.contentWindow.focus(). It looks like we don't fire that event at all in the in-process case. I filed bug 1675484.

Attachment #9186236 - Attachment description: Bug 1674743 - Part 1: Synchronously raise window when window.focus() called in OOP iframe; → Bug 1674743 - Synchronously raise window when window.focus() called in OOP iframe;

The patch breaks dom/tests/mochitest/bugs/test_bug369306.html, https://treeherder.mozilla.org/logviewer?job_id=321230422&repo=try&lineNumber=3971.

Pushed by echen@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/17658a2394cf Synchronously raise window when window.focus() called in OOP iframe; r=hsivonen
Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/26486 for changes under testing/web-platform/tests
Status: ASSIGNED → RESOLVED
Closed: 4 years ago
Resolution: --- → FIXED
Target Milestone: --- → 84 Branch
Upstream PR was closed without merging

Edgar, it looks like the WPT in focus-restoration-in-different-site-iframes.html are unstable in Chrome: https://github.com/web-platform-tests/wpt/pull/26486/checks?check_run_id=1420212453

Could you have a look at that and check if this is a Chrome-bug or an issue with the test that would require changes in this bug?

Flags: needinfo?(echen)

We have a similar intermittent failure in our CI, see bug 1642889.
Let's see if we found something in bug 1642889 first.

Upstream PR merged by jgraham
Flags: needinfo?(echen)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: