blob: 05b42179819dd7e2e1b0304c8e1060f6a598cbd5 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2013 The Chromium Authors
[email protected]7e797792013-08-05 18:24:402// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]50ae9f12013-08-29 18:03:225#ifndef COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_
6#define COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_
[email protected]7e797792013-08-05 18:24:407
8#include <map>
jkrcalbf073372016-07-29 07:21:319#include <memory>
[email protected]7e797792013-08-05 18:24:4010#include <string>
[email protected]7e797792013-08-05 18:24:4011
Caitlin Fischercce73dd2020-08-20 20:42:4212#include "base/metrics/field_trial.h"
[email protected]b3610d42014-05-19 18:07:2313#include "components/variations/active_field_trials.h"
[email protected]7e797792013-08-05 18:24:4014
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", &params)) {
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 Fischercce73dd2020-08-20 20:42:4239// VariationID id = GetGoogleVariationID(
40// GOOGLE_WEB_PROPERTIES_ANY_CONTEXT, "trial", "group1");
Sky Malice859454a2017-12-01 20:46:0641// if (id != variations::EMPTY_ID) {
[email protected]7e797792013-08-05 18:24:4042// // use |id|
43// }
44
[email protected]59b6f672014-07-26 18:35:4745namespace variations {
[email protected]7e797792013-08-05 18:24:4046
[email protected]2b9badf2013-08-16 22:15:1847typedef int VariationID;
48
49const VariationID EMPTY_ID = 0;
50
[email protected]7e797792013-08-05 18:24:4051// A key into the Associate/Get methods for VariationIDs. This is used to create
52// separate ID associations for separate parties interested in VariationIDs.
53enum IDCollectionKey {
Caitlin Fischercce73dd2020-08-20 20:42:4254 // 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 Fischer38ac6d5e2020-09-15 01:17:2458 // 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 Fischercce73dd2020-08-20 20:42:4262 GOOGLE_WEB_PROPERTIES_FIRST_PARTY,
yutak3305f49d2016-12-13 10:32:3163 // 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 Fischercce73dd2020-08-20 20:42:4266 // 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 Fischer38ac6d5e2020-09-15 01:17:2470 // 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 Fischercce73dd2020-08-20 20:42:4275 GOOGLE_WEB_PROPERTIES_TRIGGER_FIRST_PARTY,
Ben Goldbergerc791dc52020-05-14 08:24:2576 // 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]7e797792013-08-05 18:24:4079 // The total count of collections.
80 ID_COLLECTION_COUNT,
81};
82
[email protected]59b6f672014-07-26 18:35:4783// Associate a variations::VariationID value with a FieldTrial group for
[email protected]7e797792013-08-05 18:24:4084// 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]59b6f672014-07-26 18:35:4787// 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 Violet69a5d8dd2021-06-01 23:46:4890COMPONENT_EXPORT(VARIATIONS)
[email protected]7e797792013-08-05 18:24:4091void 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 Violet69a5d8dd2021-06-01 23:46:4897COMPONENT_EXPORT(VARIATIONS)
[email protected]7e797792013-08-05 18:24:4098void AssociateGoogleVariationIDForce(IDCollectionKey key,
99 const std::string& trial_name,
100 const std::string& group_name,
101 VariationID id);
102
asvitkinee0dbdbe2014-10-31 21:59:57103// As above, but takes an ActiveGroupId hash pair, rather than the string names.
Scott Violet69a5d8dd2021-06-01 23:46:48104COMPONENT_EXPORT(VARIATIONS)
asvitkinee0dbdbe2014-10-31 21:59:57105void AssociateGoogleVariationIDForceHashes(IDCollectionKey key,
106 const ActiveGroupId& active_group,
107 VariationID id);
108
[email protected]59b6f672014-07-26 18:35:47109// Retrieve the variations::VariationID associated with a FieldTrial group for
110// collection |key|. The group is denoted by |trial_name| and |group_name|.
Sky Malice859454a2017-12-01 20:46:06111// This will return variations::EMPTY_ID if there is currently no associated ID
[email protected]59b6f672014-07-26 18:35:47112// 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 Violet69a5d8dd2021-06-01 23:46:48115COMPONENT_EXPORT(VARIATIONS)
[email protected]7e797792013-08-05 18:24:40116VariationID GetGoogleVariationID(IDCollectionKey key,
117 const std::string& trial_name,
118 const std::string& group_name);
119
asvitkinee0dbdbe2014-10-31 21:59:57120// Same as GetGoogleVariationID(), but takes in a hashed |active_group| rather
121// than the string trial and group name.
Scott Violet69a5d8dd2021-06-01 23:46:48122COMPONENT_EXPORT(VARIATIONS)
asvitkinee0dbdbe2014-10-31 21:59:57123VariationID GetGoogleVariationIDFromHashes(IDCollectionKey key,
124 const ActiveGroupId& active_group);
125
asvitkine79ab08c2017-01-30 23:27:05126// Deprecated. Use base::AssociateFieldTrialParams() instead.
Scott Violet69a5d8dd2021-06-01 23:46:48127COMPONENT_EXPORT(VARIATIONS)
[email protected]7e797792013-08-05 18:24:40128bool AssociateVariationParams(const std::string& trial_name,
129 const std::string& group_name,
130 const std::map<std::string, std::string>& params);
131
asvitkine79ab08c2017-01-30 23:27:05132// Deprecated. Use base::GetFieldTrialParams() instead.
Scott Violet69a5d8dd2021-06-01 23:46:48133COMPONENT_EXPORT(VARIATIONS)
[email protected]7e797792013-08-05 18:24:40134bool GetVariationParams(const std::string& trial_name,
135 std::map<std::string, std::string>* params);
136
asvitkine79ab08c2017-01-30 23:27:05137// Deprecated. Use base::GetFieldTrialParamsByFeature() instead.
Scott Violet69a5d8dd2021-06-01 23:46:48138COMPONENT_EXPORT(VARIATIONS)
jwd07b90382016-05-06 20:39:42139bool GetVariationParamsByFeature(const base::Feature& feature,
140 std::map<std::string, std::string>* params);
141
asvitkine79ab08c2017-01-30 23:27:05142// Deprecated. Use base::GetFieldTrialParamValue() instead.
Scott Violet69a5d8dd2021-06-01 23:46:48143COMPONENT_EXPORT(VARIATIONS)
[email protected]7e797792013-08-05 18:24:40144std::string GetVariationParamValue(const std::string& trial_name,
145 const std::string& param_name);
146
asvitkine79ab08c2017-01-30 23:27:05147// Deprecated. Use base::GetFieldTrialParamValueByFeature() instead.
Scott Violet69a5d8dd2021-06-01 23:46:48148COMPONENT_EXPORT(VARIATIONS)
jwd07b90382016-05-06 20:39:42149std::string GetVariationParamValueByFeature(const base::Feature& feature,
150 const std::string& param_name);
151
asvitkine79ab08c2017-01-30 23:27:05152// Deprecated. Use base::GetFieldTrialParamByFeatureAsInt() instead.
Scott Violet69a5d8dd2021-06-01 23:46:48153COMPONENT_EXPORT(VARIATIONS)
jkrcald5dd68d2016-12-09 07:44:21154int GetVariationParamByFeatureAsInt(const base::Feature& feature,
155 const std::string& param_name,
156 int default_value);
157
asvitkine79ab08c2017-01-30 23:27:05158// Deprecated. Use base::GetFieldTrialParamByFeatureAsDouble() instead.
Scott Violet69a5d8dd2021-06-01 23:46:48159COMPONENT_EXPORT(VARIATIONS)
jkrcald5dd68d2016-12-09 07:44:21160double GetVariationParamByFeatureAsDouble(const base::Feature& feature,
161 const std::string& param_name,
162 double default_value);
163
asvitkine79ab08c2017-01-30 23:27:05164// Deprecated. Use base::GetFieldTrialParamByFeatureAsBool() instead.
Scott Violet69a5d8dd2021-06-01 23:46:48165COMPONENT_EXPORT(VARIATIONS)
jkrcald5dd68d2016-12-09 07:44:21166bool GetVariationParamByFeatureAsBool(const base::Feature& feature,
167 const std::string& param_name,
168 bool default_value);
169
[email protected]d92134c2013-08-09 07:37:29170// Expose some functions for testing.
[email protected]7e797792013-08-05 18:24:40171namespace testing {
172
Alexei Svitkine647c3ab42019-09-03 16:07:46173// Clears all of the mapped associations. Deprecated, use ScopedFeatureList
174// instead as it does a lot of work for you automatically.
Scott Violet69a5d8dd2021-06-01 23:46:48175COMPONENT_EXPORT(VARIATIONS) void ClearAllVariationIDs();
[email protected]7e797792013-08-05 18:24:40176
Alexei Svitkine647c3ab42019-09-03 16:07:46177// Clears all of the associated params. Deprecated, use ScopedFeatureList
178// instead as it does a lot of work for you automatically.
Scott Violet69a5d8dd2021-06-01 23:46:48179COMPONENT_EXPORT(VARIATIONS) void ClearAllVariationParams();
[email protected]d92134c2013-08-09 07:37:29180
[email protected]7e797792013-08-05 18:24:40181} // namespace testing
182
[email protected]59b6f672014-07-26 18:35:47183} // namespace variations
[email protected]7e797792013-08-05 18:24:40184
[email protected]50ae9f12013-08-29 18:03:22185#endif // COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_