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