Use outermost WebContents to get right view type.

Since we can have more than one nested WebContents, we should make
sure to get the outermost WebContents when initializing a popup
window.

Bug: 1002598
Change-Id: I568d31813e46af441760ef22afb9d3e4ae4c65ce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1819559
Commit-Queue: James MacLean <[email protected]>
Reviewed-by: Alex Moshchuk <[email protected]>
Cr-Commit-Position: refs/heads/master@{#700811}
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index f70faa6..d951280 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -3105,11 +3105,21 @@
   if (!widget_host_view)
     return;
 
-  RenderWidgetHostView* view = nullptr;
-  if (GetOuterWebContents()) {
-    view = GetOuterWebContents()->GetRenderWidgetHostView();
-  } else {
-    view = GetRenderWidgetHostView();
+  // GetOutermostWebContents() returns |this| if there are no outer WebContents.
+  RenderWidgetHostView* view =
+      GetOutermostWebContents()->GetRenderWidgetHostView();
+
+  gfx::Rect transformed_rect(initial_rect);
+  RenderWidgetHostView* this_view = GetRenderWidgetHostView();
+  if (this_view != view) {
+    // We need to transform the coordinates of initial_rect.
+    gfx::Point origin =
+        this_view->TransformPointToRootCoordSpace(initial_rect.origin());
+    gfx::Point bottom_right =
+        this_view->TransformPointToRootCoordSpace(initial_rect.bottom_right());
+    transformed_rect =
+        gfx::Rect(origin.x(), origin.y(), bottom_right.x() - origin.x(),
+                  bottom_right.y() - origin.y());
   }
 
   // Fullscreen child widgets are frames, other child widgets are popups.
@@ -3131,7 +3141,7 @@
     if (!widget_host_view->HasFocus())
       widget_host_view->Focus();
   } else {
-    widget_host_view->InitAsPopup(view, initial_rect);
+    widget_host_view->InitAsPopup(view, transformed_rect);
   }
 
   RenderWidgetHostImpl* render_widget_host_impl = widget_host_view->host();