| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/sync/driver/sync_service_base.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/path_service.h" |
| 14 #include "components/invalidation/public/invalidation_service.h" |
| 15 #include "components/sync/base/report_unrecoverable_error.h" |
| 16 #include "components/sync/device_info/local_device_info_provider.h" |
| 17 #include "components/sync/driver/sync_driver_switches.h" |
| 18 #include "components/sync/engine/engine_components_factory_impl.h" |
| 19 |
| 20 namespace syncer { |
| 21 |
| 22 namespace { |
| 23 |
| 24 const base::FilePath::CharType kSyncDataFolderName[] = |
| 25 FILE_PATH_LITERAL("Sync Data"); |
| 26 |
| 27 #if defined(OS_WIN) |
| 28 const base::FilePath::CharType kLoopbackServerBackendFilename[] = |
| 29 FILE_PATH_LITERAL("profile.pb"); |
| 30 #endif |
| 31 |
| 32 EngineComponentsFactory::Switches EngineSwitchesFromCommandLine() { |
| 33 EngineComponentsFactory::Switches factory_switches = { |
| 34 EngineComponentsFactory::ENCRYPTION_KEYSTORE, |
| 35 EngineComponentsFactory::BACKOFF_NORMAL}; |
| 36 |
| 37 base::CommandLine* cl = base::CommandLine::ForCurrentProcess(); |
| 38 if (cl->HasSwitch(switches::kSyncShortInitialRetryOverride)) { |
| 39 factory_switches.backoff_override = |
| 40 EngineComponentsFactory::BACKOFF_SHORT_INITIAL_RETRY_OVERRIDE; |
| 41 } |
| 42 if (cl->HasSwitch(switches::kSyncEnableGetUpdateAvoidance)) { |
| 43 factory_switches.pre_commit_updates_policy = |
| 44 EngineComponentsFactory::FORCE_ENABLE_PRE_COMMIT_UPDATE_AVOIDANCE; |
| 45 } |
| 46 if (cl->HasSwitch(switches::kSyncShortNudgeDelayForTest)) { |
| 47 factory_switches.nudge_delay = |
| 48 EngineComponentsFactory::NudgeDelay::SHORT_NUDGE_DELAY; |
| 49 } |
| 50 return factory_switches; |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 55 SyncServiceBase::SyncServiceBase(std::unique_ptr<SyncClient> sync_client, |
| 56 std::unique_ptr<SigninManagerWrapper> signin, |
| 57 const version_info::Channel& channel, |
| 58 const base::FilePath& base_directory, |
| 59 const std::string& debug_identifier) |
| 60 : sync_client_(std::move(sync_client)), |
| 61 signin_(std::move(signin)), |
| 62 channel_(channel), |
| 63 base_directory_(base_directory), |
| 64 directory_path_( |
| 65 base_directory_.Append(base::FilePath(kSyncDataFolderName))), |
| 66 debug_identifier_(debug_identifier), |
| 67 sync_prefs_(sync_client_->GetPrefService()) {} |
| 68 |
| 69 SyncServiceBase::~SyncServiceBase() = default; |
| 70 |
| 71 void SyncServiceBase::InitializeEngine() { |
| 72 DCHECK(engine_); |
| 73 |
| 74 if (!sync_thread_) { |
| 75 sync_thread_ = base::MakeUnique<base::Thread>("Chrome_SyncThread"); |
| 76 base::Thread::Options options; |
| 77 options.timer_slack = base::TIMER_SLACK_MAXIMUM; |
| 78 CHECK(sync_thread_->StartWithOptions(options)); |
| 79 } |
| 80 |
| 81 SyncEngine::InitParams params; |
| 82 params.sync_task_runner = sync_thread_->task_runner(); |
| 83 params.host = this; |
| 84 params.registrar = base::MakeUnique<SyncBackendRegistrar>( |
| 85 debug_identifier_, base::Bind(&SyncClient::CreateModelWorkerForGroup, |
| 86 base::Unretained(sync_client_.get()))); |
| 87 params.extensions_activity = sync_client_->GetExtensionsActivity(); |
| 88 params.event_handler = GetJsEventHandler(); |
| 89 params.service_url = sync_service_url(); |
| 90 params.sync_user_agent = GetLocalDeviceInfoProvider()->GetSyncUserAgent(); |
| 91 params.http_factory_getter = MakeHttpPostProviderFactoryGetter(); |
| 92 params.credentials = GetCredentials(); |
| 93 invalidation::InvalidationService* invalidator = |
| 94 sync_client_->GetInvalidationService(); |
| 95 params.invalidator_client_id = |
| 96 invalidator ? invalidator->GetInvalidatorClientId() : "", |
| 97 params.sync_manager_factory = base::MakeUnique<SyncManagerFactory>(); |
| 98 // The first time we start up the engine we want to ensure we have a clean |
| 99 // directory, so delete any old one that might be there. |
| 100 params.delete_sync_data_folder = !IsFirstSetupComplete(); |
| 101 params.enable_local_sync_backend = |
| 102 GetLocalSyncConfig(¶ms.local_sync_backend_folder); |
| 103 params.restored_key_for_bootstrapping = |
| 104 sync_prefs_.GetEncryptionBootstrapToken(); |
| 105 params.restored_keystore_key_for_bootstrapping = |
| 106 sync_prefs_.GetKeystoreEncryptionBootstrapToken(); |
| 107 params.engine_components_factory = |
| 108 base::MakeUnique<EngineComponentsFactoryImpl>( |
| 109 EngineSwitchesFromCommandLine()); |
| 110 params.unrecoverable_error_handler = GetUnrecoverableErrorHandler(); |
| 111 params.report_unrecoverable_error_function = |
| 112 base::Bind(ReportUnrecoverableError, channel_); |
| 113 params.saved_nigori_state = MoveSavedNigoriState(); |
| 114 sync_prefs_.GetInvalidationVersions(¶ms.invalidation_versions); |
| 115 |
| 116 engine_->Initialize(std::move(params)); |
| 117 } |
| 118 |
| 119 bool SyncServiceBase::GetLocalSyncConfig( |
| 120 base::FilePath* local_sync_backend_folder) const { |
| 121 bool enable_local_sync_backend = false; |
| 122 *local_sync_backend_folder = sync_prefs_.GetLocalSyncBackendDir(); |
| 123 #if defined(OS_WIN) |
| 124 enable_local_sync_backend = sync_prefs_.IsLocalSyncEnabled(); |
| 125 if (local_sync_backend_folder->empty()) { |
| 126 // TODO(pastarmovj): Add DIR_ROAMING_USER_DATA to PathService to simplify |
| 127 // this code and move the logic in its right place. See crbug/657810. |
| 128 CHECK( |
| 129 base::PathService::Get(base::DIR_APP_DATA, local_sync_backend_folder)); |
| 130 *local_sync_backend_folder = local_sync_backend_folder->Append( |
| 131 FILE_PATH_LITERAL("Chrome/User Data")); |
| 132 } |
| 133 // This code as it is now will assume the same profile order is present on all |
| 134 // machines, which is not a given. It is to be defined if only the Default |
| 135 // profile should get this treatment or all profile as is the case now. The |
| 136 // solution for now will be to assume profiles are created in the same order |
| 137 // on all machines and in the future decide if only the Default one should be |
| 138 // considered roamed. |
| 139 *local_sync_backend_folder = |
| 140 local_sync_backend_folder->Append(base_directory_.BaseName()); |
| 141 *local_sync_backend_folder = |
| 142 local_sync_backend_folder->Append(kLoopbackServerBackendFilename); |
| 143 #endif // defined(OS_WIN) |
| 144 return enable_local_sync_backend; |
| 145 } |
| 146 |
| 147 } // namespace syncer |
| OLD | NEW |