blob: f18dfe13bbed6f811c28b2004da912b2d47312b6 [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
11#include "base/strings/stringprintf.h"
12#include "base/strings/utf_string_conversions.h"
Alexei Svitkine9de32cb2018-02-06 20:21:2113#include "components/variations/hashing.h"
ekaramad4808ce312017-06-07 15:05:5614#include "components/variations/synthetic_trials_active_group_id_provider.h"
[email protected]b3610d42014-05-19 18:07:2315
16namespace variations {
17
18namespace {
19
Robert Kaplow42e265f72017-07-19 17:43:2220// Populates |name_group_ids| based on |active_groups|. Field trial names are
21// suffixed with |suffix| before hashing is executed.
[email protected]b3610d42014-05-19 18:07:2322void GetFieldTrialActiveGroupIdsForActiveGroups(
Robert Kaplow42e265f72017-07-19 17:43:2223 base::StringPiece suffix,
[email protected]b3610d42014-05-19 18:07:2324 const base::FieldTrial::ActiveGroups& active_groups,
25 std::vector<ActiveGroupId>* name_group_ids) {
26 DCHECK(name_group_ids->empty());
Robert Kaplow42e265f72017-07-19 17:43:2227 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]b3610d42014-05-19 18:07:2331 }
32}
33
ekaramad4808ce312017-06-07 15:05:5634void 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]b3610d42014-05-19 18:07:2343} // namespace
44
bcwhitec1ddec32017-06-29 14:13:0445ActiveGroupId MakeActiveGroupId(base::StringPiece trial_name,
46 base::StringPiece group_name) {
[email protected]b3610d42014-05-19 18:07:2347 ActiveGroupId id;
Alexei Svitkine9de32cb2018-02-06 20:21:2148 id.name = HashName(trial_name);
49 id.group = HashName(group_name);
[email protected]b3610d42014-05-19 18:07:2350 return id;
51}
52
Robert Kaplow42e265f72017-07-19 17:43:2253void GetFieldTrialActiveGroupIds(base::StringPiece suffix,
54 std::vector<ActiveGroupId>* name_group_ids) {
[email protected]b3610d42014-05-19 18:07:2355 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 Kaplow42e265f72017-07-19 17:43:2261 GetFieldTrialActiveGroupIdsForActiveGroups(suffix, active_groups,
[email protected]b3610d42014-05-19 18:07:2362 name_group_ids);
63}
64
Robert Kaplow42e265f72017-07-19 17:43:2265void GetFieldTrialActiveGroupIdsAsStrings(base::StringPiece suffix,
66 std::vector<std::string>* output) {
[email protected]b3610d42014-05-19 18:07:2367 DCHECK(output->empty());
68 std::vector<ActiveGroupId> name_group_ids;
Robert Kaplow42e265f72017-07-19 17:43:2269 GetFieldTrialActiveGroupIds(suffix, &name_group_ids);
ekaramad4808ce312017-06-07 15:05:5670 AppendActiveGroupIdsAsStrings(name_group_ids, output);
71}
72
73void 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]b3610d42014-05-19 18:07:2378}
79
80namespace testing {
81
82void TestGetFieldTrialActiveGroupIds(
Robert Kaplow42e265f72017-07-19 17:43:2283 base::StringPiece suffix,
[email protected]b3610d42014-05-19 18:07:2384 const base::FieldTrial::ActiveGroups& active_groups,
85 std::vector<ActiveGroupId>* name_group_ids) {
Robert Kaplow42e265f72017-07-19 17:43:2286 GetFieldTrialActiveGroupIdsForActiveGroups(suffix, active_groups,
[email protected]b3610d42014-05-19 18:07:2387 name_group_ids);
88}
89
90} // namespace testing
91
92} // namespace variations