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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_
6 #define COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_
11 #include "base/metrics/field_trial.h"
12 #include "components/variations/active_field_trials.h"
14 // This file provides various helpers that extend the functionality around
17 // This includes several simple APIs to handle getting and setting additional
18 // data related to Chrome variations, such as parameters and Google variation
19 // IDs. These APIs are meant to extend the base::FieldTrial APIs to offer extra
20 // functionality that is not offered by the simpler base::FieldTrial APIs.
22 // The AssociateGoogleVariationID and AssociateVariationParams functions are
23 // generally meant to be called by the VariationsService based on server-side
24 // variation configs, but may also be used for client-only field trials by
25 // invoking them directly after appending all the groups to a FieldTrial.
27 // Experiment code can then use the getter APIs to retrieve variation parameters
30 // std::map<std::string, std::string> params;
31 // if (GetVariationParams("trial", ¶ms)) {
35 // std::string value = GetVariationParamValue("trial", "param_x");
36 // // use |value|, which will be "" if it does not exist
38 // VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial",
40 // if (id != variations::kEmptyID) {
44 namespace variations
{
46 typedef int VariationID
;
48 const VariationID EMPTY_ID
= 0;
50 // A key into the Associate/Get methods for VariationIDs. This is used to create
51 // separate ID associations for separate parties interested in VariationIDs.
52 enum IDCollectionKey
{
53 // This collection is used by Google web properties, transmitted through the
54 // X-Client-Data header.
55 GOOGLE_WEB_PROPERTIES
,
56 // This collection is used by Google web properties for IDs that trigger
57 // server side experimental behavior, transmitted through the
58 // X-Client-Data header.
59 GOOGLE_WEB_PROPERTIES_TRIGGER
,
60 // This collection is used by Google update services, transmitted through the
61 // Google Update experiment labels.
62 GOOGLE_UPDATE_SERVICE
,
63 // The total count of collections.
67 // Associate a variations::VariationID value with a FieldTrial group for
68 // collection |key|. If an id was previously set for |trial_name| and
69 // |group_name|, this does nothing. The group is denoted by |trial_name| and
70 // |group_name|. This must be called whenever a FieldTrial is prepared (create
71 // the trial and append groups) and needs to have a variations::VariationID
72 // associated with it so Google servers can recognize the FieldTrial.
74 void AssociateGoogleVariationID(IDCollectionKey key
,
75 const std::string
& trial_name
,
76 const std::string
& group_name
,
79 // As above, but overwrites any previously set id. Thread safe.
80 void AssociateGoogleVariationIDForce(IDCollectionKey key
,
81 const std::string
& trial_name
,
82 const std::string
& group_name
,
85 // As above, but takes an ActiveGroupId hash pair, rather than the string names.
86 void AssociateGoogleVariationIDForceHashes(IDCollectionKey key
,
87 const ActiveGroupId
& active_group
,
90 // Retrieve the variations::VariationID associated with a FieldTrial group for
91 // collection |key|. The group is denoted by |trial_name| and |group_name|.
92 // This will return variations::kEmptyID if there is currently no associated ID
93 // for the named group. This API can be nicely combined with
94 // FieldTrial::GetActiveFieldTrialGroups() to enumerate the variation IDs for
95 // all active FieldTrial groups. Thread safe.
96 VariationID
GetGoogleVariationID(IDCollectionKey key
,
97 const std::string
& trial_name
,
98 const std::string
& group_name
);
100 // Same as GetGoogleVariationID(), but takes in a hashed |active_group| rather
101 // than the string trial and group name.
102 VariationID
GetGoogleVariationIDFromHashes(IDCollectionKey key
,
103 const ActiveGroupId
& active_group
);
105 // Associates the specified set of key-value |params| with the variation
106 // specified by |trial_name| and |group_name|. Fails and returns false if the
107 // specified variation already has params associated with it or the field trial
108 // is already active (group() has been called on it). Thread safe.
109 bool AssociateVariationParams(const std::string
& trial_name
,
110 const std::string
& group_name
,
111 const std::map
<std::string
, std::string
>& params
);
113 // Retrieves the set of key-value |params| for the variation associated with
114 // the specified field trial, based on its selected group. If the field trial
115 // does not exist or its selected group does not have any parameters associated
116 // with it, returns false and does not modify |params|. Calling this function
117 // will result in the field trial being marked as active if found (i.e. group()
118 // will be called on it), if it wasn't already. Currently, this information is
119 // only available from the browser process. Thread safe.
120 bool GetVariationParams(const std::string
& trial_name
,
121 std::map
<std::string
, std::string
>* params
);
123 // Retrieves a specific parameter value corresponding to |param_name| for the
124 // variation associated with the specified field trial, based on its selected
125 // group. If the field trial does not exist or the specified parameter does not
126 // exist, returns an empty string. Calling this function will result in the
127 // field trial being marked as active if found (i.e. group() will be called on
128 // it), if it wasn't already. Currently, this information is only available from
129 // the browser process. Thread safe.
130 std::string
GetVariationParamValue(const std::string
& trial_name
,
131 const std::string
& param_name
);
133 // Expose some functions for testing.
136 // Clears all of the mapped associations.
137 void ClearAllVariationIDs();
139 // Clears all of the associated params.
140 void ClearAllVariationParams();
142 } // namespace testing
144 } // namespace variations
146 #endif // COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_