Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame^] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 5 | #ifndef COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ |
| 6 | #define COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 7 | |
| 8 | #include <map> |
jkrcal | bf07337 | 2016-07-29 07:21:31 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 10 | #include <string> |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 11 | |
Caitlin Fischer | cce73dd | 2020-08-20 20:42:42 | [diff] [blame] | 12 | #include "base/metrics/field_trial.h" |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 13 | #include "components/variations/active_field_trials.h" |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 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 | // |
Caitlin Fischer | cce73dd | 2020-08-20 20:42:42 | [diff] [blame] | 39 | // VariationID id = GetGoogleVariationID( |
| 40 | // GOOGLE_WEB_PROPERTIES_ANY_CONTEXT, "trial", "group1"); |
Sky Malice | 859454a | 2017-12-01 20:46:06 | [diff] [blame] | 41 | // if (id != variations::EMPTY_ID) { |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 42 | // // use |id| |
| 43 | // } |
| 44 | |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 45 | namespace variations { |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 46 | |
[email protected] | 2b9badf | 2013-08-16 22:15:18 | [diff] [blame] | 47 | typedef int VariationID; |
| 48 | |
| 49 | const VariationID EMPTY_ID = 0; |
| 50 | |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 51 | // A key into the Associate/Get methods for VariationIDs. This is used to create |
| 52 | // separate ID associations for separate parties interested in VariationIDs. |
| 53 | enum IDCollectionKey { |
Caitlin Fischer | cce73dd | 2020-08-20 20:42:42 | [diff] [blame] | 54 | // The IDs in this collection are used by Google web properties and are |
| 55 | // transmitted via the X-Client-Data header. These IDs are transmitted in |
| 56 | // first- and third-party contexts. |
| 57 | GOOGLE_WEB_PROPERTIES_ANY_CONTEXT, |
Caitlin Fischer | 38ac6d5e | 2020-09-15 01:17:24 | [diff] [blame] | 58 | // The IDs in this collection are used by Google web properties and are |
| 59 | // transmitted via the X-Client-Data header. When kRestrictGoogleWebVisibility |
| 60 | // is enabled, these IDs are transmitted in only first-party contexts; |
| 61 | // otherwise, these IDs are transmitted in first- and third-party contexts. |
Caitlin Fischer | cce73dd | 2020-08-20 20:42:42 | [diff] [blame] | 62 | GOOGLE_WEB_PROPERTIES_FIRST_PARTY, |
yutak | 3305f49d | 2016-12-13 10:32:31 | [diff] [blame] | 63 | // This collection is used by Google web properties for signed in users only, |
| 64 | // transmitted through the X-Client-Data header. |
| 65 | GOOGLE_WEB_PROPERTIES_SIGNED_IN, |
Caitlin Fischer | cce73dd | 2020-08-20 20:42:42 | [diff] [blame] | 66 | // The IDs in this collection are used by Google web properties to trigger |
| 67 | // server-side experimental behavior and are transmitted via the X-Client-Data |
| 68 | // header. These IDs are transmitted in first- and third-party contexts. |
| 69 | GOOGLE_WEB_PROPERTIES_TRIGGER_ANY_CONTEXT, |
Caitlin Fischer | 38ac6d5e | 2020-09-15 01:17:24 | [diff] [blame] | 70 | // The IDs in this collection are used by Google web properties to trigger |
| 71 | // server-side experimental behavior and are transmitted via the X-Client-Data |
| 72 | // header. When kRestrictGoogleWebVisibility is enabled, these IDs are |
| 73 | // transmitted in only first-party contexts; otherwise, these IDs are |
| 74 | // transmitted in first- and third-party contexts. |
Caitlin Fischer | cce73dd | 2020-08-20 20:42:42 | [diff] [blame] | 75 | GOOGLE_WEB_PROPERTIES_TRIGGER_FIRST_PARTY, |
Ben Goldberger | c791dc5 | 2020-05-14 08:24:25 | [diff] [blame] | 76 | // This collection is used by the Google App and is passed at the time |
| 77 | // the cross-app communication is triggered. |
| 78 | GOOGLE_APP, |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 79 | // The total count of collections. |
| 80 | ID_COLLECTION_COUNT, |
| 81 | }; |
| 82 | |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 83 | // Associate a variations::VariationID value with a FieldTrial group for |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 84 | // collection |key|. If an id was previously set for |trial_name| and |
| 85 | // |group_name|, this does nothing. The group is denoted by |trial_name| and |
| 86 | // |group_name|. This must be called whenever a FieldTrial is prepared (create |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 87 | // the trial and append groups) and needs to have a variations::VariationID |
| 88 | // associated with it so Google servers can recognize the FieldTrial. |
| 89 | // Thread safe. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 90 | COMPONENT_EXPORT(VARIATIONS) |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 91 | void AssociateGoogleVariationID(IDCollectionKey key, |
| 92 | const std::string& trial_name, |
| 93 | const std::string& group_name, |
| 94 | VariationID id); |
| 95 | |
| 96 | // As above, but overwrites any previously set id. Thread safe. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 97 | COMPONENT_EXPORT(VARIATIONS) |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 98 | void AssociateGoogleVariationIDForce(IDCollectionKey key, |
| 99 | const std::string& trial_name, |
| 100 | const std::string& group_name, |
| 101 | VariationID id); |
| 102 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 103 | // As above, but takes an ActiveGroupId hash pair, rather than the string names. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 104 | COMPONENT_EXPORT(VARIATIONS) |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 105 | void AssociateGoogleVariationIDForceHashes(IDCollectionKey key, |
| 106 | const ActiveGroupId& active_group, |
| 107 | VariationID id); |
| 108 | |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 109 | // Retrieve the variations::VariationID associated with a FieldTrial group for |
| 110 | // collection |key|. The group is denoted by |trial_name| and |group_name|. |
Sky Malice | 859454a | 2017-12-01 20:46:06 | [diff] [blame] | 111 | // This will return variations::EMPTY_ID if there is currently no associated ID |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 112 | // for the named group. This API can be nicely combined with |
| 113 | // FieldTrial::GetActiveFieldTrialGroups() to enumerate the variation IDs for |
| 114 | // all active FieldTrial groups. Thread safe. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 115 | COMPONENT_EXPORT(VARIATIONS) |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 116 | VariationID GetGoogleVariationID(IDCollectionKey key, |
| 117 | const std::string& trial_name, |
| 118 | const std::string& group_name); |
| 119 | |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 120 | // Same as GetGoogleVariationID(), but takes in a hashed |active_group| rather |
| 121 | // than the string trial and group name. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 122 | COMPONENT_EXPORT(VARIATIONS) |
asvitkine | e0dbdbe | 2014-10-31 21:59:57 | [diff] [blame] | 123 | VariationID GetGoogleVariationIDFromHashes(IDCollectionKey key, |
| 124 | const ActiveGroupId& active_group); |
| 125 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 126 | // Deprecated. Use base::AssociateFieldTrialParams() instead. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 127 | COMPONENT_EXPORT(VARIATIONS) |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 128 | bool AssociateVariationParams(const std::string& trial_name, |
| 129 | const std::string& group_name, |
| 130 | const std::map<std::string, std::string>& params); |
| 131 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 132 | // Deprecated. Use base::GetFieldTrialParams() instead. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 133 | COMPONENT_EXPORT(VARIATIONS) |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 134 | bool GetVariationParams(const std::string& trial_name, |
| 135 | std::map<std::string, std::string>* params); |
| 136 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 137 | // Deprecated. Use base::GetFieldTrialParamsByFeature() instead. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 138 | COMPONENT_EXPORT(VARIATIONS) |
jwd | 07b9038 | 2016-05-06 20:39:42 | [diff] [blame] | 139 | bool GetVariationParamsByFeature(const base::Feature& feature, |
| 140 | std::map<std::string, std::string>* params); |
| 141 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 142 | // Deprecated. Use base::GetFieldTrialParamValue() instead. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 143 | COMPONENT_EXPORT(VARIATIONS) |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 144 | std::string GetVariationParamValue(const std::string& trial_name, |
| 145 | const std::string& param_name); |
| 146 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 147 | // Deprecated. Use base::GetFieldTrialParamValueByFeature() instead. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 148 | COMPONENT_EXPORT(VARIATIONS) |
jwd | 07b9038 | 2016-05-06 20:39:42 | [diff] [blame] | 149 | std::string GetVariationParamValueByFeature(const base::Feature& feature, |
| 150 | const std::string& param_name); |
| 151 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 152 | // Deprecated. Use base::GetFieldTrialParamByFeatureAsInt() instead. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 153 | COMPONENT_EXPORT(VARIATIONS) |
jkrcal | d5dd68d | 2016-12-09 07:44:21 | [diff] [blame] | 154 | int GetVariationParamByFeatureAsInt(const base::Feature& feature, |
| 155 | const std::string& param_name, |
| 156 | int default_value); |
| 157 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 158 | // Deprecated. Use base::GetFieldTrialParamByFeatureAsDouble() instead. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 159 | COMPONENT_EXPORT(VARIATIONS) |
jkrcal | d5dd68d | 2016-12-09 07:44:21 | [diff] [blame] | 160 | double GetVariationParamByFeatureAsDouble(const base::Feature& feature, |
| 161 | const std::string& param_name, |
| 162 | double default_value); |
| 163 | |
asvitkine | 79ab08c | 2017-01-30 23:27:05 | [diff] [blame] | 164 | // Deprecated. Use base::GetFieldTrialParamByFeatureAsBool() instead. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 165 | COMPONENT_EXPORT(VARIATIONS) |
jkrcal | d5dd68d | 2016-12-09 07:44:21 | [diff] [blame] | 166 | bool GetVariationParamByFeatureAsBool(const base::Feature& feature, |
| 167 | const std::string& param_name, |
| 168 | bool default_value); |
| 169 | |
[email protected] | d92134c | 2013-08-09 07:37:29 | [diff] [blame] | 170 | // Expose some functions for testing. |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 171 | namespace testing { |
| 172 | |
Alexei Svitkine | 647c3ab4 | 2019-09-03 16:07:46 | [diff] [blame] | 173 | // Clears all of the mapped associations. Deprecated, use ScopedFeatureList |
| 174 | // instead as it does a lot of work for you automatically. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 175 | COMPONENT_EXPORT(VARIATIONS) void ClearAllVariationIDs(); |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 176 | |
Alexei Svitkine | 647c3ab4 | 2019-09-03 16:07:46 | [diff] [blame] | 177 | // Clears all of the associated params. Deprecated, use ScopedFeatureList |
| 178 | // instead as it does a lot of work for you automatically. |
Scott Violet | 69a5d8dd | 2021-06-01 23:46:48 | [diff] [blame] | 179 | COMPONENT_EXPORT(VARIATIONS) void ClearAllVariationParams(); |
[email protected] | d92134c | 2013-08-09 07:37:29 | [diff] [blame] | 180 | |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 181 | } // namespace testing |
| 182 | |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 183 | } // namespace variations |
[email protected] | 7e79779 | 2013-08-05 18:24:40 | [diff] [blame] | 184 | |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 185 | #endif // COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_ |