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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/readaloud/android/synthetic_trial.h"
#include <string>
#include <string_view>
#include <vector>
#include "base/feature_list.h"
#include "base/metrics/field_trial.h"
#include "base/strings/strcat.h"
#include "base/strings/string_split.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
namespace readaloud {
namespace {
inline constexpr char kSeparator[] = "|||";
bool GetFeatureNameFromKey(const std::string& key, std::string_view* out) {
auto split_key = base::SplitStringPieceUsingSubstr(
key, kSeparator, base::WhitespaceHandling::KEEP_WHITESPACE,
base::SplitResult::SPLIT_WANT_NONEMPTY);
if (split_key.size() != 2) {
return false;
}
*out = split_key[0];
return true;
}
} // namespace
// static
void SyntheticTrial::ClearStalePrefs() {
PrefService* prefs = g_browser_process->local_state();
std::vector<std::string> keys_to_clear;
for (auto [key, trial_name] :
prefs->GetDict(prefs::kReadAloudSyntheticTrials)) {
std::string_view feature;
if (!GetFeatureNameFromKey(key, &feature)) {
continue;
}
base::FieldTrial* trial =
base::FeatureList::GetInstance()->GetAssociatedFieldTrialByFeatureName(
feature);
if (!trial || trial->trial_name() != trial_name) {
keys_to_clear.emplace_back(key);
}
}
{
ScopedDictPrefUpdate update(prefs, prefs::kReadAloudSyntheticTrials);
for (const std::string& key : keys_to_clear) {
update->Remove(key);
}
}
}
// static
std::unique_ptr<SyntheticTrial> SyntheticTrial::Create(
const std::string& feature_name,
const std::string& trial_suffix) {
if (feature_name.find(kSeparator) != std::string::npos ||
trial_suffix.find(kSeparator) != std::string::npos) {
return nullptr;
}
base::FieldTrial* trial =
base::FeatureList::GetInstance()->GetAssociatedFieldTrialByFeatureName(
feature_name);
if (!trial) {
return nullptr;
}
return std::make_unique<SyntheticTrial>(feature_name, trial_suffix, trial);
}
SyntheticTrial::SyntheticTrial(const std::string& feature_name,
const std::string& trial_suffix,
base::FieldTrial* base_trial)
: feature_name_(feature_name),
suffix_(trial_suffix),
base_trial_(base_trial) {
DCHECK(!suffix_.empty());
// If this synthetic trial was previously active, reactivate now.
const std::string* stored_base_trial_name =
prefs()->GetDict(prefs::kReadAloudSyntheticTrials).FindString(pref_key());
if (stored_base_trial_name &&
base_trial_->trial_name() == *stored_base_trial_name) {
Activate();
}
}
void SyntheticTrial::Activate() {
if (synthetic_trial_active_) {
return;
}
const std::string synthetic_trial_name =
base::StrCat({base_trial_->trial_name(), suffix_});
ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial(
synthetic_trial_name, base_trial_->GetGroupNameWithoutActivation(),
variations::SyntheticTrialAnnotationMode::kCurrentLog);
synthetic_trial_active_ = true;
{
ScopedDictPrefUpdate(prefs(), prefs::kReadAloudSyntheticTrials)
->Set(pref_key(), base_trial_->trial_name());
}
}
std::string SyntheticTrial::pref_key() const {
return base::StrCat({feature_name_, kSeparator, suffix_});
}
PrefService* SyntheticTrial::prefs() {
return g_browser_process->local_state();
}
} // namespace readaloud
|