blob: ae0fb9fd3bb780f20062918855ffd995aee21af6 [file] [log] [blame]
[email protected]b3610d42014-05-19 18:07:231// 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
avi5dd91f82015-12-25 22:30:467#include <stddef.h>
8
[email protected]b3610d42014-05-19 18:07:239#include <vector>
10
Steven Holtea70ef7d2018-11-21 20:03:1611#include "base/lazy_instance.h"
[email protected]b3610d42014-05-19 18:07:2312#include "base/strings/stringprintf.h"
13#include "base/strings/utf_string_conversions.h"
Alexei Svitkine9de32cb2018-02-06 20:21:2114#include "components/variations/hashing.h"
ekaramad4808ce312017-06-07 15:05:5615#include "components/variations/synthetic_trials_active_group_id_provider.h"
[email protected]b3610d42014-05-19 18:07:2316
17namespace variations {
18
19namespace {
20
Steven Holtea70ef7d2018-11-21 20:03:1621base::LazyInstance<std::string>::Leaky g_seed_version;
22
Robert Kaplow42e265f72017-07-19 17:43:2223// Populates |name_group_ids| based on |active_groups|. Field trial names are
24// suffixed with |suffix| before hashing is executed.
[email protected]b3610d42014-05-19 18:07:2325void GetFieldTrialActiveGroupIdsForActiveGroups(
Robert Kaplow42e265f72017-07-19 17:43:2226 base::StringPiece suffix,
[email protected]b3610d42014-05-19 18:07:2327 const base::FieldTrial::ActiveGroups& active_groups,
28 std::vector<ActiveGroupId>* name_group_ids) {
29 DCHECK(name_group_ids->empty());
Robert Kaplow42e265f72017-07-19 17:43:2230 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]b3610d42014-05-19 18:07:2334 }
35}
36
ekaramad4808ce312017-06-07 15:05:5637void 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]b3610d42014-05-19 18:07:2346} // namespace
47
bcwhitec1ddec32017-06-29 14:13:0448ActiveGroupId MakeActiveGroupId(base::StringPiece trial_name,
49 base::StringPiece group_name) {
[email protected]b3610d42014-05-19 18:07:2350 ActiveGroupId id;
Alexei Svitkine9de32cb2018-02-06 20:21:2151 id.name = HashName(trial_name);
52 id.group = HashName(group_name);
[email protected]b3610d42014-05-19 18:07:2353 return id;
54}
55
Robert Kaplow42e265f72017-07-19 17:43:2256void GetFieldTrialActiveGroupIds(base::StringPiece suffix,
57 std::vector<ActiveGroupId>* name_group_ids) {
[email protected]b3610d42014-05-19 18:07:2358 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 Kaplow42e265f72017-07-19 17:43:2264 GetFieldTrialActiveGroupIdsForActiveGroups(suffix, active_groups,
[email protected]b3610d42014-05-19 18:07:2365 name_group_ids);
66}
67
Robert Kaplow42e265f72017-07-19 17:43:2268void GetFieldTrialActiveGroupIdsAsStrings(base::StringPiece suffix,
69 std::vector<std::string>* output) {
[email protected]b3610d42014-05-19 18:07:2370 DCHECK(output->empty());
71 std::vector<ActiveGroupId> name_group_ids;
Robert Kaplow42e265f72017-07-19 17:43:2272 GetFieldTrialActiveGroupIds(suffix, &name_group_ids);
ekaramad4808ce312017-06-07 15:05:5673 AppendActiveGroupIdsAsStrings(name_group_ids, output);
74}
75
76void 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]b3610d42014-05-19 18:07:2381}
82
Steven Holtea70ef7d2018-11-21 20:03:1683void SetSeedVersion(const std::string& seed_version) {
84 g_seed_version.Get() = seed_version;
85}
86
87const std::string& GetSeedVersion() {
88 return g_seed_version.Get();
89}
90
[email protected]b3610d42014-05-19 18:07:2391namespace testing {
92
93void TestGetFieldTrialActiveGroupIds(
Robert Kaplow42e265f72017-07-19 17:43:2294 base::StringPiece suffix,
[email protected]b3610d42014-05-19 18:07:2395 const base::FieldTrial::ActiveGroups& active_groups,
96 std::vector<ActiveGroupId>* name_group_ids) {
Robert Kaplow42e265f72017-07-19 17:43:2297 GetFieldTrialActiveGroupIdsForActiveGroups(suffix, active_groups,
[email protected]b3610d42014-05-19 18:07:2398 name_group_ids);
99}
100
101} // namespace testing
102
103} // namespace variations