#include "components/sync/service/sync_prefs.h"
#include <utility>
#include "base/base64.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "base/observer_list.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/pref_value_map.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/signin/public/base/gaia_id_hash.h"
#include "components/signin/public/base/signin_pref_names.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/sync/base/features.h"
#include "components/sync/base/passphrase_enums.h"
#include "components/sync/base/pref_names.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/protocol/nigori_specifics.pb.h"
#include "components/sync/service/account_pref_utils.h"
#include "components/sync/service/glue/sync_transport_data_prefs.h"
#include "components/sync/service/sync_feature_status_for_migrations_recorder.h"
namespace syncer {
namespace {
constexpr char kObsoleteAutofillWalletImportEnabled[] = …;
constexpr char kObsoleteAutofillWalletImportEnabledMigrated[] = …;
constexpr char kSyncToSigninMigrationState[] = …;
constexpr char kSyncEncryptionBootstrapTokenPerAccountMigrationDone[] = …;
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
constexpr char kAutofillPerAccountPrefMigrationDone[] = …;
#endif
constexpr int kNotMigrated = …;
constexpr int kMigratedPart1ButNot2 = …;
constexpr int kMigratedPart2AndFullyDone = …;
std::string EncodeTrustedVaultAutoUpgradeExperimentGroupToString(
const sync_pb::TrustedVaultAutoUpgradeExperimentGroup& group) { … }
sync_pb::TrustedVaultAutoUpgradeExperimentGroup
DecodeTrustedVaultAutoUpgradeExperimentGroupFromString(
const std::string& encoded_group) { … }
}
SyncPrefObserver::~SyncPrefObserver() = default;
SyncPrefs::SyncPrefs(PrefService* pref_service)
: … { … }
SyncPrefs::~SyncPrefs() { … }
void SyncPrefs::RegisterProfilePrefs(PrefRegistrySimple* registry) { … }
void SyncPrefs::AddObserver(SyncPrefObserver* sync_pref_observer) { … }
void SyncPrefs::RemoveObserver(SyncPrefObserver* sync_pref_observer) { … }
bool SyncPrefs::IsInitialSyncFeatureSetupComplete() const { … }
bool SyncPrefs::IsExplicitBrowserSignin() const { … }
#if !BUILDFLAG(IS_CHROMEOS_ASH)
void SyncPrefs::SetInitialSyncFeatureSetupComplete() { … }
void SyncPrefs::ClearInitialSyncFeatureSetupComplete() { … }
#endif
bool SyncPrefs::HasKeepEverythingSynced() const { … }
UserSelectableTypeSet SyncPrefs::GetSelectedTypesForAccount(
const signin::GaiaIdHash& gaia_id_hash) const { … }
UserSelectableTypeSet SyncPrefs::GetSelectedTypesForSyncingUser() const { … }
bool SyncPrefs::IsTypeManagedByPolicy(UserSelectableType type) const { … }
bool SyncPrefs::IsTypeManagedByCustodian(UserSelectableType type) const { … }
bool SyncPrefs::IsTypeDisabledByUserForAccount(
const UserSelectableType type,
const signin::GaiaIdHash& gaia_id_hash) { … }
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
int SyncPrefs::GetNumberOfAccountsWithPasswordsSelected() const { … }
#endif
void SyncPrefs::SetSelectedTypesForSyncingUser(
bool keep_everything_synced,
UserSelectableTypeSet registered_types,
UserSelectableTypeSet selected_types) { … }
void SyncPrefs::SetSelectedTypeForAccount(
UserSelectableType type,
bool is_type_on,
const signin::GaiaIdHash& gaia_id_hash) { … }
void SyncPrefs::KeepAccountSettingsPrefsOnlyForUsers(
const std::vector<signin::GaiaIdHash>& available_gaia_ids) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
bool SyncPrefs::IsSyncFeatureDisabledViaDashboard() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetBoolean(prefs::internal::kSyncDisabledViaDashboard);
}
void SyncPrefs::SetSyncFeatureDisabledViaDashboard() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->SetBoolean(prefs::internal::kSyncDisabledViaDashboard, true);
}
void SyncPrefs::ClearSyncFeatureDisabledViaDashboard() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->ClearPref(prefs::internal::kSyncDisabledViaDashboard);
}
bool SyncPrefs::IsSyncAllOsTypesEnabled() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetBoolean(prefs::internal::kSyncAllOsTypes);
}
UserSelectableOsTypeSet SyncPrefs::GetSelectedOsTypes() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UserSelectableOsTypeSet selected_types;
const bool sync_all_os_types = IsSyncAllOsTypesEnabled();
for (UserSelectableOsType type : UserSelectableOsTypeSet::All()) {
const char* pref_name = GetPrefNameForOsType(type);
DCHECK(pref_name);
if (pref_service_->GetBoolean(pref_name) ||
(sync_all_os_types && !IsOsTypeManagedByPolicy(type))) {
selected_types.Put(type);
}
}
return selected_types;
}
bool SyncPrefs::IsOsTypeManagedByPolicy(UserSelectableOsType type) const {
const char* pref_name = GetPrefNameForOsType(type);
CHECK(pref_name);
return pref_service_->IsManagedPreference(pref_name);
}
void SyncPrefs::SetSelectedOsTypes(bool sync_all_os_types,
UserSelectableOsTypeSet registered_types,
UserSelectableOsTypeSet selected_types) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
{
base::AutoReset<bool> batch_update(&batch_updating_selected_types_, true);
pref_service_->SetBoolean(prefs::internal::kSyncAllOsTypes,
sync_all_os_types);
for (UserSelectableOsType type : registered_types) {
const char* pref_name = GetPrefNameForOsType(type);
DCHECK(pref_name);
pref_service_->SetBoolean(pref_name, selected_types.Has(type));
}
}
for (SyncPrefObserver& observer : sync_pref_observers_) {
observer.OnSelectedTypesPrefChange();
}
}
const char* SyncPrefs::GetPrefNameForOsTypeForTesting(
UserSelectableOsType type) {
return GetPrefNameForOsType(type);
}
const char* SyncPrefs::GetPrefNameForOsType(UserSelectableOsType type) {
switch (type) {
case UserSelectableOsType::kOsApps:
return prefs::internal::kSyncOsApps;
case UserSelectableOsType::kOsPreferences:
return prefs::internal::kSyncOsPreferences;
case UserSelectableOsType::kOsWifiConfigurations:
return prefs::internal::kSyncWifiConfigurations;
}
NOTREACHED_IN_MIGRATION();
return nullptr;
}
void SyncPrefs::SetOsTypeDisabledByPolicy(PrefValueMap* policy_prefs,
UserSelectableOsType type) {
const char* pref_name = syncer::SyncPrefs::GetPrefNameForOsType(type);
CHECK(pref_name);
policy_prefs->SetValue(pref_name, base::Value(false));
}
#endif
#if BUILDFLAG(IS_CHROMEOS_LACROS)
bool SyncPrefs::IsAppsSyncEnabledByOs() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetBoolean(prefs::internal::kSyncAppsEnabledByOs);
}
void SyncPrefs::SetAppsSyncEnabledByOs(bool apps_sync_enabled) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->SetBoolean(prefs::internal::kSyncAppsEnabledByOs,
apps_sync_enabled);
}
#endif
bool SyncPrefs::IsSyncClientDisabledByPolicy() const { … }
std::optional<PassphraseType> SyncPrefs::GetCachedPassphraseType() const { … }
void SyncPrefs::SetCachedPassphraseType(PassphraseType passphrase_type) { … }
void SyncPrefs::ClearCachedPassphraseType() { … }
std::optional<sync_pb::TrustedVaultAutoUpgradeExperimentGroup>
SyncPrefs::GetCachedTrustedVaultAutoUpgradeExperimentGroup() const { … }
void SyncPrefs::SetCachedTrustedVaultAutoUpgradeExperimentGroup(
const sync_pb::TrustedVaultAutoUpgradeExperimentGroup& group) { … }
void SyncPrefs::ClearCachedTrustedVaultAutoUpgradeExperimentGroup() { … }
void SyncPrefs::ClearAllEncryptionBootstrapTokens() { … }
std::string SyncPrefs::GetEncryptionBootstrapTokenForAccount(
const signin::GaiaIdHash& gaia_id_hash) const { … }
void SyncPrefs::SetEncryptionBootstrapTokenForAccount(
const std::string& token,
const signin::GaiaIdHash& gaia_id_hash) { … }
void SyncPrefs::ClearEncryptionBootstrapTokenForAccount(
const signin::GaiaIdHash& gaia_id_hash) { … }
const char* SyncPrefs::GetPrefNameForTypeForTesting(UserSelectableType type) { … }
const char* SyncPrefs::GetPrefNameForType(UserSelectableType type) { … }
void SyncPrefs::SetTypeDisabledByPolicy(PrefValueMap* policy_prefs,
UserSelectableType type) { … }
void SyncPrefs::SetTypeDisabledByCustodian(PrefValueMap* supervised_user_prefs,
UserSelectableType type) { … }
bool SyncPrefs::IsTypeSupportedInTransportMode(UserSelectableType type) { … }
void SyncPrefs::OnSyncManagedPrefChanged() { … }
void SyncPrefs::OnSelectedTypesPrefChanged(const std::string& pref_name) { … }
#if !BUILDFLAG(IS_CHROMEOS_ASH)
void SyncPrefs::OnFirstSetupCompletePrefChange() { … }
#endif
void SyncPrefs::RegisterTypeSelectedPref(PrefRegistrySimple* registry,
UserSelectableType type) { … }
bool SyncPrefs::IsLocalSyncEnabled() const { … }
int SyncPrefs::GetPassphrasePromptMutedProductVersion() const { … }
void SyncPrefs::SetPassphrasePromptMutedProductVersion(int major_version) { … }
void SyncPrefs::ClearPassphrasePromptMutedProductVersion() { … }
bool SyncPrefs::MaybeMigratePrefsForSyncToSigninPart1(
SyncAccountState account_state,
const signin::GaiaIdHash& gaia_id_hash) { … }
bool SyncPrefs::MaybeMigratePrefsForSyncToSigninPart2(
const signin::GaiaIdHash& gaia_id_hash,
bool is_using_explicit_passphrase) { … }
void SyncPrefs::MaybeMigrateCustomPassphrasePref(
const signin::GaiaIdHash& gaia_id_hash) { … }
void SyncPrefs::MigrateAutofillWalletImportEnabledPref(
PrefService* pref_service) { … }
void SyncPrefs::MigrateGlobalDataTypePrefsToAccount(
PrefService* pref_service,
const signin::GaiaIdHash& gaia_id_hash) { … }
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
void SyncPrefs::MaybeMigrateAutofillToPerAccountPref(
PrefService* pref_service) { … }
#endif
void SyncPrefs::MarkPartialSyncToSigninMigrationFullyDone() { … }
void SyncPrefs::SetPasswordSyncAllowed(bool allowed) { … }
}