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

Side by Side Diff: components/variations/variations_associated_data.cc

Issue 2558743003: Add generic functions for getting typed variation parameter values (Closed)
Patch Set: Created 4 years 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/variations/variations_associated_data.h" 5 #include "components/variations/variations_associated_data.h"
6 6
7 #include <map> 7 #include <map>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/feature_list.h" 11 #include "base/feature_list.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/singleton.h" 13 #include "base/memory/singleton.h"
14 #include "base/metrics/field_trial.h" 14 #include "base/metrics/field_trial.h"
15 #include "base/metrics/field_trial_param_associator.h" 15 #include "base/metrics/field_trial_param_associator.h"
16 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_split.h" 17 #include "base/strings/string_split.h"
17 #include "components/variations/variations_http_header_provider.h" 18 #include "components/variations/variations_http_header_provider.h"
18 19
19 namespace variations { 20 namespace variations {
20 21
21 namespace { 22 namespace {
22 23
23 // The internal singleton accessor for the map, used to keep it thread-safe. 24 // The internal singleton accessor for the map, used to keep it thread-safe.
24 class GroupMapAccessor { 25 class GroupMapAccessor {
25 public: 26 public:
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 if (!base::FeatureList::IsEnabled(feature)) 184 if (!base::FeatureList::IsEnabled(feature))
184 return std::string(); 185 return std::string();
185 186
186 base::FieldTrial* trial = base::FeatureList::GetFieldTrial(feature); 187 base::FieldTrial* trial = base::FeatureList::GetFieldTrial(feature);
187 if (!trial) 188 if (!trial)
188 return std::string(); 189 return std::string();
189 190
190 return GetVariationParamValue(trial->trial_name(), param_name); 191 return GetVariationParamValue(trial->trial_name(), param_name);
191 } 192 }
192 193
194 int GetVariationParamByFeatureAsInt(const base::Feature& feature,
195 const std::string& param_name,
196 int default_value) {
197 std::string value_as_string =
198 GetVariationParamValueByFeature(feature, param_name);
199 int value_as_int = 0;
200 if (!base::StringToInt(value_as_string, &value_as_int)) {
201 if (!value_as_string.empty()) {
202 LOG(WARNING) << "Failed to parse variation param " << param_name
Alexei Svitkine (slow) 2016/12/08 21:08:33 Please turn these into DLOG()'s. LOG() lines are
jkrcal 2016/12/09 06:59:31 Done.
203 << " with string value " << value_as_string
204 << " under feature " << feature.name
205 << " into an int. Falling back to default value of "
206 << default_value;
207 }
208 value_as_int = default_value;
209 }
210 return value_as_int;
211 }
212
213 double GetVariationParamByFeatureAsDouble(const base::Feature& feature,
214 const std::string& param_name,
215 double default_value) {
216 std::string value_as_string =
217 GetVariationParamValueByFeature(feature, param_name);
218 double value_as_double = 0;
219 if (!base::StringToDouble(value_as_string, &value_as_double)) {
220 if (!value_as_string.empty()) {
221 LOG(WARNING) << "Failed to parse variation param " << param_name
222 << " with string value " << value_as_string
223 << " under feature " << feature.name
224 << " into a double. Falling back to default value of "
225 << default_value;
226 }
227 value_as_double = default_value;
228 }
229 return value_as_double;
230 }
231
232 bool GetVariationParamByFeatureAsBool(const base::Feature& feature,
233 const std::string& param_name,
234 bool default_value) {
235 std::string value_as_string =
236 variations::GetVariationParamValueByFeature(feature, param_name);
237 if (value_as_string == "true") {
Alexei Svitkine (slow) 2016/12/08 21:08:32 Nit: No {}'s here and below.
jkrcal 2016/12/09 06:59:31 Done.
238 return true;
239 }
240 if (value_as_string == "false") {
241 return false;
242 }
243
244 if (!value_as_string.empty()) {
245 LOG(WARNING) << "Failed to parse variation param " << param_name
246 << " with string value " << value_as_string
247 << " under feature " << feature.name
248 << " into a bool. Falling back to default value of "
249 << default_value;
250 }
251 return default_value;
252 }
253
193 // Functions below are exposed for testing explicitly behind this namespace. 254 // Functions below are exposed for testing explicitly behind this namespace.
194 // They simply wrap existing functions in this file. 255 // They simply wrap existing functions in this file.
195 namespace testing { 256 namespace testing {
196 257
197 void ClearAllVariationIDs() { 258 void ClearAllVariationIDs() {
198 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting(); 259 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting();
199 } 260 }
200 261
201 void ClearAllVariationParams() { 262 void ClearAllVariationParams() {
202 base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting(); 263 base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
203 } 264 }
204 265
205 } // namespace testing 266 } // namespace testing
206 267
207 } // namespace variations 268 } // namespace variations
OLDNEW
« no previous file with comments | « components/variations/variations_associated_data.h ('k') | components/variations/variations_associated_data_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698