Chromium Code Reviews| 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_browser_main_parts.h" | |
| 22 #include "content/shell/browser/shell_content_browser_client.h" | |
| 23 #include "content/test/test_content_browser_client.h" | |
| 20 #include "media/base/bind_to_current_loop.h" | 24 #include "media/base/bind_to_current_loop.h" |
| 21 #include "media/base/media_switches.h" | 25 #include "media/base/media_switches.h" |
| 22 #include "media/base/test_data_util.h" | 26 #include "media/base/test_data_util.h" |
| 23 #include "media/mojo/features.h" | 27 #include "media/mojo/features.h" |
| 24 #include "net/test/embedded_test_server/embedded_test_server.h" | 28 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 25 | 29 |
| 26 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
| 27 #include "base/win/windows_version.h" | 31 #include "base/win/windows_version.h" |
| 28 #endif | 32 #endif |
| 29 | 33 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 } | 70 } |
| 67 void RenderProcessHostDestroyed(RenderProcessHost* host) override { | 71 void RenderProcessHostDestroyed(RenderProcessHost* host) override { |
| 68 ++host_destructions_; | 72 ++host_destructions_; |
| 69 } | 73 } |
| 70 | 74 |
| 71 int process_exits_; | 75 int process_exits_; |
| 72 int host_destructions_; | 76 int host_destructions_; |
| 73 base::Closure process_exit_callback_; | 77 base::Closure process_exit_callback_; |
| 74 }; | 78 }; |
| 75 | 79 |
| 80 // A mock ContentBrowserClient that only considers a spare renderer to be a | |
| 81 // suitable host. | |
| 82 class SpareRendererContentBrowserClient : public TestContentBrowserClient { | |
| 83 public: | |
| 84 bool IsSuitableHost(RenderProcessHost* process_host, | |
| 85 const GURL& site_url) override { | |
| 86 if (RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()) { | |
| 87 return process_host == | |
| 88 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting(); | |
| 89 } | |
| 90 return true; | |
| 91 } | |
| 92 }; | |
| 93 | |
| 76 // Sometimes the renderer process's ShutdownRequest (corresponding to the | 94 // Sometimes the renderer process's ShutdownRequest (corresponding to the |
| 77 // ViewMsg_WasSwappedOut from a previous navigation) doesn't arrive until after | 95 // ViewMsg_WasSwappedOut from a previous navigation) doesn't arrive until after |
| 78 // the browser process decides to re-use the renderer for a new purpose. This | 96 // the browser process decides to re-use the renderer for a new purpose. This |
| 79 // test makes sure the browser doesn't let the renderer die in that case. See | 97 // test makes sure the browser doesn't let the renderer die in that case. See |
| 80 // http://crbug.com/87176. | 98 // http://crbug.com/87176. |
| 81 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, | 99 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, |
| 82 ShutdownRequestFromActiveTabIgnored) { | 100 ShutdownRequestFromActiveTabIgnored) { |
| 83 ASSERT_TRUE(embedded_test_server()->Start()); | 101 ASSERT_TRUE(embedded_test_server()->Start()); |
| 84 | 102 |
| 85 GURL test_url = embedded_test_server()->GetURL("/simple_page.html"); | 103 GURL test_url = embedded_test_server()->GetURL("/simple_page.html"); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 GURL::Replacements replace_host; | 141 GURL::Replacements replace_host; |
| 124 replace_host.SetHostStr("localhost"); | 142 replace_host.SetHostStr("localhost"); |
| 125 GURL another_url = embedded_test_server()->GetURL("/simple_page.html"); | 143 GURL another_url = embedded_test_server()->GetURL("/simple_page.html"); |
| 126 another_url = another_url.ReplaceComponents(replace_host); | 144 another_url = another_url.ReplaceComponents(replace_host); |
| 127 NavigateToURL(CreateBrowser(), another_url); | 145 NavigateToURL(CreateBrowser(), another_url); |
| 128 | 146 |
| 129 // Expect that we got another process (the guest renderer was not reused). | 147 // Expect that we got another process (the guest renderer was not reused). |
| 130 EXPECT_EQ(2, RenderProcessHostCount()); | 148 EXPECT_EQ(2, RenderProcessHostCount()); |
| 131 } | 149 } |
| 132 | 150 |
| 151 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostTaken) { | |
| 152 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 153 | |
| 154 RenderProcessHost::WarmupSpareRenderProcessHost( | |
| 155 ShellContentBrowserClient::Get()->browser_context()); | |
| 156 RenderProcessHost* spare_renderer = | |
| 157 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting(); | |
| 158 EXPECT_NE(nullptr, spare_renderer); | |
| 159 | |
| 160 GURL test_url = embedded_test_server()->GetURL("/simple_page.html"); | |
| 161 Shell* window = CreateBrowser(); | |
| 162 NavigateToURL(window, test_url); | |
| 163 | |
| 164 EXPECT_EQ(spare_renderer, | |
| 165 window->web_contents()->GetMainFrame()->GetProcess()); | |
| 166 | |
| 167 // The spare render process host should no longer be available. | |
| 168 EXPECT_EQ(nullptr, | |
| 169 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | |
| 170 } | |
| 171 | |
| 172 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostNotTaken) { | |
| 173 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 174 | |
| 175 RenderProcessHost::WarmupSpareRenderProcessHost( | |
| 176 ShellContentBrowserClient::Get()->off_the_record_browser_context()); | |
| 177 RenderProcessHost* spare_renderer = | |
| 178 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting(); | |
| 179 GURL test_url = embedded_test_server()->GetURL("/simple_page.html"); | |
| 180 Shell* window = CreateBrowser(); | |
| 181 NavigateToURL(window, test_url); | |
| 182 | |
| 183 // There should have been another process created for the navigation. | |
| 184 EXPECT_NE(spare_renderer, | |
| 185 window->web_contents()->GetMainFrame()->GetProcess()); | |
| 186 | |
| 187 // The spare RenderProcessHost should have been cleaned up. Note this | |
| 188 // behavior is identical to what would have happened if the RenderProcessHost | |
| 189 // were taken. | |
| 190 EXPECT_EQ(nullptr, | |
| 191 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | |
| 192 } | |
| 193 | |
| 194 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostKilled) { | |
| 195 RenderProcessHost::WarmupSpareRenderProcessHost( | |
| 196 ShellContentBrowserClient::Get()->browser_context()); | |
| 197 | |
| 198 RenderProcessHost* spare_renderer = | |
| 199 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting(); | |
| 200 mojom::TestServicePtr service; | |
| 201 ASSERT_NE(nullptr, spare_renderer); | |
| 202 BindInterface(spare_renderer, &service); | |
| 203 | |
| 204 base::RunLoop run_loop; | |
| 205 set_process_exit_callback(run_loop.QuitClosure()); | |
| 206 spare_renderer->AddObserver(this); // For process_exit_callback. | |
| 207 | |
| 208 // Should reply with a bad message and cause process death. | |
| 209 service->DoSomething(base::Bind(&base::DoNothing)); | |
| 210 run_loop.Run(); | |
| 211 | |
| 212 // The spare RenderProcessHost should disappear when its process dies. | |
| 213 EXPECT_EQ(nullptr, | |
| 214 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | |
| 215 } | |
| 216 | |
| 217 // Test that the spare renderer works correctly when the limit on the maximum | |
| 218 // number of processes is small. | |
| 219 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, | |
| 220 SpareRendererSurpressedMaxProcesses) { | |
| 221 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 222 | |
| 223 SpareRendererContentBrowserClient browser_client; | |
| 224 ContentBrowserClient* old_client = | |
| 225 SetBrowserClientForTesting(&browser_client); | |
| 226 | |
| 227 RenderProcessHost::SetMaxRendererProcessCount(1); | |
| 228 | |
| 229 // A process is created with shell startup, so with a maximum of one renderer | |
| 230 // process the spare RPH should not be created. | |
| 231 RenderProcessHost::WarmupSpareRenderProcessHost( | |
| 232 ShellContentBrowserClient::Get()->browser_context()); | |
| 233 EXPECT_EQ(nullptr, | |
| 234 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | |
| 235 | |
| 236 // A spare RPH should be created with a max of 2 renderer processes. | |
| 237 RenderProcessHost::SetMaxRendererProcessCount(2); | |
| 238 RenderProcessHost::WarmupSpareRenderProcessHost( | |
| 239 ShellContentBrowserClient::Get()->browser_context()); | |
| 240 RenderProcessHost* spare_renderer = | |
| 241 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting(); | |
| 242 EXPECT_NE(nullptr, spare_renderer); | |
| 243 | |
| 244 // Thanks to the injected SpareRendererContentBrowserClient and the limit on | |
| 245 // processes, the spare RPH will always be used via GetExistingProcessHost() | |
| 246 // rather than picked up via MaybeTakeSpareRenderProcessHost(). | |
| 247 GURL test_url = embedded_test_server()->GetURL("/simple_page.html"); | |
| 248 NavigateToURL(CreateBrowser(), test_url); | |
| 249 // The spare RPH should have been dropped during CreateBrowser(); | |
| 250 EXPECT_EQ(nullptr, | |
| 251 RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | |
|
Charlie Reis
2017/06/30 21:42:16
nit: Might be worth including a check that the new
mattcary
2017/07/03 08:03:02
Done.
| |
| 252 | |
| 253 // Revert to the default process limit and original ContentBrowserClient. | |
| 254 RenderProcessHost::SetMaxRendererProcessCount(0); | |
| 255 SetBrowserClientForTesting(old_client); | |
| 256 } | |
| 257 | |
| 133 class ShellCloser : public RenderProcessHostObserver { | 258 class ShellCloser : public RenderProcessHostObserver { |
| 134 public: | 259 public: |
| 135 ShellCloser(Shell* shell, std::string* logging_string) | 260 ShellCloser(Shell* shell, std::string* logging_string) |
| 136 : shell_(shell), logging_string_(logging_string) {} | 261 : shell_(shell), logging_string_(logging_string) {} |
| 137 | 262 |
| 138 protected: | 263 protected: |
| 139 // RenderProcessHostObserver: | 264 // RenderProcessHostObserver: |
| 140 void RenderProcessExited(RenderProcessHost* host, | 265 void RenderProcessExited(RenderProcessHost* host, |
| 141 base::TerminationStatus status, | 266 base::TerminationStatus status, |
| 142 int exit_code) override { | 267 int exit_code) override { |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 // Verify shutdown went as expected. | 447 // Verify shutdown went as expected. |
| 323 EXPECT_EQ(0, rph->get_audio_stream_count_for_testing()); | 448 EXPECT_EQ(0, rph->get_audio_stream_count_for_testing()); |
| 324 EXPECT_EQ(1, process_exits_); | 449 EXPECT_EQ(1, process_exits_); |
| 325 EXPECT_EQ(0, host_destructions_); | 450 EXPECT_EQ(0, host_destructions_); |
| 326 if (!host_destructions_) | 451 if (!host_destructions_) |
| 327 rph->RemoveObserver(this); | 452 rph->RemoveObserver(this); |
| 328 } | 453 } |
| 329 | 454 |
| 330 } // namespace | 455 } // namespace |
| 331 } // namespace content | 456 } // namespace content |
| OLD | NEW |