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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/variations/variations_associated_data.cc
diff --git a/components/variations/variations_associated_data.cc b/components/variations/variations_associated_data.cc
index 465aed58c5407ddc22984d606120e338f0ca49e0..67011057e72ba4889bbf5d51fe97d2dd45ef72e5 100644
--- a/components/variations/variations_associated_data.cc
+++ b/components/variations/variations_associated_data.cc
@@ -13,6 +13,7 @@
#include "base/memory/singleton.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_param_associator.h"
+#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "components/variations/variations_http_header_provider.h"
@@ -190,6 +191,64 @@ std::string GetVariationParamValueByFeature(const base::Feature& feature,
return GetVariationParamValue(trial->trial_name(), param_name);
}
+int GetVariationParamByFeatureAsInt(const base::Feature& feature,
+ const std::string& param_name,
+ int default_value) {
+ std::string value_as_string =
+ GetVariationParamValueByFeature(feature, param_name);
+ int value_as_int = 0;
+ if (!base::StringToInt(value_as_string, &value_as_int)) {
+ if (!value_as_string.empty()) {
+ DLOG(WARNING) << "Failed to parse variation param " << param_name
+ << " with string value " << value_as_string
+ << " under feature " << feature.name
+ << " into an int. Falling back to default value of "
+ << default_value;
+ }
+ value_as_int = default_value;
+ }
+ return value_as_int;
+}
+
+double GetVariationParamByFeatureAsDouble(const base::Feature& feature,
+ const std::string& param_name,
+ double default_value) {
+ std::string value_as_string =
+ GetVariationParamValueByFeature(feature, param_name);
+ double value_as_double = 0;
+ if (!base::StringToDouble(value_as_string, &value_as_double)) {
+ if (!value_as_string.empty()) {
+ DLOG(WARNING) << "Failed to parse variation param " << param_name
+ << " with string value " << value_as_string
+ << " under feature " << feature.name
+ << " into a double. Falling back to default value of "
+ << default_value;
+ }
+ value_as_double = default_value;
+ }
+ return value_as_double;
+}
+
+bool GetVariationParamByFeatureAsBool(const base::Feature& feature,
+ const std::string& param_name,
+ bool default_value) {
+ std::string value_as_string =
+ variations::GetVariationParamValueByFeature(feature, param_name);
+ if (value_as_string == "true")
+ return true;
+ if (value_as_string == "false")
+ return false;
+
+ if (!value_as_string.empty()) {
+ DLOG(WARNING) << "Failed to parse variation param " << param_name
+ << " with string value " << value_as_string
+ << " under feature " << feature.name
+ << " into a bool. Falling back to default value of "
+ << default_value;
+ }
+ return default_value;
+}
+
// Functions below are exposed for testing explicitly behind this namespace.
// They simply wrap existing functions in this file.
namespace testing {
« 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