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

Side by Side Diff: components/previews/core/previews_experiments.cc

Issue 2640023007: Adds PreviewsType version mechanism for clearing blacklist entries. (Closed)
Patch Set: Few nits Created 3 years, 10 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 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 "components/previews/core/previews_experiments.h" 5 #include "components/previews/core/previews_experiments.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/metrics/field_trial.h" 11 #include "base/metrics/field_trial.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "components/variations/variations_associated_data.h" 14 #include "components/variations/variations_associated_data.h"
15 15
16 namespace previews { 16 namespace previews {
17 17
18 namespace { 18 namespace {
19 19
20 // The group of client-side previews experiments. 20 // The group of client-side previews experiments. Actually, this group is only
21 // expected to control one PreviewsType (OFFLINE) as well as the blacklist.
22 // Other PreviewsType's will be control by different field trial groups.
21 const char kClientSidePreviewsFieldTrial[] = "ClientSidePreviews"; 23 const char kClientSidePreviewsFieldTrial[] = "ClientSidePreviews";
22 24
23 const char kEnabled[] = "Enabled"; 25 const char kEnabled[] = "Enabled";
24 26
25 // Allow offline pages to show for prohibitively slow networks. 27 // Allow offline pages to show for prohibitively slow networks.
26 const char kOfflinePagesSlowNetwork[] = "show_offline_pages"; 28 const char kOfflinePagesSlowNetwork[] = "show_offline_pages";
27 29
30 // Name for the version parameter of a field trial. Version changes will
31 // result in older blacklist entries being removed.
32 const char kVersion[] = "version";
33
28 // The maximum number of recent previews navigations the black list looks at to 34 // The maximum number of recent previews navigations the black list looks at to
29 // determine if a host is blacklisted. 35 // determine if a host is blacklisted.
30 const char kMaxStoredHistoryLengthPerHost[] = 36 const char kMaxStoredHistoryLengthPerHost[] =
31 "per_host_max_stored_history_length"; 37 "per_host_max_stored_history_length";
32 38
33 // The maximum number of recent previews navigations the black list looks at to 39 // The maximum number of recent previews navigations the black list looks at to
34 // determine if all previews navigations should be disallowed. 40 // determine if all previews navigations should be disallowed.
35 const char kMaxStoredHistoryLengthHostIndifferent[] = 41 const char kMaxStoredHistoryLengthHostIndifferent[] =
36 "host_indifferent_max_stored_history_length"; 42 "host_indifferent_max_stored_history_length";
37 43
(...skipping 28 matching lines...) Expand all
66 72
67 // The threshold of EffectiveConnectionType above which previews will not be 73 // The threshold of EffectiveConnectionType above which previews will not be
68 // served. 74 // served.
69 // See net/nqe/effective_connection_type.h for mapping from string to value. 75 // See net/nqe/effective_connection_type.h for mapping from string to value.
70 const char kEffectiveConnectionTypeThreshold[] = 76 const char kEffectiveConnectionTypeThreshold[] =
71 "max_allowed_effective_connection_type"; 77 "max_allowed_effective_connection_type";
72 78
73 // The string that corresponds to enabled for the variation param experiments. 79 // The string that corresponds to enabled for the variation param experiments.
74 const char kExperimentEnabled[] = "true"; 80 const char kExperimentEnabled[] = "true";
75 81
76 // Returns the parameter value of |param| as a string. If there is no value for 82 // Returns the ClientSidePreviews parameter value of |param| as a string.
77 // |param|, returns an empty string. 83 // If there is no value for |param|, returns an empty string.
78 std::string ParamValue(const std::string& param) { 84 std::string ClientSidePreviewsParamValue(const std::string& param) {
79 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial()) 85 if (!IsIncludedInClientSidePreviewsExperimentsFieldTrial())
80 return std::string(); 86 return std::string();
81 std::map<std::string, std::string> experiment_params; 87 std::map<std::string, std::string> experiment_params;
82 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial, 88 if (!variations::GetVariationParams(kClientSidePreviewsFieldTrial,
83 &experiment_params)) { 89 &experiment_params)) {
84 return std::string(); 90 return std::string();
85 } 91 }
86 std::map<std::string, std::string>::const_iterator it = 92 std::map<std::string, std::string>::const_iterator it =
87 experiment_params.find(param); 93 experiment_params.find(param);
88 return it == experiment_params.end() ? std::string() : it->second; 94 return it == experiment_params.end() ? std::string() : it->second;
89 } 95 }
90 96
91 } // namespace 97 } // namespace
92 98
93 namespace params { 99 namespace params {
94 100
95 size_t MaxStoredHistoryLengthForPerHostBlackList() { 101 size_t MaxStoredHistoryLengthForPerHostBlackList() {
96 std::string param_value = ParamValue(kMaxStoredHistoryLengthPerHost); 102 std::string param_value =
103 ClientSidePreviewsParamValue(kMaxStoredHistoryLengthPerHost);
97 size_t history_length; 104 size_t history_length;
98 if (!base::StringToSizeT(param_value, &history_length)) 105 if (!base::StringToSizeT(param_value, &history_length))
99 history_length = 4; 106 history_length = 4;
100 return history_length; 107 return history_length;
101 } 108 }
102 109
103 size_t MaxStoredHistoryLengthForHostIndifferentBlackList() { 110 size_t MaxStoredHistoryLengthForHostIndifferentBlackList() {
104 std::string param_value = ParamValue(kMaxStoredHistoryLengthHostIndifferent); 111 std::string param_value =
112 ClientSidePreviewsParamValue(kMaxStoredHistoryLengthHostIndifferent);
105 size_t history_length; 113 size_t history_length;
106 if (!base::StringToSizeT(param_value, &history_length)) 114 if (!base::StringToSizeT(param_value, &history_length))
107 history_length = 10; 115 history_length = 10;
108 return history_length; 116 return history_length;
109 } 117 }
110 118
111 size_t MaxInMemoryHostsInBlackList() { 119 size_t MaxInMemoryHostsInBlackList() {
112 std::string param_value = ParamValue(kMaxHostsInBlackList); 120 std::string param_value = ClientSidePreviewsParamValue(kMaxHostsInBlackList);
113 size_t max_hosts; 121 size_t max_hosts;
114 if (!base::StringToSizeT(param_value, &max_hosts)) 122 if (!base::StringToSizeT(param_value, &max_hosts))
115 max_hosts = 100; 123 max_hosts = 100;
116 return max_hosts; 124 return max_hosts;
117 } 125 }
118 126
119 int PerHostBlackListOptOutThreshold() { 127 int PerHostBlackListOptOutThreshold() {
120 std::string param_value = ParamValue(kPerHostOptOutThreshold); 128 std::string param_value =
129 ClientSidePreviewsParamValue(kPerHostOptOutThreshold);
121 int opt_out_threshold; 130 int opt_out_threshold;
122 if (!base::StringToInt(param_value, &opt_out_threshold)) 131 if (!base::StringToInt(param_value, &opt_out_threshold))
123 opt_out_threshold = 2; 132 opt_out_threshold = 2;
124 return opt_out_threshold; 133 return opt_out_threshold;
125 } 134 }
126 135
127 int HostIndifferentBlackListOptOutThreshold() { 136 int HostIndifferentBlackListOptOutThreshold() {
128 std::string param_value = ParamValue(kHostIndifferentOptOutThreshold); 137 std::string param_value =
138 ClientSidePreviewsParamValue(kHostIndifferentOptOutThreshold);
129 int opt_out_threshold; 139 int opt_out_threshold;
130 if (!base::StringToInt(param_value, &opt_out_threshold)) 140 if (!base::StringToInt(param_value, &opt_out_threshold))
131 opt_out_threshold = 4; 141 opt_out_threshold = 4;
132 return opt_out_threshold; 142 return opt_out_threshold;
133 } 143 }
134 144
135 base::TimeDelta PerHostBlackListDuration() { 145 base::TimeDelta PerHostBlackListDuration() {
136 std::string param_value = ParamValue(kPerHostBlackListDurationInDays); 146 std::string param_value =
147 ClientSidePreviewsParamValue(kPerHostBlackListDurationInDays);
137 int duration; 148 int duration;
138 if (!base::StringToInt(param_value, &duration)) 149 if (!base::StringToInt(param_value, &duration))
139 duration = 30; 150 duration = 30;
140 return base::TimeDelta::FromDays(duration); 151 return base::TimeDelta::FromDays(duration);
141 } 152 }
142 153
143 base::TimeDelta HostIndifferentBlackListPerHostDuration() { 154 base::TimeDelta HostIndifferentBlackListPerHostDuration() {
144 std::string param_value = ParamValue(kHostIndifferentBlackListDurationInDays); 155 std::string param_value =
156 ClientSidePreviewsParamValue(kHostIndifferentBlackListDurationInDays);
145 int duration; 157 int duration;
146 if (!base::StringToInt(param_value, &duration)) 158 if (!base::StringToInt(param_value, &duration))
147 duration = 365 * 100; 159 duration = 365 * 100;
148 return base::TimeDelta::FromDays(duration); 160 return base::TimeDelta::FromDays(duration);
149 } 161 }
150 162
151 base::TimeDelta SingleOptOutDuration() { 163 base::TimeDelta SingleOptOutDuration() {
152 std::string param_value = ParamValue(kSingleOptOutDurationInSeconds); 164 std::string param_value =
165 ClientSidePreviewsParamValue(kSingleOptOutDurationInSeconds);
153 int duration; 166 int duration;
154 if (!base::StringToInt(param_value, &duration)) 167 if (!base::StringToInt(param_value, &duration))
155 duration = 60 * 5; 168 duration = 60 * 5;
156 return base::TimeDelta::FromSeconds(duration); 169 return base::TimeDelta::FromSeconds(duration);
157 } 170 }
158 171
159 base::TimeDelta OfflinePreviewFreshnessDuration() { 172 base::TimeDelta OfflinePreviewFreshnessDuration() {
160 std::string param_value = ParamValue(kOfflinePreviewFreshnessDurationInDays); 173 std::string param_value =
174 ClientSidePreviewsParamValue(kOfflinePreviewFreshnessDurationInDays);
161 int duration; 175 int duration;
162 if (!base::StringToInt(param_value, &duration)) 176 if (!base::StringToInt(param_value, &duration))
163 duration = 7; 177 duration = 7;
164 return base::TimeDelta::FromDays(duration); 178 return base::TimeDelta::FromDays(duration);
165 } 179 }
166 180
167 net::EffectiveConnectionType EffectiveConnectionTypeThreshold() { 181 net::EffectiveConnectionType EffectiveConnectionTypeThreshold() {
168 std::string param_value = ParamValue(kEffectiveConnectionTypeThreshold); 182 std::string param_value =
183 ClientSidePreviewsParamValue(kEffectiveConnectionTypeThreshold);
169 net::EffectiveConnectionType effective_connection_type; 184 net::EffectiveConnectionType effective_connection_type;
170 if (!net::GetEffectiveConnectionTypeForName(param_value, 185 if (!net::GetEffectiveConnectionTypeForName(param_value,
171 &effective_connection_type)) { 186 &effective_connection_type)) {
172 effective_connection_type = net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G; 187 effective_connection_type = net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
173 } 188 }
174 return effective_connection_type; 189 return effective_connection_type;
175 } 190 }
176 191
177 } // namespace params 192 } // namespace params
178 193
179 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() { 194 bool IsIncludedInClientSidePreviewsExperimentsFieldTrial() {
180 // By convention, an experiment in the client-side previews study enables use 195 // By convention, an experiment in the client-side previews study enables use
181 // of at least one client-side previews optimization if its name begins with 196 // of at least one client-side previews optimization if its name begins with
182 // "Enabled." 197 // "Enabled."
183 return base::StartsWith( 198 return base::StartsWith(
184 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial), 199 base::FieldTrialList::FindFullName(kClientSidePreviewsFieldTrial),
185 kEnabled, base::CompareCase::SENSITIVE); 200 kEnabled, base::CompareCase::SENSITIVE);
186 } 201 }
187 202
188 bool IsPreviewsTypeEnabled(PreviewsType type) { 203 bool IsPreviewsTypeEnabled(PreviewsType type) {
189 switch (type) { 204 switch (type) {
190 case PreviewsType::OFFLINE: 205 case PreviewsType::OFFLINE:
191 return ParamValue(kOfflinePagesSlowNetwork) == kExperimentEnabled; 206 return ClientSidePreviewsParamValue(kOfflinePagesSlowNetwork) ==
207 kExperimentEnabled;
192 default: 208 default:
193 NOTREACHED(); 209 NOTREACHED();
194 return false; 210 return false;
195 } 211 }
196 } 212 }
197 213
214 int GetPreviewsTypeVersion(PreviewsType type) {
215 int version = 0; // default
216 switch (type) {
217 case PreviewsType::OFFLINE:
218 base::StringToInt(ClientSidePreviewsParamValue(kVersion), &version);
219 return version;
220 // List remaining enum cases vs. default to catch when new one is added.
221 case PreviewsType::NONE:
222 break;
223 case PreviewsType::LAST:
224 break;
225 }
226 NOTREACHED();
227 return -1;
228 }
229
230 std::unique_ptr<PreviewsTypeList> GetEnabledPreviews() {
231 std::unique_ptr<PreviewsTypeList> enabled_previews(new PreviewsTypeList());
232
233 // Loop across all previews types (relies on sequential enum values).
RyanSturm 2017/02/15 00:30:29 Can you add a similar comment about sequential enu
dougarnett 2017/02/15 17:20:06 Done.
234 for (int i = static_cast<int>(PreviewsType::NONE) + 1;
235 i < static_cast<int>(PreviewsType::LAST); ++i) {
236 PreviewsType type = static_cast<PreviewsType>(i);
237 if (IsPreviewsTypeEnabled(type)) {
238 enabled_previews->push_back({type, GetPreviewsTypeVersion(type)});
239 }
240 }
241 return enabled_previews;
242 }
243
198 bool EnableOfflinePreviewsForTesting() { 244 bool EnableOfflinePreviewsForTesting() {
199 std::map<std::string, std::string> params; 245 std::map<std::string, std::string> params;
200 params[kOfflinePagesSlowNetwork] = kExperimentEnabled; 246 params[kOfflinePagesSlowNetwork] = kExperimentEnabled;
201 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial, 247 return variations::AssociateVariationParams(kClientSidePreviewsFieldTrial,
202 kEnabled, params) && 248 kEnabled, params) &&
203 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial, 249 base::FieldTrialList::CreateFieldTrial(kClientSidePreviewsFieldTrial,
204 kEnabled); 250 kEnabled);
205 } 251 }
206 252
207 } // namespace previews 253 } // namespace previews
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698