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/utf_string_conversions.h" |
5 #include "content/browser/browser_thread_impl.h" | 6 #include "content/browser/browser_thread_impl.h" |
6 #include "content/browser/browser_url_handler.h" | 7 #include "content/browser/browser_url_handler.h" |
7 #include "content/browser/mock_content_browser_client.h" | 8 #include "content/browser/mock_content_browser_client.h" |
8 #include "content/browser/renderer_host/test_render_view_host.h" | 9 #include "content/browser/renderer_host/test_render_view_host.h" |
9 #include "content/browser/site_instance.h" | 10 #include "content/browser/site_instance.h" |
10 #include "content/browser/tab_contents/navigation_controller_impl.h" | 11 #include "content/browser/tab_contents/navigation_controller_impl.h" |
11 #include "content/browser/tab_contents/navigation_entry_impl.h" | 12 #include "content/browser/tab_contents/navigation_entry_impl.h" |
12 #include "content/browser/tab_contents/render_view_host_manager.h" | 13 #include "content/browser/tab_contents/render_view_host_manager.h" |
13 #include "content/browser/tab_contents/test_tab_contents.h" | 14 #include "content/browser/tab_contents/test_tab_contents.h" |
14 #include "content/browser/webui/empty_web_ui_factory.h" | 15 #include "content/browser/webui/empty_web_ui_factory.h" |
15 #include "content/common/test_url_constants.h" | 16 #include "content/common/test_url_constants.h" |
16 #include "content/common/view_messages.h" | 17 #include "content/common/view_messages.h" |
17 #include "content/public/browser/notification_details.h" | 18 #include "content/public/browser/notification_details.h" |
18 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
19 #include "content/public/browser/notification_types.h" | 20 #include "content/public/browser/notification_types.h" |
20 #include "content/public/browser/web_ui_controller.h" | 21 #include "content/public/browser/web_ui_controller.h" |
21 #include "content/public/common/page_transition_types.h" | 22 #include "content/public/common/page_transition_types.h" |
22 #include "content/public/common/url_constants.h" | 23 #include "content/public/common/url_constants.h" |
23 #include "content/test/test_browser_context.h" | 24 #include "content/test/test_browser_context.h" |
24 #include "content/test/test_notification_tracker.h" | 25 #include "content/test/test_notification_tracker.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "googleurl/src/url_util.h" | 27 #include "googleurl/src/url_util.h" |
| 28 #include "ui/base/javascript_message_type.h" |
27 #include "webkit/glue/webkit_glue.h" | 29 #include "webkit/glue/webkit_glue.h" |
28 | 30 |
29 using content::BrowserThread; | 31 using content::BrowserThread; |
30 using content::BrowserThreadImpl; | 32 using content::BrowserThreadImpl; |
31 using content::NavigationController; | 33 using content::NavigationController; |
32 using content::NavigationEntry; | 34 using content::NavigationEntry; |
33 using content::NavigationEntryImpl; | 35 using content::NavigationEntryImpl; |
34 using content::WebContents; | 36 using content::WebContents; |
35 using content::WebUIController; | 37 using content::WebUIController; |
36 | 38 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 std::string()); | 212 std::string()); |
211 dest_rvh2->SendShouldCloseACK(true); | 213 dest_rvh2->SendShouldCloseACK(true); |
212 static_cast<TestRenderViewHost*>(contents2.GetRenderManagerForTesting()-> | 214 static_cast<TestRenderViewHost*>(contents2.GetRenderManagerForTesting()-> |
213 pending_render_view_host())->SendNavigate(102, kNtpUrl); | 215 pending_render_view_host())->SendNavigate(102, kNtpUrl); |
214 dest_rvh2->OnSwapOutACK(); | 216 dest_rvh2->OnSwapOutACK(); |
215 | 217 |
216 EXPECT_EQ(active_rvh()->site_instance(), | 218 EXPECT_EQ(active_rvh()->site_instance(), |
217 contents2.GetRenderViewHost()->site_instance()); | 219 contents2.GetRenderViewHost()->site_instance()); |
218 } | 220 } |
219 | 221 |
| 222 // Ensure that the browser ignores most IPC messages that arrive from a |
| 223 // RenderViewHost that has been swapped out. We do not want to take |
| 224 // action on requests from a non-active renderer. The main exception is |
| 225 // for synchronous messages, which cannot be ignored without leaving the |
| 226 // renderer in a stuck state. See http://crbug.com/93427. |
| 227 TEST_F(RenderViewHostManagerTest, FilterMessagesWhileSwappedOut) { |
| 228 BrowserThreadImpl ui_thread(BrowserThread::UI, MessageLoop::current()); |
| 229 const GURL kNtpUrl(chrome::kTestNewTabURL); |
| 230 const GURL kDestUrl("http://www.google.com/"); |
| 231 |
| 232 // Navigate our first tab to the new tab page and then to the destination. |
| 233 NavigateActiveAndCommit(kNtpUrl); |
| 234 TestRenderViewHost* ntp_rvh = static_cast<TestRenderViewHost*>( |
| 235 contents()->GetRenderManagerForTesting()->current_host()); |
| 236 |
| 237 // Send an update title message and make sure it works. |
| 238 const string16 ntp_title = ASCIIToUTF16("NTP Title"); |
| 239 WebKit::WebTextDirection direction = WebKit::WebTextDirectionLeftToRight; |
| 240 EXPECT_TRUE(ntp_rvh->TestOnMessageReceived( |
| 241 ViewHostMsg_UpdateTitle(rvh()->routing_id(), 0, ntp_title, direction))); |
| 242 EXPECT_EQ(ntp_title, contents()->GetTitle()); |
| 243 |
| 244 // Navigate to a cross-site URL. |
| 245 contents()->GetController().LoadURL( |
| 246 kDestUrl, content::Referrer(), content::PAGE_TRANSITION_LINK, |
| 247 std::string()); |
| 248 EXPECT_TRUE(contents()->cross_navigation_pending()); |
| 249 TestRenderViewHost* dest_rvh = static_cast<TestRenderViewHost*>( |
| 250 contents()->GetRenderManagerForTesting()->pending_render_view_host()); |
| 251 ASSERT_TRUE(dest_rvh); |
| 252 EXPECT_NE(ntp_rvh, dest_rvh); |
| 253 |
| 254 // BeforeUnload finishes. |
| 255 ntp_rvh->SendShouldCloseACK(true); |
| 256 |
| 257 // Assume SwapOutACK times out, so the dest_rvh proceeds and commits. |
| 258 dest_rvh->SendNavigate(101, kDestUrl); |
| 259 |
| 260 // The new RVH should be able to update its title. |
| 261 const string16 dest_title = ASCIIToUTF16("Google"); |
| 262 EXPECT_TRUE(dest_rvh->TestOnMessageReceived( |
| 263 ViewHostMsg_UpdateTitle(rvh()->routing_id(), 101, dest_title, |
| 264 direction))); |
| 265 EXPECT_EQ(dest_title, contents()->GetTitle()); |
| 266 |
| 267 // The old renderer, being slow, now updates the title. It should be filtered |
| 268 // out and not take effect. |
| 269 EXPECT_TRUE(ntp_rvh->is_swapped_out()); |
| 270 EXPECT_TRUE(ntp_rvh->TestOnMessageReceived( |
| 271 ViewHostMsg_UpdateTitle(rvh()->routing_id(), 0, ntp_title, direction))); |
| 272 EXPECT_EQ(dest_title, contents()->GetTitle()); |
| 273 |
| 274 // We cannot filter out synchronous IPC messages, because the renderer would |
| 275 // be left waiting for a reply. We pick RunBeforeUnloadConfirm as an example |
| 276 // that can run easily within a unit test, and that needs to receive a reply |
| 277 // without showing an actual dialog. |
| 278 MockRenderProcessHost* ntp_process_host = |
| 279 static_cast<MockRenderProcessHost*>(ntp_rvh->process()); |
| 280 ntp_process_host->sink().ClearMessages(); |
| 281 const string16 msg = ASCIIToUTF16("Message"); |
| 282 bool result = false; |
| 283 string16 unused; |
| 284 ViewHostMsg_RunBeforeUnloadConfirm before_unload_msg( |
| 285 rvh()->routing_id(), kNtpUrl, msg, &result, &unused); |
| 286 // Enable pumping for check in BrowserMessageFilter::CheckCanDispatchOnUI. |
| 287 before_unload_msg.EnableMessagePumping(); |
| 288 EXPECT_TRUE(ntp_rvh->TestOnMessageReceived(before_unload_msg)); |
| 289 EXPECT_TRUE(ntp_process_host->sink().GetUniqueMessageMatching(IPC_REPLY_ID)); |
| 290 |
| 291 // Also test RunJavaScriptMessage. |
| 292 ntp_process_host->sink().ClearMessages(); |
| 293 ViewHostMsg_RunJavaScriptMessage js_msg( |
| 294 rvh()->routing_id(), msg, msg, kNtpUrl, |
| 295 ui::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, &result, &unused); |
| 296 js_msg.EnableMessagePumping(); |
| 297 EXPECT_TRUE(ntp_rvh->TestOnMessageReceived(js_msg)); |
| 298 EXPECT_TRUE(ntp_process_host->sink().GetUniqueMessageMatching(IPC_REPLY_ID)); |
| 299 } |
| 300 |
220 // When there is an error with the specified page, renderer exits view-source | 301 // When there is an error with the specified page, renderer exits view-source |
221 // mode. See WebFrameImpl::DidFail(). We check by this test that | 302 // mode. See WebFrameImpl::DidFail(). We check by this test that |
222 // EnableViewSourceMode message is sent on every navigation regardless | 303 // EnableViewSourceMode message is sent on every navigation regardless |
223 // RenderView is being newly created or reused. | 304 // RenderView is being newly created or reused. |
224 TEST_F(RenderViewHostManagerTest, AlwaysSendEnableViewSourceMode) { | 305 TEST_F(RenderViewHostManagerTest, AlwaysSendEnableViewSourceMode) { |
225 BrowserThreadImpl ui_thread(BrowserThread::UI, MessageLoop::current()); | 306 BrowserThreadImpl ui_thread(BrowserThread::UI, MessageLoop::current()); |
226 const GURL kNtpUrl(chrome::kTestNewTabURL); | 307 const GURL kNtpUrl(chrome::kTestNewTabURL); |
227 const GURL kUrl("view-source:http://foo"); | 308 const GURL kUrl("view-source:http://foo"); |
228 | 309 |
229 // We have to navigate to some page at first since without this, the first | 310 // We have to navigate to some page at first since without this, the first |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 // current one. | 696 // current one. |
616 EXPECT_TRUE(contents()->GetRenderManagerForTesting()-> | 697 EXPECT_TRUE(contents()->GetRenderManagerForTesting()-> |
617 pending_render_view_host() == NULL); | 698 pending_render_view_host() == NULL); |
618 EXPECT_EQ(evil_rvh, contents()->GetRenderManagerForTesting()->current_host()); | 699 EXPECT_EQ(evil_rvh, contents()->GetRenderManagerForTesting()->current_host()); |
619 | 700 |
620 // Also we should not have a pending navigation entry. | 701 // Also we should not have a pending navigation entry. |
621 NavigationEntry* entry = contents()->GetController().GetActiveEntry(); | 702 NavigationEntry* entry = contents()->GetController().GetActiveEntry(); |
622 ASSERT_TRUE(entry != NULL); | 703 ASSERT_TRUE(entry != NULL); |
623 EXPECT_EQ(kUrl2, entry->GetURL()); | 704 EXPECT_EQ(kUrl2, entry->GetURL()); |
624 } | 705 } |
OLD | NEW |