chromium/components/sync/service/sync_service_impl.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_service_impl.h"

#include <cstddef>
#include <utility>

#include "base/barrier_callback.h"
#include "base/check_is_test.h"
#include "base/command_line.h"
#include "base/containers/flat_set.h"
#include "base/containers/to_vector.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/observer_list.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "components/signin/public/base/gaia_id_hash.h"
#include "components/signin/public/base/signin_metrics.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/accounts_in_cookie_jar_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_utils.h"
#include "components/signin/public/identity_manager/primary_account_mutator.h"
#include "components/sync/base/command_line_switches.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/features.h"
#include "components/sync/base/stop_source.h"
#include "components/sync/base/sync_util.h"
#include "components/sync/engine/configure_reason.h"
#include "components/sync/engine/engine_components_factory_impl.h"
#include "components/sync/engine/net/http_bridge.h"
#include "components/sync/engine/net/http_post_provider_factory.h"
#include "components/sync/engine/shutdown_reason.h"
#include "components/sync/engine/sync_encryption_handler.h"
#include "components/sync/invalidations/sync_invalidations_service.h"
#include "components/sync/model/sync_error.h"
#include "components/sync/model/type_entities_count.h"
#include "components/sync/service/backend_migrator.h"
#include "components/sync/service/configure_context.h"
#include "components/sync/service/data_type_manager_impl.h"
#include "components/sync/service/get_types_with_unsynced_data_request_barrier.h"
#include "components/sync/service/local_data_description.h"
#include "components/sync/service/sync_auth_manager.h"
#include "components/sync/service/sync_engine_factory.h"
#include "components/sync/service/sync_feature_status_for_migrations_recorder.h"
#include "components/sync/service/sync_prefs.h"
#include "components/sync/service/sync_prefs_policy_handler.h"
#include "components/sync/service/sync_service_utils.h"
#include "components/sync/service/trusted_vault_histograms.h"
#include "components/sync/service/trusted_vault_synthetic_field_trial.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "components/sync/android/jni_headers/ExplicitPassphrasePlatformClient_jni.h"
#include "components/sync/android/sync_service_android_bridge.h"
#include "components/sync/engine/nigori/nigori.h"
#include "components/sync/protocol/nigori_specifics.pb.h"
#endif  // BUILDFLAG(IS_ANDROID)

namespace syncer {

namespace {

BASE_FEATURE();

BASE_FEATURE();

#if BUILDFLAG(IS_ANDROID)
constexpr int kMinGmsVersionCodeWithCustomPassphraseApi = 235204000;

// Keep in sync with the corresponding string in
// ExplicitPassphrasePlatformClientTest.java
constexpr char kIgnoreMinGmsVersionWithPassphraseSupportForTest[] =
    "ignore-min-gms-version-with-passphrase-support-for-test";
#endif  // BUILDFLAG(IS_ANDROID)

// The initial state of sync, for the Sync.InitialState2 histogram. Even if
// this value indicates that sync (the feature or the transport) can start, the
// startup might fail for reasons such as network issues, or the version of
// Chrome being too old.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// LINT.IfChange(SyncInitialState)
enum SyncInitialState {};
// LINT.ThenChange(/tools/metrics/histograms/metadata/sync/enums.xml:SyncInitialState)

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class DownloadStatusWaitingForUpdatesReason {};

void RecordSyncInitialState(SyncService::DisableReasonSet disable_reasons,
                            bool is_sync_feature_requested,
                            bool initial_sync_feature_setup_complete) {}

EngineComponentsFactory::Switches EngineSwitchesFromCommandLine() {}

std::unique_ptr<HttpPostProviderFactory> CreateHttpBridgeFactory(
    const std::string& user_agent,
    std::unique_ptr<network::PendingSharedURLLoaderFactory>
        pending_url_loader_factory) {}

base::TimeDelta GetDeferredInitDelay() {}

void MaybeClearAccountKeyedPreferences(
    signin::IdentityManager* identity_manager,
    const signin::AccountsInCookieJarInfo& accounts_in_cookie_jar_info,
    SyncUserSettingsImpl& user_settings) {}

std::map<DataType, LocalDataDescription> JoinAllTypesAndLocalDataDescriptions(
    const std::vector<std::pair<DataType, LocalDataDescription>>& pairs) {}

std::pair<DataType, LocalDataDescription> JoinTypeAndLocalDataDescription(
    DataType type,
    LocalDataDescription description) {}

}  // namespace

SyncServiceImpl::InitParams::InitParams() = default;
SyncServiceImpl::InitParams::InitParams(InitParams&& other) = default;
SyncServiceImpl::InitParams::~InitParams() = default;

SyncServiceImpl::SyncServiceImpl(InitParams init_params)
    :{}

void SyncServiceImpl::RegisterTrustedVaultSyntheticFieldTrialsIfNecessary() {}

SyncServiceImpl::~SyncServiceImpl() {}

void SyncServiceImpl::Initialize(DataTypeController::TypeVector controllers) {}

void SyncServiceImpl::StartSyncingWithServer() {}

DataTypeSet SyncServiceImpl::GetRegisteredDataTypesForTest() const {}

bool SyncServiceImpl::HasAnyDatatypeErrorForTest(DataTypeSet types) const {}

void SyncServiceImpl::GetThrottledDataTypesForTest(
    base::OnceCallback<void(DataTypeSet)> cb) const {}

// static
ShutdownReason SyncServiceImpl::ShutdownReasonForResetEngineReason(
    ResetEngineReason reset_reason) {}

bool SyncServiceImpl::ShouldClearTransportDataForAccount(
    ResetEngineReason reset_reason) {}

void SyncServiceImpl::AccountStateChanged() {}

void SyncServiceImpl::CredentialsChanged() {}

bool SyncServiceImpl::IsEngineAllowedToRun() const {}

void SyncServiceImpl::OnProtocolEvent(const ProtocolEvent& event) {}

void SyncServiceImpl::OnDataTypeRequestsSyncStartup(DataType type) {}

void SyncServiceImpl::TryStart() {}

void SyncServiceImpl::TryStartImpl() {}

void SyncServiceImpl::Shutdown() {}

std::unique_ptr<SyncEngine> SyncServiceImpl::ResetEngine(
    ResetEngineReason reset_reason) {}

#if BUILDFLAG(IS_ANDROID)
base::android::ScopedJavaLocalRef<jobject> SyncServiceImpl::GetJavaObject() {
  if (!sync_service_android_) {
    sync_service_android_ = std::make_unique<SyncServiceAndroidBridge>(this);
  }
  return sync_service_android_->GetJavaObject();
}
#endif  // BUILDFLAG(IS_ANDROID)

void SyncServiceImpl::SetSyncFeatureRequested() {}

SyncUserSettings* SyncServiceImpl::GetUserSettings() {}

const SyncUserSettings* SyncServiceImpl::GetUserSettings() const {}

SyncService::DisableReasonSet SyncServiceImpl::GetDisableReasons() const {}

SyncService::TransportState SyncServiceImpl::GetTransportState() const {}

SyncService::UserActionableError SyncServiceImpl::GetUserActionableError()
    const {}

void SyncServiceImpl::NotifyObservers() {}

void SyncServiceImpl::NotifySyncCycleCompleted() {}

void SyncServiceImpl::NotifyShutdown() {}

void SyncServiceImpl::ClearUnrecoverableError() {}

void SyncServiceImpl::OnUnrecoverableErrorImpl(
    const base::Location& from_here,
    const std::string& message,
    UnrecoverableErrorReason reason) {}

void SyncServiceImpl::DataTypePreconditionChanged(DataType type) {}

void SyncServiceImpl::OnEngineInitialized(bool success,
                                          bool is_first_time_sync_configure) {}

void SyncServiceImpl::OnSyncCycleCompleted(const SyncCycleSnapshot& snapshot) {}

void SyncServiceImpl::OnConnectionStatusChange(ConnectionStatus status) {}

void SyncServiceImpl::OnMigrationNeededForTypes(DataTypeSet types) {}

void SyncServiceImpl::OnActionableProtocolError(
    const SyncProtocolError& error) {}

void SyncServiceImpl::OnBackedOffTypesChanged() {}

void SyncServiceImpl::OnInvalidationStatusChanged() {}

void SyncServiceImpl::OnNewInvalidatedDataTypes() {}

void SyncServiceImpl::OnConfigureDone(
    const DataTypeManager::ConfigureResult& result) {}

void SyncServiceImpl::OnConfigureStart() {}

void SyncServiceImpl::CryptoStateChanged() {}

void SyncServiceImpl::CryptoRequiredUserActionChanged() {}

void SyncServiceImpl::MaybeRecordTrustedVaultHistograms() {}

void SyncServiceImpl::ReconfigureDataTypesDueToCrypto() {}

void SyncServiceImpl::PassphraseTypeChanged(PassphraseType passphrase_type) {}

std::optional<PassphraseType> SyncServiceImpl::GetPassphraseType() const {}

void SyncServiceImpl::SetEncryptionBootstrapToken(
    const std::string& bootstrap_token) {}

std::string SyncServiceImpl::GetEncryptionBootstrapToken() const {}

bool SyncServiceImpl::IsCustomPassphraseAllowed() const {}

SyncPrefs::SyncAccountState SyncServiceImpl::GetSyncAccountStateForPrefs()
    const {}

CoreAccountInfo SyncServiceImpl::GetSyncAccountInfoForPrefs() const {}

bool SyncServiceImpl::IsSetupInProgress() const {}

bool SyncServiceImpl::QueryDetailedSyncStatusForDebugging(
    SyncStatus* result) const {}

GoogleServiceAuthError SyncServiceImpl::GetAuthError() const {}

base::Time SyncServiceImpl::GetAuthErrorTime() const {}

bool SyncServiceImpl::RequiresClientUpgrade() const {}

bool SyncServiceImpl::CanConfigureDataTypes(
    bool bypass_setup_in_progress_check) const {}

std::unique_ptr<SyncSetupInProgressHandle>
SyncServiceImpl::GetSetupInProgressHandle() {}

bool SyncServiceImpl::IsLocalSyncEnabled() const {}

void SyncServiceImpl::TriggerRefresh(const DataTypeSet& types) {}

bool SyncServiceImpl::IsSignedIn() const {}

base::Time SyncServiceImpl::GetLastSyncedTimeForDebugging() const {}

void SyncServiceImpl::OnSelectedTypesPrefChange() {}

SyncClient* SyncServiceImpl::GetSyncClientForTest() {}

void SyncServiceImpl::ReportDataTypeErrorForTest(DataType type) {}

void SyncServiceImpl::AddObserver(SyncServiceObserver* observer) {}

void SyncServiceImpl::RemoveObserver(SyncServiceObserver* observer) {}

bool SyncServiceImpl::HasObserver(const SyncServiceObserver* observer) const {}

DataTypeSet SyncServiceImpl::GetPreferredDataTypes() const {}

DataTypeSet SyncServiceImpl::GetActiveDataTypes() const {}

DataTypeSet SyncServiceImpl::GetTypesWithPendingDownloadForInitialSync() const {}

void SyncServiceImpl::ConfigureDataTypeManager(ConfigureReason reason) {}

bool SyncServiceImpl::UseTransportOnlyMode() const {}

void SyncServiceImpl::UpdateDataTypesForInvalidations() {}

SyncCycleSnapshot SyncServiceImpl::GetLastCycleSnapshotForDebugging() const {}

void SyncServiceImpl::HasUnsyncedItemsForTest(
    base::OnceCallback<void(bool)> cb) const {}

BackendMigrator* SyncServiceImpl::GetBackendMigratorForTest() {}

base::Value::List SyncServiceImpl::GetTypeStatusMapForDebugging() const {}

void SyncServiceImpl::GetEntityCountsForDebugging(
    base::RepeatingCallback<void(const TypeEntitiesCount&)> callback) const {}

void SyncServiceImpl::OnSyncManagedPrefChange(bool is_sync_managed) {}

#if !BUILDFLAG(IS_CHROMEOS_ASH)
void SyncServiceImpl::OnFirstSetupCompletePrefChange(
    bool is_initial_sync_feature_setup_complete) {}
#endif  // !BUILDFLAG(IS_CHROMEOS_ASH)

void SyncServiceImpl::OnAccountsCookieDeletedByUserAction() {}

void SyncServiceImpl::OnAccountsInCookieUpdated(
    const signin::AccountsInCookieJarInfo& accounts_in_cookie_jar_info,
    const GoogleServiceAuthError& error) {}

void SyncServiceImpl::OnPrimaryAccountChanged(
    const signin::PrimaryAccountChangeEvent& event_details) {}

void SyncServiceImpl::OnAccountsInCookieUpdatedWithCallback(
    const signin::AccountsInCookieJarInfo& accounts_in_cookie_jar_info,
    base::OnceClosure callback) {}

bool SyncServiceImpl::HasCookieJarMismatch(
    const std::vector<gaia::ListedAccount>& cookie_jar_accounts) {}

void SyncServiceImpl::AddProtocolEventObserver(
    ProtocolEventObserver* observer) {}

void SyncServiceImpl::RemoveProtocolEventObserver(
    ProtocolEventObserver* observer) {}

namespace {

class GetAllNodesRequestHelper
    : public base::RefCountedThreadSafe<GetAllNodesRequestHelper> {};

GetAllNodesRequestHelper::GetAllNodesRequestHelper(
    DataTypeSet requested_types,
    base::OnceCallback<void(base::Value::List)> callback)
    :{}

GetAllNodesRequestHelper::~GetAllNodesRequestHelper() {}

// Called when the set of nodes for a type has been returned.
// Only return one type of nodes each time.
void GetAllNodesRequestHelper::OnReceivedNodesForType(
    const DataType type,
    base::Value::List node_list) {}

}  // namespace

void SyncServiceImpl::GetAllNodesForDebugging(
    base::OnceCallback<void(base::Value::List)> callback) {}

SyncService::DataTypeDownloadStatus SyncServiceImpl::GetDownloadStatusFor(
    DataType type) const {}

void SyncServiceImpl::OnPasswordSyncAllowedChanged() {}

void SyncServiceImpl::CacheTrustedVaultDebugInfoToPrefsFromEngine() {}

CoreAccountInfo SyncServiceImpl::GetAccountInfo() const {}

bool SyncServiceImpl::HasSyncConsent() const {}

void SyncServiceImpl::SetInvalidationsForSessionsEnabled(bool enabled) {}

void SyncServiceImpl::SendExplicitPassphraseToPlatformClient() {}

void SyncServiceImpl::StopAndClear(ResetEngineReason reset_engine_reason) {}

void SyncServiceImpl::ReconfigureDatatypeManager(
    bool bypass_setup_in_progress_check) {}

bool SyncServiceImpl::IsRetryingAccessTokenFetchForTest() const {}

std::string SyncServiceImpl::GetAccessTokenForTest() const {}

SyncTokenStatus SyncServiceImpl::GetSyncTokenStatusForDebugging() const {}

void SyncServiceImpl::OverrideNetworkForTest(
    const CreateHttpPostProviderFactory& create_http_post_provider_factory_cb) {}

SyncEncryptionHandler::Observer*
SyncServiceImpl::GetEncryptionObserverForTest() {}

void SyncServiceImpl::RemoveClientFromServer() const {}

void SyncServiceImpl::RecordMemoryUsageAndCountsHistograms() {}

const GURL& SyncServiceImpl::GetSyncServiceUrlForDebugging() const {}

std::string SyncServiceImpl::GetUnrecoverableErrorMessageForDebugging() const {}

base::Location SyncServiceImpl::GetUnrecoverableErrorLocationForDebugging()
    const {}

void SyncServiceImpl::OnSetupInProgressHandleDestroyed() {}

void SyncServiceImpl::GetTypesWithUnsyncedData(
    DataTypeSet requested_types,
    base::OnceCallback<void(DataTypeSet)> callback) const {}

void SyncServiceImpl::GetLocalDataDescriptions(
    DataTypeSet types,
    base::OnceCallback<void(std::map<DataType, LocalDataDescription>)>
        callback) {}

void SyncServiceImpl::GetLocalDataDescriptionsImpl(
    DataTypeSet types,
    base::OnceCallback<void(std::map<DataType, LocalDataDescription>)>
        callback) {}

void SyncServiceImpl::TriggerLocalDataMigration(DataTypeSet types) {}

DataTypeSet SyncServiceImpl::GetDataTypesWithLocalDataBatchUploader() const {}

}  // namespace syncer