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_impl.h" | 10 #include "content/browser/site_instance_impl.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/common/test_url_constants.h" | 15 #include "content/common/test_url_constants.h" |
15 #include "content/common/view_messages.h" | 16 #include "content/common/view_messages.h" |
16 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
17 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
18 #include "content/public/browser/notification_types.h" | 19 #include "content/public/browser/notification_types.h" |
19 #include "content/public/browser/web_ui_controller.h" | 20 #include "content/public/browser/web_ui_controller.h" |
20 #include "content/public/browser/web_ui_controller_factory.h" | 21 #include "content/public/browser/web_ui_controller_factory.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::BrowserContext; | 31 using content::BrowserContext; |
30 using content::BrowserThread; | 32 using content::BrowserThread; |
31 using content::BrowserThreadImpl; | 33 using content::BrowserThreadImpl; |
32 using content::NavigationController; | 34 using content::NavigationController; |
33 using content::NavigationEntry; | 35 using content::NavigationEntry; |
34 using content::NavigationEntryImpl; | 36 using content::NavigationEntryImpl; |
35 using content::SiteInstance; | 37 using content::SiteInstance; |
36 using content::WebContents; | 38 using content::WebContents; |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 std::string()); | 228 std::string()); |
227 dest_rvh2->SendShouldCloseACK(true); | 229 dest_rvh2->SendShouldCloseACK(true); |
228 static_cast<TestRenderViewHost*>(contents2.GetRenderManagerForTesting()-> | 230 static_cast<TestRenderViewHost*>(contents2.GetRenderManagerForTesting()-> |
229 pending_render_view_host())->SendNavigate(102, kNtpUrl); | 231 pending_render_view_host())->SendNavigate(102, kNtpUrl); |
230 dest_rvh2->OnSwapOutACK(); | 232 dest_rvh2->OnSwapOutACK(); |
231 | 233 |
232 EXPECT_EQ(active_rvh()->site_instance(), | 234 EXPECT_EQ(active_rvh()->site_instance(), |
233 contents2.GetRenderViewHost()->site_instance()); | 235 contents2.GetRenderViewHost()->site_instance()); |
234 } | 236 } |
235 | 237 |
| 238 // Ensure that the browser ignores most IPC messages that arrive from a |
| 239 // RenderViewHost that has been swapped out. We do not want to take |
| 240 // action on requests from a non-active renderer. The main exception is |
| 241 // for synchronous messages, which cannot be ignored without leaving the |
| 242 // renderer in a stuck state. See http://crbug.com/93427. |
| 243 TEST_F(RenderViewHostManagerTest, FilterMessagesWhileSwappedOut) { |
| 244 BrowserThreadImpl ui_thread(BrowserThread::UI, MessageLoop::current()); |
| 245 const GURL kNtpUrl(chrome::kTestNewTabURL); |
| 246 const GURL kDestUrl("http://www.google.com/"); |
| 247 |
| 248 // Navigate our first tab to the new tab page and then to the destination. |
| 249 NavigateActiveAndCommit(kNtpUrl); |
| 250 TestRenderViewHost* ntp_rvh = static_cast<TestRenderViewHost*>( |
| 251 contents()->GetRenderManagerForTesting()->current_host()); |
| 252 |
| 253 // Send an update title message and make sure it works. |
| 254 const string16 ntp_title = ASCIIToUTF16("NTP Title"); |
| 255 WebKit::WebTextDirection direction = WebKit::WebTextDirectionLeftToRight; |
| 256 EXPECT_TRUE(ntp_rvh->TestOnMessageReceived( |
| 257 ViewHostMsg_UpdateTitle(rvh()->routing_id(), 0, ntp_title, direction))); |
| 258 EXPECT_EQ(ntp_title, contents()->GetTitle()); |
| 259 |
| 260 // Navigate to a cross-site URL. |
| 261 contents()->GetController().LoadURL( |
| 262 kDestUrl, content::Referrer(), content::PAGE_TRANSITION_LINK, |
| 263 std::string()); |
| 264 EXPECT_TRUE(contents()->cross_navigation_pending()); |
| 265 TestRenderViewHost* dest_rvh = static_cast<TestRenderViewHost*>( |
| 266 contents()->GetRenderManagerForTesting()->pending_render_view_host()); |
| 267 ASSERT_TRUE(dest_rvh); |
| 268 EXPECT_NE(ntp_rvh, dest_rvh); |
| 269 |
| 270 // BeforeUnload finishes. |
| 271 ntp_rvh->SendShouldCloseACK(true); |
| 272 |
| 273 // Assume SwapOutACK times out, so the dest_rvh proceeds and commits. |
| 274 dest_rvh->SendNavigate(101, kDestUrl); |
| 275 |
| 276 // The new RVH should be able to update its title. |
| 277 const string16 dest_title = ASCIIToUTF16("Google"); |
| 278 EXPECT_TRUE(dest_rvh->TestOnMessageReceived( |
| 279 ViewHostMsg_UpdateTitle(rvh()->routing_id(), 101, dest_title, |
| 280 direction))); |
| 281 EXPECT_EQ(dest_title, contents()->GetTitle()); |
| 282 |
| 283 // The old renderer, being slow, now updates the title. It should be filtered |
| 284 // out and not take effect. |
| 285 EXPECT_TRUE(ntp_rvh->is_swapped_out()); |
| 286 EXPECT_TRUE(ntp_rvh->TestOnMessageReceived( |
| 287 ViewHostMsg_UpdateTitle(rvh()->routing_id(), 0, ntp_title, direction))); |
| 288 EXPECT_EQ(dest_title, contents()->GetTitle()); |
| 289 |
| 290 // We cannot filter out synchronous IPC messages, because the renderer would |
| 291 // be left waiting for a reply. We pick RunBeforeUnloadConfirm as an example |
| 292 // that can run easily within a unit test, and that needs to receive a reply |
| 293 // without showing an actual dialog. |
| 294 MockRenderProcessHost* ntp_process_host = |
| 295 static_cast<MockRenderProcessHost*>(ntp_rvh->process()); |
| 296 ntp_process_host->sink().ClearMessages(); |
| 297 const string16 msg = ASCIIToUTF16("Message"); |
| 298 bool result = false; |
| 299 string16 unused; |
| 300 ViewHostMsg_RunBeforeUnloadConfirm before_unload_msg( |
| 301 rvh()->routing_id(), kNtpUrl, msg, &result, &unused); |
| 302 // Enable pumping for check in BrowserMessageFilter::CheckCanDispatchOnUI. |
| 303 before_unload_msg.EnableMessagePumping(); |
| 304 EXPECT_TRUE(ntp_rvh->TestOnMessageReceived(before_unload_msg)); |
| 305 EXPECT_TRUE(ntp_process_host->sink().GetUniqueMessageMatching(IPC_REPLY_ID)); |
| 306 |
| 307 // Also test RunJavaScriptMessage. |
| 308 ntp_process_host->sink().ClearMessages(); |
| 309 ViewHostMsg_RunJavaScriptMessage js_msg( |
| 310 rvh()->routing_id(), msg, msg, kNtpUrl, |
| 311 ui::JAVASCRIPT_MESSAGE_TYPE_CONFIRM, &result, &unused); |
| 312 js_msg.EnableMessagePumping(); |
| 313 EXPECT_TRUE(ntp_rvh->TestOnMessageReceived(js_msg)); |
| 314 EXPECT_TRUE(ntp_process_host->sink().GetUniqueMessageMatching(IPC_REPLY_ID)); |
| 315 } |
| 316 |
236 // When there is an error with the specified page, renderer exits view-source | 317 // When there is an error with the specified page, renderer exits view-source |
237 // mode. See WebFrameImpl::DidFail(). We check by this test that | 318 // mode. See WebFrameImpl::DidFail(). We check by this test that |
238 // EnableViewSourceMode message is sent on every navigation regardless | 319 // EnableViewSourceMode message is sent on every navigation regardless |
239 // RenderView is being newly created or reused. | 320 // RenderView is being newly created or reused. |
240 TEST_F(RenderViewHostManagerTest, AlwaysSendEnableViewSourceMode) { | 321 TEST_F(RenderViewHostManagerTest, AlwaysSendEnableViewSourceMode) { |
241 BrowserThreadImpl ui_thread(BrowserThread::UI, MessageLoop::current()); | 322 BrowserThreadImpl ui_thread(BrowserThread::UI, MessageLoop::current()); |
242 const GURL kNtpUrl(chrome::kTestNewTabURL); | 323 const GURL kNtpUrl(chrome::kTestNewTabURL); |
243 const GURL kUrl("view-source:http://foo"); | 324 const GURL kUrl("view-source:http://foo"); |
244 | 325 |
245 // We have to navigate to some page at first since without this, the first | 326 // We have to navigate to some page at first since without this, the first |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 // current one. | 730 // current one. |
650 EXPECT_TRUE(contents()->GetRenderManagerForTesting()-> | 731 EXPECT_TRUE(contents()->GetRenderManagerForTesting()-> |
651 pending_render_view_host() == NULL); | 732 pending_render_view_host() == NULL); |
652 EXPECT_EQ(evil_rvh, contents()->GetRenderManagerForTesting()->current_host()); | 733 EXPECT_EQ(evil_rvh, contents()->GetRenderManagerForTesting()->current_host()); |
653 | 734 |
654 // Also we should not have a pending navigation entry. | 735 // Also we should not have a pending navigation entry. |
655 NavigationEntry* entry = contents()->GetController().GetActiveEntry(); | 736 NavigationEntry* entry = contents()->GetController().GetActiveEntry(); |
656 ASSERT_TRUE(entry != NULL); | 737 ASSERT_TRUE(entry != NULL); |
657 EXPECT_EQ(kUrl2, entry->GetURL()); | 738 EXPECT_EQ(kUrl2, entry->GetURL()); |
658 } | 739 } |
OLD | NEW |