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 #ifndef COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ | 5 #ifndef COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ |
6 #define COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ | 6 #define COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/metrics/field_trial.h" | |
14 #include "components/variations/active_field_trials.h" | 13 #include "components/variations/active_field_trials.h" |
15 | 14 |
16 // This file provides various helpers that extend the functionality around | 15 // This file provides various helpers that extend the functionality around |
17 // base::FieldTrial. | 16 // base::FieldTrial. |
18 // | 17 // |
19 // This includes several simple APIs to handle getting and setting additional | 18 // This includes several simple APIs to handle getting and setting additional |
20 // data related to Chrome variations, such as parameters and Google variation | 19 // data related to Chrome variations, such as parameters and Google variation |
21 // IDs. These APIs are meant to extend the base::FieldTrial APIs to offer extra | 20 // IDs. These APIs are meant to extend the base::FieldTrial APIs to offer extra |
22 // functionality that is not offered by the simpler base::FieldTrial APIs. | 21 // functionality that is not offered by the simpler base::FieldTrial APIs. |
23 // | 22 // |
(...skipping 14 matching lines...) Expand all Loading... |
38 // // use |value|, which will be "" if it does not exist | 37 // // use |value|, which will be "" if it does not exist |
39 // | 38 // |
40 // VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", | 39 // VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", |
41 // "group1"); | 40 // "group1"); |
42 // if (id != variations::kEmptyID) { | 41 // if (id != variations::kEmptyID) { |
43 // // use |id| | 42 // // use |id| |
44 // } | 43 // } |
45 | 44 |
46 namespace base { | 45 namespace base { |
47 struct Feature; | 46 struct Feature; |
48 } | 47 } // namespace base |
49 | 48 |
50 namespace variations { | 49 namespace variations { |
51 | 50 |
52 typedef int VariationID; | 51 typedef int VariationID; |
53 | 52 |
54 const VariationID EMPTY_ID = 0; | 53 const VariationID EMPTY_ID = 0; |
55 | 54 |
56 // A key into the Associate/Get methods for VariationIDs. This is used to create | 55 // A key into the Associate/Get methods for VariationIDs. This is used to create |
57 // separate ID associations for separate parties interested in VariationIDs. | 56 // separate ID associations for separate parties interested in VariationIDs. |
58 enum IDCollectionKey { | 57 enum IDCollectionKey { |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 // exist, returns an empty string.Calling this function will result in the | 155 // exist, returns an empty string.Calling this function will result in the |
157 // associated field trial being marked as active if found (i.e. group() will be | 156 // associated field trial being marked as active if found (i.e. group() will be |
158 // called on it), if it wasn't already. Currently, this information is only | 157 // called on it), if it wasn't already. Currently, this information is only |
159 // available from the browser process. Thread safe. | 158 // available from the browser process. Thread safe. |
160 std::string GetVariationParamValueByFeature(const base::Feature& feature, | 159 std::string GetVariationParamValueByFeature(const base::Feature& feature, |
161 const std::string& param_name); | 160 const std::string& param_name); |
162 | 161 |
163 // Expose some functions for testing. | 162 // Expose some functions for testing. |
164 namespace testing { | 163 namespace testing { |
165 | 164 |
166 // Use this class as a member in your test class to set variation params for | 165 // Clears all of the mapped associations. Deprecated, try to use |
167 // your tests. You can directly set the parameters in the constructor (if they | 166 // VariationParamsManager instead as it does a lot of work for you |
168 // are used by other members upon construction). You can change them later | 167 // automatically. |
169 // arbitrarily many times using the SetVariationParams function. Internally, it | |
170 // creates a FieldTrialList as a member. It works well for multiple tests of a | |
171 // given test class, as it clears the parameters when this class is destructed. | |
172 // Note that it clears all parameters (not just those registered here). | |
173 class VariationParamsManager { | |
174 public: | |
175 VariationParamsManager(const std::string& trial_name, | |
176 const std::map<std::string, std::string>& params); | |
177 ~VariationParamsManager(); | |
178 | |
179 // Associates |params| with the given |trial_name|. It creates a new group, | |
180 // used only for testing. Between two calls of this function, | |
181 // ClearAllVariationParams() has to be called. | |
182 void SetVariationParams(const std::string& trial_name, | |
183 const std::map<std::string, std::string>& params); | |
184 | |
185 private: | |
186 std::unique_ptr<base::FieldTrialList> field_trial_list_; | |
187 | |
188 DISALLOW_COPY_AND_ASSIGN(VariationParamsManager); | |
189 }; | |
190 | |
191 // Clears all of the mapped associations. | |
192 void ClearAllVariationIDs(); | 168 void ClearAllVariationIDs(); |
193 | 169 |
194 // Clears all of the associated params. | 170 // Clears all of the associated params. Deprecated, try to use |
| 171 // VariationParamsManager instead as it does a lot of work for you |
| 172 // automatically. |
195 void ClearAllVariationParams(); | 173 void ClearAllVariationParams(); |
196 | 174 |
197 } // namespace testing | 175 } // namespace testing |
198 | 176 |
199 } // namespace variations | 177 } // namespace variations |
200 | 178 |
201 #endif // COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ | 179 #endif // COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ |
OLD | NEW |