| 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/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/strings/string16.h" | 6 #include "base/strings/string16.h" |
| 7 #include "base/strings/string_split.h" | 7 #include "base/strings/string_split.h" |
| 8 #include "base/strings/stringprintf.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/task_scheduler/post_task.h" | 10 #include "base/task_scheduler/post_task.h" |
| 11 #include "base/test/simple_test_tick_clock.h" |
| 10 #include "chrome/browser/history/history_service_factory.h" | 12 #include "chrome/browser/history/history_service_factory.h" |
| 11 #include "chrome/browser/history/history_test_utils.h" | 13 #include "chrome/browser/history/history_test_utils.h" |
| 12 #include "chrome/browser/prerender/prerender_handle.h" | 14 #include "chrome/browser/prerender/prerender_handle.h" |
| 13 #include "chrome/browser/prerender/prerender_manager.h" | 15 #include "chrome/browser/prerender/prerender_manager.h" |
| 14 #include "chrome/browser/prerender/prerender_manager_factory.h" | 16 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 15 #include "chrome/browser/prerender/prerender_test_utils.h" | 17 #include "chrome/browser/prerender/prerender_test_utils.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | 19 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" |
| 18 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_commands.h" | 21 #include "chrome/browser/ui/browser_commands.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 33 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
| 34 | 36 |
| 35 using prerender::test_utils::DestructionWaiter; | 37 using prerender::test_utils::DestructionWaiter; |
| 36 using prerender::test_utils::RequestCounter; | 38 using prerender::test_utils::RequestCounter; |
| 37 using prerender::test_utils::TestPrerender; | 39 using prerender::test_utils::TestPrerender; |
| 38 | 40 |
| 39 namespace prerender { | 41 namespace prerender { |
| 40 | 42 |
| 41 // These URLs used for test resources must be relative with the exception of | 43 // These URLs used for test resources must be relative with the exception of |
| 42 // |kPrefetchLoaderPath|. | 44 // |kPrefetchLoaderPath|. |
| 45 const char kPrefetchAppcache[] = "prerender/prefetch_appcache.html"; |
| 46 const char kPrefetchAppcacheManifest[] = "prerender/appcache.manifest"; |
| 43 const char kPrefetchImagePage[] = "prerender/prefetch_image.html"; | 47 const char kPrefetchImagePage[] = "prerender/prefetch_image.html"; |
| 44 const char kPrefetchJpeg[] = "prerender/image.jpeg"; | 48 const char kPrefetchJpeg[] = "prerender/image.jpeg"; |
| 45 const char kPrefetchLoaderPath[] = "/prerender/prefetch_loader.html"; | 49 const char kPrefetchLoaderPath[] = "/prerender/prefetch_loader.html"; |
| 46 const char kPrefetchLoopPage[] = "prerender/prefetch_loop.html"; | 50 const char kPrefetchLoopPage[] = "prerender/prefetch_loop.html"; |
| 47 const char kPrefetchMetaCSP[] = "prerender/prefetch_meta_csp.html"; | 51 const char kPrefetchMetaCSP[] = "prerender/prefetch_meta_csp.html"; |
| 48 const char kPrefetchPage[] = "prerender/prefetch_page.html"; | 52 const char kPrefetchPage[] = "prerender/prefetch_page.html"; |
| 49 const char kPrefetchPage2[] = "prerender/prefetch_page2.html"; | 53 const char kPrefetchPage2[] = "prerender/prefetch_page2.html"; |
| 50 const char kPrefetchPng[] = "prerender/image.png"; | 54 const char kPrefetchPng[] = "prerender/image.png"; |
| 51 const char kPrefetchResponseHeaderCSP[] = | 55 const char kPrefetchResponseHeaderCSP[] = |
| 52 "prerender/prefetch_response_csp.html"; | 56 "prerender/prefetch_response_csp.html"; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 82 const std::string& path_str, | 86 const std::string& path_str, |
| 83 RequestCounter* counter) { | 87 RequestCounter* counter) { |
| 84 base::FilePath url_file = ui_test_utils::GetTestFilePath( | 88 base::FilePath url_file = ui_test_utils::GetTestFilePath( |
| 85 base::FilePath(), base::FilePath::FromUTF8Unsafe(path_str)); | 89 base::FilePath(), base::FilePath::FromUTF8Unsafe(path_str)); |
| 86 content::BrowserThread::PostTask( | 90 content::BrowserThread::PostTask( |
| 87 content::BrowserThread::IO, FROM_HERE, | 91 content::BrowserThread::IO, FROM_HERE, |
| 88 base::Bind(&prerender::test_utils::CreateCountingInterceptorOnIO, url, | 92 base::Bind(&prerender::test_utils::CreateCountingInterceptorOnIO, url, |
| 89 url_file, counter->AsWeakPtr())); | 93 url_file, counter->AsWeakPtr())); |
| 90 } | 94 } |
| 91 | 95 |
| 96 base::SimpleTestTickClock* OverridePrerenderManagerTimeTicks() { |
| 97 auto clock = base::MakeUnique<base::SimpleTestTickClock>(); |
| 98 auto clock_ptr = clock.get(); |
| 99 // The default zero time causes the prerender manager to do strange things. |
| 100 clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 101 GetPrerenderManager()->SetTickClockForTesting(std::move(clock)); |
| 102 return clock_ptr; |
| 103 } |
| 104 |
| 92 protected: | 105 protected: |
| 93 std::unique_ptr<TestPrerender> PrefetchFromURL( | 106 std::unique_ptr<TestPrerender> PrefetchFromURL( |
| 94 const GURL& target_url, | 107 const GURL& target_url, |
| 95 FinalStatus expected_final_status) { | 108 FinalStatus expected_final_status) { |
| 96 GURL loader_url = ServeLoaderURL( | 109 GURL loader_url = ServeLoaderURL( |
| 97 kPrefetchLoaderPath, "REPLACE_WITH_PREFETCH_URL", target_url, ""); | 110 kPrefetchLoaderPath, "REPLACE_WITH_PREFETCH_URL", target_url, ""); |
| 98 std::vector<FinalStatus> expected_final_status_queue(1, | 111 std::vector<FinalStatus> expected_final_status_queue(1, |
| 99 expected_final_status); | 112 expected_final_status); |
| 100 std::vector<std::unique_ptr<TestPrerender>> prerenders = | 113 std::vector<std::unique_ptr<TestPrerender>> prerenders = |
| 101 NavigateWithPrerenders(loader_url, expected_final_status_queue); | 114 NavigateWithPrerenders(loader_url, expected_final_status_queue); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 "Prerender.websame_PrefetchTTFCP.Warm.Cacheable.Visible", 1); | 191 "Prerender.websame_PrefetchTTFCP.Warm.Cacheable.Visible", 1); |
| 179 histogram_tester().ExpectTotalCount( | 192 histogram_tester().ExpectTotalCount( |
| 180 "Prerender.websame_NoStatePrefetchResponseTypes", 2); | 193 "Prerender.websame_NoStatePrefetchResponseTypes", 2); |
| 181 histogram_tester().ExpectTotalCount("Prerender.websame_PrefetchAge", 1); | 194 histogram_tester().ExpectTotalCount("Prerender.websame_PrefetchAge", 1); |
| 182 } | 195 } |
| 183 | 196 |
| 184 // Checks the prefetch of an img tag. | 197 // Checks the prefetch of an img tag. |
| 185 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchImage) { | 198 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchImage) { |
| 186 RequestCounter image_counter; | 199 RequestCounter image_counter; |
| 187 CountRequestFor(kPrefetchJpeg, &image_counter); | 200 CountRequestFor(kPrefetchJpeg, &image_counter); |
| 188 base::StringPairs replacement_text; | 201 GURL main_page_url = |
| 189 replacement_text.push_back( | 202 GetURLWithReplacement(kPrefetchImagePage, "REPLACE_WITH_IMAGE_URL", |
| 190 std::make_pair("REPLACE_WITH_IMAGE_URL", MakeAbsolute(kPrefetchJpeg))); | 203 MakeAbsolute(kPrefetchJpeg)); |
| 191 std::string main_page_path; | 204 PrefetchFromURL(main_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 192 net::test_server::GetFilePathWithReplacements( | |
| 193 kPrefetchImagePage, replacement_text, &main_page_path); | |
| 194 // Note CountRequestFor cannot be used on the main page as the test server | |
| 195 // must handling the image url replacement. | |
| 196 PrefetchFromFile(main_page_path, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | |
| 197 image_counter.WaitForCount(1); | 205 image_counter.WaitForCount(1); |
| 198 } | 206 } |
| 199 | 207 |
| 200 // Checks that a cross-domain prefetching works correctly. | 208 // Checks that a cross-domain prefetching works correctly. |
| 201 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { | 209 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { |
| 202 static const std::string secondary_domain = "www.foo.com"; | 210 static const std::string secondary_domain = "www.foo.com"; |
| 203 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | 211 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); |
| 204 GURL cross_domain_url(base::StringPrintf( | 212 GURL cross_domain_url(base::StringPrintf( |
| 205 "http://%s:%d/%s", secondary_domain.c_str(), | 213 "http://%s:%d/%s", secondary_domain.c_str(), |
| 206 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); | 214 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, SSLSubresourceError) { | 406 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, SSLSubresourceError) { |
| 399 // First confirm that the image loads as expected. | 407 // First confirm that the image loads as expected. |
| 400 | 408 |
| 401 // A separate HTTPS server is started for the subresource; src_server() is | 409 // A separate HTTPS server is started for the subresource; src_server() is |
| 402 // non-HTTPS. | 410 // non-HTTPS. |
| 403 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); | 411 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 404 https_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); | 412 https_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 405 https_server.ServeFilesFromSourceDirectory("chrome/test/data"); | 413 https_server.ServeFilesFromSourceDirectory("chrome/test/data"); |
| 406 ASSERT_TRUE(https_server.Start()); | 414 ASSERT_TRUE(https_server.Start()); |
| 407 GURL https_url = https_server.GetURL("/prerender/image.jpeg"); | 415 GURL https_url = https_server.GetURL("/prerender/image.jpeg"); |
| 408 base::StringPairs replacement_text; | 416 GURL main_page_url = GetURLWithReplacement( |
| 409 replacement_text.push_back( | 417 kPrefetchImagePage, "REPLACE_WITH_IMAGE_URL", https_url.spec()); |
| 410 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec())); | |
| 411 std::string main_page_path; | |
| 412 net::test_server::GetFilePathWithReplacements( | |
| 413 kPrefetchImagePage, replacement_text, &main_page_path); | |
| 414 RequestCounter script_counter; | 418 RequestCounter script_counter; |
| 415 CountRequestFor(kPrefetchScript, &script_counter); | 419 CountRequestFor(kPrefetchScript, &script_counter); |
| 416 | 420 |
| 417 std::unique_ptr<TestPrerender> prerender = | 421 std::unique_ptr<TestPrerender> prerender = |
| 418 PrefetchFromFile(main_page_path, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 422 PrefetchFromURL(main_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 419 // Checks that the presumed failure of the image load didn't affect the script | 423 // Checks that the presumed failure of the image load didn't affect the script |
| 420 // fetch. This assumes waiting for the script load is enough to see any error | 424 // fetch. This assumes waiting for the script load is enough to see any error |
| 421 // from the image load. | 425 // from the image load. |
| 422 script_counter.WaitForCount(1); | 426 script_counter.WaitForCount(1); |
| 423 } | 427 } |
| 424 | 428 |
| 425 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, Loop) { | 429 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, Loop) { |
| 426 RequestCounter script_counter; | 430 RequestCounter script_counter; |
| 427 CountRequestFor(kPrefetchScript, &script_counter); | 431 CountRequestFor(kPrefetchScript, &script_counter); |
| 428 RequestCounter main_counter; | 432 RequestCounter main_counter; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 | 600 |
| 597 // The SW intercepts kPrefetchPage and replaces it with a body that contains | 601 // The SW intercepts kPrefetchPage and replaces it with a body that contains |
| 598 // an <img> tage for kPrefetchPng. This verifies that the SW ran correctly by | 602 // an <img> tage for kPrefetchPng. This verifies that the SW ran correctly by |
| 599 // observing the fetch of the image. | 603 // observing the fetch of the image. |
| 600 RequestCounter image_counter; | 604 RequestCounter image_counter; |
| 601 CountRequestFor(kPrefetchPng, &image_counter); | 605 CountRequestFor(kPrefetchPng, &image_counter); |
| 602 PrefetchFromFile(kPrefetchPage, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 606 PrefetchFromFile(kPrefetchPage, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 603 image_counter.WaitForCount(1); | 607 image_counter.WaitForCount(1); |
| 604 } | 608 } |
| 605 | 609 |
| 610 // Checks that prefetching does not happen if an appcache is mentioned in the |
| 611 // html tag. |
| 612 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, AppCacheHtml) { |
| 613 RequestCounter image_counter; |
| 614 CountRequestFor(kPrefetchPng, &image_counter); |
| 615 // While the prefetch stops when it sees the AppCache manifest, from the point |
| 616 // of view of the prerender manager the prefetch stops normally. |
| 617 PrefetchFromFile(kPrefetchAppcache, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 618 // The prefetch should have been canceled before the image in |
| 619 // kPrefetchAppcache is loaded. |
| 620 image_counter.WaitForCount(0); |
| 621 } |
| 622 |
| 623 // If an AppCache manifest exists but is not mentioned explicitly, prefetch will |
| 624 // continue. The page should be fetched from the appcache. |
| 625 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, AppCacheRegistered) { |
| 626 base::TimeTicks current_time = GetPrerenderManager()->GetCurrentTimeTicks(); |
| 627 auto* clock = OverridePrerenderManagerTimeTicks(); |
| 628 // Some navigations have already occurred in test setup. In order to track |
| 629 // duplicate prefetches correctly the test clock needs to be beyond those |
| 630 // navigations. |
| 631 clock->SetNowTicks(current_time); |
| 632 clock->Advance(base::TimeDelta::FromSeconds(600)); |
| 633 |
| 634 // Fill manifest with kPrefetchPage so that it is cached without explicitly |
| 635 // listing a manifest. |
| 636 GURL prefetch_page_url = src_server()->GetURL(MakeAbsolute(kPrefetchPage)); |
| 637 GURL manifest_url = GetURLWithReplacement( |
| 638 kPrefetchAppcacheManifest, "REPLACE_WITH_URL", prefetch_page_url.spec()); |
| 639 |
| 640 GURL appcache_page_url = GetURLWithReplacement( |
| 641 kPrefetchAppcache, "REPLACE_WITH_MANIFEST", manifest_url.spec()); |
| 642 |
| 643 // Load the page into the appcache. |
| 644 ui_test_utils::NavigateToURL(current_browser(), appcache_page_url); |
| 645 // Load the prefetch page so it can be cached. |
| 646 ui_test_utils::NavigateToURL(current_browser(), prefetch_page_url); |
| 647 |
| 648 // If a page is prefetch shortly after being loading, the prefetch is |
| 649 // canceled. Advancing the clock prevents the cancellation. |
| 650 clock->Advance(base::TimeDelta::FromSeconds(6000)); |
| 651 |
| 652 RequestCounter page_counter; |
| 653 CountRequestFor(kPrefetchPage, &page_counter); |
| 654 RequestCounter script_counter; |
| 655 CountRequestFor(kPrefetchScript, &script_counter); |
| 656 PrefetchFromURL(prefetch_page_url, FINAL_STATUS_CANCELLED_FOR_APPCACHE); |
| 657 // The script is fetched from the network, but the prefetch page is |
| 658 // intercepted by the AppCache and the RequestCounter will never see it. |
| 659 |
| 660 } |
| 661 |
| 606 } // namespace prerender | 662 } // namespace prerender |
| OLD | NEW |