Index: chrome/browser/predictors/loading_predictor.cc |
diff --git a/chrome/browser/predictors/loading_predictor.cc b/chrome/browser/predictors/loading_predictor.cc |
index d5bd264009427aead4df1a4926e85807fee38f60..e81841889e1fae246ea27d32670ccc2ee016c575 100644 |
--- a/chrome/browser/predictors/loading_predictor.cc |
+++ b/chrome/browser/predictors/loading_predictor.cc |
@@ -5,24 +5,37 @@ |
#include "chrome/browser/predictors/loading_predictor.h" |
#include "base/memory/ptr_util.h" |
+#include "base/metrics/histogram_macros.h" |
+#include "chrome/browser/predictors/resource_prefetch_common.h" |
#include "chrome/browser/predictors/resource_prefetch_predictor.h" |
namespace predictors { |
+using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
+ |
LoadingPredictor::LoadingPredictor(const LoadingPredictorConfig& config, |
- Profile* profile) { |
- resource_prefetch_predictor_ = |
- base::MakeUnique<ResourcePrefetchPredictor>(config, profile); |
-} |
+ Profile* profile) |
+ : config_(config), |
+ profile_(profile), |
+ resource_prefetch_predictor_( |
+ base::MakeUnique<ResourcePrefetchPredictor>(config, profile)) {} |
LoadingPredictor::~LoadingPredictor() = default; |
void LoadingPredictor::PrepareForPageLoad(const GURL& url, HintOrigin origin) { |
- resource_prefetch_predictor_->StartPrefetching(url, origin); |
+ if (active_hints_.find(url) != active_hints_.end() || |
+ !resource_prefetch_predictor_->IsUrlPrefetchable(url)) |
+ return; |
+ |
+ // To report hint durations. |
+ active_hints_.emplace(url, base::TimeTicks::Now()); |
+ |
+ if (config_.IsPrefetchingEnabledForOrigin(profile_, origin)) |
+ resource_prefetch_predictor_->StartPrefetching(url); |
} |
void LoadingPredictor::CancelPageLoadHint(const GURL& url) { |
- resource_prefetch_predictor_->StopPrefetching(url); |
+ CancelActiveHint(active_hints_.find(url)); |
} |
void LoadingPredictor::StartInitialization() { |
@@ -38,4 +51,87 @@ void LoadingPredictor::Shutdown() { |
resource_prefetch_predictor_->Shutdown(); |
} |
+void LoadingPredictor::OnMainFrameRequest(const URLRequestSummary& summary) { |
+ DCHECK(summary.resource_type == content::RESOURCE_TYPE_MAIN_FRAME); |
+ |
+ const NavigationID& navigation_id = summary.navigation_id; |
+ CleanupAbandonedHintsAndNavigations(navigation_id); |
+ active_navigations_.emplace(navigation_id, navigation_id.main_frame_url); |
+ PrepareForPageLoad(navigation_id.main_frame_url, HintOrigin::NAVIGATION); |
+} |
+ |
+void LoadingPredictor::OnMainFrameRedirect(const URLRequestSummary& summary) { |
+ DCHECK(summary.resource_type == content::RESOURCE_TYPE_MAIN_FRAME); |
+ |
+ auto it = active_navigations_.find(summary.navigation_id); |
+ if (it != active_navigations_.end()) { |
+ if (summary.navigation_id.main_frame_url == summary.redirect_url) |
+ return; |
+ NavigationID navigation_id = summary.navigation_id; |
+ navigation_id.main_frame_url = summary.redirect_url; |
+ active_navigations_.emplace(navigation_id, it->second); |
+ active_navigations_.erase(it); |
+ } |
+} |
+ |
+void LoadingPredictor::OnMainFrameResponse(const URLRequestSummary& summary) { |
+ DCHECK(summary.resource_type == content::RESOURCE_TYPE_MAIN_FRAME); |
+ |
+ const NavigationID& navigation_id = summary.navigation_id; |
+ auto it = active_navigations_.find(navigation_id); |
+ if (it != active_navigations_.end()) { |
+ const GURL& initial_url = it->second; |
+ CancelPageLoadHint(initial_url); |
+ active_navigations_.erase(it); |
+ } else { |
+ CancelPageLoadHint(navigation_id.main_frame_url); |
+ } |
+} |
+ |
+std::map<GURL, base::TimeTicks>::iterator LoadingPredictor::CancelActiveHint( |
+ std::map<GURL, base::TimeTicks>::iterator hint_it) { |
+ if (hint_it == active_hints_.end()) |
+ return hint_it; |
+ |
+ const GURL& url = hint_it->first; |
+ resource_prefetch_predictor_->StopPrefetching(url); |
+ |
+ UMA_HISTOGRAM_TIMES( |
+ internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, |
+ base::TimeTicks::Now() - hint_it->second); |
+ return active_hints_.erase(hint_it); |
+} |
+ |
+void LoadingPredictor::CleanupAbandonedHintsAndNavigations( |
+ const NavigationID& navigation_id) { |
+ base::TimeTicks time_now = base::TimeTicks::Now(); |
+ const base::TimeDelta max_navigation_age = |
+ base::TimeDelta::FromSeconds(config_.max_navigation_lifetime_seconds); |
+ |
+ // Hints. |
+ for (auto it = active_hints_.begin(); it != active_hints_.end();) { |
+ base::TimeDelta prefetch_age = time_now - it->second; |
+ if (prefetch_age > max_navigation_age) { |
+ // Will go to the last bucket in the duration reported in |
+ // CancelActiveHint() meaning that the duration was unlimited. |
+ it = CancelActiveHint(it); |
+ } else { |
+ ++it; |
+ } |
+ } |
+ |
+ // Navigations. |
+ for (auto it = active_navigations_.begin(); |
+ it != active_navigations_.end();) { |
+ if ((it->first.tab_id == navigation_id.tab_id) || |
+ (time_now - it->first.creation_time > max_navigation_age)) { |
+ const GURL& initial_url = it->second; |
+ CancelActiveHint(active_hints_.find(initial_url)); |
+ it = active_navigations_.erase(it); |
+ } else { |
+ ++it; |
+ } |
+ } |
+} |
+ |
} // namespace predictors |