Move RFH initialization for non-visible pages into WCI::Init()

This ends up calling ResumeBlockedRequests() earlier for
non-visible WebContents (which currently consists of only
BackgroundContents) but this seems okay as a contract as there
isn't any initialization that needs to happy between creation
and usage. This is different from the "normal" case where there
is presumably a lot of setup that may need to occur before
a widget is Shown and made visible.

Bug: 545684
Change-Id: Ife0baa07f9f72ba3b69b1e278dc28c9037577ece
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1825476
Commit-Queue: Albert J. Wong <[email protected]>
Auto-Submit: Albert J. Wong <[email protected]>
Reviewed-by: John Abd-El-Malek <[email protected]>
Cr-Commit-Position: refs/heads/master@{#700610}
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index 6be79a8..c0d8053 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -2203,6 +2203,14 @@
   // happens after RenderFrameHostManager::Init.
   NotifySwappedFromRenderManager(
       nullptr, GetRenderManager()->current_frame_host(), true);
+
+  // For WebContents that are never shown, do critical initialization here which
+  // would normally only happen when the WebContents is shown.
+  if (params.is_never_visible) {
+    // This has just been created so there can only be one frame. Thus it is
+    // safe to initialize the root.
+    GetMainFrame()->Init();
+  }
 }
 
 void WebContentsImpl::OnWebContentsDestroyed(WebContentsImpl* web_contents) {
@@ -2838,8 +2846,6 @@
     RenderFrameHostImpl* rfh =
         RenderFrameHostImpl::FromID(render_process_id, main_frame_route_id);
     if (rfh) {
-      DCHECK(rfh->IsRenderFrameLive());
-      rfh->Init();
       // TODO(crbug.com/545684): It's super surprising that
       // ShouldCreateWebContents() is actually a way to allow
       // BackgroundWebContents to intercede and provide a completely different