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

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

Issue 2847183002: predictors: Introduce GlowplugPredictor. (Closed)
Patch Set: Address comments. Created 3 years, 7 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 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_common.h" 5 #include "chrome/browser/predictors/resource_prefetch_common.h"
6 6
7 #include <string> 7 #include <string>
8 #include <tuple> 8 #include <tuple>
9 9
10 #include "base/metrics/field_trial_params.h" 10 #include "base/metrics/field_trial_params.h"
11 #include "chrome/browser/net/prediction_options.h" 11 #include "chrome/browser/net/prediction_options.h"
12 #include "chrome/browser/predictors/glowplug_config.h"
12 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/sessions/session_tab_helper.h" 14 #include "chrome/browser/sessions/session_tab_helper.h"
14 #include "components/prefs/pref_service.h" 15 #include "components/prefs/pref_service.h"
15 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/web_contents.h" 17 #include "content/public/browser/web_contents.h"
17 18
18 namespace predictors { 19 namespace predictors {
19 20
20 const char kSpeculativeResourcePrefetchingFeatureName[] = 21 const char kSpeculativeResourcePrefetchingFeatureName[] =
21 "SpeculativeResourcePrefetching"; 22 "SpeculativeResourcePrefetching";
22 const char kModeParamName[] = "mode"; 23 const char kModeParamName[] = "mode";
23 const char kLearningMode[] = "learning"; 24 const char kLearningMode[] = "learning";
24 const char kExternalPrefetchingMode[] = "external-prefetching"; 25 const char kExternalPrefetchingMode[] = "external-prefetching";
25 const char kPrefetchingMode[] = "prefetching"; 26 const char kPrefetchingMode[] = "prefetching";
26 const char kEnableUrlLearningParamName[] = "enable-url-learning"; 27 const char kEnableUrlLearningParamName[] = "enable-url-learning";
27 const char kEnableManifestsParamName[] = "enable-manifests"; 28 const char kEnableManifestsParamName[] = "enable-manifests";
28 const char kEnableOriginLearningParamName[] = "enable-origin-learning"; 29 const char kEnableOriginLearningParamName[] = "enable-origin-learning";
29 30
30 const base::Feature kSpeculativeResourcePrefetchingFeature = 31 const base::Feature kSpeculativeResourcePrefetchingFeature =
31 base::Feature(kSpeculativeResourcePrefetchingFeatureName, 32 base::Feature(kSpeculativeResourcePrefetchingFeatureName,
32 base::FEATURE_DISABLED_BY_DEFAULT); 33 base::FEATURE_DISABLED_BY_DEFAULT);
33 34
34 namespace { 35 namespace internal {
35 36
36 bool IsPrefetchingEnabledInternal(Profile* profile, int mode, int mask) { 37 bool IsPrefetchingEnabledInternal(Profile* profile, int mode, int mask) {
alexilin 2017/05/12 14:26:46 Why don't move it to glowplug_config.cc? The only
37 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 38 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
38 if ((mode & mask) == 0) 39 if ((mode & mask) == 0)
39 return false; 40 return false;
40 41
41 if (!profile || !profile->GetPrefs() || 42 if (!profile || !profile->GetPrefs() ||
42 chrome_browser_net::CanPrefetchAndPrerenderUI(profile->GetPrefs()) != 43 chrome_browser_net::CanPrefetchAndPrerenderUI(profile->GetPrefs()) !=
43 chrome_browser_net::NetworkPredictionStatus::ENABLED) { 44 chrome_browser_net::NetworkPredictionStatus::ENABLED) {
44 return false; 45 return false;
45 } 46 }
46 47
47 return true; 48 return true;
48 } 49 }
49 50
50 } // namespace 51 } // namespace internal
51 52
52 bool IsSpeculativeResourcePrefetchingEnabled( 53 bool IsSpeculativeResourcePrefetchingEnabled(Profile* profile,
53 Profile* profile, 54 GlowplugPredictorConfig* config) {
54 ResourcePrefetchPredictorConfig* config) {
55 DCHECK(config);
56
57 // Disabled for of-the-record. Policy choice, not a technical limitation. 55 // Disabled for of-the-record. Policy choice, not a technical limitation.
58 if (!profile || profile->IsOffTheRecord()) 56 if (!profile || profile->IsOffTheRecord())
59 return false; 57 return false;
60 58
61 if (!base::FeatureList::IsEnabled(kSpeculativeResourcePrefetchingFeature)) 59 if (!base::FeatureList::IsEnabled(kSpeculativeResourcePrefetchingFeature))
62 return false; 60 return false;
63 61
64 std::string enable_url_learning_value = 62 if (config) {
65 base::GetFieldTrialParamValueByFeature( 63 std::string enable_url_learning_value =
66 kSpeculativeResourcePrefetchingFeature, kEnableUrlLearningParamName); 64 base::GetFieldTrialParamValueByFeature(
67 if (enable_url_learning_value == "true") 65 kSpeculativeResourcePrefetchingFeature,
68 config->is_url_learning_enabled = true; 66 kEnableUrlLearningParamName);
67 if (enable_url_learning_value == "true")
68 config->is_url_learning_enabled = true;
69 69
70 std::string enable_manifests_value = base::GetFieldTrialParamValueByFeature( 70 std::string enable_manifests_value = base::GetFieldTrialParamValueByFeature(
71 kSpeculativeResourcePrefetchingFeature, kEnableManifestsParamName); 71 kSpeculativeResourcePrefetchingFeature, kEnableManifestsParamName);
72 if (enable_manifests_value == "true") 72 if (enable_manifests_value == "true")
73 config->is_manifests_enabled = true; 73 config->is_manifests_enabled = true;
74 74
75 bool enable_origin_learning = base::GetFieldTrialParamValueByFeature( 75 bool enable_origin_learning = base::GetFieldTrialParamValueByFeature(
76 kSpeculativeResourcePrefetchingFeature, 76 kSpeculativeResourcePrefetchingFeature,
77 kEnableOriginLearningParamName) == "true"; 77 kEnableOriginLearningParamName) == "true";
78 config->is_origin_learning_enabled = enable_origin_learning; 78 config->is_origin_learning_enabled = enable_origin_learning;
79 79
80 // Ensure that a resource that was only seen once is never prefetched. This 80 // Ensure that a resource that was only seen once is never prefetched. This
81 // prevents the easy mistake of trying to prefetch an ephemeral url. 81 // prevents the easy mistake of trying to prefetch an ephemeral url.
82 DCHECK_GT(config->min_resource_hits_to_trigger_prefetch, 1U); 82 DCHECK_GT(config->min_resource_hits_to_trigger_prefetch, 1U);
83 if (config->min_resource_hits_to_trigger_prefetch < 2) 83 if (config->min_resource_hits_to_trigger_prefetch < 2)
84 config->min_resource_hits_to_trigger_prefetch = 2; 84 config->min_resource_hits_to_trigger_prefetch = 2;
85 }
85 86
86 std::string mode_value = base::GetFieldTrialParamValueByFeature( 87 std::string mode_value = base::GetFieldTrialParamValueByFeature(
87 kSpeculativeResourcePrefetchingFeature, kModeParamName); 88 kSpeculativeResourcePrefetchingFeature, kModeParamName);
88 if (mode_value == kLearningMode) { 89 if (mode_value == kLearningMode) {
89 config->mode |= ResourcePrefetchPredictorConfig::LEARNING; 90 if (config)
91 config->mode |= GlowplugPredictorConfig::LEARNING;
90 return true; 92 return true;
91 } else if (mode_value == kExternalPrefetchingMode) { 93 } else if (mode_value == kExternalPrefetchingMode) {
92 config->mode |= ResourcePrefetchPredictorConfig::LEARNING | 94 if (config) {
93 ResourcePrefetchPredictorConfig::PREFETCHING_FOR_EXTERNAL; 95 config->mode |= GlowplugPredictorConfig::LEARNING |
96 GlowplugPredictorConfig::PREFETCHING_FOR_EXTERNAL;
97 }
94 return true; 98 return true;
95 } else if (mode_value == kPrefetchingMode) { 99 } else if (mode_value == kPrefetchingMode) {
96 config->mode |= ResourcePrefetchPredictorConfig::LEARNING | 100 if (config) {
97 ResourcePrefetchPredictorConfig::PREFETCHING_FOR_EXTERNAL | 101 config->mode |= GlowplugPredictorConfig::LEARNING |
98 ResourcePrefetchPredictorConfig::PREFETCHING_FOR_NAVIGATION; 102 GlowplugPredictorConfig::PREFETCHING_FOR_EXTERNAL |
103 GlowplugPredictorConfig::PREFETCHING_FOR_NAVIGATION;
104 }
99 return true; 105 return true;
100 } 106 }
101 107
102 return false; 108 return false;
103 } 109 }
104 110
105 NavigationID::NavigationID() : tab_id(-1) {} 111 NavigationID::NavigationID() : tab_id(-1) {}
106 112
107 NavigationID::NavigationID(const NavigationID& other) 113 NavigationID::NavigationID(const NavigationID& other)
108 : tab_id(other.tab_id), 114 : tab_id(other.tab_id),
(...skipping 20 matching lines...) Expand all
129 DCHECK(is_valid() && rhs.is_valid()); 135 DCHECK(is_valid() && rhs.is_valid());
130 return std::tie(tab_id, main_frame_url) < 136 return std::tie(tab_id, main_frame_url) <
131 std::tie(rhs.tab_id, rhs.main_frame_url); 137 std::tie(rhs.tab_id, rhs.main_frame_url);
132 } 138 }
133 139
134 bool NavigationID::operator==(const NavigationID& rhs) const { 140 bool NavigationID::operator==(const NavigationID& rhs) const {
135 DCHECK(is_valid() && rhs.is_valid()); 141 DCHECK(is_valid() && rhs.is_valid());
136 return tab_id == rhs.tab_id && main_frame_url == rhs.main_frame_url; 142 return tab_id == rhs.tab_id && main_frame_url == rhs.main_frame_url;
137 } 143 }
138 144
139 ResourcePrefetchPredictorConfig::ResourcePrefetchPredictorConfig()
140 : mode(0),
141 max_navigation_lifetime_seconds(60),
142 max_urls_to_track(500),
143 max_hosts_to_track(200),
144 min_url_visit_count(2),
145 max_resources_per_entry(50),
146 max_origins_per_entry(50),
147 max_consecutive_misses(3),
148 max_redirect_consecutive_misses(5),
149 min_resource_confidence_to_trigger_prefetch(0.7f),
150 min_resource_hits_to_trigger_prefetch(2),
151 max_prefetches_inflight_per_navigation(5),
152 max_prefetches_inflight_per_host_per_navigation(3),
153 is_url_learning_enabled(false),
154 is_manifests_enabled(false),
155 is_origin_learning_enabled(false) {}
156
157 ResourcePrefetchPredictorConfig::ResourcePrefetchPredictorConfig(
158 const ResourcePrefetchPredictorConfig& other) = default;
159
160 ResourcePrefetchPredictorConfig::~ResourcePrefetchPredictorConfig() {
161 }
162
163 bool ResourcePrefetchPredictorConfig::IsLearningEnabled() const {
164 return (mode & LEARNING) > 0;
165 }
166
167 bool ResourcePrefetchPredictorConfig::IsPrefetchingEnabledForSomeOrigin(
168 Profile* profile) const {
169 int mask = PREFETCHING_FOR_NAVIGATION | PREFETCHING_FOR_EXTERNAL;
170 return IsPrefetchingEnabledInternal(profile, mode, mask);
171 }
172
173 bool ResourcePrefetchPredictorConfig::IsPrefetchingEnabledForOrigin(
174 Profile* profile,
175 PrefetchOrigin origin) const {
176 int mask = 0;
177 switch (origin) {
178 case PrefetchOrigin::NAVIGATION:
179 mask = PREFETCHING_FOR_NAVIGATION;
180 break;
181 case PrefetchOrigin::EXTERNAL:
182 mask = PREFETCHING_FOR_EXTERNAL;
183 break;
184 }
185 return IsPrefetchingEnabledInternal(profile, mode, mask);
186 }
187
188 bool ResourcePrefetchPredictorConfig::IsLowConfidenceForTest() const {
189 return min_url_visit_count == 1 &&
190 std::abs(min_resource_confidence_to_trigger_prefetch - 0.5f) < 1e-6 &&
191 min_resource_hits_to_trigger_prefetch == 1;
192 }
193
194 bool ResourcePrefetchPredictorConfig::IsHighConfidenceForTest() const {
195 return min_url_visit_count == 3 &&
196 std::abs(min_resource_confidence_to_trigger_prefetch - 0.9f) < 1e-6 &&
197 min_resource_hits_to_trigger_prefetch == 3;
198 }
199
200 bool ResourcePrefetchPredictorConfig::IsMoreResourcesEnabledForTest() const {
201 return max_resources_per_entry == 100;
202 }
203
204 bool ResourcePrefetchPredictorConfig::IsSmallDBEnabledForTest() const {
205 return max_urls_to_track == 200 && max_hosts_to_track == 100;
206 }
207
208 } // namespace predictors 145 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698