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 #include "components/variations/active_field_trials.h" 6 7 #include <vector> 8 9 #include "base/strings/stringprintf.h" 10 #include "base/strings/utf_string_conversions.h" 11 #include "components/variations/metrics_util.h" 12 13 namespace variations { 14 15 namespace { 16 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)); 26 } 27 } 28 29 } // namespace 30 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; 37 } 38 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); 49 } 50 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)); 58 } 59 } 60 61 namespace testing { 62 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); 68 } 69 70 } // namespace testing 71 72 } // namespace variations 73