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

Side by Side Diff: chrome/browser/renderer_host/render_process_host_chrome_browsertest.cc

Issue 9751001: Don't allow a renderer to exit if we are using it in other views. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move test to chrome Created 8 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | content/browser/renderer_host/render_process_host_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/command_line.h" 5 #include "base/command_line.h"
6 #include "chrome/browser/chrome_content_browser_client.h"
6 #include "chrome/browser/debugger/devtools_window.h" 7 #include "chrome/browser/debugger/devtools_window.h"
7 #include "chrome/browser/ui/browser.h" 8 #include "chrome/browser/ui/browser.h"
8 #include "chrome/common/chrome_switches.h" 9 #include "chrome/common/chrome_switches.h"
9 #include "chrome/common/url_constants.h" 10 #include "chrome/common/url_constants.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"
13 #include "content/common/child_process_messages.h"
Charlie Reis 2012/03/22 00:27:13 Drat. I was able to get all of it to work in chro
14 #include "content/public/browser/browser_message_filter.h"
15 #include "content/public/browser/content_browser_client.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/notification_types.h"
18 #include "content/public/browser/page_navigator.h"
12 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/render_view_host.h" 20 #include "content/public/browser/render_view_host.h"
14 #include "content/public/browser/render_view_host_delegate.h" 21 #include "content/public/browser/render_view_host_delegate.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/referrer.h"
24 #include "ipc/ipc_message_macros.h"
15 25
16 using content::RenderViewHost; 26 using content::RenderViewHost;
17 using content::RenderWidgetHost; 27 using content::RenderWidgetHost;
18 28
19 namespace { 29 namespace {
20 30
21 int RenderProcessHostCount() { 31 // Custom message filter to consume shutdown messages on demand.
22 content::RenderProcessHost::iterator hosts = 32 class RenderProcessHostTestMessageFilter
23 content::RenderProcessHost::AllHostsIterator(); 33 : public content::BrowserMessageFilter {
24 int count = 0; 34 public:
25 while (!hosts.IsAtEnd()) { 35 RenderProcessHostTestMessageFilter() :
26 if (hosts.GetCurrentValue()->HasConnection()) 36 should_filter_shutdown_requests_(false) {}
27 count++; 37 virtual ~RenderProcessHostTestMessageFilter() {}
28 hosts.Advance(); 38
39 void set_should_filter_shutdown_requests(bool should_filter) {
40 should_filter_shutdown_requests_ = should_filter;
29 } 41 }
30 return count;
31 }
32 42
33 RenderViewHost* FindFirstDevToolsHost() { 43 bool OnMessageReceived(const IPC::Message& message,
34 content::RenderProcessHost::iterator hosts = 44 bool* message_was_ok) OVERRIDE {
35 content::RenderProcessHost::AllHostsIterator(); 45 if (!should_filter_shutdown_requests_)
36 for (; !hosts.IsAtEnd(); hosts.Advance()) { 46 return false;
37 content::RenderProcessHost* render_process_host = hosts.GetCurrentValue();
38 DCHECK(render_process_host);
39 if (!render_process_host->HasConnection())
40 continue;
41 content::RenderProcessHost::RenderWidgetHostsIterator iter(
42 render_process_host->GetRenderWidgetHostsIterator());
43 for (; !iter.IsAtEnd(); iter.Advance()) {
44 const RenderWidgetHost* widget = iter.GetCurrentValue();
45 DCHECK(widget);
46 if (!widget || !widget->IsRenderView())
47 continue;
48 RenderViewHost* host =
49 RenderViewHost::From(const_cast<RenderWidgetHost*>(widget));
50 47
51 content::RenderViewHostDelegate* host_delegate = host->GetDelegate(); 48 bool handled = true;
52 GURL url = host_delegate->GetURL(); 49 IPC_BEGIN_MESSAGE_MAP_EX(RenderProcessHostTestMessageFilter,
53 if (url.SchemeIs(chrome::kChromeDevToolsScheme)) 50 message, *message_was_ok)
54 return host; 51 IPC_MESSAGE_HANDLER(ChildProcessHostMsg_ShutdownRequest,
55 } 52 OnShutdownRequest)
53 IPC_MESSAGE_UNHANDLED(handled = false)
54 IPC_END_MESSAGE_MAP()
55 return handled;
56 } 56 }
57 return NULL; 57
58 } 58 void OnShutdownRequest() {
59 // Discard the message so the browser doesn't see it.
60 }
61
62 private:
63 bool should_filter_shutdown_requests_;
64
65 DISALLOW_COPY_AND_ASSIGN(RenderProcessHostTestMessageFilter);
66 };
67
68 class RenderProcessHostTestBrowserClient
69 : public chrome::ChromeContentBrowserClient {
70 public:
71 RenderProcessHostTestBrowserClient() :
72 message_filter_(new RenderProcessHostTestMessageFilter()) {}
73 virtual ~RenderProcessHostTestBrowserClient() {}
74
75 // ChromeContentBrowserClient implementation.
76 virtual void RenderProcessHostCreated(content::RenderProcessHost* host)
77 OVERRIDE {
78 // Install our custom filter first.
79 host->GetChannel()->AddFilter(message_filter_);
80
81 // Install the remaining message filters.
82 ChromeContentBrowserClient::RenderProcessHostCreated(host);
83 }
84
85 scoped_refptr<RenderProcessHostTestMessageFilter> message_filter_;
86
87 DISALLOW_COPY_AND_ASSIGN(RenderProcessHostTestBrowserClient);
88 };
59 89
60 } // namespace 90 } // namespace
61 91
62 typedef InProcessBrowserTest ChromeRenderProcessHostTest; 92 class ChromeRenderProcessHostTest : public InProcessBrowserTest {
93 public:
94 ChromeRenderProcessHostTest() {}
95
96 int RenderProcessHostCount() {
97 content::RenderProcessHost::iterator hosts =
98 content::RenderProcessHost::AllHostsIterator();
99 int count = 0;
100 while (!hosts.IsAtEnd()) {
101 if (hosts.GetCurrentValue()->HasConnection())
102 count++;
103 hosts.Advance();
104 }
105 return count;
106 }
107
108 RenderViewHost* FindFirstDevToolsHost() {
109 content::RenderProcessHost::iterator hosts =
110 content::RenderProcessHost::AllHostsIterator();
111 for (; !hosts.IsAtEnd(); hosts.Advance()) {
112 content::RenderProcessHost* render_process_host = hosts.GetCurrentValue();
113 DCHECK(render_process_host);
114 if (!render_process_host->HasConnection())
115 continue;
116 content::RenderProcessHost::RenderWidgetHostsIterator iter(
117 render_process_host->GetRenderWidgetHostsIterator());
118 for (; !iter.IsAtEnd(); iter.Advance()) {
119 const RenderWidgetHost* widget = iter.GetCurrentValue();
120 DCHECK(widget);
121 if (!widget || !widget->IsRenderView())
122 continue;
123 RenderViewHost* host =
124 RenderViewHost::From(const_cast<RenderWidgetHost*>(widget));
125
126 content::RenderViewHostDelegate* host_delegate = host->GetDelegate();
127 GURL url = host_delegate->GetURL();
128 if (url.SchemeIs(chrome::kChromeDevToolsScheme))
129 return host;
130 }
131 }
132 return NULL;
133 }
134
135 virtual void SetUp() OVERRIDE {
136 old_browser_client_ = content::GetContentClient()->browser();
137 content::GetContentClient()->set_browser(&browser_client_);
138 InProcessBrowserTest::SetUp();
139 }
140
141 virtual void TearDown() OVERRIDE {
142 InProcessBrowserTest::TearDown();
143 content::GetContentClient()->set_browser(old_browser_client_);
144 }
145
146 protected:
147 RenderProcessHostTestBrowserClient browser_client_;
148
149 private:
150 content::ContentBrowserClient* old_browser_client_;
151 };
63 152
64 // Ensure that DevTools opened to debug DevTools is launched in a separate 153 // Ensure that DevTools opened to debug DevTools is launched in a separate
65 // process when --process-per-tab is set. See crbug.com/69873. 154 // process when --process-per-tab is set. See crbug.com/69873.
66 IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostTest, 155 IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostTest,
67 DevToolsOnSelfInOwnProcessPPT) { 156 DevToolsOnSelfInOwnProcessPPT) {
68 CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); 157 CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
69 parsed_command_line.AppendSwitch(switches::kProcessPerTab); 158 parsed_command_line.AppendSwitch(switches::kProcessPerTab);
70 159
71 int tab_count = 1; 160 int tab_count = 1;
72 int host_count = 1; 161 int host_count = 1;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 RenderViewHost* devtools = FindFirstDevToolsHost(); 219 RenderViewHost* devtools = FindFirstDevToolsHost();
131 DCHECK(devtools); 220 DCHECK(devtools);
132 221
133 // DevTools start in a separate process. 222 // DevTools start in a separate process.
134 DevToolsWindow::ToggleDevToolsWindow( 223 DevToolsWindow::ToggleDevToolsWindow(
135 devtools, DEVTOOLS_TOGGLE_ACTION_INSPECT); 224 devtools, DEVTOOLS_TOGGLE_ACTION_INSPECT);
136 host_count++; 225 host_count++;
137 EXPECT_EQ(tab_count, browser()->tab_count()); 226 EXPECT_EQ(tab_count, browser()->tab_count());
138 EXPECT_EQ(host_count, RenderProcessHostCount()); 227 EXPECT_EQ(host_count, RenderProcessHostCount());
139 } 228 }
229
230 // Ensure that if a second NTP is opened as the first one is exiting, we don't
231 // end up killing the second NTP as well. http://crbug.com/87176.
232 IN_PROC_BROWSER_TEST_F(ChromeRenderProcessHostTest, DontPutNTPInDyingProcess) {
233 // Change the first tab to be the new tab page (TYPE_WEBUI).
234 GURL newtab(chrome::kChromeUINewTabURL);
235 ui_test_utils::NavigateToURL(browser(), newtab);
236 content::RenderProcessHost* rph =
237 browser()->GetSelectedWebContents()->GetRenderProcessHost();
238
239 // Before navigating away, make sure the ShutdownRequest message is "delayed"
240 // by consuming it and sending a new one later.
241 browser_client_.message_filter_->set_should_filter_shutdown_requests(true);
242
243 // Now navigate to a page that causes a process swap, causing the NTP's
244 // process to send a ShutdownRequest message (which we "delay").
245 GURL page1("data:text/html,hello world1");
246 ui_test_utils::NavigateToURL(browser(), page1);
247
248 // Before the ShutdownRequest message arrives, open a new tab with the NTP.
249 ui_test_utils::WindowedNotificationObserver nav_observer(
250 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
251 content::NotificationService::AllSources());
252 ui_test_utils::NavigateToURLWithDisposition(
253 browser(), newtab, NEW_FOREGROUND_TAB,
254 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
255
256 // After the tab opens but before the navigation completes, simulate the
257 // ShutdownRequest message arriving.
258 browser_client_.message_filter_->set_should_filter_shutdown_requests(false);
259 rph->OnMessageReceived(ChildProcessHostMsg_ShutdownRequest());
260
261 // Wait for the NTP navigation to finish.
262 nav_observer.Wait();
263 EXPECT_EQ(rph, browser()->GetSelectedWebContents()->GetRenderProcessHost());
264 EXPECT_TRUE(rph->HasConnection());
265 }
OLDNEW
« no previous file with comments | « no previous file | content/browser/renderer_host/render_process_host_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698