Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(368)

Side by Side Diff: chrome/browser/predictors/loading_predictor.cc

Issue 2887133003: predictors: Refactor resource_prefetch_predictor triggering. (Closed)
Patch Set: . Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/loading_predictor.h" 5 #include "chrome/browser/predictors/loading_predictor.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "chrome/browser/predictors/resource_prefetch_common.h"
8 #include "chrome/browser/predictors/resource_prefetch_predictor.h" 10 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
9 11
10 namespace predictors { 12 namespace predictors {
11 13
14 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary;
15
12 LoadingPredictor::LoadingPredictor(const LoadingPredictorConfig& config, 16 LoadingPredictor::LoadingPredictor(const LoadingPredictorConfig& config,
13 Profile* profile) { 17 Profile* profile)
14 resource_prefetch_predictor_ = 18 : config_(config),
15 base::MakeUnique<ResourcePrefetchPredictor>(config, profile); 19 profile_(profile),
16 } 20 resource_prefetch_predictor_(
21 base::MakeUnique<ResourcePrefetchPredictor>(config, profile)) {}
17 22
18 LoadingPredictor::~LoadingPredictor() = default; 23 LoadingPredictor::~LoadingPredictor() = default;
19 24
20 void LoadingPredictor::PrepareForPageLoad(const GURL& url, HintOrigin origin) { 25 void LoadingPredictor::PrepareForPageLoad(const GURL& url, HintOrigin origin) {
21 resource_prefetch_predictor_->StartPrefetching(url, origin); 26 // To report hint durations.
27 if (active_hints_.find(url) == active_hints_.end())
28 active_hints_.emplace(url, base::TimeTicks::Now());
alexilin 2017/05/30 16:19:14 We used to report a hint duration only for prefetc
Benoit L 2017/05/31 14:34:41 Done.
29
30 if (!config_.IsPrefetchingEnabledForOrigin(profile_, origin) ||
31 !resource_prefetch_predictor_->IsUrlPrefetchable(url)) {
alexilin 2017/05/30 16:19:14 nit: There is no real need to check for IsUrlPrefe
Benoit L 2017/05/31 14:34:42 Kept the check anyway, both for tracking and also
32 return;
33 }
34
35 resource_prefetch_predictor_->StartPrefetching(url);
alexilin 2017/05/30 16:19:14 Should we do something if the url is already in ac
Benoit L 2017/05/31 14:34:41 Done.
22 } 36 }
23 37
24 void LoadingPredictor::CancelPageLoadHint(const GURL& url) { 38 void LoadingPredictor::CancelPageLoadHint(const GURL& url) {
25 resource_prefetch_predictor_->StopPrefetching(url); 39 auto it = active_hints_.find(url);
40 CancelActiveHint(it);
alexilin 2017/05/30 16:19:14 tiny tiny nit: Why not in one line? CancelActiveHi
Benoit L 2017/05/31 14:34:41 Done.
26 } 41 }
27 42
28 void LoadingPredictor::StartInitialization() { 43 void LoadingPredictor::StartInitialization() {
29 resource_prefetch_predictor_->StartInitialization(); 44 resource_prefetch_predictor_->StartInitialization();
30 } 45 }
31 46
32 ResourcePrefetchPredictor* LoadingPredictor::resource_prefetch_predictor() 47 ResourcePrefetchPredictor* LoadingPredictor::resource_prefetch_predictor()
33 const { 48 const {
34 return resource_prefetch_predictor_.get(); 49 return resource_prefetch_predictor_.get();
35 } 50 }
36 51
37 void LoadingPredictor::Shutdown() { 52 void LoadingPredictor::Shutdown() {
38 resource_prefetch_predictor_->Shutdown(); 53 resource_prefetch_predictor_->Shutdown();
39 } 54 }
40 55
56 void LoadingPredictor::OnMainFrameRequest(const URLRequestSummary& summary) {
57 const NavigationID& navigation_id = summary.navigation_id;
58 CleanupAbandonedHintsAndNavigations(navigation_id);
59 active_navigations_.emplace(
alexilin 2017/05/30 11:55:27 Shouldn't it be a part of LoadingDataCollector? I'
Benoit L 2017/05/31 14:34:42 After offline discussion, decided to keep it this
60 navigation_id,
61 std::make_pair(navigation_id.main_frame_url, base::TimeTicks::Now()));
alexilin 2017/05/30 16:19:14 The second member of the pair is unused. You're us
Benoit L 2017/05/31 14:34:41 Done.
62 PrepareForPageLoad(navigation_id.main_frame_url, HintOrigin::NAVIGATION);
63 }
64
65 void LoadingPredictor::OnMainFrameRedirect(const URLRequestSummary& summary) {
66 auto it = active_navigations_.find(summary.navigation_id);
67 if (it != active_navigations_.end()) {
68 NavigationID navigation_id = summary.navigation_id;
69 navigation_id.main_frame_url = summary.redirect_url;
alexilin 2017/05/30 16:19:14 What if redirect_url == main_frame_url? I know red
Benoit L 2017/05/31 14:34:42 That's likely filtered lower in the stack (as unle
alexilin 2017/05/31 16:02:03 Well, a redirect response could set a new cookie s
70 active_navigations_.emplace(navigation_id, it->second);
71 active_navigations_.erase(it);
72 }
73 }
74
75 void LoadingPredictor::OnMainFrameResponse(const URLRequestSummary& summary) {
76 const NavigationID& navigation_id = summary.navigation_id;
77 auto it = active_navigations_.find(navigation_id);
78 if (it != active_navigations_.end()) {
79 const auto& initial_url = it->second.first;
alexilin 2017/05/30 16:19:14 nit: s/auto/GURL/ since it's the same number of ch
Benoit L 2017/05/31 14:34:42 Done.
80 CancelPageLoadHint(initial_url);
81 active_navigations_.erase(it);
82 } else {
83 CancelPageLoadHint(navigation_id.main_frame_url);
84 }
85 }
86
87 std::map<GURL, base::TimeTicks>::iterator LoadingPredictor::CancelActiveHint(
88 std::map<GURL, base::TimeTicks>::iterator hint_it) {
89 if (hint_it == active_hints_.end())
90 return hint_it;
91
92 const GURL& url = hint_it->first;
93 resource_prefetch_predictor_->StopPrefetching(url);
94
95 UMA_HISTOGRAM_TIMES(
96 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram,
97 base::TimeTicks::Now() - hint_it->second);
98 return active_hints_.erase(hint_it);
99 }
100
101 void LoadingPredictor::CleanupAbandonedHintsAndNavigations(
102 const NavigationID& navigation_id) {
103 base::TimeTicks time_now = base::TimeTicks::Now();
104 const base::TimeDelta max_navigation_age =
105 base::TimeDelta::FromSeconds(config_.max_navigation_lifetime_seconds);
106
107 // Hints.
108 for (auto it = active_hints_.begin(); it != active_hints_.end();) {
109 base::TimeDelta prefetch_age = time_now - it->second;
110 if (prefetch_age > max_navigation_age) {
111 // Will to the last bucket meaning that the duration was unlimited.
alexilin 2017/05/30 15:06:00 nit: Delete the comment because it isn't relevant.
Benoit L 2017/05/31 14:34:41 I actually wanted to point that out, clarified the
alexilin 2017/05/31 16:02:03 s/Will to/Will go to/ Initially I was confused by
112 it = CancelActiveHint(it);
113 } else {
114 ++it;
115 }
116 }
117
118 // Navigations.
119 for (auto it = active_navigations_.begin();
120 it != active_navigations_.end();) {
121 if ((it->first.tab_id == navigation_id.tab_id) ||
122 (time_now - it->first.creation_time > max_navigation_age)) {
123 const GURL& initial_url = it->second.first;
124 CancelActiveHint(active_hints_.find(initial_url));
alexilin 2017/05/30 16:19:14 nit: CancelPageLoadHint(initial_url) since we don'
Benoit L 2017/05/31 14:34:41 I would prefer to keep it as is to be consistent w
125 it = active_navigations_.erase(it);
126 } else {
127 ++it;
128 }
129 }
130 }
131
41 } // namespace predictors 132 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698