| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
| 14 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 14 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
| 15 | 15 |
| 16 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" | 16 #include "chrome/browser/predictors/loading_predictor.h" |
| 17 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 17 #include "chrome/browser/predictors/loading_predictor_factory.h" |
| 18 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | |
| 19 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 18 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
| 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 23 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/test/base/in_process_browser_test.h" | 23 #include "chrome/test/base/in_process_browser_test.h" |
| 25 #include "chrome/test/base/ui_test_utils.h" | 24 #include "chrome/test/base/ui_test_utils.h" |
| 26 #include "content/public/browser/browsing_data_remover.h" | 25 #include "content/public/browser/browsing_data_remover.h" |
| 27 #include "net/base/host_port_pair.h" | 26 #include "net/base/host_port_pair.h" |
| 28 #include "net/base/url_util.h" | 27 #include "net/base/url_util.h" |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 base::Unretained(this))); | 357 base::Unretained(this))); |
| 359 server->RegisterRequestHandler(base::Bind( | 358 server->RegisterRequestHandler(base::Bind( |
| 360 &ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 359 &ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
| 361 base::Unretained(this))); | 360 base::Unretained(this))); |
| 362 server->RegisterRequestMonitor(base::Bind( | 361 server->RegisterRequestMonitor(base::Bind( |
| 363 &ResourcePrefetchPredictorBrowserTest::MonitorResourceRequest, | 362 &ResourcePrefetchPredictorBrowserTest::MonitorResourceRequest, |
| 364 base::Unretained(this))); | 363 base::Unretained(this))); |
| 365 ASSERT_TRUE(server->Start()); | 364 ASSERT_TRUE(server->Start()); |
| 366 } | 365 } |
| 367 | 366 |
| 368 predictor_ = | 367 predictor_ = LoadingPredictorFactory::GetForProfile(browser()->profile()); |
| 369 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | |
| 370 ASSERT_TRUE(predictor_); | 368 ASSERT_TRUE(predictor_); |
| 369 resource_prefetch_predictor_ = predictor_->resource_prefetch_predictor(); |
| 371 // URLs from the test server contain a port number. | 370 // URLs from the test server contain a port number. |
| 372 ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(true); | 371 ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(true); |
| 373 EnsurePredictorInitialized(); | 372 EnsurePredictorInitialized(); |
| 374 histogram_tester_.reset(new base::HistogramTester()); | 373 histogram_tester_.reset(new base::HistogramTester()); |
| 375 } | 374 } |
| 376 | 375 |
| 377 void TearDownOnMainThread() override { | 376 void TearDownOnMainThread() override { |
| 378 ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(false); | 377 ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(false); |
| 379 } | 378 } |
| 380 | 379 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 if (kv.second.is_observable) { | 421 if (kv.second.is_observable) { |
| 423 url_request_summaries.push_back( | 422 url_request_summaries.push_back( |
| 424 GetURLRequestSummaryForResource(main_frame_url, kv.second)); | 423 GetURLRequestSummaryForResource(main_frame_url, kv.second)); |
| 425 } | 424 } |
| 426 } | 425 } |
| 427 | 426 |
| 428 bool match_navigation_id = | 427 bool match_navigation_id = |
| 429 disposition == WindowOpenDisposition::CURRENT_TAB; | 428 disposition == WindowOpenDisposition::CURRENT_TAB; |
| 430 | 429 |
| 431 LearningObserver observer( | 430 LearningObserver observer( |
| 432 predictor_, UpdateAndGetVisitCount(initial_url), | 431 resource_prefetch_predictor_, UpdateAndGetVisitCount(initial_url), |
| 433 CreatePageRequestSummary(main_frame_url.spec(), initial_url.spec(), | 432 CreatePageRequestSummary(main_frame_url.spec(), initial_url.spec(), |
| 434 url_request_summaries), | 433 url_request_summaries), |
| 435 match_navigation_id, match_before_first_contentful_paint); | 434 match_navigation_id, match_before_first_contentful_paint); |
| 436 ui_test_utils::NavigateToURLWithDisposition( | 435 ui_test_utils::NavigateToURLWithDisposition( |
| 437 browser(), navigation_url, disposition, | 436 browser(), navigation_url, disposition, |
| 438 ui_test_utils::BROWSER_TEST_NONE); | 437 ui_test_utils::BROWSER_TEST_NONE); |
| 439 observer.Wait(); | 438 observer.Wait(); |
| 440 | 439 |
| 441 for (auto& kv : resources_) { | 440 for (auto& kv : resources_) { |
| 442 if (kv.second.is_observable) | 441 if (kv.second.is_observable) |
| 443 kv.second.request.was_cached = true; | 442 kv.second.request.was_cached = true; |
| 444 } | 443 } |
| 445 for (const auto& nav : observer.current_navigation_ids()) | 444 for (const auto& nav : observer.current_navigation_ids()) |
| 446 navigation_id_history_.insert(nav); | 445 navigation_id_history_.insert(nav); |
| 447 } | 446 } |
| 448 | 447 |
| 449 void NavigateToURLAndCheckPrefetching(const GURL& main_frame_url) { | 448 void NavigateToURLAndCheckPrefetching(const GURL& main_frame_url) { |
| 450 PrefetchingObserver observer(predictor_, main_frame_url, true); | 449 PrefetchingObserver observer(resource_prefetch_predictor_, main_frame_url, |
| 450 true); |
| 451 ui_test_utils::NavigateToURL(browser(), main_frame_url); | 451 ui_test_utils::NavigateToURL(browser(), main_frame_url); |
| 452 observer.Wait(); | 452 observer.Wait(); |
| 453 for (auto& kv : resources_) { | 453 for (auto& kv : resources_) { |
| 454 if (kv.second.is_observable) | 454 if (kv.second.is_observable) |
| 455 kv.second.request.was_cached = true; | 455 kv.second.request.was_cached = true; |
| 456 } | 456 } |
| 457 } | 457 } |
| 458 | 458 |
| 459 void PrefetchURL(const GURL& main_frame_url) { | 459 void PrefetchURL(const GURL& main_frame_url) { |
| 460 PrefetchingObserver observer(predictor_, main_frame_url, false); | 460 PrefetchingObserver observer(resource_prefetch_predictor_, main_frame_url, |
| 461 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); | 461 false); |
| 462 predictor_->PrepareForPageLoad(main_frame_url, HintOrigin::EXTERNAL); |
| 462 observer.Wait(); | 463 observer.Wait(); |
| 463 for (auto& kv : resources_) { | 464 for (auto& kv : resources_) { |
| 464 if (kv.second.is_observable) | 465 if (kv.second.is_observable) |
| 465 kv.second.request.was_cached = true; | 466 kv.second.request.was_cached = true; |
| 466 } | 467 } |
| 467 } | 468 } |
| 468 | 469 |
| 469 void TryToPrefetchURL(const GURL& main_frame_url) { | 470 void TryToPrefetchURL(const GURL& main_frame_url) { |
| 470 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); | 471 predictor_->PrepareForPageLoad(main_frame_url, HintOrigin::EXTERNAL); |
| 471 } | 472 } |
| 472 | 473 |
| 473 ResourceSummary* AddResource(const GURL& resource_url, | 474 ResourceSummary* AddResource(const GURL& resource_url, |
| 474 content::ResourceType resource_type, | 475 content::ResourceType resource_type, |
| 475 net::RequestPriority priority) { | 476 net::RequestPriority priority) { |
| 476 auto pair_and_whether_inserted = | 477 auto pair_and_whether_inserted = |
| 477 resources_.insert(std::make_pair(resource_url, ResourceSummary())); | 478 resources_.insert(std::make_pair(resource_url, ResourceSummary())); |
| 478 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url | 479 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url |
| 479 << " was inserted twice"; | 480 << " was inserted twice"; |
| 480 ResourceSummary* resource = &pair_and_whether_inserted.first->second; | 481 ResourceSummary* resource = &pair_and_whether_inserted.first->second; |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 AddResource(GetURLWithHost(kFont[0], kFont[1]), | 582 AddResource(GetURLWithHost(kFont[0], kFont[1]), |
| 582 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST)}; | 583 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST)}; |
| 583 } | 584 } |
| 584 | 585 |
| 585 std::unique_ptr<base::HistogramTester> histogram_tester_; | 586 std::unique_ptr<base::HistogramTester> histogram_tester_; |
| 586 | 587 |
| 587 private: | 588 private: |
| 588 // ResourcePrefetchPredictor needs to be initialized before the navigation | 589 // ResourcePrefetchPredictor needs to be initialized before the navigation |
| 589 // happens otherwise this navigation will be ignored by predictor. | 590 // happens otherwise this navigation will be ignored by predictor. |
| 590 void EnsurePredictorInitialized() { | 591 void EnsurePredictorInitialized() { |
| 591 if (predictor_->initialization_state_ == | 592 if (resource_prefetch_predictor_->initialization_state_ == |
| 592 ResourcePrefetchPredictor::INITIALIZED) { | 593 ResourcePrefetchPredictor::INITIALIZED) { |
| 593 return; | 594 return; |
| 594 } | 595 } |
| 595 | 596 |
| 596 InitializationObserver observer(predictor_); | 597 InitializationObserver observer(resource_prefetch_predictor_); |
| 597 if (predictor_->initialization_state_ == | 598 if (resource_prefetch_predictor_->initialization_state_ == |
| 598 ResourcePrefetchPredictor::NOT_INITIALIZED) { | 599 ResourcePrefetchPredictor::NOT_INITIALIZED) { |
| 599 predictor_->StartInitialization(); | 600 resource_prefetch_predictor_->StartInitialization(); |
| 600 } | 601 } |
| 601 observer.Wait(); | 602 observer.Wait(); |
| 602 } | 603 } |
| 603 | 604 |
| 604 URLRequestSummary GetURLRequestSummaryForResource( | 605 URLRequestSummary GetURLRequestSummaryForResource( |
| 605 const GURL& main_frame_url, | 606 const GURL& main_frame_url, |
| 606 const ResourceSummary& resource_summary) const { | 607 const ResourceSummary& resource_summary) const { |
| 607 URLRequestSummary summary(resource_summary.request); | 608 URLRequestSummary summary(resource_summary.request); |
| 608 content::WebContents* web_contents = | 609 content::WebContents* web_contents = |
| 609 browser()->tab_strip_model()->GetActiveWebContents(); | 610 browser()->tab_strip_model()->GetActiveWebContents(); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 base::MakeUnique<net::test_server::BasicHttpResponse>(); | 720 base::MakeUnique<net::test_server::BasicHttpResponse>(); |
| 720 http_response->set_code(redirect_it->second.code); | 721 http_response->set_code(redirect_it->second.code); |
| 721 http_response->AddCustomHeader("Location", redirect_it->second.url.spec()); | 722 http_response->AddCustomHeader("Location", redirect_it->second.url.spec()); |
| 722 return std::move(http_response); | 723 return std::move(http_response); |
| 723 } | 724 } |
| 724 | 725 |
| 725 size_t UpdateAndGetVisitCount(const GURL& main_frame_url) { | 726 size_t UpdateAndGetVisitCount(const GURL& main_frame_url) { |
| 726 return ++visit_count_[main_frame_url]; | 727 return ++visit_count_[main_frame_url]; |
| 727 } | 728 } |
| 728 | 729 |
| 729 ResourcePrefetchPredictor* predictor_; | 730 LoadingPredictor* predictor_; |
| 731 ResourcePrefetchPredictor* resource_prefetch_predictor_; |
| 730 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 732 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
| 731 std::map<GURL, ResourceSummary> resources_; | 733 std::map<GURL, ResourceSummary> resources_; |
| 732 std::map<GURL, RedirectEdge> redirects_; | 734 std::map<GURL, RedirectEdge> redirects_; |
| 733 std::map<GURL, size_t> visit_count_; | 735 std::map<GURL, size_t> visit_count_; |
| 734 std::set<NavigationID> navigation_id_history_; | 736 std::set<NavigationID> navigation_id_history_; |
| 735 }; | 737 }; |
| 736 | 738 |
| 737 // Subclass to test PrefetchOrigin::NAVIGATION. | 739 // Subclass to test HintOrigin::NAVIGATION. |
| 738 class ResourcePrefetchPredictorPrefetchingBrowserTest | 740 class ResourcePrefetchPredictorPrefetchingBrowserTest |
| 739 : public ResourcePrefetchPredictorBrowserTest { | 741 : public ResourcePrefetchPredictorBrowserTest { |
| 740 protected: | 742 protected: |
| 741 void SetUpCommandLine(base::CommandLine* command_line) override { | 743 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 742 command_line->AppendSwitchASCII("force-fieldtrials", "trial/group"); | 744 command_line->AppendSwitchASCII("force-fieldtrials", "trial/group"); |
| 743 std::string parameter = base::StringPrintf( | 745 std::string parameter = base::StringPrintf( |
| 744 "trial.group:%s/%s", kModeParamName, kPrefetchingMode); | 746 "trial.group:%s/%s", kModeParamName, kPrefetchingMode); |
| 745 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); | 747 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); |
| 746 std::string enabled_feature = base::StringPrintf( | 748 std::string enabled_feature = base::StringPrintf( |
| 747 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); | 749 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1007 AddResourcesFromSubresourceHtml(); | 1009 AddResourcesFromSubresourceHtml(); |
| 1008 | 1010 |
| 1009 NavigateToURLAndCheckSubresources(initial_url); | 1011 NavigateToURLAndCheckSubresources(initial_url); |
| 1010 ClearCache(); | 1012 ClearCache(); |
| 1011 NavigateToURLAndCheckSubresources(initial_url); | 1013 NavigateToURLAndCheckSubresources(initial_url); |
| 1012 ClearCache(); | 1014 ClearCache(); |
| 1013 NavigateToURLAndCheckPrefetching(initial_url); | 1015 NavigateToURLAndCheckPrefetching(initial_url); |
| 1014 } | 1016 } |
| 1015 | 1017 |
| 1016 } // namespace predictors | 1018 } // namespace predictors |
| OLD | NEW |