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"
13 // This file provides various helpers that extend the functionality around
16 // This includes several simple APIs to handle getting and setting additional
17 // data related to Chrome variations, such as parameters and Google variation
18 // IDs. These APIs are meant to extend the base::FieldTrial APIs to offer extra
19 // functionality that is not offered by the simpler base::FieldTrial APIs.
21 // The AssociateGoogleVariationID and AssociateVariationParams functions are
22 // generally meant to be called by the VariationsService based on server-side
23 // variation configs, but may also be used for client-only field trials by
24 // invoking them directly after appending all the groups to a FieldTrial.
26 // Experiment code can then use the getter APIs to retrieve variation parameters
29 // std::map<std::string, std::string> params;
30 // if (GetVariationParams("trial", ¶ms)) {
34 // std::string value = GetVariationParamValue("trial", "param_x");
35 // // use |value|, which will be "" if it does not exist
37 // VariationID id = GetGoogleVariationID(GOOGLE_WEB_PROPERTIES, "trial",
39 // if (id != chrome_variations::kEmptyID) {
43 namespace chrome_variations {
45 typedef int VariationID;
47 const VariationID EMPTY_ID = 0;
49 // The Unique ID of a trial and its active group, where the name and group
50 // identifiers are hashes of the trial and group name strings.
51 struct ActiveGroupId {
56 // Returns an ActiveGroupId struct for the given trial and group names.
57 ActiveGroupId MakeActiveGroupId(const std::string& trial_name,
58 const std::string& group_name);
60 // We need to supply a Compare class for templates since ActiveGroupId is a
62 struct ActiveGroupIdCompare {
63 bool operator() (const ActiveGroupId& lhs, const ActiveGroupId& rhs) const {
64 // The group and name fields are just SHA-1 Hashes, so we just need to treat
65 // them as IDs and do a less-than comparison. We test group first, since
66 // name is more likely to collide.
67 if (lhs.group != rhs.group)
68 return lhs.group < rhs.group;
69 return lhs.name < rhs.name;
73 // A key into the Associate/Get methods for VariationIDs. This is used to create
74 // separate ID associations for separate parties interested in VariationIDs.
75 enum IDCollectionKey {
76 // This collection is used by Google web properties, transmitted through the
77 // X-Client-Data header.
78 GOOGLE_WEB_PROPERTIES,
79 // This collection is used by Google web properties for IDs that trigger
80 // server side experimental behavior, transmitted through the
81 // X-Client-Data header.
82 GOOGLE_WEB_PROPERTIES_TRIGGER,
83 // This collection is used by Google update services, transmitted through the
84 // Google Update experiment labels.
85 GOOGLE_UPDATE_SERVICE,
86 // The total count of collections.
90 // Associate a chrome_variations::VariationID value with a FieldTrial group for
91 // collection |key|. If an id was previously set for |trial_name| and
92 // |group_name|, this does nothing. The group is denoted by |trial_name| and
93 // |group_name|. This must be called whenever a FieldTrial is prepared (create
94 // the trial and append groups) and needs to have a
95 // chrome_variations::VariationID associated with it so Google servers can
96 // recognize the FieldTrial. Thread safe.
97 void AssociateGoogleVariationID(IDCollectionKey key,
98 const std::string& trial_name,
99 const std::string& group_name,
102 // As above, but overwrites any previously set id. Thread safe.
103 void AssociateGoogleVariationIDForce(IDCollectionKey key,
104 const std::string& trial_name,
105 const std::string& group_name,
108 // Retrieve the chrome_variations::VariationID associated with a FieldTrial
109 // group for collection |key|. The group is denoted by |trial_name| and
110 // |group_name|. This will return chrome_variations::kEmptyID if there is
111 // currently no associated ID for the named group. This API can be nicely
112 // combined with FieldTrial::GetActiveFieldTrialGroups() to enumerate the
113 // variation IDs for all active FieldTrial groups. Thread safe.
114 VariationID GetGoogleVariationID(IDCollectionKey key,
115 const std::string& trial_name,
116 const std::string& group_name);
118 // Associates the specified set of key-value |params| with the variation
119 // specified by |trial_name| and |group_name|. Fails and returns false if the
120 // specified variation already has params associated with it or the field trial
121 // is already active (group() has been called on it). Thread safe.
122 bool AssociateVariationParams(const std::string& trial_name,
123 const std::string& group_name,
124 const std::map<std::string, std::string>& params);
126 // Retrieves the set of key-value |params| for the variation associated with
127 // the specified field trial, based on its selected group. If the field trial
128 // does not exist or its selected group does not have any parameters associated
129 // with it, returns false and does not modify |params|. Calling this function
130 // will result in the field trial being marked as active if found (i.e. group()
131 // will be called on it), if it wasn't already. Currently, this information is
132 // only available from the browser process. Thread safe.
133 bool GetVariationParams(const std::string& trial_name,
134 std::map<std::string, std::string>* params);
136 // Retrieves a specific parameter value corresponding to |param_name| for the
137 // variation associated with the specified field trial, based on its selected
138 // group. If the field trial does not exist or the specified parameter does not
139 // exist, returns an empty string. Calling this function will result in the
140 // field trial being marked as active if found (i.e. group() will be called on
141 // it), if it wasn't already. Currently, this information is only available from
142 // the browser process. Thread safe.
143 std::string GetVariationParamValue(const std::string& trial_name,
144 const std::string& param_name);
146 // Expose some functions for testing.
149 // Clears all of the mapped associations.
150 void ClearAllVariationIDs();
152 // Clears all of the associated params.
153 void ClearAllVariationParams();
155 } // namespace testing
157 } // namespace chrome_variations
159 #endif // COMPONENTS_VARIATIONS_VARIATIONS_ASSOCIATED_DATA_H_