Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/process.h" | 8 #include "base/process.h" |
| 9 #include "chrome/browser/chrome_content_browser_client.h" | |
|
jam
2012/03/21 23:30:12
you're going to get a checkdeps failure for this..
Charlie Reis
2012/03/21 23:44:14
Yeah, I realized that... This seems like a limita
Charlie Reis
2012/03/21 23:51:17
Actually, we've already got a render_process_host_
| |
| 9 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/test/base/in_process_browser_test.h" | 11 #include "chrome/test/base/in_process_browser_test.h" |
| 11 #include "chrome/test/base/ui_test_utils.h" | 12 #include "chrome/test/base/ui_test_utils.h" |
| 12 #include "content/browser/renderer_host/render_process_host_impl.h" | 13 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 13 #include "content/browser/tab_contents/tab_contents.h" | 14 #include "content/browser/tab_contents/tab_contents.h" |
| 15 #include "content/common/child_process_messages.h" | |
| 14 #include "content/common/test_url_constants.h" | 16 #include "content/common/test_url_constants.h" |
| 17 #include "content/public/browser/browser_message_filter.h" | |
| 15 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/content_browser_client.h" | |
| 20 #include "content/public/browser/notification_service.h" | |
| 21 #include "content/public/browser/notification_types.h" | |
| 22 #include "content/public/browser/page_navigator.h" | |
| 16 #include "content/public/common/content_switches.h" | 23 #include "content/public/common/content_switches.h" |
| 24 #include "content/public/common/referrer.h" | |
| 25 #include "ipc/ipc_message_macros.h" | |
| 17 | 26 |
| 18 using content::WebContents; | 27 using content::WebContents; |
| 19 | 28 |
| 20 void PostQuit(MessageLoop* loop) { | 29 void PostQuit(MessageLoop* loop) { |
| 21 loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 30 loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 22 } | 31 } |
| 23 | 32 |
| 33 namespace { | |
| 34 | |
| 35 // Custom message filter to consume shutdown messages on demand. | |
| 36 class RenderProcessHostTestMessageFilter | |
| 37 : public content::BrowserMessageFilter { | |
| 38 public: | |
| 39 RenderProcessHostTestMessageFilter() : | |
| 40 should_filter_shutdown_requests_(false) {} | |
| 41 virtual ~RenderProcessHostTestMessageFilter() {} | |
| 42 | |
| 43 void set_should_filter_shutdown_requests(bool should_filter) { | |
| 44 should_filter_shutdown_requests_ = should_filter; | |
| 45 } | |
| 46 | |
| 47 bool OnMessageReceived(const IPC::Message& message, | |
| 48 bool* message_was_ok) OVERRIDE { | |
| 49 if (!should_filter_shutdown_requests_) | |
| 50 return false; | |
| 51 | |
| 52 bool handled = true; | |
| 53 IPC_BEGIN_MESSAGE_MAP_EX(RenderProcessHostTestMessageFilter, | |
| 54 message, *message_was_ok) | |
| 55 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ShutdownRequest, | |
| 56 OnShutdownRequest) | |
| 57 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 58 IPC_END_MESSAGE_MAP() | |
| 59 return handled; | |
| 60 } | |
| 61 | |
| 62 void OnShutdownRequest() { | |
| 63 // Discard the message so the browser doesn't see it. | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 bool should_filter_shutdown_requests_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(RenderProcessHostTestMessageFilter); | |
| 70 }; | |
| 71 | |
| 72 class RenderProcessHostTestBrowserClient | |
| 73 : public chrome::ChromeContentBrowserClient { | |
| 74 public: | |
| 75 RenderProcessHostTestBrowserClient() : | |
| 76 message_filter_(new RenderProcessHostTestMessageFilter()) {} | |
| 77 virtual ~RenderProcessHostTestBrowserClient() {} | |
| 78 | |
| 79 // ChromeContentBrowserClient implementation. | |
| 80 virtual void RenderProcessHostCreated(content::RenderProcessHost* host) | |
| 81 OVERRIDE { | |
| 82 // Install our custom filter first. | |
| 83 host->GetChannel()->AddFilter(message_filter_); | |
| 84 | |
| 85 // Install the remaining message filters. | |
| 86 ChromeContentBrowserClient::RenderProcessHostCreated(host); | |
| 87 } | |
| 88 | |
| 89 scoped_refptr<RenderProcessHostTestMessageFilter> message_filter_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(RenderProcessHostTestBrowserClient); | |
| 92 }; | |
| 93 | |
| 94 } // namespace | |
| 95 | |
| 24 class RenderProcessHostTest : public InProcessBrowserTest { | 96 class RenderProcessHostTest : public InProcessBrowserTest { |
| 25 public: | 97 public: |
| 26 RenderProcessHostTest() { | 98 RenderProcessHostTest() { |
| 27 EnableDOMAutomation(); | 99 EnableDOMAutomation(); |
| 28 } | 100 } |
| 29 | 101 |
| 30 int RenderProcessHostCount() { | 102 int RenderProcessHostCount() { |
| 31 content::RenderProcessHost::iterator hosts = | 103 content::RenderProcessHost::iterator hosts = |
| 32 content::RenderProcessHost::AllHostsIterator(); | 104 content::RenderProcessHost::AllHostsIterator(); |
| 33 int count = 0; | 105 int count = 0; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 49 | 121 |
| 50 // Ensure that the backgrounding / foregrounding gets a chance to run. | 122 // Ensure that the backgrounding / foregrounding gets a chance to run. |
| 51 content::BrowserThread::PostTaskAndReply( | 123 content::BrowserThread::PostTaskAndReply( |
| 52 content::BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 124 content::BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 53 base::Bind(&base::DoNothing), MessageLoop::QuitClosure()); | 125 base::Bind(&base::DoNothing), MessageLoop::QuitClosure()); |
| 54 MessageLoop::current()->Run(); | 126 MessageLoop::current()->Run(); |
| 55 | 127 |
| 56 return wc->GetRenderProcessHost()->GetHandle(); | 128 return wc->GetRenderProcessHost()->GetHandle(); |
| 57 } | 129 } |
| 58 | 130 |
| 131 virtual void SetUp() OVERRIDE { | |
| 132 old_browser_client_ = content::GetContentClient()->browser(); | |
| 133 content::GetContentClient()->set_browser(&browser_client_); | |
| 134 InProcessBrowserTest::SetUp(); | |
| 135 } | |
| 136 | |
| 137 virtual void TearDown() OVERRIDE { | |
| 138 InProcessBrowserTest::TearDown(); | |
| 139 content::GetContentClient()->set_browser(old_browser_client_); | |
| 140 } | |
| 141 | |
| 59 // When we hit the max number of renderers, verify that the way we do process | 142 // When we hit the max number of renderers, verify that the way we do process |
| 60 // sharing behaves correctly. In particular, this test is verifying that even | 143 // sharing behaves correctly. In particular, this test is verifying that even |
| 61 // when we hit the max process limit, that renderers of each type will wind up | 144 // when we hit the max process limit, that renderers of each type will wind up |
| 62 // in a process of that type, even if that means creating a new process. | 145 // in a process of that type, even if that means creating a new process. |
| 63 void TestProcessOverflow() { | 146 void TestProcessOverflow() { |
| 64 int tab_count = 1; | 147 int tab_count = 1; |
| 65 int host_count = 1; | 148 int host_count = 1; |
| 66 WebContents* tab1 = NULL; | 149 WebContents* tab1 = NULL; |
| 67 WebContents* tab2 = NULL; | 150 WebContents* tab2 = NULL; |
| 68 content::RenderProcessHost* rph1 = NULL; | 151 content::RenderProcessHost* rph1 = NULL; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 host_count++; | 219 host_count++; |
| 137 #endif | 220 #endif |
| 138 EXPECT_EQ(tab_count, browser()->tab_count()); | 221 EXPECT_EQ(tab_count, browser()->tab_count()); |
| 139 tab1 = browser()->GetWebContentsAt(tab_count - 1); | 222 tab1 = browser()->GetWebContentsAt(tab_count - 1); |
| 140 rph3 = tab1->GetRenderProcessHost(); | 223 rph3 = tab1->GetRenderProcessHost(); |
| 141 EXPECT_EQ(tab1->GetURL(), bookmarks); | 224 EXPECT_EQ(tab1->GetURL(), bookmarks); |
| 142 EXPECT_EQ(host_count, RenderProcessHostCount()); | 225 EXPECT_EQ(host_count, RenderProcessHostCount()); |
| 143 EXPECT_NE(rph1, rph3); | 226 EXPECT_NE(rph1, rph3); |
| 144 EXPECT_NE(rph2, rph3); | 227 EXPECT_NE(rph2, rph3); |
| 145 } | 228 } |
| 229 | |
| 230 protected: | |
| 231 RenderProcessHostTestBrowserClient browser_client_; | |
| 232 | |
| 233 private: | |
| 234 content::ContentBrowserClient* old_browser_client_; | |
| 146 }; | 235 }; |
| 147 | 236 |
| 148 | 237 |
| 149 class RenderProcessHostTestWithCommandLine : public RenderProcessHostTest { | 238 class RenderProcessHostTestWithCommandLine : public RenderProcessHostTest { |
| 150 protected: | 239 protected: |
| 151 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 240 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 152 InProcessBrowserTest::SetUpCommandLine(command_line); | 241 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 153 command_line->AppendSwitchASCII(switches::kRendererProcessLimit, "1"); | 242 command_line->AppendSwitchASCII(switches::kRendererProcessLimit, "1"); |
| 154 } | 243 } |
| 155 }; | 244 }; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 // Set max renderers to 1 to force running out of processes. | 339 // Set max renderers to 1 to force running out of processes. |
| 251 content::RenderProcessHost::SetMaxRendererProcessCount(1); | 340 content::RenderProcessHost::SetMaxRendererProcessCount(1); |
| 252 TestProcessOverflow(); | 341 TestProcessOverflow(); |
| 253 } | 342 } |
| 254 | 343 |
| 255 // Variation of the ProcessOverflow test, which is driven through command line | 344 // Variation of the ProcessOverflow test, which is driven through command line |
| 256 // parameter instead of direct function call into the class. | 345 // parameter instead of direct function call into the class. |
| 257 IN_PROC_BROWSER_TEST_F(RenderProcessHostTestWithCommandLine, ProcessOverflow) { | 346 IN_PROC_BROWSER_TEST_F(RenderProcessHostTestWithCommandLine, ProcessOverflow) { |
| 258 TestProcessOverflow(); | 347 TestProcessOverflow(); |
| 259 } | 348 } |
| 349 | |
| 350 // Ensure that if a second NTP is opened as the first one is exiting, we don't | |
| 351 // end up killing the second NTP as well. http://crbug.com/87176. | |
| 352 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, DontPutNTPInDyingProcess) { | |
| 353 // Change the first tab to be the new tab page (TYPE_WEBUI). | |
| 354 GURL newtab(chrome::kTestNewTabURL); | |
| 355 ui_test_utils::NavigateToURL(browser(), newtab); | |
| 356 content::RenderProcessHost* rph = | |
| 357 browser()->GetSelectedWebContents()->GetRenderProcessHost(); | |
| 358 RenderProcessHostImpl* process = static_cast<RenderProcessHostImpl*>(rph); | |
| 359 | |
| 360 // Before navigating away, make sure the ShutdownRequest message is "delayed" | |
| 361 // by consuming it and sending a new one later. | |
| 362 browser_client_.message_filter_->set_should_filter_shutdown_requests(true); | |
| 363 | |
| 364 // Now navigate to a page that causes a process swap, causing the NTP's | |
| 365 // process to send a ShutdownRequest message (which we "delay"). | |
| 366 GURL page1("data:text/html,hello world1"); | |
| 367 ui_test_utils::NavigateToURL(browser(), page1); | |
| 368 | |
| 369 // Before the ShutdownRequest message arrives, open a new tab with the NTP. | |
| 370 ui_test_utils::WindowedNotificationObserver nav_observer( | |
| 371 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 372 content::NotificationService::AllSources()); | |
| 373 ui_test_utils::NavigateToURLWithDisposition( | |
| 374 browser(), newtab, NEW_FOREGROUND_TAB, | |
| 375 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); | |
| 376 | |
| 377 // After the tab opens but before the navigation completes, simulate the | |
| 378 // ShutdownRequest message arriving. | |
| 379 browser_client_.message_filter_->set_should_filter_shutdown_requests(false); | |
| 380 process->OnShutdownRequest(); | |
| 381 | |
| 382 // Wait for the NTP navigation to finish. | |
| 383 nav_observer.Wait(); | |
| 384 WebContents* contents = browser()->GetSelectedWebContents(); | |
| 385 EXPECT_EQ(process, contents->GetRenderProcessHost()); | |
| 386 EXPECT_TRUE(process->HasConnection()); | |
| 387 } | |
| OLD | NEW |