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

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

Issue 2667553002: Move API for field trial params to base from variations. (Closed)
Patch Set: 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 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"
12 #include "base/macros.h" 11 #include "base/macros.h"
13 #include "base/memory/singleton.h" 12 #include "base/memory/singleton.h"
14 #include "base/metrics/field_trial.h" 13 #include "base/metrics/field_trial.h"
15 #include "base/metrics/field_trial_param_associator.h" 14 #include "base/metrics/field_trial_param_associator.h"
16 #include "base/strings/string_number_conversions.h" 15 #include "base/metrics/field_trial_params.h"
17 #include "base/strings/string_split.h" 16 #include "base/strings/string_split.h"
18 #include "components/variations/variations_http_header_provider.h" 17 #include "components/variations/variations_http_header_provider.h"
19 18
20 namespace variations { 19 namespace variations {
21 20
22 namespace { 21 namespace {
23 22
24 // The internal singleton accessor for the map, used to keep it thread-safe. 23 // The internal singleton accessor for the map, used to keep it thread-safe.
25 class GroupMapAccessor { 24 class GroupMapAccessor {
26 public: 25 public:
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 VariationID GetGoogleVariationIDFromHashes( 146 VariationID GetGoogleVariationIDFromHashes(
148 IDCollectionKey key, 147 IDCollectionKey key,
149 const ActiveGroupId& active_group) { 148 const ActiveGroupId& active_group) {
150 return GroupMapAccessor::GetInstance()->GetID(key, active_group); 149 return GroupMapAccessor::GetInstance()->GetID(key, active_group);
151 } 150 }
152 151
153 bool AssociateVariationParams( 152 bool AssociateVariationParams(
154 const std::string& trial_name, 153 const std::string& trial_name,
155 const std::string& group_name, 154 const std::string& group_name,
156 const std::map<std::string, std::string>& params) { 155 const std::map<std::string, std::string>& params) {
157 return base::FieldTrialParamAssociator::GetInstance() 156 return base::AssociateFieldTrialParams(trial_name, group_name, params);
158 ->AssociateFieldTrialParams(trial_name, group_name, params);
159 } 157 }
160 158
161 bool GetVariationParams(const std::string& trial_name, 159 bool GetVariationParams(const std::string& trial_name,
162 std::map<std::string, std::string>* params) { 160 std::map<std::string, std::string>* params) {
163 return base::FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams( 161 return base::GetFieldTrialParams(trial_name, params);
164 trial_name, params);
165 } 162 }
166 163
167 bool GetVariationParamsByFeature(const base::Feature& feature, 164 bool GetVariationParamsByFeature(const base::Feature& feature,
168 std::map<std::string, std::string>* params) { 165 std::map<std::string, std::string>* params) {
169 if (!base::FeatureList::IsEnabled(feature)) 166 return base::GetFieldTrialParamsByFeature(feature, params);
170 return false;
171
172 base::FieldTrial* trial = base::FeatureList::GetFieldTrial(feature);
173 if (!trial)
174 return false;
175
176 return GetVariationParams(trial->trial_name(), params);
177 } 167 }
178 168
179 std::string GetVariationParamValue(const std::string& trial_name, 169 std::string GetVariationParamValue(const std::string& trial_name,
180 const std::string& param_name) { 170 const std::string& param_name) {
181 std::map<std::string, std::string> params; 171 return base::GetFieldTrialParamValue(trial_name, param_name);
182 if (GetVariationParams(trial_name, &params)) {
183 std::map<std::string, std::string>::iterator it = params.find(param_name);
184 if (it != params.end())
185 return it->second;
186 }
187 return std::string();
188 } 172 }
189 173
190 std::string GetVariationParamValueByFeature(const base::Feature& feature, 174 std::string GetVariationParamValueByFeature(const base::Feature& feature,
191 const std::string& param_name) { 175 const std::string& param_name) {
192 if (!base::FeatureList::IsEnabled(feature)) 176 return base::GetFieldTrialParamValueByFeature(feature, param_name);
193 return std::string();
194
195 base::FieldTrial* trial = base::FeatureList::GetFieldTrial(feature);
196 if (!trial)
197 return std::string();
198
199 return GetVariationParamValue(trial->trial_name(), param_name);
200 } 177 }
201 178
202 int GetVariationParamByFeatureAsInt(const base::Feature& feature, 179 int GetVariationParamByFeatureAsInt(const base::Feature& feature,
203 const std::string& param_name, 180 const std::string& param_name,
204 int default_value) { 181 int default_value) {
205 std::string value_as_string = 182 return base::GetFieldTrialParamByFeatureAsInt(feature, param_name,
206 GetVariationParamValueByFeature(feature, param_name); 183 default_value);
207 int value_as_int = 0;
208 if (!base::StringToInt(value_as_string, &value_as_int)) {
209 if (!value_as_string.empty()) {
210 DLOG(WARNING) << "Failed to parse variation param " << param_name
211 << " with string value " << value_as_string
212 << " under feature " << feature.name
213 << " into an int. Falling back to default value of "
214 << default_value;
215 }
216 value_as_int = default_value;
217 }
218 return value_as_int;
219 } 184 }
220 185
221 double GetVariationParamByFeatureAsDouble(const base::Feature& feature, 186 double GetVariationParamByFeatureAsDouble(const base::Feature& feature,
222 const std::string& param_name, 187 const std::string& param_name,
223 double default_value) { 188 double default_value) {
224 std::string value_as_string = 189 return base::GetFieldTrialParamByFeatureAsDouble(feature, param_name,
225 GetVariationParamValueByFeature(feature, param_name); 190 default_value);
226 double value_as_double = 0;
227 if (!base::StringToDouble(value_as_string, &value_as_double)) {
228 if (!value_as_string.empty()) {
229 DLOG(WARNING) << "Failed to parse variation param " << param_name
230 << " with string value " << value_as_string
231 << " under feature " << feature.name
232 << " into a double. Falling back to default value of "
233 << default_value;
234 }
235 value_as_double = default_value;
236 }
237 return value_as_double;
238 } 191 }
239 192
240 bool GetVariationParamByFeatureAsBool(const base::Feature& feature, 193 bool GetVariationParamByFeatureAsBool(const base::Feature& feature,
241 const std::string& param_name, 194 const std::string& param_name,
242 bool default_value) { 195 bool default_value) {
243 std::string value_as_string = 196 return base::GetFieldTrialParamByFeatureAsBool(feature, param_name,
244 variations::GetVariationParamValueByFeature(feature, param_name); 197 default_value);
245 if (value_as_string == "true")
246 return true;
247 if (value_as_string == "false")
248 return false;
249
250 if (!value_as_string.empty()) {
251 DLOG(WARNING) << "Failed to parse variation param " << param_name
252 << " with string value " << value_as_string
253 << " under feature " << feature.name
254 << " into a bool. Falling back to default value of "
255 << default_value;
256 }
257 return default_value;
258 } 198 }
259 199
260 // Functions below are exposed for testing explicitly behind this namespace. 200 // Functions below are exposed for testing explicitly behind this namespace.
261 // They simply wrap existing functions in this file. 201 // They simply wrap existing functions in this file.
262 namespace testing { 202 namespace testing {
263 203
264 void ClearAllVariationIDs() { 204 void ClearAllVariationIDs() {
265 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting(); 205 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting();
266 } 206 }
267 207
268 void ClearAllVariationParams() { 208 void ClearAllVariationParams() {
269 base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting(); 209 base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting();
270 } 210 }
271 211
272 } // namespace testing 212 } // namespace testing
273 213
274 } // namespace variations 214 } // 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