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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
|
// 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/engine/sync_manager_impl.h"
#include <stddef.h>
#include <utility>
#include "base/compiler_specific.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/metrics/histogram_macros.h"
#include "base/observer_list.h"
#include "base/task/sequenced_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "base/values.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/sync_invalidation.h"
#include "components/sync/engine/cancelation_signal.h"
#include "components/sync/engine/configure_reason.h"
#include "components/sync/engine/data_type_connector_proxy.h"
#include "components/sync/engine/data_type_worker.h"
#include "components/sync/engine/engine_components_factory.h"
#include "components/sync/engine/loopback_server/loopback_connection_manager.h"
#include "components/sync/engine/net/http_post_provider_factory.h"
#include "components/sync/engine/net/sync_server_connection_manager.h"
#include "components/sync/engine/net/url_translator.h"
#include "components/sync/engine/nigori/cryptographer.h"
#include "components/sync/engine/nigori/key_derivation_params.h"
#include "components/sync/engine/nigori/keystore_keys_handler.h"
#include "components/sync/engine/polling_constants.h"
#include "components/sync/engine/sync_scheduler.h"
#include "components/sync/engine/update_handler.h"
#include "components/sync/protocol/sync_enums.pb.h"
namespace syncer {
namespace {
sync_pb::SyncEnums::GetUpdatesOrigin GetOriginFromReason(
ConfigureReason reason) {
switch (reason) {
case CONFIGURE_REASON_RECONFIGURATION:
return sync_pb::SyncEnums::RECONFIGURATION;
case CONFIGURE_REASON_MIGRATION:
return sync_pb::SyncEnums::MIGRATION;
case CONFIGURE_REASON_NEW_CLIENT:
return sync_pb::SyncEnums::NEW_CLIENT;
case CONFIGURE_REASON_EXISTING_CLIENT_RESTART:
case CONFIGURE_REASON_CRYPTO:
// Mapping these cases to NEWLY_SUPPORTED_DATATYPE is rather wrong, as it
// includes common cases like sync being unpaused or a crypto error having
// been resolved, if initial sync didn't complete earlier (or data was
// cleared while paused). The legacy behavior is kept until a better
// solution is found.
return sync_pb::SyncEnums::NEWLY_SUPPORTED_DATATYPE;
case CONFIGURE_REASON_PROGRAMMATIC:
return sync_pb::SyncEnums::PROGRAMMATIC;
case CONFIGURE_REASON_UNKNOWN:
NOTREACHED();
}
return sync_pb::SyncEnums::UNKNOWN_ORIGIN;
}
const char kSyncServerSyncPath[] = "/command/";
std::string StripTrailingSlash(const std::string& s) {
int stripped_end_pos = s.size();
if (s.at(stripped_end_pos - 1) == '/') {
stripped_end_pos = stripped_end_pos - 1;
}
return s.substr(0, stripped_end_pos);
}
GURL MakeConnectionURL(const GURL& sync_server, const std::string& client_id) {
DCHECK_EQ(kSyncServerSyncPath[0], '/');
std::string full_path =
StripTrailingSlash(sync_server.path()) + kSyncServerSyncPath;
GURL::Replacements path_replacement;
path_replacement.SetPathStr(full_path);
return AppendSyncQueryString(sync_server.ReplaceComponents(path_replacement),
client_id);
}
} // namespace
SyncManagerImpl::SyncManagerImpl(
const std::string& name,
network::NetworkConnectionTracker* network_connection_tracker)
: name_(name), network_connection_tracker_(network_connection_tracker) {}
SyncManagerImpl::~SyncManagerImpl() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!initialized_);
}
DataTypeSet SyncManagerImpl::InitialSyncEndedTypes() {
DCHECK(initialized_);
return data_type_registry_->GetInitialSyncEndedTypes();
}
DataTypeSet SyncManagerImpl::GetConnectedTypes() {
DCHECK(initialized_);
return data_type_registry_->GetConnectedTypes();
}
void SyncManagerImpl::ConfigureSyncer(ConfigureReason reason,
DataTypeSet to_download,
SyncFeatureState sync_feature_state,
base::OnceClosure ready_task) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!ready_task.is_null());
DCHECK(initialized_);
DVLOG(1) << "Configuring -" << "\n\t"
<< "types to download: " << DataTypeSetToDebugString(to_download);
scheduler_->Start(SyncScheduler::CONFIGURATION_MODE, base::Time());
scheduler_->ScheduleConfiguration(GetOriginFromReason(reason), to_download,
std::move(ready_task));
if (sync_feature_state != SyncFeatureState::INITIALIZING) {
cycle_context_->set_is_sync_feature_enabled(sync_feature_state ==
SyncFeatureState::ON);
}
}
void SyncManagerImpl::Init(InitArgs* args) {
DCHECK(!initialized_);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!args->cache_guid.empty());
DCHECK(args->post_factory);
DCHECK(!args->poll_interval.is_zero());
DCHECK(args->cancelation_signal);
DVLOG(1) << "SyncManager starting Init...";
DCHECK(args->encryption_observer_proxy);
encryption_observer_proxy_ = std::move(args->encryption_observer_proxy);
AddObserver(&debug_info_event_listener_);
DCHECK(args->encryption_handler);
sync_encryption_handler_ = args->encryption_handler;
// Register for encryption related changes now. We have to do this before
// the initial download of control types or initializing the encryption
// handler in order to receive notifications triggered during encryption
// startup.
sync_encryption_handler_->AddObserver(this);
sync_encryption_handler_->AddObserver(encryption_observer_proxy_.get());
sync_encryption_handler_->AddObserver(&debug_info_event_listener_);
// base::Unretained() is safe here because SyncManagerImpl outlives
// sync_status_tracker_.
sync_status_tracker_ =
std::make_unique<SyncStatusTracker>(base::BindRepeating(
&SyncManagerImpl::NotifySyncStatusChanged, base::Unretained(this)));
sync_status_tracker_->SetHasKeystoreKey(
!sync_encryption_handler_->GetKeystoreKeysHandler()->NeedKeystoreKey());
if (args->enable_local_sync_backend) {
VLOG(1) << "Running against local sync backend.";
sync_status_tracker_->SetLocalBackendFolder(
args->local_sync_backend_folder.AsUTF8Unsafe());
connection_manager_ = std::make_unique<LoopbackConnectionManager>(
args->local_sync_backend_folder);
} else {
connection_manager_ = std::make_unique<SyncServerConnectionManager>(
MakeConnectionURL(args->service_url, args->cache_guid),
std::move(args->post_factory), args->cancelation_signal);
}
connection_manager_->AddListener(this);
DVLOG(1) << "Setting sync client ID: " << args->cache_guid;
sync_status_tracker_->SetCacheGuid(args->cache_guid);
data_type_registry_ = std::make_unique<DataTypeRegistry>(
this, args->cancelation_signal, sync_encryption_handler_);
// Build a SyncCycleContext and store the worker in it.
DVLOG(1) << "Sync is bringing up SyncCycleContext.";
std::vector<SyncEngineEventListener*> listeners = {
this, sync_status_tracker_.get()};
cycle_context_ = args->engine_components_factory->BuildContext(
connection_manager_.get(), args->extensions_activity, listeners,
&debug_info_event_listener_, data_type_registry_.get(), args->cache_guid,
args->birthday, args->bag_of_chips, args->poll_interval);
scheduler_ = args->engine_components_factory->BuildScheduler(
name_, cycle_context_.get(), args->cancelation_signal,
args->enable_local_sync_backend);
scheduler_->Start(SyncScheduler::CONFIGURATION_MODE, base::Time());
initialized_ = true;
if (!args->enable_local_sync_backend) {
network_connection_tracker_->AddNetworkConnectionObserver(this);
} else {
scheduler_->OnCredentialsUpdated();
}
debug_info_event_listener_.InitializationComplete();
}
void SyncManagerImpl::OnPassphraseRequired(
const KeyDerivationParams& key_derivation_params,
const sync_pb::EncryptedData& pending_keys) {
// Does nothing.
}
void SyncManagerImpl::OnPassphraseAccepted() {
// Does nothing.
}
void SyncManagerImpl::OnTrustedVaultKeyRequired() {
// Does nothing.
}
void SyncManagerImpl::OnTrustedVaultKeyAccepted() {
// Does nothing.
}
void SyncManagerImpl::OnEncryptedTypesChanged(DataTypeSet encrypted_types,
bool encrypt_everything) {
sync_status_tracker_->SetEncryptedTypes(encrypted_types);
}
void SyncManagerImpl::OnCryptographerStateChanged(Cryptographer* cryptographer,
bool has_pending_keys) {
sync_status_tracker_->SetCryptographerCanEncrypt(cryptographer->CanEncrypt());
sync_status_tracker_->SetCryptoHasPendingKeys(has_pending_keys);
sync_status_tracker_->SetKeystoreMigrationTime(
sync_encryption_handler_->GetKeystoreMigrationTime());
sync_status_tracker_->SetTrustedVaultDebugInfo(
sync_encryption_handler_->GetTrustedVaultDebugInfo());
sync_status_tracker_->SetHasKeystoreKey(
!sync_encryption_handler_->GetKeystoreKeysHandler()->NeedKeystoreKey());
}
void SyncManagerImpl::OnPassphraseTypeChanged(
PassphraseType type,
base::Time explicit_passphrase_time) {
sync_status_tracker_->SetPassphraseType(type);
sync_status_tracker_->SetKeystoreMigrationTime(
sync_encryption_handler_->GetKeystoreMigrationTime());
}
void SyncManagerImpl::StartSyncingNormally(base::Time last_poll_time) {
// Start the sync scheduler.
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
scheduler_->Start(SyncScheduler::NORMAL_MODE, last_poll_time);
}
void SyncManagerImpl::StartConfiguration() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
scheduler_->Start(SyncScheduler::CONFIGURATION_MODE, base::Time());
}
void SyncManagerImpl::UpdateCredentials(const SyncCredentials& credentials) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(initialized_);
cycle_context_->set_account_name(credentials.email);
observing_network_connectivity_changes_ = true;
if (!connection_manager_->SetAccessToken(credentials.access_token)) {
return; // Auth token is known to be invalid, so exit early.
}
scheduler_->OnCredentialsUpdated();
// TODO(zea): pass the credential age to the debug info event listener.
}
void SyncManagerImpl::InvalidateCredentials() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
connection_manager_->SetAccessToken(std::string());
}
void SyncManagerImpl::AddObserver(SyncManager::Observer* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.AddObserver(observer);
}
void SyncManagerImpl::RemoveObserver(SyncManager::Observer* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.RemoveObserver(observer);
}
void SyncManagerImpl::ShutdownOnSyncThread() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Prevent any in-flight method calls from running.
weak_ptr_factory_.InvalidateWeakPtrs();
scheduler_.reset();
cycle_context_.reset();
data_type_registry_.reset();
if (sync_encryption_handler_) {
sync_encryption_handler_->RemoveObserver(&debug_info_event_listener_);
sync_encryption_handler_->RemoveObserver(this);
sync_encryption_handler_->RemoveObserver(encryption_observer_proxy_.get());
}
RemoveObserver(&debug_info_event_listener_);
// `connection_manager_` may end up being null here in tests (in synchronous
// initialization mode).
//
// TODO(akalin): Fix this behavior.
if (connection_manager_) {
connection_manager_->RemoveListener(this);
}
connection_manager_.reset();
network_connection_tracker_->RemoveNetworkConnectionObserver(this);
observing_network_connectivity_changes_ = false;
initialized_ = false;
}
void SyncManagerImpl::OnConnectionChanged(network::mojom::ConnectionType type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!observing_network_connectivity_changes_) {
DVLOG(1) << "Network change dropped.";
return;
}
DVLOG(1) << "Network change detected.";
scheduler_->OnConnectionStatusChange(type);
}
void SyncManagerImpl::OnServerConnectionEvent(
const ServerConnectionEvent& event) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (event.connection_code == HttpResponse::SERVER_CONNECTION_OK) {
for (SyncManager::Observer& observer : observers_) {
observer.OnConnectionStatusChange(CONNECTION_OK);
}
}
if (event.connection_code == HttpResponse::SYNC_AUTH_ERROR) {
observing_network_connectivity_changes_ = false;
for (SyncManager::Observer& observer : observers_) {
observer.OnConnectionStatusChange(CONNECTION_AUTH_ERROR);
}
}
if (event.connection_code == HttpResponse::SYNC_SERVER_ERROR) {
for (SyncManager::Observer& observer : observers_) {
observer.OnConnectionStatusChange(CONNECTION_SERVER_ERROR);
}
}
}
void SyncManagerImpl::NudgeForInitialDownload(DataType type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
scheduler_->ScheduleInitialSyncNudge(type);
}
void SyncManagerImpl::NudgeForCommit(DataType type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
debug_info_event_listener_.OnNudgeFromDatatype(type);
scheduler_->ScheduleLocalNudge(type);
}
void SyncManagerImpl::SetHasPendingInvalidations(
DataType type,
bool has_pending_invalidations) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
scheduler_->SetHasPendingInvalidations(type, has_pending_invalidations);
sync_status_tracker_->SetHasPendingInvalidations(type,
has_pending_invalidations);
}
void SyncManagerImpl::NotifySyncStatusChanged(const SyncStatus& status) {
for (SyncManager::Observer& observer : observers_) {
observer.OnSyncStatusChanged(status);
}
}
void SyncManagerImpl::OnSyncCycleEvent(const SyncCycleEvent& event) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Only send an event if this is due to a cycle ending and this cycle
// concludes a canonical "sync" process; that is, based on what is known
// locally we are "all happy" and up to date. There may be new changes on
// the server, but we'll get them on a subsequent sync.
//
// Notifications are sent at the end of every sync cycle, regardless of
// whether we should sync again.
if (event.what_happened == SyncCycleEvent::SYNC_CYCLE_ENDED) {
if (!initialized_) {
DVLOG(1) << "OnSyncCycleCompleted not sent because sync api is not "
<< "initialized";
return;
}
DVLOG(1) << "Sending OnSyncCycleCompleted";
for (SyncManager::Observer& observer : observers_) {
observer.OnSyncCycleCompleted(event.snapshot);
}
}
}
void SyncManagerImpl::OnActionableProtocolError(
const SyncProtocolError& error) {
for (SyncManager::Observer& observer : observers_) {
observer.OnActionableProtocolError(error);
}
}
void SyncManagerImpl::OnRetryTimeChanged(base::Time) {}
void SyncManagerImpl::OnThrottledTypesChanged(DataTypeSet) {}
void SyncManagerImpl::OnBackedOffTypesChanged(DataTypeSet) {}
void SyncManagerImpl::OnMigrationRequested(DataTypeSet types) {
for (SyncManager::Observer& observer : observers_) {
observer.OnMigrationRequested(types);
}
}
void SyncManagerImpl::OnProtocolEvent(const ProtocolEvent& event) {
protocol_event_buffer_.RecordProtocolEvent(event);
for (SyncManager::Observer& observer : observers_) {
observer.OnProtocolEvent(event);
}
}
void SyncManagerImpl::SetInvalidatorEnabled(bool invalidator_enabled) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DVLOG(1) << "Invalidator enabled state is now: " << invalidator_enabled;
sync_status_tracker_->SetNotificationsEnabled(invalidator_enabled);
scheduler_->SetNotificationsEnabled(invalidator_enabled);
}
void SyncManagerImpl::OnIncomingInvalidation(
DataType type,
std::unique_ptr<SyncInvalidation> invalidation) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UpdateHandler* handler = data_type_registry_->GetMutableUpdateHandler(type);
if (handler) {
handler->RecordRemoteInvalidation(std::move(invalidation));
} else {
DataTypeWorker::LogPendingInvalidationStatus(
PendingInvalidationStatus::kDataTypeNotConnected);
}
sync_status_tracker_->IncrementNotificationsReceived();
scheduler_->ScheduleInvalidationNudge(type);
}
void SyncManagerImpl::RefreshTypes(DataTypeSet types) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const DataTypeSet types_to_refresh =
Intersection(types, data_type_registry_->GetConnectedTypes());
if (!types_to_refresh.empty()) {
scheduler_->ScheduleLocalRefreshRequest(types_to_refresh);
}
}
DataTypeConnector* SyncManagerImpl::GetDataTypeConnector() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return data_type_registry_.get();
}
std::unique_ptr<DataTypeConnector>
SyncManagerImpl::GetDataTypeConnectorProxy() {
DCHECK(initialized_);
return std::make_unique<DataTypeConnectorProxy>(
base::SequencedTaskRunner::GetCurrentDefault(),
data_type_registry_->AsWeakPtr());
}
std::string SyncManagerImpl::cache_guid() {
DCHECK(initialized_);
return cycle_context_->cache_guid();
}
std::string SyncManagerImpl::birthday() {
DCHECK(initialized_);
DCHECK(cycle_context_);
return cycle_context_->birthday();
}
std::string SyncManagerImpl::bag_of_chips() {
DCHECK(initialized_);
DCHECK(cycle_context_);
return cycle_context_->bag_of_chips();
}
bool SyncManagerImpl::HasUnsyncedItemsForTest() {
return data_type_registry_->HasUnsyncedItems();
}
SyncEncryptionHandler* SyncManagerImpl::GetEncryptionHandler() {
DCHECK(sync_encryption_handler_);
return sync_encryption_handler_;
}
std::vector<std::unique_ptr<ProtocolEvent>>
SyncManagerImpl::GetBufferedProtocolEvents() {
return protocol_event_buffer_.GetBufferedProtocolEvents();
}
void SyncManagerImpl::OnCookieJarChanged(bool account_mismatch) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
cycle_context_->set_cookie_jar_mismatch(account_mismatch);
}
void SyncManagerImpl::UpdateActiveDevicesInvalidationInfo(
ActiveDevicesInvalidationInfo active_devices_invalidation_info) {
cycle_context_->set_active_devices_invalidation_info(
std::move(active_devices_invalidation_info));
}
} // namespace syncer
|