Don't process messages sent to dead routing ids.

BUG=556351, 445054
CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:linux_site_isolation

Review URL: https://codereview.chromium.org/1534933002

Cr-Commit-Position: refs/heads/master@{#375685}
diff --git a/chrome/browser/extensions/api/declarative_content/declarative_content_condition_tracker_test.cc b/chrome/browser/extensions/api/declarative_content/declarative_content_condition_tracker_test.cc
index 9ad530d..811af35 100644
--- a/chrome/browser/extensions/api/declarative_content/declarative_content_condition_tracker_test.cc
+++ b/chrome/browser/extensions/api/declarative_content/declarative_content_condition_tracker_test.cc
@@ -28,9 +28,12 @@
 
 scoped_ptr<content::WebContents>
 DeclarativeContentConditionTrackerTest::MakeTab() {
-  return make_scoped_ptr(content::WebContentsTester::CreateTestWebContents(
-      profile_.get(),
-      nullptr));
+  scoped_ptr<content::WebContents> tab(
+      content::WebContentsTester::CreateTestWebContents(profile_.get(),
+                                                        nullptr));
+  content::RenderFrameHostTester::For(tab->GetMainFrame())
+      ->InitializeRenderFrameIfNeeded();
+  return tab;
 }
 
 content::MockRenderProcessHost*
diff --git a/chrome/browser/web_applications/web_app_unittest.cc b/chrome/browser/web_applications/web_app_unittest.cc
index 38ef896..2cf437b 100644
--- a/chrome/browser/web_applications/web_app_unittest.cc
+++ b/chrome/browser/web_applications/web_app_unittest.cc
@@ -42,6 +42,8 @@
   web_app_info.description = description;
   web_app_info.app_url = url;
 
+  content::RenderFrameHostTester::For(main_rfh())
+      ->InitializeRenderFrameIfNeeded();
   RenderViewHostTester::TestOnMessageReceived(
       rvh(), ChromeViewHostMsg_DidGetWebApplicationInfo(0, web_app_info));
   scoped_ptr<web_app::ShortcutInfo> info =
diff --git a/content/browser/frame_host/render_frame_host_impl.cc b/content/browser/frame_host/render_frame_host_impl.cc
index b7df6ff..416221c 100644
--- a/content/browser/frame_host/render_frame_host_impl.cc
+++ b/content/browser/frame_host/render_frame_host_impl.cc
@@ -463,6 +463,10 @@
 }
 
 bool RenderFrameHostImpl::OnMessageReceived(const IPC::Message &msg) {
+  // Only process messages if the RenderFrame is alive.
+  if (!render_frame_created_)
+    return false;
+
   // Filter out most IPC messages if this frame is swapped out.
   // We still want to handle certain ACKs to keep our state consistent.
   if (is_swapped_out()) {
diff --git a/content/browser/renderer_host/render_widget_host_impl.cc b/content/browser/renderer_host/render_widget_host_impl.cc
index ae1ef2e..baf1cc4 100644
--- a/content/browser/renderer_host/render_widget_host_impl.cc
+++ b/content/browser/renderer_host/render_widget_host_impl.cc
@@ -438,6 +438,10 @@
 }
 
 bool RenderWidgetHostImpl::OnMessageReceived(const IPC::Message &msg) {
+  // Only process messages if the RenderWidget is alive.
+  if (!renderer_initialized())
+    return false;
+
   if (owner_delegate_ && owner_delegate_->OnMessageReceived(msg))
     return true;
 
diff --git a/content/browser/renderer_host/render_widget_host_view_mac_unittest.mm b/content/browser/renderer_host/render_widget_host_view_mac_unittest.mm
index ac3cb94..5178d8c 100644
--- a/content/browser/renderer_host/render_widget_host_view_mac_unittest.mm
+++ b/content/browser/renderer_host/render_widget_host_view_mac_unittest.mm
@@ -150,7 +150,9 @@
   MockRenderWidgetHostImpl(RenderWidgetHostDelegate* delegate,
                            RenderProcessHost* process,
                            int32_t routing_id)
-      : RenderWidgetHostImpl(delegate, process, routing_id, false) {}
+      : RenderWidgetHostImpl(delegate, process, routing_id, false) {
+    set_renderer_initialized(true);
+  }
 
   MOCK_METHOD0(Focus, void());
   MOCK_METHOD0(Blur, void());
@@ -754,6 +756,7 @@
   TestBrowserContext browser_context;
   MockRenderProcessHost* process_host =
       new MockRenderProcessHost(&browser_context);
+  process_host->Init();
 
   // Owned by its |cocoa_view()|.
   int32_t routing_id = process_host->GetNextRoutingID();
@@ -808,6 +811,7 @@
   MockRenderWidgetHostImpl* host =
       new MockRenderWidgetHostImpl(&delegate, process_host, routing_id);
   RenderWidgetHostViewMac* view = new RenderWidgetHostViewMac(host, false);
+  process_host->sink().ClearMessages();
 
   // Send an initial wheel event with NSEventPhaseBegan to the view.
   NSEvent* event1 = MockScrollWheelEventWithPhase(@selector(phaseBegan), 0);
@@ -849,6 +853,7 @@
   MockRenderWidgetHostImpl* host =
       new MockRenderWidgetHostImpl(&delegate, process_host, routing_id);
   RenderWidgetHostViewMac* view = new RenderWidgetHostViewMac(host, false);
+  process_host->sink().ClearMessages();
 
   // Add a delegate to the view.
   base::scoped_nsobject<MockRenderWidgetHostViewMacDelegate> view_delegate(
@@ -896,6 +901,7 @@
   TestBrowserContext browser_context;
   MockRenderProcessHost* process_host =
       new MockRenderProcessHost(&browser_context);
+  process_host->Init();
   int32_t routing_id = process_host->GetNextRoutingID();
 
   // Owned by its |cocoa_view()|.
@@ -938,6 +944,7 @@
   TestBrowserContext browser_context;
   MockRenderProcessHost* process_host =
       new MockRenderProcessHost(&browser_context);
+  process_host->Init();
   MockRenderWidgetHostDelegate delegate;
   int32_t routing_id = process_host->GetNextRoutingID();
   MockRenderWidgetHostImpl* host =
@@ -1020,6 +1027,7 @@
   MockRenderWidgetHostImpl* host =
       new MockRenderWidgetHostImpl(&delegate, process_host_, routing_id);
   RenderWidgetHostViewMac* view = new RenderWidgetHostViewMac(host, false);
+  process_host_->sink().ClearMessages();
 
   // We'll use this IPC message to ack events.
   InputEventAck ack(blink::WebInputEvent::GesturePinchUpdate,
diff --git a/content/browser/web_contents/web_contents_impl_unittest.cc b/content/browser/web_contents/web_contents_impl_unittest.cc
index 5464042..965da64 100644
--- a/content/browser/web_contents/web_contents_impl_unittest.cc
+++ b/content/browser/web_contents/web_contents_impl_unittest.cc
@@ -3319,6 +3319,7 @@
 TEST_F(WebContentsImplTest, ThemeColorChangeDependingOnFirstVisiblePaint) {
   TestWebContentsObserver observer(contents());
   TestRenderFrameHost* rfh = contents()->GetMainFrame();
+  rfh->InitializeRenderFrameIfNeeded();
 
   SkColor transparent = SK_ColorTRANSPARENT;
 
diff --git a/content/test/test_render_frame_host.cc b/content/test/test_render_frame_host.cc
index 6c48604..a842757 100644
--- a/content/test/test_render_frame_host.cc
+++ b/content/test/test_render_frame_host.cc
@@ -75,6 +75,7 @@
 
 void TestRenderFrameHost::InitializeRenderFrameIfNeeded() {
   if (!render_view_host()->IsRenderViewLive()) {
+    render_view_host()->GetProcess()->Init();
     RenderViewHostTester::For(render_view_host())->CreateTestRenderView(
         base::string16(), MSG_ROUTING_NONE, MSG_ROUTING_NONE, -1, false);
   }
diff --git a/content/test/web_contents_observer_sanity_checker.cc b/content/test/web_contents_observer_sanity_checker.cc
index 8ac2dd0f..24b8ac9 100644
--- a/content/test/web_contents_observer_sanity_checker.cc
+++ b/content/test/web_contents_observer_sanity_checker.cc
@@ -317,11 +317,9 @@
   // FrameHostMsg_RenderProcessGone is special internal IPC message that
   // should not be leaking outside of RenderFrameHost.
   CHECK(message.type() != FrameHostMsg_RenderProcessGone::ID);
+  CHECK(render_frame_host->IsRenderFrameLive());
 
-#if !defined(OS_MACOSX)
-// TODO(avi): Disabled because of http://crbug.com/445054
   AssertRenderFrameExists(render_frame_host);
-#endif
   return false;
 }