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
|
// 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 "components/variations/service/google_groups_manager.h"
#include <iterator>
#include <string>
#include <string_view>
#include <vector>
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/string_split.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/sync/service/sync_service.h"
#include "components/variations/pref_names.h"
#include "components/variations/service/google_groups_manager_prefs.h"
#include "components/variations/variations_seed_processor.h"
namespace {
std::string GetGoogleGroupsParam(const base::Feature& feature) {
// The `FeatureParam` is created on the fly because `feature` is only known
// at runtime.
return base::FeatureParam<std::string>(
&feature, variations::internal::kGoogleGroupFeatureParamName, "")
.Get();
}
} // namespace
GoogleGroupsManager::GoogleGroupsManager(
PrefService& target_prefs,
const std::string& key,
PrefService& source_prefs)
: target_prefs_(target_prefs), key_(key), source_prefs_(source_prefs) {
// Register for preference changes.
pref_change_registrar_.Init(&source_prefs_.get());
pref_change_registrar_.Add(
#if BUILDFLAG(IS_CHROMEOS)
variations::kOsDogfoodGroupsSyncPrefName,
#else
variations::kDogfoodGroupsSyncPrefName,
#endif
base::BindRepeating(&GoogleGroupsManager::UpdateGoogleGroups,
base::Unretained(this)));
// Also process the initial value.
UpdateGoogleGroups();
}
GoogleGroupsManager::~GoogleGroupsManager() = default;
// static
void GoogleGroupsManager::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterListPref(
#if BUILDFLAG(IS_CHROMEOS)
variations::kOsDogfoodGroupsSyncPrefName,
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF
#else
variations::kDogfoodGroupsSyncPrefName,
user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF
#endif
);
}
// static
bool GoogleGroupsManager::IsFeatureGroupControlled(
const base::Feature& feature) {
return !GetGoogleGroupsParam(feature).empty();
}
bool GoogleGroupsManager::IsFeatureEnabledForProfile(
const base::Feature& feature) const {
if (!base::FeatureList::IsEnabled(feature)) {
return false;
}
const std::string google_groups_param = GetGoogleGroupsParam(feature);
const std::vector<std::string_view> group_strings = base::SplitStringPiece(
google_groups_param,
variations::internal::kGoogleGroupFeatureParamSeparator,
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (group_strings.empty()) {
// No required Google Group for this experiment.
return true;
}
return std::ranges::any_of(group_strings, [&user_groups = google_group_ids_](
std::string_view group) {
return user_groups.contains(group);
});
}
void GoogleGroupsManager::Shutdown() {
sync_service_observation_.Reset();
}
void GoogleGroupsManager::OnSyncServiceInitialized(
syncer::SyncService* sync_service) {
sync_service_observation_.Observe(sync_service);
// Honor the initial state, in case OnStateChanged() never gets called.
OnStateChanged(sync_service);
}
void GoogleGroupsManager::OnStateChanged(syncer::SyncService* sync) {
switch (sync->GetTransportState()) {
case syncer::SyncService::TransportState::DISABLED:
ClearSigninScopedState();
break;
case syncer::SyncService::TransportState::PAUSED:
case syncer::SyncService::TransportState::START_DEFERRED:
case syncer::SyncService::TransportState::INITIALIZING:
case syncer::SyncService::TransportState::PENDING_DESIRED_CONFIGURATION:
case syncer::SyncService::TransportState::CONFIGURING:
case syncer::SyncService::TransportState::ACTIVE:
break;
}
}
void GoogleGroupsManager::OnSyncShutdown(syncer::SyncService* sync) {
sync_service_observation_.Reset();
}
void GoogleGroupsManager::ClearSigninScopedState() {
source_prefs_->ClearPref(
#if BUILDFLAG(IS_CHROMEOS)
variations::kOsDogfoodGroupsSyncPrefName
#else
variations::kDogfoodGroupsSyncPrefName
#endif
);
// UpdateGoogleGroups() will be called via the PrefChangeRegistrar, and will
// propagate this change to local state.
}
void GoogleGroupsManager::UpdateGoogleGroups() {
// Get the current value of the local state dict.
ScopedDictPrefUpdate target_prefs_update(
&target_prefs_.get(), variations::prefs::kVariationsGoogleGroups);
base::Value::Dict& target_prefs_dict = target_prefs_update.Get();
const base::Value::List& source_list = source_prefs_->GetList(
#if BUILDFLAG(IS_CHROMEOS)
variations::kOsDogfoodGroupsSyncPrefName
#else
variations::kDogfoodGroupsSyncPrefName
#endif
);
base::Value::List groups;
std::vector<std::string> group_ids;
group_ids.reserve(source_list.size());
for (const auto& group_value : source_list) {
const base::Value::Dict* group_dict = group_value.GetIfDict();
if (group_dict == nullptr) {
continue;
}
const std::string* group_str =
group_dict->FindString(variations::kDogfoodGroupsSyncPrefGaiaIdKey);
if ((group_str == nullptr) || group_str->empty()) {
continue;
}
groups.Append(*group_str);
group_ids.push_back(*group_str);
}
target_prefs_dict.Set(key_, std::move(groups));
google_group_ids_ = {std::make_move_iterator(group_ids.begin()),
std::make_move_iterator(group_ids.end())};
}
|