Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/predictors/resource_prefetch_predictor.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 599 return; | 599 return; |
| 600 | 600 |
| 601 ResourcePrefetchPredictor::Prediction prediction; | 601 ResourcePrefetchPredictor::Prediction prediction; |
| 602 if (!GetPrefetchData(url, &prediction)) | 602 if (!GetPrefetchData(url, &prediction)) |
| 603 return; | 603 return; |
| 604 | 604 |
| 605 BrowserThread::PostTask( | 605 BrowserThread::PostTask( |
| 606 BrowserThread::IO, FROM_HERE, | 606 BrowserThread::IO, FROM_HERE, |
| 607 base::Bind(&ResourcePrefetcherManager::MaybeAddPrefetch, | 607 base::Bind(&ResourcePrefetcherManager::MaybeAddPrefetch, |
| 608 prefetch_manager_, url, prediction.subresource_urls)); | 608 prefetch_manager_, url, prediction.subresource_urls)); |
| 609 | |
| 610 if (observer_) | |
| 611 observer_->OnPrefetchingStarted(url); | |
| 609 } | 612 } |
| 610 | 613 |
| 611 void ResourcePrefetchPredictor::StopPrefetching(const GURL& url) { | 614 void ResourcePrefetchPredictor::StopPrefetching(const GURL& url) { |
| 612 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StopPrefetching", "url", | 615 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StopPrefetching", "url", |
| 613 url.spec()); | 616 url.spec()); |
| 614 auto it = inflight_prefetches_.find(url); | 617 auto it = inflight_prefetches_.find(url); |
| 615 if (it != inflight_prefetches_.end()) { | 618 if (it != inflight_prefetches_.end()) { |
| 616 UMA_HISTOGRAM_TIMES( | 619 UMA_HISTOGRAM_TIMES( |
| 617 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, | 620 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, |
| 618 base::TimeTicks::Now() - it->second); | 621 base::TimeTicks::Now() - it->second); |
| 619 inflight_prefetches_.erase(it); | 622 inflight_prefetches_.erase(it); |
| 620 } | 623 } |
| 621 if (!prefetch_manager_.get()) // Not enabled. | 624 if (!prefetch_manager_.get()) // Not enabled. |
| 622 return; | 625 return; |
| 623 | 626 |
| 624 BrowserThread::PostTask( | 627 BrowserThread::PostTask( |
| 625 BrowserThread::IO, FROM_HERE, | 628 BrowserThread::IO, FROM_HERE, |
| 626 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, | 629 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, |
| 627 prefetch_manager_, url)); | 630 prefetch_manager_, url)); |
| 631 | |
| 632 if (observer_) | |
| 633 observer_->OnPrefetchingStopped(url); | |
| 628 } | 634 } |
| 629 | 635 |
| 630 void ResourcePrefetchPredictor::OnPrefetchingFinished( | 636 void ResourcePrefetchPredictor::OnPrefetchingFinished( |
| 631 const GURL& main_frame_url, | 637 const GURL& main_frame_url, |
| 632 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats) { | 638 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats) { |
| 633 if (observer_) | 639 if (observer_) |
| 634 observer_->OnPrefetchingFinished(main_frame_url); | 640 observer_->OnPrefetchingFinished(main_frame_url); |
| 635 | 641 |
| 636 prefetcher_stats_.insert(std::make_pair(main_frame_url, std::move(stats))); | 642 prefetcher_stats_.insert(std::make_pair(main_frame_url, std::move(stats))); |
| 637 } | 643 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 668 | 674 |
| 669 // New empty navigation entry. | 675 // New empty navigation entry. |
| 670 inflight_navigations_.insert( | 676 inflight_navigations_.insert( |
| 671 std::make_pair(request.navigation_id, | 677 std::make_pair(request.navigation_id, |
| 672 base::MakeUnique<PageRequestSummary>(main_frame_url))); | 678 base::MakeUnique<PageRequestSummary>(main_frame_url))); |
| 673 } | 679 } |
| 674 | 680 |
| 675 void ResourcePrefetchPredictor::OnMainFrameResponse( | 681 void ResourcePrefetchPredictor::OnMainFrameResponse( |
| 676 const URLRequestSummary& response) { | 682 const URLRequestSummary& response) { |
| 677 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 683 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 678 if (initialization_state_ != INITIALIZED) | 684 DCHECK_EQ(INITIALIZED, initialization_state_); |
|
alexilin
2017/02/27 15:53:36
This function shouldn't be called before initializ
| |
| 679 return; | |
| 680 | 685 |
| 681 StopPrefetching(response.navigation_id.main_frame_url); | 686 NavigationMap::iterator nav_it = |
| 687 inflight_navigations_.find(response.navigation_id); | |
| 688 if (nav_it != inflight_navigations_.end()) { | |
| 689 // To match an URL in StartPrefetching(). | |
| 690 StopPrefetching(nav_it->second->initial_url); | |
| 691 } else { | |
| 692 StopPrefetching(response.navigation_id.main_frame_url); | |
|
alexilin
2017/02/27 15:53:36
I'm not sure if we need this fallback.
Benoit L
2017/02/27 17:31:19
Why are you keeping it?
alexilin
2017/02/27 18:15:01
Well, if for some reason we don't find an entry in
Benoit L
2017/02/28 14:26:30
Acknowledged.
| |
| 693 } | |
| 682 } | 694 } |
| 683 | 695 |
| 684 void ResourcePrefetchPredictor::OnMainFrameRedirect( | 696 void ResourcePrefetchPredictor::OnMainFrameRedirect( |
| 685 const URLRequestSummary& response) { | 697 const URLRequestSummary& response) { |
| 686 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 698 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 699 DCHECK_EQ(INITIALIZED, initialization_state_); | |
| 687 | 700 |
| 688 const GURL& main_frame_url = response.navigation_id.main_frame_url; | 701 const GURL& main_frame_url = response.navigation_id.main_frame_url; |
| 689 std::unique_ptr<PageRequestSummary> summary; | 702 std::unique_ptr<PageRequestSummary> summary; |
| 690 NavigationMap::iterator nav_it = | 703 NavigationMap::iterator nav_it = |
| 691 inflight_navigations_.find(response.navigation_id); | 704 inflight_navigations_.find(response.navigation_id); |
| 692 if (nav_it != inflight_navigations_.end()) { | 705 if (nav_it != inflight_navigations_.end()) { |
| 693 summary.reset(nav_it->second.release()); | 706 summary.reset(nav_it->second.release()); |
| 694 inflight_navigations_.erase(nav_it); | 707 inflight_navigations_.erase(nav_it); |
| 695 } | 708 } |
| 696 | 709 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 708 NavigationID navigation_id(response.navigation_id); | 721 NavigationID navigation_id(response.navigation_id); |
| 709 navigation_id.main_frame_url = response.redirect_url; | 722 navigation_id.main_frame_url = response.redirect_url; |
| 710 summary->main_frame_url = response.redirect_url; | 723 summary->main_frame_url = response.redirect_url; |
| 711 inflight_navigations_.insert( | 724 inflight_navigations_.insert( |
| 712 std::make_pair(navigation_id, std::move(summary))); | 725 std::make_pair(navigation_id, std::move(summary))); |
| 713 } | 726 } |
| 714 | 727 |
| 715 void ResourcePrefetchPredictor::OnSubresourceResponse( | 728 void ResourcePrefetchPredictor::OnSubresourceResponse( |
| 716 const URLRequestSummary& response) { | 729 const URLRequestSummary& response) { |
| 717 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 730 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 731 DCHECK_EQ(INITIALIZED, initialization_state_); | |
| 718 | 732 |
| 719 NavigationMap::const_iterator nav_it = | 733 NavigationMap::const_iterator nav_it = |
| 720 inflight_navigations_.find(response.navigation_id); | 734 inflight_navigations_.find(response.navigation_id); |
| 721 if (nav_it == inflight_navigations_.end()) { | 735 if (nav_it == inflight_navigations_.end()) { |
| 722 return; | 736 return; |
| 723 } | 737 } |
| 724 | 738 |
| 725 nav_it->second->subresource_requests.push_back(response); | 739 nav_it->second->subresource_requests.push_back(response); |
| 726 } | 740 } |
| 727 | 741 |
| 728 void ResourcePrefetchPredictor::OnNavigationComplete( | 742 void ResourcePrefetchPredictor::OnNavigationComplete( |
| 729 const NavigationID& nav_id_without_timing_info) { | 743 const NavigationID& nav_id_without_timing_info) { |
| 730 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 744 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 745 DCHECK_EQ(INITIALIZED, initialization_state_); | |
| 731 | 746 |
| 732 NavigationMap::iterator nav_it = | 747 NavigationMap::iterator nav_it = |
| 733 inflight_navigations_.find(nav_id_without_timing_info); | 748 inflight_navigations_.find(nav_id_without_timing_info); |
| 734 if (nav_it == inflight_navigations_.end()) | 749 if (nav_it == inflight_navigations_.end()) |
| 735 return; | 750 return; |
| 736 | 751 |
| 737 // Remove the navigation from the inflight navigations. | 752 // Remove the navigation from the inflight navigations. |
| 738 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); | 753 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); |
| 739 inflight_navigations_.erase(nav_it); | 754 inflight_navigations_.erase(nav_it); |
| 740 | 755 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 896 if ((it->first.tab_id == navigation_id.tab_id) || | 911 if ((it->first.tab_id == navigation_id.tab_id) || |
| 897 (time_now - it->first.creation_time > max_navigation_age)) { | 912 (time_now - it->first.creation_time > max_navigation_age)) { |
| 898 inflight_navigations_.erase(it++); | 913 inflight_navigations_.erase(it++); |
| 899 } else { | 914 } else { |
| 900 ++it; | 915 ++it; |
| 901 } | 916 } |
| 902 } | 917 } |
| 903 | 918 |
| 904 for (auto it = inflight_prefetches_.begin(); | 919 for (auto it = inflight_prefetches_.begin(); |
| 905 it != inflight_prefetches_.end();) { | 920 it != inflight_prefetches_.end();) { |
| 906 if (time_now - it->second > max_navigation_age) | 921 if (time_now - it->second > max_navigation_age) { |
| 922 // It goes to the last bucket meaning that the duration was unlimited. | |
| 923 UMA_HISTOGRAM_TIMES( | |
| 924 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, | |
| 925 base::TimeTicks::Now() - it->second); | |
|
Benoit L
2017/02/27 17:31:19
tiny nit: What computing the interval twice? Can y
alexilin
2017/02/27 18:15:01
Done, thanks.
Actually, this value is always outsi
| |
| 907 it = inflight_prefetches_.erase(it); | 926 it = inflight_prefetches_.erase(it); |
| 908 else | 927 } else { |
| 909 ++it; | 928 ++it; |
| 929 } | |
| 910 } | 930 } |
| 911 | 931 |
| 912 // Remove old prefetches that haven't been claimed. | 932 // Remove old prefetches that haven't been claimed. |
| 913 for (auto stats_it = prefetcher_stats_.begin(); | 933 for (auto stats_it = prefetcher_stats_.begin(); |
| 914 stats_it != prefetcher_stats_.end();) { | 934 stats_it != prefetcher_stats_.end();) { |
| 915 if (time_now - stats_it->second->start_time > max_navigation_age) { | 935 if (time_now - stats_it->second->start_time > max_navigation_age) { |
| 916 // No requests -> everything is a miss. | 936 // No requests -> everything is a miss. |
| 917 ReportPrefetchAccuracy(*stats_it->second, | 937 ReportPrefetchAccuracy(*stats_it->second, |
| 918 std::vector<URLRequestSummary>()); | 938 std::vector<URLRequestSummary>()); |
| 919 stats_it = prefetcher_stats_.erase(stats_it); | 939 stats_it = prefetcher_stats_.erase(stats_it); |
| (...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1393 TestObserver::~TestObserver() { | 1413 TestObserver::~TestObserver() { |
| 1394 predictor_->SetObserverForTesting(nullptr); | 1414 predictor_->SetObserverForTesting(nullptr); |
| 1395 } | 1415 } |
| 1396 | 1416 |
| 1397 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1417 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
| 1398 : predictor_(predictor) { | 1418 : predictor_(predictor) { |
| 1399 predictor_->SetObserverForTesting(this); | 1419 predictor_->SetObserverForTesting(this); |
| 1400 } | 1420 } |
| 1401 | 1421 |
| 1402 } // namespace predictors | 1422 } // namespace predictors |
| OLD | NEW |