Fix FocusOwningWebContents to handle RenderWidgetHosts for OOPIFs.
Previously, FocusOwningWebContents() would not focus anything when
called for an OOPIF's RenderWidgetHost. This is because
GetFocusedRenderWidgetHost() would always return that RWH back,
causing FocusOwningWebContents() to skip the call to
SetAsFocusedWebContentsIfNecessary() because the passed-in RWH matched
the focused RWH.
This is usually not a problem in Chrome, because inner WebContents
can't have OOPIFs and so an inner WebContents would only need to be
focused when this is called from a main frame's RenderWidgetHost, and
the outermost WebContents would probably already be focused via other
means. However, apparently inner WebContents could have OOPIFs in
embedders like Electron, and then this becomes problematic. This CL
fixes FocusOwningWebContents() to always pass in the main frame's
RenderWidgetHost to GetFocusedRenderWidgetHost(), since the latter was
never designed to take an OOPIF's RenderWidgetHost (it expects to take
an event arriving at a main frame's RenderWidgetHostView and then
target it to a subframe's RenderWidgetHost, if needed).
The setup in the added test is similar to ProcessSwapOnInnerContents,
which was also apparently added for an Electron-specific use case
(cross-process navigations inside a <webview>) which isn't currently
possible in regular Chrome.
Change-Id: If9559caf53274d415a360a976ebddfcc323d37dd
Bug: 1026056
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1922650
Reviewed-by: James MacLean <[email protected]>
Commit-Queue: Alex Moshchuk <[email protected]>
Cr-Commit-Position: refs/heads/master@{#716803}
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index fb62a28..eb383b9 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -6456,8 +6456,10 @@
if (!GuestMode::IsCrossProcessFrameGuest(this) && browser_plugin_guest_)
return;
+ RenderWidgetHostImpl* main_frame_widget_host =
+ GetMainFrame()->GetRenderWidgetHost();
RenderWidgetHostImpl* focused_widget =
- GetFocusedRenderWidgetHost(render_widget_host);
+ GetFocusedRenderWidgetHost(main_frame_widget_host);
if (focused_widget != render_widget_host &&
(!focused_widget ||