chromium/components/sync/service/sync_prefs.cc

// Copyright 2012 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/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 {

// Historic artifact for payment methods, migrated since 2023/07 to
// prefs::internal::kSyncPayments to make the name consistent with other
// user-selectable types.
constexpr char kObsoleteAutofillWalletImportEnabled[] =;

// Boolean representing whether kObsoleteAutofillWalletImportEnabled was
// migrated to the new pref representing a UserSelectableType. Should be cleaned
// up together with the migration code (after 2024-07).
constexpr char kObsoleteAutofillWalletImportEnabledMigrated[] =;

// State of the migration done by
// MaybeMigratePrefsForSyncToSigninPart1() and
// MaybeMigratePrefsForSyncToSigninPart2(). Should be cleaned up
// after those migration methods are gone.
constexpr char kSyncToSigninMigrationState[] =;

// State of the migration done by MaybeMigrateCustomPassphrasePref().
constexpr char kSyncEncryptionBootstrapTokenPerAccountMigrationDone[] =;

#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
// Pref to record if the one-off MaybeMigrateAutofillToPerAccountPref()
// migration ran.
constexpr char kAutofillPerAccountPrefMigrationDone[] =;
#endif  // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)

constexpr int kNotMigrated =;
constexpr int kMigratedPart1ButNot2 =;
constexpr int kMigratedPart2AndFullyDone =;

// Encodes a protobuf instance of type
// sync_pb::TrustedVaultAutoUpgradeExperimentGroup in a way that can be safely
// stored in prefs, i.e. using base64 encoding.
std::string EncodeTrustedVaultAutoUpgradeExperimentGroupToString(
    const sync_pb::TrustedVaultAutoUpgradeExperimentGroup& group) {}

// Does the opposite of EncodeTrustedVaultAutoUpgradeExperimentGroupToString(),
// i.e. transforms from a string representation to a protobuf instance.
sync_pb::TrustedVaultAutoUpgradeExperimentGroup
DecodeTrustedVaultAutoUpgradeExperimentGroupFromString(
    const std::string& encoded_group) {}

}  // namespace

SyncPrefObserver::~SyncPrefObserver() = default;

SyncPrefs::SyncPrefs(PrefService* pref_service)
    :{}

SyncPrefs::~SyncPrefs() {}

// static
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  // !BUILDFLAG(IS_CHROMEOS_ASH)

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 the type is managed, |sync_all_os_types| is ignored for this type.
    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_);
  {
    // Prevent OnSelectedTypesPrefChanged() from notifying about each of the
    // type changes individually.
    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();
  }
}

// static
const char* SyncPrefs::GetPrefNameForOsTypeForTesting(
    UserSelectableOsType type) {
  return GetPrefNameForOsType(type);
}

// static
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;
}

// static
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  // BUILDFLAG(IS_CHROMEOS_ASH)

#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  // BUILDFLAG(IS_CHROMEOS_LACROS)

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) {}

// static
const char* SyncPrefs::GetPrefNameForTypeForTesting(UserSelectableType type) {}

// static
const char* SyncPrefs::GetPrefNameForType(UserSelectableType type) {}

// static
void SyncPrefs::SetTypeDisabledByPolicy(PrefValueMap* policy_prefs,
                                        UserSelectableType type) {}

// static void
void SyncPrefs::SetTypeDisabledByCustodian(PrefValueMap* supervised_user_prefs,
                                           UserSelectableType type) {}

// static
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  // !BUILDFLAG(IS_CHROMEOS_ASH)

// static
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) {}

// static
void SyncPrefs::MigrateAutofillWalletImportEnabledPref(
    PrefService* pref_service) {}

// static
void SyncPrefs::MigrateGlobalDataTypePrefsToAccount(
    PrefService* pref_service,
    const signin::GaiaIdHash& gaia_id_hash) {}

#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
// static
void SyncPrefs::MaybeMigrateAutofillToPerAccountPref(
    PrefService* pref_service) {}
#endif  // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)

void SyncPrefs::MarkPartialSyncToSigninMigrationFullyDone() {}

void SyncPrefs::SetPasswordSyncAllowed(bool allowed) {}

}  // namespace syncer