Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / variations / active_field_trials.cc
blob7199a91be5183f43f89bbe4758eb5780d303a93b
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.
5 #include "components/variations/active_field_trials.h"
7 #include <vector>
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/variations/metrics_util.h"
13 namespace variations {
15 namespace {
17 // Populates |name_group_ids| based on |active_groups|.
18 void GetFieldTrialActiveGroupIdsForActiveGroups(
19 const base::FieldTrial::ActiveGroups& active_groups,
20 std::vector<ActiveGroupId>* name_group_ids) {
21 DCHECK(name_group_ids->empty());
22 for (base::FieldTrial::ActiveGroups::const_iterator it =
23 active_groups.begin(); it != active_groups.end(); ++it) {
24 name_group_ids->push_back(MakeActiveGroupId(it->trial_name,
25 it->group_name));
29 } // namespace
31 ActiveGroupId MakeActiveGroupId(const std::string& trial_name,
32 const std::string& group_name) {
33 ActiveGroupId id;
34 id.name = metrics::HashName(trial_name);
35 id.group = metrics::HashName(group_name);
36 return id;
39 void GetFieldTrialActiveGroupIds(
40 std::vector<ActiveGroupId>* name_group_ids) {
41 DCHECK(name_group_ids->empty());
42 // A note on thread safety: Since GetActiveFieldTrialGroups() is thread
43 // safe, and we operate on a separate list of that data, this function is
44 // technically thread safe as well, with respect to the FieldTrialList data.
45 base::FieldTrial::ActiveGroups active_groups;
46 base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
47 GetFieldTrialActiveGroupIdsForActiveGroups(active_groups,
48 name_group_ids);
51 void GetFieldTrialActiveGroupIdsAsStrings(std::vector<std::string>* output) {
52 DCHECK(output->empty());
53 std::vector<ActiveGroupId> name_group_ids;
54 GetFieldTrialActiveGroupIds(&name_group_ids);
55 for (size_t i = 0; i < name_group_ids.size(); ++i) {
56 output->push_back(base::StringPrintf(
57 "%x-%x", name_group_ids[i].name, name_group_ids[i].group));
61 namespace testing {
63 void TestGetFieldTrialActiveGroupIds(
64 const base::FieldTrial::ActiveGroups& active_groups,
65 std::vector<ActiveGroupId>* name_group_ids) {
66 GetFieldTrialActiveGroupIdsForActiveGroups(active_groups,
67 name_group_ids);
70 } // namespace testing
72 } // namespace variations