| 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 <deque> | 5 #include <deque> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 #include "content/public/browser/web_contents.h" | 52 #include "content/public/browser/web_contents.h" |
| 53 #include "content/public/common/url_constants.h" | 53 #include "content/public/common/url_constants.h" |
| 54 #include "content/public/test/browser_test_utils.h" | 54 #include "content/public/test/browser_test_utils.h" |
| 55 #include "content/public/test/test_navigation_observer.h" | 55 #include "content/public/test/test_navigation_observer.h" |
| 56 #include "content/public/test/test_utils.h" | 56 #include "content/public/test/test_utils.h" |
| 57 #include "googleurl/src/gurl.h" | 57 #include "googleurl/src/gurl.h" |
| 58 #include "grit/generated_resources.h" | 58 #include "grit/generated_resources.h" |
| 59 #include "net/dns/mock_host_resolver.h" | 59 #include "net/dns/mock_host_resolver.h" |
| 60 #include "net/url_request/url_request_context.h" | 60 #include "net/url_request/url_request_context.h" |
| 61 #include "net/url_request/url_request_context_getter.h" | 61 #include "net/url_request/url_request_context_getter.h" |
| 62 #include "net/url_request/url_request_filter.h" |
| 63 #include "net/url_request/url_request_job.h" |
| 62 #include "ui/base/l10n/l10n_util.h" | 64 #include "ui/base/l10n/l10n_util.h" |
| 63 | 65 |
| 64 using content::BrowserThread; | 66 using content::BrowserThread; |
| 65 using content::DevToolsAgentHost; | 67 using content::DevToolsAgentHost; |
| 66 using content::DevToolsClientHost; | 68 using content::DevToolsClientHost; |
| 67 using content::DevToolsManager; | 69 using content::DevToolsManager; |
| 68 using content::NavigationController; | 70 using content::NavigationController; |
| 69 using content::OpenURLParams; | 71 using content::OpenURLParams; |
| 70 using content::Referrer; | 72 using content::Referrer; |
| 71 using content::RenderViewHost; | 73 using content::RenderViewHost; |
| (...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 class RestorePrerenderMode { | 574 class RestorePrerenderMode { |
| 573 public: | 575 public: |
| 574 RestorePrerenderMode() : prev_mode_(PrerenderManager::GetMode()) { | 576 RestorePrerenderMode() : prev_mode_(PrerenderManager::GetMode()) { |
| 575 } | 577 } |
| 576 | 578 |
| 577 ~RestorePrerenderMode() { PrerenderManager::SetMode(prev_mode_); } | 579 ~RestorePrerenderMode() { PrerenderManager::SetMode(prev_mode_); } |
| 578 private: | 580 private: |
| 579 PrerenderManager::PrerenderManagerMode prev_mode_; | 581 PrerenderManager::PrerenderManagerMode prev_mode_; |
| 580 }; | 582 }; |
| 581 | 583 |
| 584 // URLRequestJob (and associated handler) which never starts. |
| 585 class NeverStartURLRequestJob : public net::URLRequestJob { |
| 586 public: |
| 587 NeverStartURLRequestJob(net::URLRequest* request, |
| 588 net::NetworkDelegate* network_delegate) |
| 589 : net::URLRequestJob(request, network_delegate) { |
| 590 } |
| 591 |
| 592 virtual void Start() OVERRIDE {} |
| 593 |
| 594 private: |
| 595 virtual ~NeverStartURLRequestJob() {} |
| 596 }; |
| 597 |
| 598 class NeverStartProtocolHandler |
| 599 : public net::URLRequestJobFactory::ProtocolHandler { |
| 600 public: |
| 601 NeverStartProtocolHandler() {} |
| 602 virtual ~NeverStartProtocolHandler() {} |
| 603 |
| 604 virtual net::URLRequestJob* MaybeCreateJob( |
| 605 net::URLRequest* request, |
| 606 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 607 return new NeverStartURLRequestJob(request, network_delegate); |
| 608 } |
| 609 }; |
| 610 |
| 611 void CreateNeverStartProtocolHandlerOnIO(const GURL& url) { |
| 612 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 613 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> never_respond_handler( |
| 614 new NeverStartProtocolHandler()); |
| 615 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler( |
| 616 url, never_respond_handler.Pass()); |
| 617 } |
| 618 |
| 582 } // namespace | 619 } // namespace |
| 583 | 620 |
| 584 class PrerenderBrowserTest : virtual public InProcessBrowserTest { | 621 class PrerenderBrowserTest : virtual public InProcessBrowserTest { |
| 585 public: | 622 public: |
| 586 PrerenderBrowserTest() | 623 PrerenderBrowserTest() |
| 587 : autostart_test_server_(true), | 624 : autostart_test_server_(true), |
| 588 prerender_contents_factory_(NULL), | 625 prerender_contents_factory_(NULL), |
| 589 #if defined(FULL_SAFE_BROWSING) | 626 #if defined(FULL_SAFE_BROWSING) |
| 590 safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), | 627 safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), |
| 591 #endif | 628 #endif |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 int expected_number_of_loads) { | 731 int expected_number_of_loads) { |
| 695 std::deque<FinalStatus> expected_final_status_queue(1, | 732 std::deque<FinalStatus> expected_final_status_queue(1, |
| 696 expected_final_status); | 733 expected_final_status); |
| 697 PrerenderTestURLImpl(prerender_url, destination_url, | 734 PrerenderTestURLImpl(prerender_url, destination_url, |
| 698 expected_final_status_queue, | 735 expected_final_status_queue, |
| 699 expected_number_of_loads, | 736 expected_number_of_loads, |
| 700 false); | 737 false); |
| 701 } | 738 } |
| 702 | 739 |
| 703 void NavigateToDestURL() const { | 740 void NavigateToDestURL() const { |
| 704 NavigateToDestURLWithDisposition(CURRENT_TAB); | 741 NavigateToDestURLWithDisposition(CURRENT_TAB, true); |
| 705 } | 742 } |
| 706 | 743 |
| 707 // Opens the url in a new tab, with no opener. | 744 // Opens the url in a new tab, with no opener. |
| 708 void NavigateToDestURLWithDisposition( | 745 void NavigateToDestURLWithDisposition( |
| 709 WindowOpenDisposition disposition) const { | 746 WindowOpenDisposition disposition, |
| 710 NavigateToURLImpl(dest_url_, disposition); | 747 bool expect_swap_to_succeed) const { |
| 748 NavigateToURLImpl(dest_url_, disposition, expect_swap_to_succeed); |
| 711 } | 749 } |
| 712 | 750 |
| 713 void OpenDestURLViaClick() const { | 751 void OpenDestURLViaClick() const { |
| 714 OpenDestURLWithJSImpl("Click()"); | 752 OpenDestURLWithJSImpl("Click()"); |
| 715 } | 753 } |
| 716 | 754 |
| 717 void OpenDestURLViaClickTarget() const { | 755 void OpenDestURLViaClickTarget() const { |
| 718 OpenDestURLWithJSImpl("ClickTarget()"); | 756 OpenDestURLWithJSImpl("ClickTarget()"); |
| 719 } | 757 } |
| 720 | 758 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 bool js_result; | 849 bool js_result; |
| 812 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 850 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 813 tab, | 851 tab, |
| 814 "window.domAutomationController.send(DidBackToOriginalPagePass())", | 852 "window.domAutomationController.send(DidBackToOriginalPagePass())", |
| 815 &js_result)); | 853 &js_result)); |
| 816 EXPECT_TRUE(js_result); | 854 EXPECT_TRUE(js_result); |
| 817 } | 855 } |
| 818 | 856 |
| 819 void NavigateToURL(const std::string& dest_html_file) const { | 857 void NavigateToURL(const std::string& dest_html_file) const { |
| 820 GURL dest_url = test_server()->GetURL(dest_html_file); | 858 GURL dest_url = test_server()->GetURL(dest_html_file); |
| 821 NavigateToURLImpl(dest_url, CURRENT_TAB); | 859 NavigateToURLImpl(dest_url, CURRENT_TAB, true); |
| 822 } | 860 } |
| 823 | 861 |
| 824 bool UrlIsInPrerenderManager(const std::string& html_file) const { | 862 bool UrlIsInPrerenderManager(const std::string& html_file) const { |
| 825 return UrlIsInPrerenderManager(test_server()->GetURL(html_file)); | 863 return UrlIsInPrerenderManager(test_server()->GetURL(html_file)); |
| 826 } | 864 } |
| 827 | 865 |
| 828 bool UrlIsInPrerenderManager(const GURL& url) const { | 866 bool UrlIsInPrerenderManager(const GURL& url) const { |
| 829 return GetPrerenderManager()->FindPrerenderData( | 867 return GetPrerenderManager()->FindPrerenderData( |
| 830 url, GetSessionStorageNamespace()) != NULL; | 868 url, GetSessionStorageNamespace()) != NULL; |
| 831 } | 869 } |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1078 // In the failure case, we should have removed |dest_url_| from the | 1116 // In the failure case, we should have removed |dest_url_| from the |
| 1079 // prerender_manager. We ignore dummy PrerenderContents (as indicated | 1117 // prerender_manager. We ignore dummy PrerenderContents (as indicated |
| 1080 // by not having started), and PrerenderContents that are expected to | 1118 // by not having started), and PrerenderContents that are expected to |
| 1081 // be left in the manager until the test finishes. | 1119 // be left in the manager until the test finishes. |
| 1082 EXPECT_TRUE(prerender_contents == NULL || | 1120 EXPECT_TRUE(prerender_contents == NULL || |
| 1083 !prerender_contents->prerendering_has_started()); | 1121 !prerender_contents->prerendering_has_started()); |
| 1084 } | 1122 } |
| 1085 } | 1123 } |
| 1086 | 1124 |
| 1087 void NavigateToURLImpl(const GURL& dest_url, | 1125 void NavigateToURLImpl(const GURL& dest_url, |
| 1088 WindowOpenDisposition disposition) const { | 1126 WindowOpenDisposition disposition, |
| 1127 bool expect_swap_to_succeed) const { |
| 1089 ASSERT_NE(static_cast<PrerenderManager*>(NULL), GetPrerenderManager()); | 1128 ASSERT_NE(static_cast<PrerenderManager*>(NULL), GetPrerenderManager()); |
| 1090 // Make sure in navigating we have a URL to use in the PrerenderManager. | 1129 // Make sure in navigating we have a URL to use in the PrerenderManager. |
| 1091 ASSERT_NE(static_cast<PrerenderContents*>(NULL), GetPrerenderContents()); | 1130 ASSERT_NE(static_cast<PrerenderContents*>(NULL), GetPrerenderContents()); |
| 1092 | 1131 |
| 1093 // If opening the page in a background tab, it won't be shown when swapped | 1132 // If opening the page in a background tab, it won't be shown when swapped |
| 1094 // in. | 1133 // in. |
| 1095 if (disposition == NEW_BACKGROUND_TAB) | 1134 if (disposition == NEW_BACKGROUND_TAB) |
| 1096 GetPrerenderContents()->set_should_be_shown(false); | 1135 GetPrerenderContents()->set_should_be_shown(false); |
| 1097 | 1136 |
| 1098 scoped_ptr<content::WindowedNotificationObserver> page_load_observer; | 1137 scoped_ptr<content::WindowedNotificationObserver> page_load_observer; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1111 } | 1150 } |
| 1112 } | 1151 } |
| 1113 | 1152 |
| 1114 // Navigate to the prerendered URL, but don't run the message loop. Browser | 1153 // Navigate to the prerendered URL, but don't run the message loop. Browser |
| 1115 // issued navigations to prerendered pages will synchronously swap in the | 1154 // issued navigations to prerendered pages will synchronously swap in the |
| 1116 // prerendered page. | 1155 // prerendered page. |
| 1117 ui_test_utils::NavigateToURLWithDisposition( | 1156 ui_test_utils::NavigateToURLWithDisposition( |
| 1118 current_browser(), dest_url, disposition, | 1157 current_browser(), dest_url, disposition, |
| 1119 ui_test_utils::BROWSER_TEST_NONE); | 1158 ui_test_utils::BROWSER_TEST_NONE); |
| 1120 | 1159 |
| 1121 // Make sure the PrerenderContents found earlier was used or removed. | 1160 // Make sure the PrerenderContents found earlier was used or removed, |
| 1122 EXPECT_EQ(static_cast<PrerenderContents*>(NULL), GetPrerenderContents()); | 1161 // unless we expect the swap in to fail. |
| 1162 EXPECT_EQ(expect_swap_to_succeed, !GetPrerenderContents()); |
| 1123 | 1163 |
| 1124 if (call_javascript_ && web_contents) { | 1164 if (call_javascript_ && web_contents && expect_swap_to_succeed) { |
| 1125 if (page_load_observer.get()) | 1165 if (page_load_observer.get()) |
| 1126 page_load_observer->Wait(); | 1166 page_load_observer->Wait(); |
| 1127 | 1167 |
| 1128 bool display_test_result = false; | 1168 bool display_test_result = false; |
| 1129 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 1169 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| 1130 web_contents, | 1170 web_contents, |
| 1131 "window.domAutomationController.send(DidDisplayPass())", | 1171 "window.domAutomationController.send(DidDisplayPass())", |
| 1132 &display_test_result)); | 1172 &display_test_result)); |
| 1133 EXPECT_TRUE(display_test_result); | 1173 EXPECT_TRUE(display_test_result); |
| 1134 } | 1174 } |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1358 } | 1398 } |
| 1359 | 1399 |
| 1360 // Checks that the visibility API works. | 1400 // Checks that the visibility API works. |
| 1361 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderVisibility) { | 1401 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderVisibility) { |
| 1362 PrerenderTestURL("files/prerender/prerender_visibility.html", | 1402 PrerenderTestURL("files/prerender/prerender_visibility.html", |
| 1363 FINAL_STATUS_USED, | 1403 FINAL_STATUS_USED, |
| 1364 1); | 1404 1); |
| 1365 NavigateToDestURL(); | 1405 NavigateToDestURL(); |
| 1366 } | 1406 } |
| 1367 | 1407 |
| 1368 // Checks that the visibility API works when the prerender is quickly opened | 1408 // Checks that the prerendering of a page is canceled correctly if we try to |
| 1369 // in a new tab before it stops loading. | 1409 // swap it in before it commits. |
| 1370 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderVisibilityQuickSwitch) { | 1410 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoCommitNoSwap) { |
| 1371 PrerenderTestURL("files/prerender/prerender_visibility_quick.html", | 1411 // Navigate to a page that triggers a prerender for a URL that never commits. |
| 1372 FINAL_STATUS_USED, 0); | 1412 const GURL kNoCommitUrl("http://never-respond.example.com"); |
| 1373 NavigateToDestURL(); | 1413 BrowserThread::PostTask( |
| 1414 BrowserThread::IO, FROM_HERE, |
| 1415 base::Bind(&CreateNeverStartProtocolHandlerOnIO, kNoCommitUrl)); |
| 1416 PrerenderTestURL(kNoCommitUrl, |
| 1417 FINAL_STATUS_CANCELLED, |
| 1418 0); |
| 1419 |
| 1420 // Navigate to the URL, but assume the contents won't be swapped in. |
| 1421 NavigateToDestURLWithDisposition(CURRENT_TAB, false); |
| 1422 |
| 1423 // Confirm that the prerendered version of the URL is not swapped in, |
| 1424 // since it never committed. |
| 1425 EXPECT_TRUE(UrlIsInPrerenderManager(kNoCommitUrl)); |
| 1426 |
| 1427 // Post a task to cancel all the prerenders, so that we don't wait further. |
| 1428 base::MessageLoop::current()->PostTask( |
| 1429 FROM_HERE, base::Bind(&CancelAllPrerenders, GetPrerenderManager())); |
| 1430 content::RunMessageLoop(); |
| 1374 } | 1431 } |
| 1375 | 1432 |
| 1376 // Checks that the prerendering of a page is canceled correctly when a | 1433 // Checks that the prerendering of a page is canceled correctly when a |
| 1377 // Javascript alert is called. | 1434 // Javascript alert is called. |
| 1378 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAlertBeforeOnload) { | 1435 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderAlertBeforeOnload) { |
| 1379 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html", | 1436 PrerenderTestURL("files/prerender/prerender_alert_before_onload.html", |
| 1380 FINAL_STATUS_JAVASCRIPT_ALERT, | 1437 FINAL_STATUS_JAVASCRIPT_ALERT, |
| 1381 1); | 1438 1); |
| 1382 } | 1439 } |
| 1383 | 1440 |
| (...skipping 1404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2788 // Checks that non-http/https main page redirects cancel the prerender. | 2845 // Checks that non-http/https main page redirects cancel the prerender. |
| 2789 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, | 2846 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, |
| 2790 PrerenderCancelMainFrameRedirectUnsupportedScheme) { | 2847 PrerenderCancelMainFrameRedirectUnsupportedScheme) { |
| 2791 GURL url = test_server()->GetURL( | 2848 GURL url = test_server()->GetURL( |
| 2792 CreateServerRedirect("invalidscheme://www.google.com/test.html")); | 2849 CreateServerRedirect("invalidscheme://www.google.com/test.html")); |
| 2793 PrerenderTestURL(url, FINAL_STATUS_UNSUPPORTED_SCHEME, 1); | 2850 PrerenderTestURL(url, FINAL_STATUS_UNSUPPORTED_SCHEME, 1); |
| 2794 NavigateToDestURL(); | 2851 NavigateToDestURL(); |
| 2795 } | 2852 } |
| 2796 | 2853 |
| 2797 } // namespace prerender | 2854 } // namespace prerender |
| OLD | NEW |