[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 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 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 10 | #include <string> |
| 11 | |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 12 | #include "base/metrics/field_trial.h" |
| 13 | |
| 14 | namespace variations { |
| 15 | |
| 16 | // The Unique ID of a trial and its active group, where the name and group |
| 17 | // identifiers are hashes of the trial and group name strings. |
| 18 | struct ActiveGroupId { |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 19 | uint32_t name; |
| 20 | uint32_t group; |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | // Returns an ActiveGroupId struct for the given trial and group names. |
| 24 | ActiveGroupId MakeActiveGroupId(const std::string& trial_name, |
| 25 | const std::string& group_name); |
| 26 | |
| 27 | // We need to supply a Compare class for templates since ActiveGroupId is a |
| 28 | // user-defined type. |
| 29 | struct ActiveGroupIdCompare { |
| 30 | bool operator() (const ActiveGroupId& lhs, const ActiveGroupId& rhs) const { |
| 31 | // The group and name fields are just SHA-1 Hashes, so we just need to treat |
| 32 | // them as IDs and do a less-than comparison. We test group first, since |
| 33 | // name is more likely to collide. |
| 34 | if (lhs.group != rhs.group) |
| 35 | return lhs.group < rhs.group; |
| 36 | return lhs.name < rhs.name; |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | // Fills the supplied vector |name_group_ids| (which must be empty when called) |
| 41 | // with unique ActiveGroupIds for each Field Trial that has a chosen group. |
| 42 | // Field Trials for which a group has not been chosen yet are NOT returned in |
| 43 | // this list. |
| 44 | void GetFieldTrialActiveGroupIds(std::vector<ActiveGroupId>* name_group_ids); |
| 45 | |
| 46 | // Fills the supplied vector |output| (which must be empty when called) with |
| 47 | // unique string representations of ActiveGroupIds for each Field Trial that |
| 48 | // has a chosen group. The strings are formatted as "<TrialName>-<GroupName>", |
| 49 | // with the names as hex strings. Field Trials for which a group has not been |
| 50 | // chosen yet are NOT returned in this list. |
| 51 | void GetFieldTrialActiveGroupIdsAsStrings(std::vector<std::string>* output); |
| 52 | |
| 53 | // Expose some functions for testing. These functions just wrap functionality |
| 54 | // that is implemented above. |
| 55 | namespace testing { |
| 56 | |
| 57 | void TestGetFieldTrialActiveGroupIds( |
| 58 | const base::FieldTrial::ActiveGroups& active_groups, |
| 59 | std::vector<ActiveGroupId>* name_group_ids); |
| 60 | |
| 61 | } // namespace testing |
| 62 | |
| 63 | } // namespace variations |
| 64 | |
| 65 | #endif // COMPONENTS_VARIATIONS_ACTIVE_FIELD_TRIALS_H_ |