summaryrefslogtreecommitdiff
path: root/chromium/components/variations/active_field_trials.cc
blob: f4c612b9e1a9a1ec4e5f864059d0383895bee1b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/variations/active_field_trials.h"

#include <stddef.h>

#include <vector>

#include "base/containers/contains.h"
#include "base/lazy_instance.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "components/variations/hashing.h"
#include "components/variations/synthetic_trials_active_group_id_provider.h"

namespace variations {

namespace {

base::LazyInstance<std::string>::Leaky g_seed_version;

void AppendActiveGroupIdsAsStrings(
    const std::vector<ActiveGroupId> name_group_ids,
    std::vector<std::string>* output) {
  for (const auto& active_group_id : name_group_ids) {
    output->push_back(base::StringPrintf("%x-%x", active_group_id.name,
                                         active_group_id.group));
  }
}

}  // namespace

ActiveGroupId MakeActiveGroupId(base::StringPiece trial_name,
                                base::StringPiece group_name) {
  ActiveGroupId id;
  id.name = HashName(trial_name);
  id.group = HashName(group_name);
  return id;
}

void GetFieldTrialActiveGroupIdsForActiveGroups(
    base::StringPiece suffix,
    const base::FieldTrial::ActiveGroups& active_groups,
    std::vector<ActiveGroupId>* name_group_ids) {
  DCHECK(name_group_ids->empty());
  for (const auto& active_group : active_groups) {
    name_group_ids->push_back(
        MakeActiveGroupId(active_group.trial_name + std::string(suffix),
                          active_group.group_name + std::string(suffix)));
  }
}

void GetFieldTrialActiveGroupIds(base::StringPiece suffix,
                                 std::vector<ActiveGroupId>* name_group_ids) {
  DCHECK(name_group_ids->empty());
  // A note on thread safety: Since GetActiveFieldTrialGroups() is thread
  // safe, and we operate on a separate list of that data, this function is
  // technically thread safe as well, with respect to the FieldTrialList data.
  base::FieldTrial::ActiveGroups active_groups;
  base::FieldTrialList::GetActiveFieldTrialGroups(&active_groups);
  GetFieldTrialActiveGroupIdsForActiveGroups(suffix, active_groups,
                                             name_group_ids);
}

void GetFieldTrialActiveGroupIdsAsStrings(base::StringPiece suffix,
                                          std::vector<std::string>* output) {
  DCHECK(output->empty());
  std::vector<ActiveGroupId> name_group_ids;
  GetFieldTrialActiveGroupIds(suffix, &name_group_ids);
  AppendActiveGroupIdsAsStrings(name_group_ids, output);
}

void GetSyntheticTrialGroupIdsAsString(std::vector<std::string>* output) {
  std::vector<ActiveGroupId> name_group_ids;
  SyntheticTrialsActiveGroupIdProvider::GetInstance()->GetActiveGroupIds(
      &name_group_ids);
  AppendActiveGroupIdsAsStrings(name_group_ids, output);
}

bool HasSyntheticTrial(const std::string& trial_name) {
  std::vector<std::string> synthetic_trials;
  variations::GetSyntheticTrialGroupIdsAsString(&synthetic_trials);
  std::string trial_hash =
      base::StringPrintf("%x", variations::HashName(trial_name));
  return base::ranges::any_of(synthetic_trials, [trial_hash](
                                                    const auto& trial) {
    return base::StartsWith(trial, trial_hash, base::CompareCase::SENSITIVE);
  });
}

bool IsInSyntheticTrialGroup(const std::string& trial_name,
                             const std::string& trial_group) {
  std::vector<std::string> synthetic_trials;
  GetSyntheticTrialGroupIdsAsString(&synthetic_trials);
  return base::Contains(
      synthetic_trials,
      base::StringPrintf("%x-%x", HashName(trial_name), HashName(trial_group)));
}

void SetSeedVersion(const std::string& seed_version) {
  g_seed_version.Get() = seed_version;
}

const std::string& GetSeedVersion() {
  return g_seed_version.Get();
}

namespace testing {

void TestGetFieldTrialActiveGroupIds(
    base::StringPiece suffix,
    const base::FieldTrial::ActiveGroups& active_groups,
    std::vector<ActiveGroupId>* name_group_ids) {
  GetFieldTrialActiveGroupIdsForActiveGroups(suffix, active_groups,
                                             name_group_ids);
}

}  // namespace testing

}  // namespace variations