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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
// Copyright 2024 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/field_trial_internals_utils.h"
#include <algorithm>
#include <string>
#include <string_view>
#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram_functions.h"
#include "base/version.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/variations/client_filterable_state.h"
#include "components/variations/hashing.h"
#include "components/variations/pref_names.h"
#include "components/variations/proto/variations_seed.pb.h"
#include "components/variations/study_filtering.h"
#include "components/variations/variations_layers.h"
#include "components/version_info/version_info.h"
namespace variations {
namespace {
using TrialGroup = std::pair<std::string, std::string>;
using variations::HashNameAsHexString;
// Returns whether (`study_name`, `experiment_name`) is found in `studies`.
bool ContainsExperiment(const std::vector<variations::StudyGroupNames>& studies,
std::string_view study_name,
std::string_view experiment_name) {
for (const auto& study : studies) {
if (study.name == study_name) {
if (base::Contains(study.groups, experiment_name)) {
return true;
}
}
}
return false;
}
// Expiration state for field trial overrides. Note that all overrides share the
// same expiration.
struct ExpirationInfo {
// Are field trial overrides expired, either by number of restarts or by
// elapsed time.
bool expired = false;
// Number of Chrome restarts.
int chrome_start_count = 0;
};
ExpirationInfo GetExpirationInfo(PrefService& local_state) {
ExpirationInfo result{.expired = true, .chrome_start_count = 0};
base::Time expiration =
local_state.GetTime(variations::prefs::kVariationsForcedTrialExpiration);
if (expiration.is_null()) {
return result;
}
result.chrome_start_count =
local_state.GetInteger(variations::prefs::kVariationsForcedTrialStarts);
base::TimeDelta remaining_time = expiration - base::Time::Now();
// Is it expired by time? If the expiry time too far in the future, treat
// it as expired as well.
if (remaining_time < base::TimeDelta() ||
remaining_time > kManualForceFieldTrialDuration ||
result.chrome_start_count >=
kChromeStartCountBeforeResetForcedFieldTrials) {
// Clear the time pref, so that GetExpirationInfo can read only one pref
// next time.
local_state.ClearPref(variations::prefs::kVariationsForcedTrialExpiration);
return result;
}
// Not expired by time or by restart count.
result.expired = false;
return result;
}
} // namespace
StudyGroupNames::StudyGroupNames(const Study& study) {
name = study.name();
for (const auto& group : study.experiment()) {
groups.push_back(group.name());
}
}
StudyGroupNames::StudyGroupNames() = default;
StudyGroupNames::~StudyGroupNames() = default;
StudyGroupNames::StudyGroupNames(const StudyGroupNames& src) = default;
StudyGroupNames& StudyGroupNames::operator=(const StudyGroupNames& src) =
default;
std::vector<StudyGroupNames> GetStudiesAvailableToForce(
VariationsSeed seed,
const EntropyProviders& entropy_providers,
const ClientFilterableState& client_filterable_state) {
std::vector<StudyGroupNames> result;
const base::Version& current_version = version_info::GetVersion();
if (!current_version.IsValid()) {
return result;
}
VariationsLayers layers(seed, entropy_providers);
std::vector<variations::ProcessedStudy> filtered_studies =
FilterAndValidateStudies(seed, client_filterable_state, layers);
for (const auto& processed_study : filtered_studies) {
// Remove groups that are forced by flags or features.
Study study = *processed_study.study();
auto exp = study.mutable_experiment()->begin();
while (exp != study.mutable_experiment()->end()) {
if (exp->has_forcing_flag() ||
exp->feature_association().has_forcing_feature_off() ||
exp->feature_association().has_forcing_feature_on()) {
exp = study.mutable_experiment()->erase(exp);
} else {
++exp;
}
}
if (study.experiment_size() > 0) {
result.emplace_back(study);
}
}
return result;
}
void RegisterFieldTrialInternalsPrefs(PrefRegistrySimple& registry) {
registry.RegisterStringPref(prefs::kVariationsForcedFieldTrials,
std::string());
registry.RegisterTimePref(prefs::kVariationsForcedTrialExpiration,
base::Time());
registry.RegisterIntegerPref(prefs::kVariationsForcedTrialStarts, 0);
}
void ForceTrialsAtStartup(PrefService& local_state) {
ExpirationInfo expiration = GetExpirationInfo(local_state);
base::UmaHistogramBoolean(
"Variations.ForcedFieldTrialsAtStartupForInternalsPage",
!expiration.expired);
if (expiration.expired) {
return;
}
local_state.SetInteger(variations::prefs::kVariationsForcedTrialStarts,
expiration.chrome_start_count + 1);
// Write eagerly to avoid a crash loop.
local_state.CommitPendingWrite();
std::string forced_field_trials =
local_state.GetString(variations::prefs::kVariationsForcedFieldTrials);
bool result = base::FieldTrialList::CreateTrialsFromString(
forced_field_trials, /*override_trials=*/true);
if (!result) {
DLOG(WARNING) << "Failed to create field trials from "
"MetricsInternalsForcedFieldTrials: "
<< forced_field_trials;
}
}
bool SetTemporaryTrialOverrides(
PrefService& local_state,
base::span<std::pair<std::string, std::string>> override_groups) {
std::vector<base::FieldTrial::State> states;
for (const auto& g : override_groups) {
base::FieldTrial::State state;
state.trial_name = g.first;
state.group_name = g.second;
states.push_back(std::move(state));
}
std::string force_string =
base::FieldTrial::BuildFieldTrialStateString(states);
std::string prior_force_string =
local_state.GetString(variations::prefs::kVariationsForcedFieldTrials);
ExpirationInfo expiration_info = GetExpirationInfo(local_state);
bool needs_restart = prior_force_string != force_string;
// Update the cached ForcedFieldTrials with the new state
local_state.SetString(variations::prefs::kVariationsForcedFieldTrials,
force_string);
if (force_string.empty()) {
local_state.SetTime(variations::prefs::kVariationsForcedTrialExpiration,
base::Time());
return needs_restart;
}
needs_restart = needs_restart || expiration_info.expired ||
expiration_info.chrome_start_count == 0;
base::Time expiration;
if (!override_groups.empty()) {
expiration = base::Time::Now() + kManualForceFieldTrialDuration;
}
local_state.SetTime(variations::prefs::kVariationsForcedTrialExpiration,
expiration);
// If restart is not required, set number of starts to 1 instead of 0. This
// is a way to remember that restart is required, as after restart this
// value will be incremented.
local_state.SetInteger(variations::prefs::kVariationsForcedTrialStarts,
needs_restart ? 0 : 1);
return needs_restart;
}
base::flat_map<std::string, std::string> RefreshAndGetFieldTrialOverrides(
const std::vector<variations::StudyGroupNames>& available_studies,
PrefService& local_state,
bool& requires_restart) {
requires_restart = false;
ExpirationInfo expiration = GetExpirationInfo(local_state);
if (expiration.expired) {
return {};
}
base::flat_map<std::string, std::string> overrides;
std::vector<base::FieldTrial::State> entries;
base::FieldTrial::ParseFieldTrialsString(
local_state.GetString(variations::prefs::kVariationsForcedFieldTrials),
/*override_trials=*/true, entries);
std::vector<std::pair<std::string, std::string>> active_groups;
for (const base::FieldTrial::State& state : entries) {
if (ContainsExperiment(available_studies, state.trial_name,
state.group_name)) {
overrides[HashNameAsHexString(state.trial_name)] =
HashNameAsHexString(state.group_name);
active_groups.emplace_back(state.trial_name, state.group_name);
}
}
requires_restart = SetTemporaryTrialOverrides(local_state, active_groups);
return overrides;
}
} // namespace variations
|