Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/profile_sync_service.h" | 5 #include "chrome/browser/sync/profile_sync_service.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 !(!auto_start_enabled_ && token_service->TokensLoadedFromDB())) { | 290 !(!auto_start_enabled_ && token_service->TokensLoadedFromDB())) { |
| 291 return; | 291 return; |
| 292 } | 292 } |
| 293 | 293 |
| 294 // If sync setup has completed we always start the backend. If the user is in | 294 // If sync setup has completed we always start the backend. If the user is in |
| 295 // the process of setting up now, we should start the backend to download | 295 // the process of setting up now, we should start the backend to download |
| 296 // account control state / encryption information). If autostart is enabled, | 296 // account control state / encryption information). If autostart is enabled, |
| 297 // but we haven't completed sync setup, we try to start sync anyway, since | 297 // but we haven't completed sync setup, we try to start sync anyway, since |
| 298 // it's possible we crashed/shutdown after logging in but before the backend | 298 // it's possible we crashed/shutdown after logging in but before the backend |
| 299 // finished initializing the last time. | 299 // finished initializing the last time. |
| 300 if (!HasSyncSetupCompleted() && !setup_in_progress_ && !auto_start_enabled_) | 300 // |
| 301 return; | 301 // However, the only time we actually need to start sync _immediately_ is if |
| 302 | 302 // we haven't completed sync setup and the user is in the process of setting |
| 303 // All systems Go for launch. | 303 // up - either they just signed in (for the first time) on an auto-start |
| 304 StartUp(); | 304 // platform or they explicitly kicked off sync setup, and e.g we need to |
| 305 // fetch account details like encryption state to populate UI. Otherwise, | |
| 306 // for performance reasons and maximizing parallelism at chrome startup, we | |
| 307 // defer the heavy lifting for sync init until things have calmed down. | |
| 308 if (HasSyncSetupCompleted()) { | |
| 309 StartUp(STARTUP_BACKEND_DEFERRED); | |
| 310 } else if (setup_in_progress_ || auto_start_enabled_) { | |
| 311 // We haven't completed sync setup. Start immediately if the user explicitly | |
| 312 // kicked this off or we're supposed to automatically start syncing. | |
| 313 StartUp(STARTUP_IMMEDIATE); | |
| 314 } | |
| 305 } | 315 } |
| 306 | 316 |
| 307 void ProfileSyncService::StartSyncingWithServer() { | 317 void ProfileSyncService::StartSyncingWithServer() { |
| 308 if (backend_.get()) | 318 if (backend_.get()) |
| 309 backend_->StartSyncingWithServer(); | 319 backend_->StartSyncingWithServer(); |
| 310 } | 320 } |
| 311 | 321 |
| 312 void ProfileSyncService::RegisterAuthNotifications() { | 322 void ProfileSyncService::RegisterAuthNotifications() { |
| 313 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | 323 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); |
| 314 registrar_.Add(this, | 324 registrar_.Add(this, |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 460 | 470 |
| 461 void ProfileSyncService::OnSyncConfigureRetry() { | 471 void ProfileSyncService::OnSyncConfigureRetry() { |
| 462 // Note: in order to handle auth failures that arise before the backend is | 472 // Note: in order to handle auth failures that arise before the backend is |
| 463 // initialized (e.g. from invalidation notifier, or downloading new control | 473 // initialized (e.g. from invalidation notifier, or downloading new control |
| 464 // types), we have to gracefully handle configuration retries at all times. | 474 // types), we have to gracefully handle configuration retries at all times. |
| 465 // At this point an auth error badge should be shown, which once resolved | 475 // At this point an auth error badge should be shown, which once resolved |
| 466 // will trigger a new sync cycle. | 476 // will trigger a new sync cycle. |
| 467 NotifyObservers(); | 477 NotifyObservers(); |
| 468 } | 478 } |
| 469 | 479 |
| 470 | 480 void ProfileSyncService::StartUp(StartUpDeferredOption deferred_option) { |
| 471 void ProfileSyncService::StartUp() { | |
| 472 // Don't start up multiple times. | 481 // Don't start up multiple times. |
| 473 if (backend_.get()) { | 482 if (backend_.get()) { |
| 474 DVLOG(1) << "Skipping bringing up backend host."; | 483 DVLOG(1) << "Skipping bringing up backend host."; |
| 475 return; | 484 return; |
| 476 } | 485 } |
| 477 | 486 |
| 478 DCHECK(IsSyncEnabledAndLoggedIn()); | 487 DCHECK(IsSyncEnabledAndLoggedIn()); |
| 479 | 488 |
| 480 last_synced_time_ = sync_prefs_.GetLastSyncedTime(); | 489 last_synced_time_ = sync_prefs_.GetLastSyncedTime(); |
| 481 start_up_time_ = base::Time::Now(); | 490 start_up_time_ = base::Time::Now(); |
|
Nicolas Zea
2013/02/25 21:12:04
I'm a bit concerned about the possibility of TrySt
| |
| 482 | 491 |
| 483 #if defined(OS_CHROMEOS) | 492 #if defined(OS_CHROMEOS) |
| 484 std::string bootstrap_token = sync_prefs_.GetEncryptionBootstrapToken(); | 493 std::string bootstrap_token = sync_prefs_.GetEncryptionBootstrapToken(); |
| 485 if (bootstrap_token.empty()) { | 494 if (bootstrap_token.empty()) { |
| 486 sync_prefs_.SetEncryptionBootstrapToken( | 495 sync_prefs_.SetEncryptionBootstrapToken( |
| 487 sync_prefs_.GetSpareBootstrapToken()); | 496 sync_prefs_.GetSpareBootstrapToken()); |
| 488 } | 497 } |
| 489 #endif | 498 #endif |
| 499 | |
| 500 if (!sync_global_error_.get()) { | |
| 501 #if !defined(OS_ANDROID) | |
| 502 sync_global_error_.reset(new SyncGlobalError(this, signin())); | |
| 503 #endif | |
| 504 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError( | |
| 505 sync_global_error_.get()); | |
| 506 AddObserver(sync_global_error_.get()); | |
| 507 } | |
| 508 | |
| 509 if (deferred_option == STARTUP_BACKEND_DEFERRED && | |
| 510 CommandLine::ForCurrentProcess()-> | |
| 511 HasSwitch(switches::kSyncEnableDeferredStartup)) { | |
| 512 return; | |
| 513 } | |
| 514 DCHECK_EQ(STARTUP_IMMEDIATE, deferred_option); | |
| 515 StartUpSlowBackendComponents(STARTUP_IMMEDIATE); | |
| 516 } | |
| 517 | |
| 518 void ProfileSyncService::StartUpSlowBackendComponents( | |
| 519 StartUpDeferredOption deferred_option) { | |
| 520 // Don't start up multiple times. | |
| 521 if (backend_.get()) { | |
| 522 DVLOG(1) << "Skipping bringing up backend host."; | |
| 523 return; | |
| 524 } | |
| 525 | |
| 526 DCHECK(!start_up_time_.is_null()); | |
| 527 if (deferred_option == STARTUP_BACKEND_DEFERRED) { | |
| 528 base::TimeDelta time_deferred = base::Time::Now() - start_up_time_; | |
| 529 UMA_HISTOGRAM_TIMES("Sync.Startup.TimeDeferred", time_deferred); | |
| 530 } | |
| 531 | |
| 532 DCHECK(IsSyncEnabledAndLoggedIn()); | |
| 490 CreateBackend(); | 533 CreateBackend(); |
| 491 | 534 |
| 492 // Initialize the backend. Every time we start up a new SyncBackendHost, | 535 // Initialize the backend. Every time we start up a new SyncBackendHost, |
| 493 // we'll want to start from a fresh SyncDB, so delete any old one that might | 536 // we'll want to start from a fresh SyncDB, so delete any old one that might |
| 494 // be there. | 537 // be there. |
| 495 InitializeBackend(!HasSyncSetupCompleted()); | 538 InitializeBackend(!HasSyncSetupCompleted()); |
| 496 | 539 |
| 497 // |backend_| may end up being NULL here in tests (in synchronous | 540 // |backend_| may end up being NULL here in tests (in synchronous |
| 498 // initialization mode). | 541 // initialization mode). |
| 499 // | 542 // |
| 500 // TODO(akalin): Fix this horribly non-intuitive behavior (see | 543 // TODO(akalin): Fix this horribly non-intuitive behavior (see |
| 501 // http://crbug.com/140354). | 544 // http://crbug.com/140354). |
| 502 if (backend_.get()) { | 545 if (backend_.get()) { |
| 503 backend_->UpdateRegisteredInvalidationIds( | 546 backend_->UpdateRegisteredInvalidationIds( |
| 504 invalidator_registrar_->GetAllRegisteredIds()); | 547 invalidator_registrar_->GetAllRegisteredIds()); |
| 505 } | 548 } |
| 506 | |
| 507 if (!sync_global_error_.get()) { | |
| 508 #if !defined(OS_ANDROID) | |
| 509 sync_global_error_.reset(new SyncGlobalError(this, signin())); | |
| 510 #endif | |
| 511 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError( | |
| 512 sync_global_error_.get()); | |
| 513 AddObserver(sync_global_error_.get()); | |
| 514 } | |
| 515 } | 549 } |
| 516 | 550 |
| 517 void ProfileSyncService::RegisterInvalidationHandler( | 551 void ProfileSyncService::RegisterInvalidationHandler( |
| 518 syncer::InvalidationHandler* handler) { | 552 syncer::InvalidationHandler* handler) { |
| 519 invalidator_registrar_->RegisterHandler(handler); | 553 invalidator_registrar_->RegisterHandler(handler); |
| 520 } | 554 } |
| 521 | 555 |
| 522 void ProfileSyncService::UpdateRegisteredInvalidationIds( | 556 void ProfileSyncService::UpdateRegisteredInvalidationIds( |
| 523 syncer::InvalidationHandler* handler, | 557 syncer::InvalidationHandler* handler, |
| 524 const syncer::ObjectIdSet& ids) { | 558 const syncer::ObjectIdSet& ids) { |
| (...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1233 data_type_manager_.get() && | 1267 data_type_manager_.get() && |
| 1234 data_type_manager_->state() != DataTypeManager::CONFIGURED) { | 1268 data_type_manager_->state() != DataTypeManager::CONFIGURED) { |
| 1235 return "Datatypes not fully initialized"; | 1269 return "Datatypes not fully initialized"; |
| 1236 } else if (ShouldPushChanges()) { | 1270 } else if (ShouldPushChanges()) { |
| 1237 return "Sync service initialized"; | 1271 return "Sync service initialized"; |
| 1238 } else { | 1272 } else { |
| 1239 return "Status unknown: Internal error?"; | 1273 return "Status unknown: Internal error?"; |
| 1240 } | 1274 } |
| 1241 } | 1275 } |
| 1242 | 1276 |
| 1277 std::string ProfileSyncService::GetBackendInitializationStateString() const { | |
| 1278 if (sync_initialized()) | |
| 1279 return "Done"; | |
| 1280 else if (!start_up_time_.is_null()) | |
| 1281 return "Deferred"; | |
| 1282 else | |
| 1283 return "Not started"; | |
| 1284 } | |
| 1285 | |
| 1243 bool ProfileSyncService::QueryDetailedSyncStatus( | 1286 bool ProfileSyncService::QueryDetailedSyncStatus( |
| 1244 SyncBackendHost::Status* result) { | 1287 SyncBackendHost::Status* result) { |
| 1245 if (backend_.get() && backend_initialized_) { | 1288 if (backend_.get() && backend_initialized_) { |
| 1246 *result = backend_->GetDetailedStatus(); | 1289 *result = backend_->GetDetailedStatus(); |
| 1247 return true; | 1290 return true; |
| 1248 } else { | 1291 } else { |
| 1249 SyncBackendHost::Status status; | 1292 SyncBackendHost::Status status; |
| 1250 status.sync_protocol_error = last_actionable_error_; | 1293 status.sync_protocol_error = last_actionable_error_; |
| 1251 *result = status; | 1294 *result = status; |
| 1252 return false; | 1295 return false; |
| (...skipping 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1974 // See http://stackoverflow.com/questions/6224121/is-new-this-myclass-undefine d-behaviour-after-directly-calling-the-destru. | 2017 // See http://stackoverflow.com/questions/6224121/is-new-this-myclass-undefine d-behaviour-after-directly-calling-the-destru. |
| 1975 ProfileSyncService* old_this = this; | 2018 ProfileSyncService* old_this = this; |
| 1976 this->~ProfileSyncService(); | 2019 this->~ProfileSyncService(); |
| 1977 new(old_this) ProfileSyncService( | 2020 new(old_this) ProfileSyncService( |
| 1978 new ProfileSyncComponentsFactoryImpl(profile, | 2021 new ProfileSyncComponentsFactoryImpl(profile, |
| 1979 CommandLine::ForCurrentProcess()), | 2022 CommandLine::ForCurrentProcess()), |
| 1980 profile, | 2023 profile, |
| 1981 signin, | 2024 signin, |
| 1982 behavior); | 2025 behavior); |
| 1983 } | 2026 } |
| OLD | NEW |