Show the RenderView/Page before showing RenderWidgets.

RenderWidgets start making compositor frames when shown. If the Page
IPC is delayed then they may start trying to produce BeginMainFrames
on a non-visible Page which is confusing to blink and incorrect.

[email protected]

Change-Id: If77b892d7d5a9d7606dff529519e7febf1e4440a
Bug: 908582
Reviewed-on: https://chromium-review.googlesource.com/c/1361937
Reviewed-by: Avi Drissman <[email protected]>
Commit-Queue: danakj <[email protected]>
Cr-Commit-Position: refs/heads/master@{#614060}
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index 3a7f429..12a41fb7 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -1639,6 +1639,13 @@
 void WebContentsImpl::WasShown() {
   controller_.SetActive(true);
 
+  // This shows the Page before showing the individual RenderWidgets, as
+  // RenderWidgets will work to produce compositor frames and handle input
+  // as soon as they are shown. But the Page and other classes do not expect to
+  // be producing frames when the Page is hidden. So we make sure the Page is
+  // shown first.
+  SendPageMessage(new PageMsg_WasShown(MSG_ROUTING_NONE));
+
   if (auto* view = GetRenderWidgetHostView()) {
     view->Show();
 #if defined(OS_MACOSX)
@@ -1649,8 +1656,6 @@
   if (!ShowingInterstitialPage())
     SetVisibilityForChildViews(true);
 
-  SendPageMessage(new PageMsg_WasShown(MSG_ROUTING_NONE));
-
   last_active_time_ = base::TimeTicks::Now();
   SetVisibility(Visibility::VISIBLE);
 
@@ -1668,6 +1673,12 @@
   // or in Picture-in-Picture mode, don't activate the "disable rendering"
   // optimization.
   if (!IsBeingCaptured() && !HasPictureInPictureVideo()) {
+    // This hides the individual RenderWidgets before hiding the Page, as
+    // RenderWidgets will work to produce compositor frames and handle input
+    // until they are hidden. But the Page and other classes do not expect to
+    // be producing frames when the Page is hidden. So we stop the widgets,
+    // then hide the Page.
+    //
     // |GetRenderViewHost()| can be NULL if the user middle clicks a link to
     // open a tab in the background, then closes the tab before selecting it.
     // This is because closing the tab calls WebContentsImpl::Destroy(), which