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