[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 | #include "components/variations/active_field_trials.h" |
| 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | #include "base/strings/stringprintf.h" |
| 12 | #include "base/strings/utf_string_conversions.h" |
| 13 | #include "components/variations/metrics_util.h" |
ekaramad | 4808ce31 | 2017-06-07 15:05:56 | [diff] [blame^] | 14 | #include "components/variations/synthetic_trials_active_group_id_provider.h" |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 15 | |
| 16 | namespace variations { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // Populates |name_group_ids| based on |active_groups|. |
| 21 | void GetFieldTrialActiveGroupIdsForActiveGroups( |
| 22 | const base::FieldTrial::ActiveGroups& active_groups, |
| 23 | std::vector<ActiveGroupId>* name_group_ids) { |
| 24 | DCHECK(name_group_ids->empty()); |
| 25 | for (base::FieldTrial::ActiveGroups::const_iterator it = |
| 26 | active_groups.begin(); it != active_groups.end(); ++it) { |
| 27 | name_group_ids->push_back(MakeActiveGroupId(it->trial_name, |
| 28 | it->group_name)); |
| 29 | } |
| 30 | } |
| 31 | |
ekaramad | 4808ce31 | 2017-06-07 15:05:56 | [diff] [blame^] | 32 | void AppendActiveGroupIdsAsStrings( |
| 33 | const std::vector<ActiveGroupId> name_group_ids, |
| 34 | std::vector<std::string>* output) { |
| 35 | for (const auto& active_group_id : name_group_ids) { |
| 36 | output->push_back(base::StringPrintf("%x-%x", active_group_id.name, |
| 37 | active_group_id.group)); |
| 38 | } |
| 39 | } |
| 40 | |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 41 | } // namespace |
| 42 | |
| 43 | ActiveGroupId MakeActiveGroupId(const std::string& trial_name, |
| 44 | const std::string& group_name) { |
| 45 | ActiveGroupId id; |
| 46 | id.name = metrics::HashName(trial_name); |
| 47 | id.group = metrics::HashName(group_name); |
| 48 | return id; |
| 49 | } |
| 50 | |
| 51 | void GetFieldTrialActiveGroupIds( |
| 52 | std::vector<ActiveGroupId>* name_group_ids) { |
| 53 | DCHECK(name_group_ids->empty()); |
| 54 | // A note on thread safety: Since GetActiveFieldTrialGroups() is thread |
| 55 | // safe, and we operate on a separate list of that data, this function is |
| 56 | // technically thread safe as well, with respect to the FieldTrialList data. |
| 57 | base::FieldTrial::ActiveGroups active_groups; |
| 58 | base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups); |
| 59 | GetFieldTrialActiveGroupIdsForActiveGroups(active_groups, |
| 60 | name_group_ids); |
| 61 | } |
| 62 | |
| 63 | void GetFieldTrialActiveGroupIdsAsStrings(std::vector<std::string>* output) { |
| 64 | DCHECK(output->empty()); |
| 65 | std::vector<ActiveGroupId> name_group_ids; |
| 66 | GetFieldTrialActiveGroupIds(&name_group_ids); |
ekaramad | 4808ce31 | 2017-06-07 15:05:56 | [diff] [blame^] | 67 | AppendActiveGroupIdsAsStrings(name_group_ids, output); |
| 68 | } |
| 69 | |
| 70 | void GetSyntheticTrialGroupIdsAsString(std::vector<std::string>* output) { |
| 71 | std::vector<ActiveGroupId> name_group_ids; |
| 72 | SyntheticTrialsActiveGroupIdProvider::GetInstance()->GetActiveGroupIds( |
| 73 | &name_group_ids); |
| 74 | AppendActiveGroupIdsAsStrings(name_group_ids, output); |
[email protected] | b3610d4 | 2014-05-19 18:07:23 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | namespace testing { |
| 78 | |
| 79 | void TestGetFieldTrialActiveGroupIds( |
| 80 | const base::FieldTrial::ActiveGroups& active_groups, |
| 81 | std::vector<ActiveGroupId>* name_group_ids) { |
| 82 | GetFieldTrialActiveGroupIdsForActiveGroups(active_groups, |
| 83 | name_group_ids); |
| 84 | } |
| 85 | |
| 86 | } // namespace testing |
| 87 | |
| 88 | } // namespace variations |