| 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 | 12 |
| 13 namespace variations { | 13 namespace variations { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // The internal singleton accessor for the map, used to keep it thread-safe. | 17 // The internal singleton accessor for the map, used to keep it thread-safe. |
| 18 class GroupMapAccessor { | 18 class GroupMapAccessor { |
| 19 public: | 19 public: |
| 20 typedef std::map<ActiveGroupId, VariationID, ActiveGroupIdCompare> | 20 typedef std::map<ActiveGroupId, VariationID, ActiveGroupIdCompare> |
| 21 GroupToIDMap; | 21 GroupToIDMap; |
| 22 | 22 |
| 23 // Retrieve the singleton. | 23 // Retrieve the singleton. |
| 24 static GroupMapAccessor* GetInstance() { | 24 static GroupMapAccessor* GetInstance() { |
| 25 return Singleton<GroupMapAccessor>::get(); | 25 return base::Singleton<GroupMapAccessor>::get(); |
| 26 } | 26 } |
| 27 | 27 |
| 28 // Note that this normally only sets the ID for a group the first time, unless | 28 // Note that this normally only sets the ID for a group the first time, unless |
| 29 // |force| is set to true, in which case it will always override it. | 29 // |force| is set to true, in which case it will always override it. |
| 30 void AssociateID(IDCollectionKey key, | 30 void AssociateID(IDCollectionKey key, |
| 31 const ActiveGroupId& group_identifier, | 31 const ActiveGroupId& group_identifier, |
| 32 const VariationID id, | 32 const VariationID id, |
| 33 const bool force) { | 33 const bool force) { |
| 34 #if !defined(NDEBUG) | 34 #if !defined(NDEBUG) |
| 35 DCHECK_EQ(4, ID_COLLECTION_COUNT); | 35 DCHECK_EQ(4, ID_COLLECTION_COUNT); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 base::AutoLock scoped_lock(lock_); | 74 base::AutoLock scoped_lock(lock_); |
| 75 | 75 |
| 76 for (int i = 0; i < ID_COLLECTION_COUNT; ++i) { | 76 for (int i = 0; i < ID_COLLECTION_COUNT; ++i) { |
| 77 GroupToIDMap* map = GetGroupToIDMap(static_cast<IDCollectionKey>(i)); | 77 GroupToIDMap* map = GetGroupToIDMap(static_cast<IDCollectionKey>(i)); |
| 78 DCHECK(map); | 78 DCHECK(map); |
| 79 map->clear(); | 79 map->clear(); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 private: | 83 private: |
| 84 friend struct DefaultSingletonTraits<GroupMapAccessor>; | 84 friend struct base::DefaultSingletonTraits<GroupMapAccessor>; |
| 85 | 85 |
| 86 // Retrieves the GroupToIDMap for |key|. | 86 // Retrieves the GroupToIDMap for |key|. |
| 87 GroupToIDMap* GetGroupToIDMap(IDCollectionKey key) { | 87 GroupToIDMap* GetGroupToIDMap(IDCollectionKey key) { |
| 88 return &group_to_id_maps_[key]; | 88 return &group_to_id_maps_[key]; |
| 89 } | 89 } |
| 90 | 90 |
| 91 GroupMapAccessor() { | 91 GroupMapAccessor() { |
| 92 group_to_id_maps_.resize(ID_COLLECTION_COUNT); | 92 group_to_id_maps_.resize(ID_COLLECTION_COUNT); |
| 93 } | 93 } |
| 94 ~GroupMapAccessor() {} | 94 ~GroupMapAccessor() {} |
| 95 | 95 |
| 96 base::Lock lock_; | 96 base::Lock lock_; |
| 97 std::vector<GroupToIDMap> group_to_id_maps_; | 97 std::vector<GroupToIDMap> group_to_id_maps_; |
| 98 | 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(GroupMapAccessor); | 99 DISALLOW_COPY_AND_ASSIGN(GroupMapAccessor); |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 // Singleton helper class that keeps track of the parameters of all variations | 102 // Singleton helper class that keeps track of the parameters of all variations |
| 103 // and ensures access to these is thread-safe. | 103 // and ensures access to these is thread-safe. |
| 104 class VariationsParamAssociator { | 104 class VariationsParamAssociator { |
| 105 public: | 105 public: |
| 106 typedef std::pair<std::string, std::string> VariationKey; | 106 typedef std::pair<std::string, std::string> VariationKey; |
| 107 typedef std::map<std::string, std::string> VariationParams; | 107 typedef std::map<std::string, std::string> VariationParams; |
| 108 | 108 |
| 109 // Retrieve the singleton. | 109 // Retrieve the singleton. |
| 110 static VariationsParamAssociator* GetInstance() { | 110 static VariationsParamAssociator* GetInstance() { |
| 111 return Singleton<VariationsParamAssociator>::get(); | 111 return base::Singleton<VariationsParamAssociator>::get(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 bool AssociateVariationParams(const std::string& trial_name, | 114 bool AssociateVariationParams(const std::string& trial_name, |
| 115 const std::string& group_name, | 115 const std::string& group_name, |
| 116 const VariationParams& params) { | 116 const VariationParams& params) { |
| 117 base::AutoLock scoped_lock(lock_); | 117 base::AutoLock scoped_lock(lock_); |
| 118 | 118 |
| 119 if (IsFieldTrialActive(trial_name)) | 119 if (IsFieldTrialActive(trial_name)) |
| 120 return false; | 120 return false; |
| 121 | 121 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 140 *params = variation_params_[key]; | 140 *params = variation_params_[key]; |
| 141 return true; | 141 return true; |
| 142 } | 142 } |
| 143 | 143 |
| 144 void ClearAllParamsForTesting() { | 144 void ClearAllParamsForTesting() { |
| 145 base::AutoLock scoped_lock(lock_); | 145 base::AutoLock scoped_lock(lock_); |
| 146 variation_params_.clear(); | 146 variation_params_.clear(); |
| 147 } | 147 } |
| 148 | 148 |
| 149 private: | 149 private: |
| 150 friend struct DefaultSingletonTraits<VariationsParamAssociator>; | 150 friend struct base::DefaultSingletonTraits<VariationsParamAssociator>; |
| 151 | 151 |
| 152 VariationsParamAssociator() {} | 152 VariationsParamAssociator() {} |
| 153 ~VariationsParamAssociator() {} | 153 ~VariationsParamAssociator() {} |
| 154 | 154 |
| 155 // Tests whether a field trial is active (i.e. group() has been called on it). | 155 // Tests whether a field trial is active (i.e. group() has been called on it). |
| 156 // TODO(asvitkine): Expose this as an API on base::FieldTrial. | 156 // TODO(asvitkine): Expose this as an API on base::FieldTrial. |
| 157 bool IsFieldTrialActive(const std::string& trial_name) { | 157 bool IsFieldTrialActive(const std::string& trial_name) { |
| 158 base::FieldTrial::ActiveGroups active_groups; | 158 base::FieldTrial::ActiveGroups active_groups; |
| 159 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); | 159 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
| 160 for (size_t i = 0; i < active_groups.size(); ++i) { | 160 for (size_t i = 0; i < active_groups.size(); ++i) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting(); | 240 GroupMapAccessor::GetInstance()->ClearAllMapsForTesting(); |
| 241 } | 241 } |
| 242 | 242 |
| 243 void ClearAllVariationParams() { | 243 void ClearAllVariationParams() { |
| 244 VariationsParamAssociator::GetInstance()->ClearAllParamsForTesting(); | 244 VariationsParamAssociator::GetInstance()->ClearAllParamsForTesting(); |
| 245 } | 245 } |
| 246 | 246 |
| 247 } // namespace testing | 247 } // namespace testing |
| 248 | 248 |
| 249 } // namespace variations | 249 } // namespace variations |
| OLD | NEW |