OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/test/histogram_tester.h" | |
12 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 13 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
13 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 14 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
14 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" | 15 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" |
15 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 16 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
16 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | 17 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" |
17 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 18 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
21 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 PrefetchingObserver(ResourcePrefetchPredictor* predictor, | 265 PrefetchingObserver(ResourcePrefetchPredictor* predictor, |
265 const GURL& expected_main_frame_url) | 266 const GURL& expected_main_frame_url) |
266 : TestObserver(predictor), main_frame_url_(expected_main_frame_url) {} | 267 : TestObserver(predictor), main_frame_url_(expected_main_frame_url) {} |
267 | 268 |
268 // TestObserver: | 269 // TestObserver: |
269 void OnNavigationLearned(size_t url_visit_count, | 270 void OnNavigationLearned(size_t url_visit_count, |
270 const PageRequestSummary& summary) override { | 271 const PageRequestSummary& summary) override { |
271 ADD_FAILURE() << "Prefetching shouldn't activate learning"; | 272 ADD_FAILURE() << "Prefetching shouldn't activate learning"; |
272 } | 273 } |
273 | 274 |
274 void OnPrefetchingFinished(const GURL& main_frame_url) override { | 275 void OnPrefetchingFinished( |
276 const GURL& main_frame_url, | |
277 const ResourcePrefetcher::PrefetcherStats& stats) override { | |
275 EXPECT_EQ(main_frame_url_, main_frame_url); | 278 EXPECT_EQ(main_frame_url_, main_frame_url); |
276 run_loop_.Quit(); | 279 run_loop_.Quit(); |
277 } | 280 } |
278 | 281 |
279 void Wait() { run_loop_.Run(); } | 282 void Wait() { run_loop_.Run(); } |
280 | 283 |
281 private: | 284 private: |
282 base::RunLoop run_loop_; | 285 base::RunLoop run_loop_; |
283 GURL main_frame_url_; | 286 GURL main_frame_url_; |
284 | 287 |
(...skipping 25 matching lines...) Expand all Loading... | |
310 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 313 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
311 base::Unretained(this))); | 314 base::Unretained(this))); |
312 embedded_test_server()->RegisterRequestMonitor(base::Bind( | 315 embedded_test_server()->RegisterRequestMonitor(base::Bind( |
313 &ResourcePrefetchPredictorBrowserTest::MonitorResourceRequest, | 316 &ResourcePrefetchPredictorBrowserTest::MonitorResourceRequest, |
314 base::Unretained(this))); | 317 base::Unretained(this))); |
315 ASSERT_TRUE(embedded_test_server()->Start()); | 318 ASSERT_TRUE(embedded_test_server()->Start()); |
316 predictor_ = | 319 predictor_ = |
317 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | 320 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); |
318 ASSERT_TRUE(predictor_); | 321 ASSERT_TRUE(predictor_); |
319 EnsurePredictorInitialized(); | 322 EnsurePredictorInitialized(); |
323 histogram_tester_.reset(new base::HistogramTester()); | |
320 } | 324 } |
321 | 325 |
322 void TestLearningAndPrefetching(const GURL& main_frame_url) { | 326 void TestLearningAndPrefetching(const GURL& main_frame_url) { |
323 // Navigate to |main_frame_url| and check all the expectations. | 327 // Navigate to |main_frame_url| and check all the expectations. |
324 NavigateToURLAndCheckSubresources(main_frame_url); | 328 NavigateToURLAndCheckSubresources(main_frame_url); |
325 ClearCache(); | 329 ClearCache(); |
326 // It is needed to have at least two resource hits to trigger prefetch. | 330 // It is needed to have at least two resource hits to trigger prefetch. |
327 NavigateToURLAndCheckSubresources(main_frame_url); | 331 NavigateToURLAndCheckSubresources(main_frame_url); |
328 ClearCache(); | 332 ClearCache(); |
329 // Prefetch all needed resources and change expectations so that all | 333 // Prefetch all needed resources and change expectations so that all |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
580 request.headers.at("If-None-Match") == | 584 request.headers.at("If-None-Match") == |
581 CreateVersionedETag(summary.version, request.relative_url)) { | 585 CreateVersionedETag(summary.version, request.relative_url)) { |
582 http_response->set_code(net::HTTP_NOT_MODIFIED); | 586 http_response->set_code(net::HTTP_NOT_MODIFIED); |
583 } else { | 587 } else { |
584 http_response->set_code(net::HTTP_OK); | 588 http_response->set_code(net::HTTP_OK); |
585 } | 589 } |
586 | 590 |
587 if (!summary.request.mime_type.empty()) | 591 if (!summary.request.mime_type.empty()) |
588 http_response->set_content_type(summary.request.mime_type); | 592 http_response->set_content_type(summary.request.mime_type); |
589 if (!summary.content.empty()) | 593 if (!summary.content.empty()) |
590 http_response->set_content(summary.content); | 594 http_response->set_content(summary.content); |
alexilin
2017/02/09 15:24:17
There already is http_response->set_content() call
Benoit L
2017/02/13 16:13:15
Oops... was myopic.
Done.
| |
591 if (summary.is_no_store) | 595 if (summary.is_no_store) |
592 http_response->AddCustomHeader("Cache-Control", "no-store"); | 596 http_response->AddCustomHeader("Cache-Control", "no-store"); |
593 if (summary.request.has_validators) { | 597 if (summary.request.has_validators) { |
594 http_response->AddCustomHeader( | 598 http_response->AddCustomHeader( |
595 "ETag", CreateVersionedETag(summary.version, request.relative_url)); | 599 "ETag", CreateVersionedETag(summary.version, request.relative_url)); |
596 } | 600 } |
597 if (summary.request.always_revalidate) | 601 if (summary.request.always_revalidate) |
598 http_response->AddCustomHeader("Cache-Control", "no-cache"); | 602 http_response->AddCustomHeader("Cache-Control", "no-cache"); |
599 else | 603 else |
600 http_response->AddCustomHeader("Cache-Control", "max-age=2147483648"); | 604 http_response->AddCustomHeader("Cache-Control", "max-age=2147483648"); |
601 | 605 |
606 // Add some content, otherwise the prefetch size histogram rounds down to | |
607 // 0kB. | |
608 http_response->set_content(std::string(1024, static_cast<char>(42))); | |
alexilin
2017/02/09 15:24:17
It will cause additional parsing errors in tests.
Benoit L
2017/02/13 16:13:15
Changed to a space, which should be fine for the v
| |
609 | |
602 return std::move(http_response); | 610 return std::move(http_response); |
603 } | 611 } |
604 | 612 |
605 // The custom handler for redirect requests from the browser to an | 613 // The custom handler for redirect requests from the browser to an |
606 // EmbeddedTestServer. Running on the EmbeddedTestServer IO thread. | 614 // EmbeddedTestServer. Running on the EmbeddedTestServer IO thread. |
607 // Finds the data to serve requests in |redirects_| map keyed by a request | 615 // Finds the data to serve requests in |redirects_| map keyed by a request |
608 // URL. | 616 // URL. |
609 std::unique_ptr<net::test_server::HttpResponse> HandleRedirectRequest( | 617 std::unique_ptr<net::test_server::HttpResponse> HandleRedirectRequest( |
610 const net::test_server::HttpRequest& request) const { | 618 const net::test_server::HttpRequest& request) const { |
611 std::map<GURL, RedirectEdge>::const_iterator redirect_it = | 619 std::map<GURL, RedirectEdge>::const_iterator redirect_it = |
612 redirects_.find(GetRequestURL(request)); | 620 redirects_.find(GetRequestURL(request)); |
613 if (redirect_it == redirects_.end() || redirect_it->second.is_client_side) | 621 if (redirect_it == redirects_.end() || redirect_it->second.is_client_side) |
614 return nullptr; | 622 return nullptr; |
615 | 623 |
616 auto http_response = | 624 auto http_response = |
617 base::MakeUnique<net::test_server::BasicHttpResponse>(); | 625 base::MakeUnique<net::test_server::BasicHttpResponse>(); |
618 http_response->set_code(redirect_it->second.code); | 626 http_response->set_code(redirect_it->second.code); |
619 http_response->AddCustomHeader("Location", redirect_it->second.url.spec()); | 627 http_response->AddCustomHeader("Location", redirect_it->second.url.spec()); |
620 return std::move(http_response); | 628 return std::move(http_response); |
621 } | 629 } |
622 | 630 |
623 size_t UpdateAndGetVisitCount(const GURL& main_frame_url) { | 631 size_t UpdateAndGetVisitCount(const GURL& main_frame_url) { |
624 return ++visit_count_[main_frame_url]; | 632 return ++visit_count_[main_frame_url]; |
625 } | 633 } |
626 | 634 |
635 protected: | |
636 std::unique_ptr<base::HistogramTester> histogram_tester_; | |
alexilin
2017/02/09 15:24:18
Could you put it at the end of (first) protected s
Benoit L
2017/02/13 16:13:15
Done.
| |
637 | |
638 private: | |
627 ResourcePrefetchPredictor* predictor_; | 639 ResourcePrefetchPredictor* predictor_; |
628 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 640 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
629 std::map<GURL, ResourceSummary> resources_; | 641 std::map<GURL, ResourceSummary> resources_; |
630 std::map<GURL, RedirectEdge> redirects_; | 642 std::map<GURL, RedirectEdge> redirects_; |
631 std::map<GURL, size_t> visit_count_; | 643 std::map<GURL, size_t> visit_count_; |
632 std::set<NavigationID> navigation_id_history_; | 644 std::set<NavigationID> navigation_id_history_; |
633 }; | 645 }; |
634 | 646 |
635 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { | 647 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { |
636 // These resources have default priorities that correspond to | 648 // These resources have default priorities that correspond to |
637 // blink::typeToPriority function. | 649 // blink::typeToPriority function. |
638 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 650 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
639 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 651 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
640 net::HIGHEST); | 652 net::HIGHEST); |
641 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 653 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
642 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | 654 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
643 net::HIGHEST); | 655 net::HIGHEST); |
644 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath)); | 656 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath)); |
657 | |
658 // The local cache is cleared. | |
659 histogram_tester_->ExpectBucketCount( | |
660 internal::kResourcePrefetchPredictorPrefetchMissesCountCached, 0, 1); | |
661 histogram_tester_->ExpectBucketCount( | |
662 internal::kResourcePrefetchPredictorPrefetchMissesCountNotCached, 0, 1); | |
663 histogram_tester_->ExpectBucketCount( | |
664 internal::kResourcePrefetchPredictorPrefetchHitsCountCached, 0, 1); | |
665 histogram_tester_->ExpectBucketCount( | |
666 internal::kResourcePrefetchPredictorPrefetchHitsCountNotCached, 4, 1); | |
667 | |
668 histogram_tester_->ExpectBucketCount( | |
669 internal::kResourcePrefetchPredictorPrefetchMissesSize, 0, 1); | |
670 // Each request is ~1k, see HandleResourceRequest() above. | |
671 histogram_tester_->ExpectBucketCount( | |
672 internal::kResourcePrefetchPredictorPrefetchHitsSize, 4, 1); | |
645 } | 673 } |
646 | 674 |
647 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Redirect) { | 675 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Redirect) { |
648 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); | 676 GURL initial_url = embedded_test_server()->GetURL(kFooHost, kRedirectPath); |
649 AddRedirectChain(initial_url, {{net::HTTP_MOVED_PERMANENTLY, | 677 AddRedirectChain(initial_url, {{net::HTTP_MOVED_PERMANENTLY, |
650 GetURL(kHtmlSubresourcesPath)}}); | 678 GetURL(kHtmlSubresourcesPath)}}); |
651 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 679 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
652 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 680 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
653 net::HIGHEST); | 681 net::HIGHEST); |
654 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 682 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
872 TryToPrefetchURL(initial_url); | 900 TryToPrefetchURL(initial_url); |
873 NavigateToURLAndCheckSubresources(initial_url); | 901 NavigateToURLAndCheckSubresources(initial_url); |
874 ClearCache(); | 902 ClearCache(); |
875 // But the predictor database contains all subresources for the endpoint url | 903 // But the predictor database contains all subresources for the endpoint url |
876 // so this prefetch works. | 904 // so this prefetch works. |
877 PrefetchURL(GetURL(kHtmlSubresourcesPath)); | 905 PrefetchURL(GetURL(kHtmlSubresourcesPath)); |
878 NavigateToURLAndCheckSubresourcesAllCached(GetURL(kHtmlSubresourcesPath)); | 906 NavigateToURLAndCheckSubresourcesAllCached(GetURL(kHtmlSubresourcesPath)); |
879 } | 907 } |
880 | 908 |
881 } // namespace predictors | 909 } // namespace predictors |
OLD | NEW |