| 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/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_param_associator.h" |
| 14 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 15 #include "components/variations/variations_http_header_provider.h" | 16 #include "components/variations/variations_http_header_provider.h" |
| 16 | 17 |
| 17 namespace variations { | 18 namespace variations { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 const char kGroupTesting[] = "Testing"; | 22 const char kGroupTesting[] = "Testing"; |
| 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. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 GroupMapAccessor() { | 98 GroupMapAccessor() { |
| 98 group_to_id_maps_.resize(ID_COLLECTION_COUNT); | 99 group_to_id_maps_.resize(ID_COLLECTION_COUNT); |
| 99 } | 100 } |
| 100 ~GroupMapAccessor() {} | 101 ~GroupMapAccessor() {} |
| 101 | 102 |
| 102 base::Lock lock_; | 103 base::Lock lock_; |
| 103 std::vector<GroupToIDMap> group_to_id_maps_; | 104 std::vector<GroupToIDMap> group_to_id_maps_; |
| 104 | 105 |
| 105 DISALLOW_COPY_AND_ASSIGN(GroupMapAccessor); | 106 DISALLOW_COPY_AND_ASSIGN(GroupMapAccessor); |
| 106 }; | 107 }; |
| 107 | |
| 108 // Singleton helper class that keeps track of the parameters of all variations | |
| 109 // and ensures access to these is thread-safe. | |
| 110 class VariationsParamAssociator { | |
| 111 public: | |
| 112 typedef std::pair<std::string, std::string> VariationKey; | |
| 113 typedef std::map<std::string, std::string> VariationParams; | |
| 114 | |
| 115 // Retrieve the singleton. | |
| 116 static VariationsParamAssociator* GetInstance() { | |
| 117 return base::Singleton< | |
| 118 VariationsParamAssociator, | |
| 119 base::LeakySingletonTraits<VariationsParamAssociator>>::get(); | |
| 120 } | |
| 121 | |
| 122 bool AssociateVariationParams(const std::string& trial_name, | |
| 123 const std::string& group_name, | |
| 124 const VariationParams& params) { | |
| 125 base::AutoLock scoped_lock(lock_); | |
| 126 | |
| 127 if (base::FieldTrialList::IsTrialActive(trial_name)) | |
| 128 return false; | |
| 129 | |
| 130 const VariationKey key(trial_name, group_name); | |
| 131 if (base::ContainsKey(variation_params_, key)) | |
| 132 return false; | |
| 133 | |
| 134 variation_params_[key] = params; | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 bool GetVariationParams(const std::string& trial_name, | |
| 139 VariationParams* params) { | |
| 140 base::AutoLock scoped_lock(lock_); | |
| 141 | |
| 142 const std::string group_name = | |
| 143 base::FieldTrialList::FindFullName(trial_name); | |
| 144 const VariationKey key(trial_name, group_name); | |
| 145 if (!base::ContainsKey(variation_params_, key)) | |
| 146 return false; | |
| 147 | |
| 148 *params = variation_params_[key]; | |
| 149 return true; | |
| 150 } | |
| 151 | |
| 152 void ClearAllParamsForTesting() { | |
| 153 base::AutoLock scoped_lock(lock_); | |
| 154 variation_params_.clear(); | |
| 155 } | |
| 156 | |
| 157 private: | |
| 158 friend struct base::DefaultSingletonTraits<VariationsParamAssociator>; | |
| 159 | |
| 160 VariationsParamAssociator() {} | |
| 161 ~VariationsParamAssociator() {} | |
| 162 | |
| 163 base::Lock lock_; | |
| 164 std::map<VariationKey, VariationParams> variation_params_; | |
| 165 | |
| 166 DISALLOW_COPY_AND_ASSIGN(VariationsParamAssociator); | |
| 167 }; | |
| 168 | |
| 169 } // namespace | 108 } // namespace |
| 170 | 109 |
| 171 void AssociateGoogleVariationID(IDCollectionKey key, | 110 void AssociateGoogleVariationID(IDCollectionKey key, |
| 172 const std::string& trial_name, | 111 const std::string& trial_name, |
| 173 const std::string& group_name, | 112 const std::string& group_name, |
| 174 VariationID id) { | 113 VariationID id) { |
| 175 GroupMapAccessor::GetInstance()->AssociateID( | 114 GroupMapAccessor::GetInstance()->AssociateID( |
| 176 key, MakeActiveGroupId(trial_name, group_name), id, false); | 115 key, MakeActiveGroupId(trial_name, group_name), id, false); |
| 177 } | 116 } |
| 178 | 117 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 200 VariationID GetGoogleVariationIDFromHashes( | 139 VariationID GetGoogleVariationIDFromHashes( |
| 201 IDCollectionKey key, | 140 IDCollectionKey key, |
| 202 const ActiveGroupId& active_group) { | 141 const ActiveGroupId& active_group) { |
| 203 return GroupMapAccessor::GetInstance()->GetID(key, active_group); | 142 return GroupMapAccessor::GetInstance()->GetID(key, active_group); |
| 204 } | 143 } |
| 205 | 144 |
| 206 bool AssociateVariationParams( | 145 bool AssociateVariationParams( |
| 207 const std::string& trial_name, | 146 const std::string& trial_name, |
| 208 const std::string& group_name, | 147 const std::string& group_name, |
| 209 const std::map<std::string, std::string>& params) { | 148 const std::map<std::string, std::string>& params) { |
| 210 return VariationsParamAssociator::GetInstance()->AssociateVariationParams( | 149 return base::FieldTrialParamAssociator::GetInstance() |
| 211 trial_name, group_name, params); | 150 ->AssociateFieldTrialParams(trial_name, group_name, params); |
| 212 } | 151 } |
| 213 | 152 |
| 214 bool GetVariationParams(const std::string& trial_name, | 153 bool GetVariationParams(const std::string& trial_name, |
| 215 std::map<std::string, std::string>* params) { | 154 std::map<std::string, std::string>* params) { |
| 216 return VariationsParamAssociator::GetInstance()->GetVariationParams( | 155 return base::FieldTrialParamAssociator::GetInstance()->GetFieldTrialParams( |
| 217 trial_name, params); | 156 trial_name, params); |
| 218 } | 157 } |
| 219 | 158 |
| 220 bool GetVariationParamsByFeature(const base::Feature& feature, | 159 bool GetVariationParamsByFeature(const base::Feature& feature, |
| 221 std::map<std::string, std::string>* params) { | 160 std::map<std::string, std::string>* params) { |
| 222 if (!base::FeatureList::IsEnabled(feature)) | 161 if (!base::FeatureList::IsEnabled(feature)) |
| 223 return false; | 162 return false; |
| 224 | 163 |
| 225 base::FieldTrial* trial = base::FeatureList::GetFieldTrial(feature); | 164 base::FieldTrial* trial = base::FeatureList::GetFieldTrial(feature); |
| 226 if (!trial) | 165 if (!trial) |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 field_trial_list_.reset(new base::FieldTrialList(nullptr)); | 213 field_trial_list_.reset(new base::FieldTrialList(nullptr)); |
| 275 variations::AssociateVariationParams(trial_name, kGroupTesting, params); | 214 variations::AssociateVariationParams(trial_name, kGroupTesting, params); |
| 276 base::FieldTrialList::CreateFieldTrial(trial_name, kGroupTesting); | 215 base::FieldTrialList::CreateFieldTrial(trial_name, kGroupTesting); |
| 277 } | 216 } |
| 278 | 217 |
| 279 void ClearAllVariationIDs() { | 218 void ClearAllVariationIDs() { |
| 280 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting(); | 219 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting(); |
| 281 } | 220 } |
| 282 | 221 |
| 283 void ClearAllVariationParams() { | 222 void ClearAllVariationParams() { |
| 284 VariationsParamAssociator::GetInstance()->ClearAllParamsForTesting(); | 223 base::FieldTrialParamAssociator::GetInstance()->ClearAllParamsForTesting(); |
| 285 } | 224 } |
| 286 | 225 |
| 287 } // namespace testing | 226 } // namespace testing |
| 288 | 227 |
| 289 } // namespace variations | 228 } // namespace variations |
| OLD | NEW |