OLD | NEW |
---|---|
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 int32_t FirstRenderProcessHostKey() { | |
48 content::RenderProcessHost::iterator hosts = | |
49 content::RenderProcessHost::AllHostsIterator(); | |
50 if (!hosts.IsAtEnd()) { | |
51 return hosts.GetCurrentKey(); | |
52 } | |
53 return 0; | |
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 Loading... | |
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 int32_t spare_key = FirstRenderProcessHostKey(); | |
Charlie Reis
2017/06/25 23:48:27
I don't understand why we need FirstRenderProcessH
mattcary
2017/06/26 14:45:06
Done. I wasn't sure what the most reliable abstrac
Charlie Reis
2017/06/26 21:22:49
Sorry, maybe I was unclear. Can we avoid FirstRen
mattcary
2017/06/28 13:14:38
Done, the key bit being that your shell()->web_con
| |
151 EXPECT_NE(0, spare_key); | |
152 | |
153 GURL test_url = embedded_test_server()->GetURL("/simple_page.html"); | |
154 NavigateToURL(CreateBrowser(), test_url); | |
155 | |
156 EXPECT_EQ(spare_key, FirstRenderProcessHostKey()); | |
157 | |
158 // The spare render process host should no longer be available. | |
159 EXPECT_EQ(nullptr, | |
160 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | |
161 } | |
162 | |
163 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostNotTaken) { | |
164 ASSERT_TRUE(embedded_test_server()->Start()); | |
165 | |
166 RenderProcessHost::WarmupSpareRenderProcessHost( | |
167 ShellContentBrowserClient::Get()->off_the_record_browser_context()); | |
168 int32_t spare_key = FirstRenderProcessHostKey(); | |
169 GURL test_url = embedded_test_server()->GetURL("/simple_page.html"); | |
170 NavigateToURL(CreateBrowser(), test_url); | |
171 | |
172 // There should have been another process created for the navigation. | |
173 EXPECT_NE(spare_key, FirstRenderProcessHostKey()); | |
174 | |
175 // The spare RenderProcessHost should have been cleaned up. Note this | |
176 // behavior is identical to what would have happened if the RenderProcessHost | |
177 // were taken. | |
178 EXPECT_EQ(nullptr, | |
179 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | |
180 } | |
181 | |
182 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostKilled) { | |
183 RenderProcessHost::WarmupSpareRenderProcessHost( | |
184 ShellContentBrowserClient::Get()->browser_context()); | |
185 | |
186 RenderProcessHost* spare = | |
187 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting(); | |
188 mojom::TestServicePtr service; | |
189 ASSERT_NE(nullptr, spare); | |
190 BindInterface(spare, &service); | |
191 | |
192 base::RunLoop run_loop; | |
193 set_process_exit_callback(run_loop.QuitClosure()); | |
194 spare->AddObserver(this); // For process_exit_callback. | |
195 | |
196 // Should reply with a bad message and cause process death. | |
197 service->DoSomething(base::Bind(&base::DoNothing)); | |
198 run_loop.Run(); | |
199 | |
200 // The spare RenderProcessHost should disappear when its process dies. | |
201 EXPECT_EQ(nullptr, | |
202 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | |
203 } | |
204 | |
205 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, | |
206 SpareRenderProcessHostStoragePartitionKilled) { | |
207 RenderProcessHost::WarmupSpareRenderProcessHost( | |
208 ShellContentBrowserClient::Get()->browser_context()); | |
209 EXPECT_NE(nullptr, | |
210 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | |
211 | |
212 ShellContentBrowserClient::Get() | |
213 ->browser_context() | |
214 ->ShutdownStoragePartitions(); | |
215 | |
216 // The spare RenderProcessHost should disappear along with the | |
217 // StoragePartitions. | |
218 EXPECT_EQ(nullptr, | |
219 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | |
220 } | |
221 | |
133 class ShellCloser : public RenderProcessHostObserver { | 222 class ShellCloser : public RenderProcessHostObserver { |
134 public: | 223 public: |
135 ShellCloser(Shell* shell, std::string* logging_string) | 224 ShellCloser(Shell* shell, std::string* logging_string) |
136 : shell_(shell), logging_string_(logging_string) {} | 225 : shell_(shell), logging_string_(logging_string) {} |
137 | 226 |
138 protected: | 227 protected: |
139 // RenderProcessHostObserver: | 228 // RenderProcessHostObserver: |
140 void RenderProcessExited(RenderProcessHost* host, | 229 void RenderProcessExited(RenderProcessHost* host, |
141 base::TerminationStatus status, | 230 base::TerminationStatus status, |
142 int exit_code) override { | 231 int exit_code) override { |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 // Verify shutdown went as expected. | 411 // Verify shutdown went as expected. |
323 EXPECT_EQ(0, rph->get_audio_stream_count_for_testing()); | 412 EXPECT_EQ(0, rph->get_audio_stream_count_for_testing()); |
324 EXPECT_EQ(1, process_exits_); | 413 EXPECT_EQ(1, process_exits_); |
325 EXPECT_EQ(0, host_destructions_); | 414 EXPECT_EQ(0, host_destructions_); |
326 if (!host_destructions_) | 415 if (!host_destructions_) |
327 rph->RemoveObserver(this); | 416 rph->RemoveObserver(this); |
328 } | 417 } |
329 | 418 |
330 } // namespace | 419 } // namespace |
331 } // namespace content | 420 } // namespace content |
OLD | NEW |