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