blob: b7425510a10473091d2639884e72f0e97857e7ae [file] [log] [blame]
[email protected]b3610d42014-05-19 18:07:231// 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
avi5dd91f82015-12-25 22:30:468#include <stdint.h>
9
[email protected]b3610d42014-05-19 18:07:2310#include <string>
11
[email protected]b3610d42014-05-19 18:07:2312#include "base/metrics/field_trial.h"
13
14namespace 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.
18struct ActiveGroupId {
avi5dd91f82015-12-25 22:30:4619 uint32_t name;
20 uint32_t group;
[email protected]b3610d42014-05-19 18:07:2321};
22
23// Returns an ActiveGroupId struct for the given trial and group names.
24ActiveGroupId 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.
29struct 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.
44void 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.
51void GetFieldTrialActiveGroupIdsAsStrings(std::vector<std::string>* output);
52
53// Expose some functions for testing. These functions just wrap functionality
54// that is implemented above.
55namespace testing {
56
57void 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_