Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: content/browser/renderer_host/render_process_host_browsertest.cc

Issue 2929113002: Enable spare RenderProcessHost to be preinitialized. (Closed)
Patch Set: comments Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 #include "content/browser/renderer_host/render_process_host_impl.h" 9 #include "content/browser/renderer_host/render_process_host_impl.h"
10 #include "content/common/child_process_messages.h" 10 #include "content/common/child_process_messages.h"
11 #include "content/public/browser/render_frame_host.h" 11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/render_process_host.h" 12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/render_process_host_observer.h" 13 #include "content/public/browser/render_process_host_observer.h"
14 #include "content/public/browser/web_contents.h" 14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/url_constants.h" 15 #include "content/public/common/url_constants.h"
16 #include "content/public/test/content_browser_test.h" 16 #include "content/public/test/content_browser_test.h"
17 #include "content/public/test/content_browser_test_utils.h" 17 #include "content/public/test/content_browser_test_utils.h"
18 #include "content/public/test/test_service.mojom.h" 18 #include "content/public/test/test_service.mojom.h"
19 #include "content/shell/browser/shell.h" 19 #include "content/shell/browser/shell.h"
20 #include "content/shell/browser/shell_browser_context.h"
21 #include "content/shell/browser/shell_content_browser_client.h"
20 #include "media/base/bind_to_current_loop.h" 22 #include "media/base/bind_to_current_loop.h"
21 #include "media/base/media_switches.h" 23 #include "media/base/media_switches.h"
22 #include "media/base/test_data_util.h" 24 #include "media/base/test_data_util.h"
23 #include "media/mojo/features.h" 25 #include "media/mojo/features.h"
24 #include "net/test/embedded_test_server/embedded_test_server.h" 26 #include "net/test/embedded_test_server/embedded_test_server.h"
25 27
26 #if defined(OS_WIN) 28 #if defined(OS_WIN)
27 #include "base/win/windows_version.h" 29 #include "base/win/windows_version.h"
28 #endif 30 #endif
29 31
30 namespace content { 32 namespace content {
31 namespace { 33 namespace {
32 34
33 int RenderProcessHostCount() { 35 int RenderProcessHostCount() {
34 content::RenderProcessHost::iterator hosts = 36 content::RenderProcessHost::iterator hosts =
35 content::RenderProcessHost::AllHostsIterator(); 37 content::RenderProcessHost::AllHostsIterator();
36 int count = 0; 38 int count = 0;
37 while (!hosts.IsAtEnd()) { 39 while (!hosts.IsAtEnd()) {
38 if (hosts.GetCurrentValue()->HasConnection()) 40 if (hosts.GetCurrentValue()->HasConnection())
39 count++; 41 count++;
40 hosts.Advance(); 42 hosts.Advance();
41 } 43 }
42 return count; 44 return count;
43 } 45 }
44 46
47 RenderProcessHost* FirstRenderProcessHost() {
48 content::RenderProcessHost::iterator hosts =
49 content::RenderProcessHost::AllHostsIterator();
50 if (!hosts.IsAtEnd()) {
51 return hosts.GetCurrentValue();
52 }
53 return nullptr;
54 }
55
45 class RenderProcessHostTest : public ContentBrowserTest, 56 class RenderProcessHostTest : public ContentBrowserTest,
46 public RenderProcessHostObserver { 57 public RenderProcessHostObserver {
47 public: 58 public:
48 RenderProcessHostTest() : process_exits_(0), host_destructions_(0) {} 59 RenderProcessHostTest() : process_exits_(0), host_destructions_(0) {}
49 60
50 void SetUpCommandLine(base::CommandLine* command_line) override { 61 void SetUpCommandLine(base::CommandLine* command_line) override {
51 command_line->AppendSwitch(switches::kIgnoreAutoplayRestrictionsForTests); 62 command_line->AppendSwitch(switches::kIgnoreAutoplayRestrictionsForTests);
52 } 63 }
53 64
54 protected: 65 protected:
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 set_is_for_guests_only_for_testing(true); 130 set_is_for_guests_only_for_testing(true);
120 EXPECT_EQ(1, RenderProcessHostCount()); 131 EXPECT_EQ(1, RenderProcessHostCount());
121 132
122 // Navigate to a different page. 133 // Navigate to a different page.
123 GURL::Replacements replace_host; 134 GURL::Replacements replace_host;
124 replace_host.SetHostStr("localhost"); 135 replace_host.SetHostStr("localhost");
125 GURL another_url = embedded_test_server()->GetURL("/simple_page.html"); 136 GURL another_url = embedded_test_server()->GetURL("/simple_page.html");
126 another_url = another_url.ReplaceComponents(replace_host); 137 another_url = another_url.ReplaceComponents(replace_host);
127 NavigateToURL(CreateBrowser(), another_url); 138 NavigateToURL(CreateBrowser(), another_url);
128 139
129 // Expect that we got another process (the guest renderer was not reused). 140 // Expect that we got another process (the guest RenderProcessHost was not
141 // reused).
130 EXPECT_EQ(2, RenderProcessHostCount()); 142 EXPECT_EQ(2, RenderProcessHostCount());
131 } 143 }
132 144
145 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostTaken) {
146 ASSERT_TRUE(embedded_test_server()->Start());
147
148 RenderProcessHost::WarmupSpareRenderProcessHost(
149 ShellContentBrowserClient::Get()->browser_context());
150 RenderProcessHost* spare_renderer = FirstRenderProcessHost();
151 EXPECT_NE(nullptr, spare_renderer);
152 EXPECT_EQ(RenderProcessHostImpl::GetSpareRenderProcessHostForTesting(),
153 spare_renderer);
Charlie Reis 2017/06/26 21:22:50 We might not need this line, if we can just use Ge
mattcary 2017/06/28 13:14:38 Done.
154
155 GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
156 NavigateToURL(CreateBrowser(), test_url);
157
158 EXPECT_EQ(spare_renderer, FirstRenderProcessHost());
Charlie Reis 2017/06/26 21:22:50 I would suggest checking shell()->web_contents()->
mattcary 2017/06/28 13:14:38 Done, that's a good idea, thanks. This is what I w
159
160 // The spare render process host should no longer be available.
161 EXPECT_EQ(nullptr,
162 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
163 }
164
165 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostNotTaken) {
166 ASSERT_TRUE(embedded_test_server()->Start());
167
168 RenderProcessHost::WarmupSpareRenderProcessHost(
169 ShellContentBrowserClient::Get()->off_the_record_browser_context());
170 RenderProcessHost* spare_renderer = FirstRenderProcessHost();
171 GURL test_url = embedded_test_server()->GetURL("/simple_page.html");
172 NavigateToURL(CreateBrowser(), test_url);
173
174 // There should have been another process created for the navigation.
175 EXPECT_NE(spare_renderer, FirstRenderProcessHost());
Charlie Reis 2017/06/26 21:22:50 Same here.
mattcary 2017/06/28 13:14:38 Done.
176
177 // The spare RenderProcessHost should have been cleaned up. Note this
178 // behavior is identical to what would have happened if the RenderProcessHost
179 // were taken.
180 EXPECT_EQ(nullptr,
181 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
182 }
183
184 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostKilled) {
185 RenderProcessHost::WarmupSpareRenderProcessHost(
186 ShellContentBrowserClient::Get()->browser_context());
187
188 RenderProcessHost* spare =
189 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting();
190 mojom::TestServicePtr service;
191 ASSERT_NE(nullptr, spare);
192 BindInterface(spare, &service);
193
194 base::RunLoop run_loop;
195 set_process_exit_callback(run_loop.QuitClosure());
196 spare->AddObserver(this); // For process_exit_callback.
197
198 // Should reply with a bad message and cause process death.
199 service->DoSomething(base::Bind(&base::DoNothing));
200 run_loop.Run();
201
202 // The spare RenderProcessHost should disappear when its process dies.
203 EXPECT_EQ(nullptr,
204 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
205 }
206
207 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest,
208 SpareRenderProcessHostStoragePartitionKilled) {
209 RenderProcessHost::WarmupSpareRenderProcessHost(
210 ShellContentBrowserClient::Get()->browser_context());
211 EXPECT_NE(nullptr,
212 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
213
214 ShellContentBrowserClient::Get()
215 ->browser_context()
216 ->ShutdownStoragePartitions();
217
218 // The spare RenderProcessHost should disappear along with the
219 // StoragePartitions.
220 EXPECT_EQ(nullptr,
221 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting());
222 }
223
133 class ShellCloser : public RenderProcessHostObserver { 224 class ShellCloser : public RenderProcessHostObserver {
134 public: 225 public:
135 ShellCloser(Shell* shell, std::string* logging_string) 226 ShellCloser(Shell* shell, std::string* logging_string)
136 : shell_(shell), logging_string_(logging_string) {} 227 : shell_(shell), logging_string_(logging_string) {}
137 228
138 protected: 229 protected:
139 // RenderProcessHostObserver: 230 // RenderProcessHostObserver:
140 void RenderProcessExited(RenderProcessHost* host, 231 void RenderProcessExited(RenderProcessHost* host,
141 base::TerminationStatus status, 232 base::TerminationStatus status,
142 int exit_code) override { 233 int exit_code) override {
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 // Verify shutdown went as expected. 413 // Verify shutdown went as expected.
323 EXPECT_EQ(0, rph->get_audio_stream_count_for_testing()); 414 EXPECT_EQ(0, rph->get_audio_stream_count_for_testing());
324 EXPECT_EQ(1, process_exits_); 415 EXPECT_EQ(1, process_exits_);
325 EXPECT_EQ(0, host_destructions_); 416 EXPECT_EQ(0, host_destructions_);
326 if (!host_destructions_) 417 if (!host_destructions_)
327 rph->RemoveObserver(this); 418 rph->RemoveObserver(this);
328 } 419 }
329 420
330 } // namespace 421 } // namespace
331 } // namespace content 422 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698