1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/render_process_host_impl.h"
6 #include "content/common/child_process_messages.h"
7 #include "content/public/browser/render_process_host.h"
8 #include "content/public/browser/render_process_host_observer.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/shell/browser/shell.h"
12 #include "content/test/content_browser_test.h"
13 #include "content/test/content_browser_test_utils.h"
14 #include "net/test/embedded_test_server/embedded_test_server.h"
19 int RenderProcessHostCount() {
20 content::RenderProcessHost::iterator hosts
=
21 content::RenderProcessHost::AllHostsIterator();
23 while (!hosts
.IsAtEnd()) {
24 if (hosts
.GetCurrentValue()->HasConnection())
31 class RenderProcessHostTest
: public ContentBrowserTest
,
32 public RenderProcessHostObserver
{
34 RenderProcessHostTest() : process_exits_(0), host_destructions_(0) {}
37 // RenderProcessHostObserver:
38 virtual void RenderProcessExited(RenderProcessHost
* host
,
39 base::ProcessHandle handle
,
40 base::TerminationStatus status
,
41 int exit_code
) OVERRIDE
{
44 virtual void RenderProcessHostDestroyed(RenderProcessHost
* host
) OVERRIDE
{
49 int host_destructions_
;
52 // Sometimes the renderer process's ShutdownRequest (corresponding to the
53 // ViewMsg_WasSwappedOut from a previous navigation) doesn't arrive until after
54 // the browser process decides to re-use the renderer for a new purpose. This
55 // test makes sure the browser doesn't let the renderer die in that case. See
56 // http://crbug.com/87176.
57 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest
,
58 ShutdownRequestFromActiveTabIgnored
) {
59 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
61 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
62 NavigateToURL(shell(), test_url
);
63 RenderProcessHost
* rph
=
64 shell()->web_contents()->GetRenderViewHost()->GetProcess();
66 host_destructions_
= 0;
68 rph
->AddObserver(this);
69 ChildProcessHostMsg_ShutdownRequest msg
;
70 rph
->OnMessageReceived(msg
);
72 // If the RPH sends a mistaken ChildProcessMsg_Shutdown, the renderer process
73 // will take some time to die. Wait for a second tab to load in order to give
74 // that time to happen.
75 NavigateToURL(CreateBrowser(), test_url
);
77 EXPECT_EQ(0, process_exits_
);
78 if (!host_destructions_
)
79 rph
->RemoveObserver(this);
82 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest
,
83 GuestsAreNotSuitableHosts
) {
84 // Set max renderers to 1 to force running out of processes.
85 content::RenderProcessHost::SetMaxRendererProcessCount(1);
87 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
89 GURL test_url
= embedded_test_server()->GetURL("/simple_page.html");
90 NavigateToURL(shell(), test_url
);
91 RenderProcessHost
* rph
=
92 shell()->web_contents()->GetRenderViewHost()->GetProcess();
93 // Make it believe it's a guest.
94 reinterpret_cast<RenderProcessHostImpl
*>(rph
)->SetIsGuestForTesting(true);
95 EXPECT_EQ(1, RenderProcessHostCount());
97 // Navigate to a different page.
98 GURL::Replacements replace_host
;
99 std::string
host_str("localhost"); // Must stay in scope with replace_host.
100 replace_host
.SetHostStr(host_str
);
101 GURL another_url
= embedded_test_server()->GetURL("/simple_page.html");
102 another_url
= another_url
.ReplaceComponents(replace_host
);
103 NavigateToURL(CreateBrowser(), another_url
);
105 // Expect that we got another process (the guest renderer was not reused).
106 EXPECT_EQ(2, RenderProcessHostCount());
110 } // namespace content