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

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

Issue 2558743003: Add generic functions for getting typed variation parameter values (Closed)
Patch Set: Alexei's comments 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 DLOG(WARNING) << "Failed to parse variation param " << param_name
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 DLOG(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")
238 return true;
239 if (value_as_string == "false")
240 return false;
241
242 if (!value_as_string.empty()) {
243 DLOG(WARNING) << "Failed to parse variation param " << param_name
244 << " with string value " << value_as_string
245 << " under feature " << feature.name
246 << " into a bool. Falling back to default value of "
247 << default_value;
248 }
249 return default_value;
250 }
251
193 // Functions below are exposed for testing explicitly behind this namespace. 252 // Functions below are exposed for testing explicitly behind this namespace.
194 // They simply wrap existing functions in this file. 253 // They simply wrap existing functions in this file.
195 namespace testing { 254 namespace testing {
196 255
197 void ClearAllVariationIDs() { 256 void ClearAllVariationIDs() {
198 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting(); 257 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting();
199 } 258 }
200 259
201 void ClearAllVariationParams() { 260 void ClearAllVariationParams() {
202 base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting(); 261 base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
203 } 262 }
204 263
205 } // namespace testing 264 } // namespace testing
206 265
207 } // namespace variations 266 } // 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