OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 BASE_METRICS_FIELD_TRIAL_PARAMS_H_ |
6 #define COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ | 6 #define BASE_METRICS_FIELD_TRIAL_PARAMS_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | |
10 #include <string> | 9 #include <string> |
11 #include <vector> | |
12 | 10 |
13 #include "components/variations/active_field_trials.h" | 11 #include "base/base_export.h" |
14 | |
15 // This file provides various helpers that extend the functionality around | |
16 // base::FieldTrial. | |
17 // | |
18 // This includes several simple APIs to handle getting and setting additional | |
19 // data related to Chrome variations, such as parameters and Google variation | |
20 // IDs. These APIs are meant to extend the base::FieldTrial APIs to offer extra | |
21 // functionality that is not offered by the simpler base::FieldTrial APIs. | |
22 // | |
23 // The AssociateGoogleVariationID and AssociateVariationParams functions are | |
24 // generally meant to be called by the VariationsService based on server-side | |
25 // variation configs, but may also be used for client-only field trials by | |
26 // invoking them directly after appending all the groups to a FieldTrial. | |
27 // | |
28 // Experiment code can then use the getter APIs to retrieve variation parameters | |
29 // or IDs: | |
30 // | |
31 // std::map<std::string, std::string> params; | |
32 // if (GetVariationParams("trial", ¶ms)) { | |
33 // // use |params| | |
34 // } | |
35 // | |
36 // std::string value = GetVariationParamValue("trial", "param_x"); | |
37 // // use |value|, which will be "" if it does not exist | |
38 // | |
39 // VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial", | |
40 // "group1"); | |
41 // if (id != variations::kEmptyID) { | |
42 // // use |id| | |
43 // } | |
44 | 12 |
45 namespace base { | 13 namespace base { |
| 14 |
46 struct Feature; | 15 struct Feature; |
47 } // namespace base | |
48 | 16 |
49 namespace variations { | 17 // Associates the specified set of key-value |params| with the field trial |
| 18 // specified by |trial_name| and |group_name|. Fails and returns false if the |
| 19 // specified field trial already has params associated with it or the trial |
| 20 // is already active (group() has been called on it). Thread safe. |
| 21 BASE_EXPORT bool AssociateFieldTrialParams( |
| 22 const std::string& trial_name, |
| 23 const std::string& group_name, |
| 24 const std::map<std::string, std::string>& params); |
50 | 25 |
51 typedef int VariationID; | 26 // Retrieves the set of key-value |params| for the specified field trial, based |
| 27 // on its selected group. If the field trial does not exist or its selected |
| 28 // group does not have any parameters associated with it, returns false and |
| 29 // does not modify |params|. Calling this function will result in the field |
| 30 // trial being marked as active if found (i.e. group() will be called on it), |
| 31 // if it wasn't already. Thread safe. |
| 32 BASE_EXPORT bool GetFieldTrialParams( |
| 33 const std::string& trial_name, |
| 34 std::map<std::string, std::string>* params); |
52 | 35 |
53 const VariationID EMPTY_ID = 0; | 36 // Retrieves the set of key-value |params| for the field trial associated with |
54 | 37 // the specified |feature|. A feature is associated with at most one field |
55 // A key into the Associate/Get methods for VariationIDs. This is used to create | 38 // trial and selected group. See base/feature_list.h for more information on |
56 // separate ID associations for separate parties interested in VariationIDs. | 39 // features. If the feature is not enabled, or if there's no associated params, |
57 enum IDCollectionKey { | 40 // returns false and does not modify |params|. Calling this function will |
58 // This collection is used by Google web properties, transmitted through the | 41 // result in the associated field trial being marked as active if found (i.e. |
59 // X-Client-Data header. | 42 // group() will be called on it), if it wasn't already. Thread safe. |
60 GOOGLE_WEB_PROPERTIES, | 43 BASE_EXPORT bool GetFieldTrialParamsByFeature( |
61 // This collection is used by Google web properties for signed in users only, | 44 const base::Feature& feature, |
62 // transmitted through the X-Client-Data header. | 45 std::map<std::string, std::string>* params); |
63 GOOGLE_WEB_PROPERTIES_SIGNED_IN, | |
64 // This collection is used by Google web properties for IDs that trigger | |
65 // server side experimental behavior, transmitted through the | |
66 // X-Client-Data header. | |
67 GOOGLE_WEB_PROPERTIES_TRIGGER, | |
68 // This collection is used by Chrome Sync services, transmitted through the | |
69 // Chrome Sync experiment labels. | |
70 CHROME_SYNC_SERVICE, | |
71 // The total count of collections. | |
72 ID_COLLECTION_COUNT, | |
73 }; | |
74 | |
75 // Associate a variations::VariationID value with a FieldTrial group for | |
76 // collection |key|. If an id was previously set for |trial_name| and | |
77 // |group_name|, this does nothing. The group is denoted by |trial_name| and | |
78 // |group_name|. This must be called whenever a FieldTrial is prepared (create | |
79 // the trial and append groups) and needs to have a variations::VariationID | |
80 // associated with it so Google servers can recognize the FieldTrial. | |
81 // Thread safe. | |
82 void AssociateGoogleVariationID(IDCollectionKey key, | |
83 const std::string& trial_name, | |
84 const std::string& group_name, | |
85 VariationID id); | |
86 | |
87 // As above, but overwrites any previously set id. Thread safe. | |
88 void AssociateGoogleVariationIDForce(IDCollectionKey key, | |
89 const std::string& trial_name, | |
90 const std::string& group_name, | |
91 VariationID id); | |
92 | |
93 // As above, but takes an ActiveGroupId hash pair, rather than the string names. | |
94 void AssociateGoogleVariationIDForceHashes(IDCollectionKey key, | |
95 const ActiveGroupId& active_group, | |
96 VariationID id); | |
97 | |
98 // Retrieve the variations::VariationID associated with a FieldTrial group for | |
99 // collection |key|. The group is denoted by |trial_name| and |group_name|. | |
100 // This will return variations::kEmptyID if there is currently no associated ID | |
101 // for the named group. This API can be nicely combined with | |
102 // FieldTrial::GetActiveFieldTrialGroups() to enumerate the variation IDs for | |
103 // all active FieldTrial groups. Thread safe. | |
104 VariationID GetGoogleVariationID(IDCollectionKey key, | |
105 const std::string& trial_name, | |
106 const std::string& group_name); | |
107 | |
108 // Same as GetGoogleVariationID(), but takes in a hashed |active_group| rather | |
109 // than the string trial and group name. | |
110 VariationID GetGoogleVariationIDFromHashes(IDCollectionKey key, | |
111 const ActiveGroupId& active_group); | |
112 | |
113 // Associates the specified set of key-value |params| with the variation | |
114 // specified by |trial_name| and |group_name|. Fails and returns false if the | |
115 // specified variation already has params associated with it or the field trial | |
116 // is already active (group() has been called on it). Thread safe. | |
117 bool AssociateVariationParams(const std::string& trial_name, | |
118 const std::string& group_name, | |
119 const std::map<std::string, std::string>& params); | |
120 | |
121 // Retrieves the set of key-value |params| for the variation associated with | |
122 // the specified field trial, based on its selected group. If the field trial | |
123 // does not exist or its selected group does not have any parameters associated | |
124 // with it, returns false and does not modify |params|. Calling this function | |
125 // will result in the field trial being marked as active if found (i.e. group() | |
126 // will be called on it), if it wasn't already. Currently, this information is | |
127 // only available from the browser process. Thread safe. | |
128 bool GetVariationParams(const std::string& trial_name, | |
129 std::map<std::string, std::string>* params); | |
130 | |
131 // Retrieves the set of key-value |params| for the variation associated with the | |
132 // specified |feature|. A feature is associated with at most one variation, | |
133 // through the variation's associated field trial, and selected group. See | |
134 // base/feature_list.h for more information on features. If the feature is not | |
135 // enabled, or if there's no associated variation params, returns false and does | |
136 // not modify |params|. Calling this function will result in the associated | |
137 // field trial being marked as active if found (i.e. group() will be called on | |
138 // it), if it wasn't already. Currently, this information is only available from | |
139 // the browser process. Thread safe. | |
140 bool GetVariationParamsByFeature(const base::Feature& feature, | |
141 std::map<std::string, std::string>* params); | |
142 | 46 |
143 // Retrieves a specific parameter value corresponding to |param_name| for the | 47 // Retrieves a specific parameter value corresponding to |param_name| for the |
144 // variation associated with the specified field trial, based on its selected | 48 // specified field trial, based on its selected group. If the field trial does |
145 // group. If the field trial does not exist or the specified parameter does not | 49 // not exist or the specified parameter does not exist, returns an empty |
146 // exist, returns an empty string. Calling this function will result in the | 50 // string. Calling this function will result in the field trial being marked as |
147 // field trial being marked as active if found (i.e. group() will be called on | 51 // active if found (i.e. group() will be called on it), if it wasn't already. |
148 // it), if it wasn't already. Currently, this information is only available from | 52 // Thread safe. |
149 // the browser process. Thread safe. | 53 BASE_EXPORT std::string GetFieldTrialParamValue(const std::string& trial_name, |
150 std::string GetVariationParamValue(const std::string& trial_name, | 54 const std::string& param_name); |
151 const std::string& param_name); | |
152 | 55 |
153 // Retrieves a specific parameter value corresponding to |param_name| for the | 56 // Retrieves a specific parameter value corresponding to |param_name| for the |
154 // variation associated with the specified |feature|. A feature is associated | 57 // field trial associated with the specified |feature|. A feature is associated |
155 // with at most one variation, through the variation's associated field trial, | 58 // with at most one field trial and selected group. See base/feature_list.h for |
156 // and selected group. See base/feature_list.h for more information on | 59 // more information on features. If the feature is not enabled, or the |
157 // features. If the feature is not enabled, or the specified parameter does not | 60 // specified parameter does not exist, returns an empty string. Calling this |
158 // exist, returns an empty string. Calling this function will result in the | 61 // function will result in the associated field trial being marked as active if |
159 // associated field trial being marked as active if found (i.e. group() will be | 62 // found (i.e. group() will be called on it), if it wasn't already. Thread safe. |
160 // called on it), if it wasn't already. Currently, this information is only | 63 BASE_EXPORT std::string GetFieldTrialParamValueByFeature( |
161 // available from the browser process. Thread safe. | 64 const base::Feature& feature, |
162 std::string GetVariationParamValueByFeature(const base::Feature& feature, | 65 const std::string& param_name); |
163 const std::string& param_name); | |
164 | 66 |
165 // Same as GetVariationParamValueByFeature(). On top of that, it converts the | 67 // Same as GetFieldTrialParamValueByFeature(). On top of that, it converts the |
166 // string value into an int using base::StringToInt() and returns it, if | 68 // string value into an int using base::StringToInt() and returns it, if |
167 // successful. Otherwise, it returns |default_value|. If the string value is not | 69 // successful. Otherwise, it returns |default_value|. If the string value is not |
168 // empty and the conversion does not succeed, it produces a warning to LOG. | 70 // empty and the conversion does not succeed, it produces a warning to LOG. |
169 int GetVariationParamByFeatureAsInt(const base::Feature& feature, | 71 BASE_EXPORT int GetFieldTrialParamByFeatureAsInt(const base::Feature& feature, |
170 const std::string& param_name, | 72 const std::string& param_name, |
171 int default_value); | 73 int default_value); |
172 | 74 |
173 // Same as GetVariationParamValueByFeature(). On top of that, it converts the | 75 // Same as GetFieldTrialParamValueByFeature(). On top of that, it converts the |
174 // string value into a double using base::StringToDouble() and returns it, if | 76 // string value into a double using base::StringToDouble() and returns it, if |
175 // successful. Otherwise, it returns |default_value|. If the string value is not | 77 // successful. Otherwise, it returns |default_value|. If the string value is not |
176 // empty and the conversion does not succeed, it produces a warning to LOG. | 78 // empty and the conversion does not succeed, it produces a warning to LOG. |
177 double GetVariationParamByFeatureAsDouble(const base::Feature& feature, | 79 BASE_EXPORT double GetFieldTrialParamByFeatureAsDouble( |
178 const std::string& param_name, | 80 const base::Feature& feature, |
179 double default_value); | 81 const std::string& param_name, |
| 82 double default_value); |
180 | 83 |
181 // Same as GetVariationParamValueByFeature(). On top of that, it converts the | 84 // Same as GetFieldTrialParamValueByFeature(). On top of that, it converts the |
182 // string value into a boolean and returns it, if successful. Otherwise, it | 85 // string value into a boolean and returns it, if successful. Otherwise, it |
183 // returns |default_value|. The only string representations accepted here are | 86 // returns |default_value|. The only string representations accepted here are |
184 // "true" and "false". If the string value is not empty and the conversion does | 87 // "true" and "false". If the string value is not empty and the conversion does |
185 // not succeed, it produces a warning to LOG. | 88 // not succeed, it produces a warning to LOG. |
186 bool GetVariationParamByFeatureAsBool(const base::Feature& feature, | 89 BASE_EXPORT bool GetFieldTrialParamByFeatureAsBool( |
187 const std::string& param_name, | 90 const base::Feature& feature, |
188 bool default_value); | 91 const std::string& param_name, |
| 92 bool default_value); |
189 | 93 |
190 // Expose some functions for testing. | 94 } // namespace base |
191 namespace testing { | |
192 | 95 |
193 // Clears all of the mapped associations. Deprecated, try to use | 96 #endif // BASE_METRICS_FIELD_TRIAL_PARAMS_H_ |
194 // VariationParamsManager instead as it does a lot of work for you | |
195 // automatically. | |
196 void ClearAllVariationIDs(); | |
197 | |
198 // Clears all of the associated params. Deprecated, try to use | |
199 // VariationParamsManager instead as it does a lot of work for you | |
200 // automatically. | |
201 void ClearAllVariationParams(); | |
202 | |
203 } // namespace testing | |
204 | |
205 } // namespace variations | |
206 | |
207 #endif // COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ | |
OLD | NEW |