OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #ifndef CHROME_COMMON_METRICS_VARIATIONS_UTIL_H_ | |
6 #define CHROME_COMMON_METRICS_VARIATIONS_UTIL_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/metrics/field_trial.h" | |
12 #include "base/string16.h" | |
13 #include "chrome/common/metrics/variation_ids.h" | |
14 | |
15 // This namespace provides various helpers that extend the functionality around | |
16 // base::FieldTrial. | |
17 // | |
18 // This includes a simple API used to handle getting and setting | |
19 // data related to Google-specific variations in the browser. This is meant to | |
20 // be an extension to the base::FieldTrial for Google-specific functionality. | |
21 // | |
22 // These calls are meant to be made directly after appending all your groups to | |
23 // a FieldTrial (for associating IDs) and any time after the group selection has | |
24 // been done (for retrieving IDs). | |
25 // | |
26 // Typical usage looks like this: | |
27 // | |
28 // // Set up your trial and groups as usual. | |
29 // FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial( | |
30 // "trial", 1000, "default", 2012, 12, 31, NULL); | |
31 // const int kHighMemGroup = trial->AppendGroup("HighMem", 20); | |
32 // const int kLowMemGroup = trial->AppendGroup("LowMem", 20); | |
33 // // All groups are now created. We want to associate | |
34 // // chrome_variation::VariationIDs with them, so do that now. | |
35 // AssociateGoogleVariationID("trial", "default", chrome_variations::kValueA); | |
36 // AssociateGoogleVariationID("trial", "HighMem", chrome_variations::kValueB); | |
37 // AssociateGoogleVariationID("trial", "LowMem", chrome_variations::kValueC); | |
38 // | |
39 // // Elsewhere, we are interested in retrieving the VariationID associated | |
40 // // with |trial|. | |
41 // chrome_variations::VariationID id = | |
42 // GetGoogleVariationID(trial->name(), trial->group_name()); | |
43 // // Do stuff with |id|... | |
44 // | |
45 // The AssociateGoogleVariationID and GetGoogleVariationID API methods are | |
46 // thread safe. | |
47 | |
48 namespace chrome_variations { | |
49 | |
50 // The Unique ID of a trial and its selected group, where the name and group | |
51 // identifiers are hashes of the trial and group name strings. | |
52 struct SelectedGroupId { | |
53 uint32 name; | |
54 uint32 group; | |
55 }; | |
56 | |
57 // We need to supply a Compare class for templates since SelectedGroupId is a | |
58 // user-defined type. | |
59 struct SelectedGroupIdCompare { | |
60 bool operator() (const SelectedGroupId& lhs, | |
61 const SelectedGroupId& rhs) const { | |
62 // The group and name fields are just SHA-1 Hashes, so we just need to treat | |
63 // them as IDs and do a less-than comparison. We test group first, since | |
64 // name is more likely to collide. | |
65 if (lhs.group != rhs.group) | |
66 return lhs.group < rhs.group; | |
67 return lhs.name < rhs.name; | |
68 } | |
69 }; | |
70 | |
71 // Fills the supplied vector |name_group_ids| (which must be empty when called) | |
72 // with unique SelectedGroupIds for each Field Trial that has a chosen group. | |
73 // Field Trials for which a group has not been chosen yet are NOT returned in | |
74 // this list. | |
75 void GetFieldTrialSelectedGroupIds( | |
76 std::vector<SelectedGroupId>* name_group_ids); | |
77 | |
78 // Associate a chrome_variations::VariationID value with a FieldTrial group. If | |
79 // an id was previously set for |trial_name| and |group_name|, this does | |
80 // nothing. The group is denoted by |trial_name| and |group_name|. This must be | |
81 // called whenever you prepare a FieldTrial (create the trial and append groups) | |
82 // that needs to have a chrome_variations::VariationID associated with it so | |
83 // Google servers can recognize the FieldTrial. | |
84 void AssociateGoogleVariationID(const std::string& trial_name, | |
85 const std::string& group_name, | |
86 chrome_variations::VariationID id); | |
87 | |
88 // As above, but overwrites any previously set id. | |
89 void AssociateGoogleVariationIDForce(const std::string& trial_name, | |
90 const std::string& group_name, | |
91 chrome_variations::VariationID id); | |
92 | |
93 // Retrieve the chrome_variations::VariationID associated with a FieldTrial | |
94 // group. The group is denoted by |trial_name| and |group_name|. This will | |
95 // return chrome_variations::kEmptyID if there is currently no associated ID | |
96 // for the named group. This API can be nicely combined with | |
97 // FieldTrial::GetFieldTrialSelectedGroupIds to enumerate the | |
98 // variation IDs for all active FieldTrial groups. | |
99 chrome_variations::VariationID GetGoogleVariationID( | |
100 const std::string& trial_name, | |
101 const std::string& group_name); | |
102 | |
103 // Generates variation chunks from |variation_strings| that are suitable for | |
104 // crash reporting. | |
105 void GenerateVariationChunks(const std::vector<string16>& variation_strings, | |
106 std::vector<string16>* chunks); | |
107 | |
108 // Get the current set of chosen FieldTrial groups (aka variations) and send | |
109 // them to the child process logging module so it can save it for crash dumps. | |
110 void SetChildProcessLoggingVariationList(); | |
111 | |
112 } // namespace chrome_variations | |
113 | |
114 // Expose some functions for testing. These functions just wrap functionality | |
115 // that is implemented above. | |
116 namespace testing { | |
117 | |
118 void TestGetFieldTrialSelectedGroupIdsForSelectedGroups( | |
119 const base::FieldTrial::SelectedGroups& selected_groups, | |
120 std::vector<chrome_variations::SelectedGroupId>* name_group_ids); | |
121 | |
122 uint32 TestHashName(const std::string& name); | |
123 | |
124 } // namespace testing | |
125 | |
126 #endif // CHROME_COMMON_METRICS_VARIATIONS_UTIL_H_ | |
OLD | NEW |