1 // Copyright 2014 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 COMPONENTS_VARIATIONS_ACTIVE_FIELD_TRIALS_H_
6 #define COMPONENTS_VARIATIONS_ACTIVE_FIELD_TRIALS_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 
12 #include "base/metrics/field_trial.h"
13 #include "base/strings/string_piece.h"
14 
15 namespace variations {
16 
17 // The Unique ID of a trial and its active group, where the name and group
18 // identifiers are hashes of the trial and group name strings.
19 struct ActiveGroupId {
20   uint32_t name;
21   uint32_t group;
22 };
23 
24 // Returns an ActiveGroupId struct for the given trial and group names.
25 ActiveGroupId MakeActiveGroupId(base::StringPiece trial_name,
26                                 base::StringPiece group_name);
27 
28 // We need to supply a Compare class for templates since ActiveGroupId is a
29 // user-defined type.
30 struct ActiveGroupIdCompare {
operatorActiveGroupIdCompare31   bool operator() (const ActiveGroupId& lhs, const ActiveGroupId& rhs) const {
32     // The group and name fields are just SHA-1 Hashes, so we just need to treat
33     // them as IDs and do a less-than comparison. We test group first, since
34     // name is more likely to collide.
35     if (lhs.group != rhs.group)
36       return lhs.group < rhs.group;
37     return lhs.name < rhs.name;
38   }
39 };
40 
41 // Fills the supplied vector |name_group_ids| (which must be empty when called)
42 // with unique ActiveGroupIds for each Field Trial that has a chosen group.
43 // Field Trials for which a group has not been chosen yet are NOT returned in
44 // this list. Field trial names are suffixed with |suffix| before hashing is
45 // executed.
46 void GetFieldTrialActiveGroupIds(base::StringPiece suffix,
47                                  std::vector<ActiveGroupId>* name_group_ids);
48 
49 // Fills the supplied vector |output| (which must be empty when called) with
50 // unique string representations of ActiveGroupIds for each Field Trial that
51 // has a chosen group. The strings are formatted as "<TrialName>-<GroupName>",
52 // with the names as hex strings. Field Trials for which a group has not been
53 // chosen yet are NOT returned in this list. Field trial names are suffixed with
54 // |suffix| before hashing is executed.
55 void GetFieldTrialActiveGroupIdsAsStrings(base::StringPiece suffix,
56                                           std::vector<std::string>* output);
57 
58 // TODO(rkaplow): Support suffixing for synthetic trials.
59 // Fills the supplied vector |output| (which must be empty when called) with
60 // unique string representations of ActiveGroupIds for each Syntehtic Trial
61 // group. The strings are formatted as "<TrialName>-<GroupName>",
62 // with the names as hex strings. Synthetic Field Trials for which a group
63 // which hasn't been chosen yet are NOT returned in this list.
64 void GetSyntheticTrialGroupIdsAsString(std::vector<std::string>* output);
65 
66 // Sets the version of the seed that the current set of FieldTrials was
67 // generated from.
68 // TODO(crbug/507665): Move this to field_trials_provider once it moves
69 // into components/variations
70 void SetSeedVersion(const std::string& seed_version);
71 
72 // Gets the version of the seed that the current set of FieldTrials was
73 // generated from.
74 // TODO(crbug/507665): Move this to field_trials_provider once it moves
75 // into components/variations
76 const std::string& GetSeedVersion();
77 
78 // Expose some functions for testing. These functions just wrap functionality
79 // that is implemented above.
80 namespace testing {
81 
82 void TestGetFieldTrialActiveGroupIds(
83     base::StringPiece suffix,
84     const base::FieldTrial::ActiveGroups& active_groups,
85     std::vector<ActiveGroupId>* name_group_ids);
86 
87 }  // namespace testing
88 
89 }  // namespace variations
90 
91 #endif  // COMPONENTS_VARIATIONS_ACTIVE_FIELD_TRIALS_H_
92