OLD | NEW |
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/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
| 12 #include "base/strings/string_split.h" |
12 | 13 |
13 namespace variations { | 14 namespace variations { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // The internal singleton accessor for the map, used to keep it thread-safe. | 18 // The internal singleton accessor for the map, used to keep it thread-safe. |
18 class GroupMapAccessor { | 19 class GroupMapAccessor { |
19 public: | 20 public: |
20 typedef std::map<ActiveGroupId, VariationID, ActiveGroupIdCompare> | 21 typedef std::map<ActiveGroupId, VariationID, ActiveGroupIdCompare> |
21 GroupToIDMap; | 22 GroupToIDMap; |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 } | 209 } |
209 | 210 |
210 bool AssociateVariationParams( | 211 bool AssociateVariationParams( |
211 const std::string& trial_name, | 212 const std::string& trial_name, |
212 const std::string& group_name, | 213 const std::string& group_name, |
213 const std::map<std::string, std::string>& params) { | 214 const std::map<std::string, std::string>& params) { |
214 return VariationsParamAssociator::GetInstance()->AssociateVariationParams( | 215 return VariationsParamAssociator::GetInstance()->AssociateVariationParams( |
215 trial_name, group_name, params); | 216 trial_name, group_name, params); |
216 } | 217 } |
217 | 218 |
| 219 struct VarationsParams { |
| 220 std::string GroupName; |
| 221 std::map<std::string, std::string> Params; |
| 222 }; |
| 223 |
| 224 bool AssociateParamsFromString(const std::string& varations_string) { |
| 225 std::map <std::string, VarationsParams> data; |
| 226 std::vector<std::string> tokens; |
| 227 base::SplitString(varations_string, '/', &tokens); |
| 228 if (tokens.size() % 2 == 0) { |
| 229 // Build out parameters |
| 230 for (int i = 0; i < tokens.size(); i += 2) { |
| 231 std::vector<std::string> key; |
| 232 base::SplitString(tokens[i], '.', &key); |
| 233 DCHECK(key.size() == 3) << "Expecting format Trial.Group.Key"; |
| 234 if (key.size() == 3) { |
| 235 const std::string& trial = key[0]; |
| 236 const std::string& group = key[1]; |
| 237 const std::string& param = key[2]; |
| 238 const std::string& value = tokens[i + 1]; |
| 239 const auto& iter = data.find(trial); |
| 240 if (iter != data.end()) { |
| 241 if (iter->second.GroupName == group) { |
| 242 iter->second.Params[param] = value; |
| 243 } |
| 244 } |
| 245 else { |
| 246 VarationsParams vp; |
| 247 vp.GroupName = group; |
| 248 vp.Params[param] = value; |
| 249 data[trial] = vp; |
| 250 } |
| 251 } |
| 252 } |
| 253 // Associate parameters |
| 254 for (const auto& iter : data) { |
| 255 const std::string& trial = iter.first; |
| 256 const VarationsParams& params = iter.second; |
| 257 AssociateVariationParams(trial, params.GroupName, params.Params); |
| 258 } |
| 259 } else { |
| 260 DCHECK(false) << "Expecting pairs of values"; |
| 261 return false; |
| 262 } |
| 263 return true; |
| 264 } |
| 265 |
218 bool GetVariationParams(const std::string& trial_name, | 266 bool GetVariationParams(const std::string& trial_name, |
219 std::map<std::string, std::string>* params) { | 267 std::map<std::string, std::string>* params) { |
220 return VariationsParamAssociator::GetInstance()->GetVariationParams( | 268 return VariationsParamAssociator::GetInstance()->GetVariationParams( |
221 trial_name, params); | 269 trial_name, params); |
222 } | 270 } |
223 | 271 |
224 std::string GetVariationParamValue(const std::string& trial_name, | 272 std::string GetVariationParamValue(const std::string& trial_name, |
225 const std::string& param_name) { | 273 const std::string& param_name) { |
226 std::map<std::string, std::string> params; | 274 std::map<std::string, std::string> params; |
227 if (GetVariationParams(trial_name, ¶ms)) { | 275 if (GetVariationParams(trial_name, ¶ms)) { |
(...skipping 12 matching lines...) Expand all Loading... |
240 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting(); | 288 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting(); |
241 } | 289 } |
242 | 290 |
243 void ClearAllVariationParams() { | 291 void ClearAllVariationParams() { |
244 VariationsParamAssociator::GetInstance()->ClearAllParamsForTesting(); | 292 VariationsParamAssociator::GetInstance()->ClearAllParamsForTesting(); |
245 } | 293 } |
246 | 294 |
247 } // namespace testing | 295 } // namespace testing |
248 | 296 |
249 } // namespace variations | 297 } // namespace variations |
OLD | NEW |