Chromium Code Reviews| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 histogram_tester().ExpectTotalCount( | 190 histogram_tester().ExpectTotalCount( |
| 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 } | 194 } |
| 182 | 195 |
| 183 // Checks the prefetch of an img tag. | 196 // Checks the prefetch of an img tag. |
| 184 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchImage) { | 197 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchImage) { |
| 185 RequestCounter image_counter; | 198 RequestCounter image_counter; |
| 186 CountRequestFor(kPrefetchJpeg, &image_counter); | 199 CountRequestFor(kPrefetchJpeg, &image_counter); |
| 187 base::StringPairs replacement_text; | 200 GURL main_page_url = |
| 188 replacement_text.push_back( | 201 GetURLWithReplacement(kPrefetchImagePage, "REPLACE_WITH_IMAGE_URL", |
| 189 std::make_pair("REPLACE_WITH_IMAGE_URL", MakeAbsolute(kPrefetchJpeg))); | 202 MakeAbsolute(kPrefetchJpeg)); |
| 190 std::string main_page_path; | 203 PrefetchFromURL(main_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 191 net::test_server::GetFilePathWithReplacements( | |
| 192 kPrefetchImagePage, replacement_text, &main_page_path); | |
| 193 // Note CountRequestFor cannot be used on the main page as the test server | |
| 194 // must handling the image url replacement. | |
| 195 PrefetchFromFile(main_page_path, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | |
| 196 image_counter.WaitForCount(1); | 204 image_counter.WaitForCount(1); |
| 197 } | 205 } |
| 198 | 206 |
| 199 // Checks that a cross-domain prefetching works correctly. | 207 // Checks that a cross-domain prefetching works correctly. |
| 200 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { | 208 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { |
| 201 static const std::string secondary_domain = "www.foo.com"; | 209 static const std::string secondary_domain = "www.foo.com"; |
| 202 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | 210 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); |
| 203 GURL cross_domain_url(base::StringPrintf( | 211 GURL cross_domain_url(base::StringPrintf( |
| 204 "http://%s:%d/%s", secondary_domain.c_str(), | 212 "http://%s:%d/%s", secondary_domain.c_str(), |
| 205 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); | 213 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 397 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, SSLSubresourceError) { | 405 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, SSLSubresourceError) { |
| 398 // First confirm that the image loads as expected. | 406 // First confirm that the image loads as expected. |
| 399 | 407 |
| 400 // A separate HTTPS server is started for the subresource; src_server() is | 408 // A separate HTTPS server is started for the subresource; src_server() is |
| 401 // non-HTTPS. | 409 // non-HTTPS. |
| 402 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); | 410 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 403 https_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); | 411 https_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 404 https_server.ServeFilesFromSourceDirectory("chrome/test/data"); | 412 https_server.ServeFilesFromSourceDirectory("chrome/test/data"); |
| 405 ASSERT_TRUE(https_server.Start()); | 413 ASSERT_TRUE(https_server.Start()); |
| 406 GURL https_url = https_server.GetURL("/prerender/image.jpeg"); | 414 GURL https_url = https_server.GetURL("/prerender/image.jpeg"); |
| 407 base::StringPairs replacement_text; | 415 GURL main_page_url = GetURLWithReplacement( |
| 408 replacement_text.push_back( | 416 kPrefetchImagePage, "REPLACE_WITH_IMAGE_URL", https_url.spec()); |
| 409 std::make_pair("REPLACE_WITH_IMAGE_URL", https_url.spec())); | |
| 410 std::string main_page_path; | |
| 411 net::test_server::GetFilePathWithReplacements( | |
| 412 kPrefetchImagePage, replacement_text, &main_page_path); | |
| 413 RequestCounter script_counter; | 417 RequestCounter script_counter; |
| 414 CountRequestFor(kPrefetchScript, &script_counter); | 418 CountRequestFor(kPrefetchScript, &script_counter); |
| 415 | 419 |
| 416 std::unique_ptr<TestPrerender> prerender = | 420 std::unique_ptr<TestPrerender> prerender = |
| 417 PrefetchFromFile(main_page_path, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 421 PrefetchFromURL(main_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 418 // Checks that the presumed failure of the image load didn't affect the script | 422 // Checks that the presumed failure of the image load didn't affect the script |
| 419 // fetch. This assumes waiting for the script load is enough to see any error | 423 // fetch. This assumes waiting for the script load is enough to see any error |
| 420 // from the image load. | 424 // from the image load. |
| 421 script_counter.WaitForCount(1); | 425 script_counter.WaitForCount(1); |
| 422 } | 426 } |
| 423 | 427 |
| 424 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, Loop) { | 428 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, Loop) { |
| 425 RequestCounter script_counter; | 429 RequestCounter script_counter; |
| 426 CountRequestFor(kPrefetchScript, &script_counter); | 430 CountRequestFor(kPrefetchScript, &script_counter); |
| 427 RequestCounter main_counter; | 431 RequestCounter main_counter; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 595 | 599 |
| 596 // The SW intercepts kPrefetchPage and replaces it with a body that contains | 600 // The SW intercepts kPrefetchPage and replaces it with a body that contains |
| 597 // an <img> tage for kPrefetchPng. This verifies that the SW ran correctly by | 601 // an <img> tage for kPrefetchPng. This verifies that the SW ran correctly by |
| 598 // observing the fetch of the image. | 602 // observing the fetch of the image. |
| 599 RequestCounter image_counter; | 603 RequestCounter image_counter; |
| 600 CountRequestFor(kPrefetchPng, &image_counter); | 604 CountRequestFor(kPrefetchPng, &image_counter); |
| 601 PrefetchFromFile(kPrefetchPage, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | 605 PrefetchFromFile(kPrefetchPage, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); |
| 602 image_counter.WaitForCount(1); | 606 image_counter.WaitForCount(1); |
| 603 } | 607 } |
| 604 | 608 |
| 609 // Checks that prefetching does not happen if an appcache is mentioned in the | |
| 610 // html tag. | |
| 611 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, AppCacheHtml) { | |
| 612 RequestCounter image_counter; | |
| 613 CountRequestFor(kPrefetchPng, &image_counter); | |
| 614 // While the prefetch stops when it sees the AppCache manifest, from the point | |
| 615 // of view of the prerender manager the prefetch stops normally. | |
| 616 PrefetchFromFile(kPrefetchAppcache, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | |
| 617 // The prefetch should have been canceled before the image in | |
| 618 // kPrefetchAppcache is loaded. | |
| 619 image_counter.WaitForCount(0); | |
| 620 } | |
| 621 | |
| 622 // If an AppCache manifest exists but is not mentioned explicitly, prefetch will | |
| 623 // continue. The page should be fetched from the appcache. | |
|
droger
2017/01/18 13:59:49
I'm a bit confused, mostly because I am not famili
mattcary
2017/01/18 14:06:03
Discussed somewhat offline.
AFAIU the reason for
| |
| 624 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, AppCacheRegistered) { | |
| 625 base::TimeTicks current_time = GetPrerenderManager()->GetCurrentTimeTicks(); | |
| 626 auto* clock = OverridePrerenderManagerTimeTicks(); | |
| 627 // Some navigations have already occurred in test setup. In order to track | |
| 628 // duplicate prefetches correctly the test clock needs to be beyond those | |
| 629 // navigations. | |
| 630 clock->SetNowTicks(current_time); | |
| 631 clock->Advance(base::TimeDelta::FromSeconds(600)); | |
| 632 | |
| 633 // Fill manifest with kPrefetchPage so that it is cached without explicitly | |
| 634 // listing a manifest. | |
| 635 GURL prefetch_page_url = src_server()->GetURL(MakeAbsolute(kPrefetchPage)); | |
| 636 GURL manifest_url = GetURLWithReplacement( | |
| 637 kPrefetchAppcacheManifest, "REPLACE_WITH_URL", prefetch_page_url.spec()); | |
| 638 | |
| 639 GURL appcache_page_url = GetURLWithReplacement( | |
| 640 kPrefetchAppcache, "REPLACE_WITH_MANIFEST", manifest_url.spec()); | |
| 641 | |
| 642 // Load the page into the appcache. | |
| 643 ui_test_utils::NavigateToURL(current_browser(), appcache_page_url); | |
| 644 // Load the prefetch page so it can be cached. | |
| 645 ui_test_utils::NavigateToURL(current_browser(), prefetch_page_url); | |
| 646 | |
| 647 // If a page is prefetch shortly after being loading, the prefetch is | |
| 648 // canceled. Advancing the clock prevents the cancelation. | |
| 649 clock->Advance(base::TimeDelta::FromSeconds(6000)); | |
| 650 | |
| 651 RequestCounter page_counter; | |
| 652 CountRequestFor(kPrefetchPage, &page_counter); | |
| 653 RequestCounter script_counter; | |
| 654 CountRequestFor(kPrefetchScript, &script_counter); | |
| 655 PrefetchFromURL(prefetch_page_url, FINAL_STATUS_NOSTATE_PREFETCH_FINISHED); | |
| 656 // The script is fetched from the network, but the prefetch page is | |
| 657 // intercepted by the AppCache and the RequestCounter will never see it. | |
| 658 script_counter.WaitForCount(1); | |
| 659 page_counter.WaitForCount(0); | |
| 660 } | |
| 661 | |
| 605 } // namespace prerender | 662 } // namespace prerender |
| OLD | NEW |