Index: chrome/browser/sync/profile_sync_service.cc |
diff --git a/chrome/browser/sync/profile_sync_service.cc b/chrome/browser/sync/profile_sync_service.cc |
index b649c14e32d39f0e7eb634bf6dd11cba7c4b9633..0bfa94bc578e0cf5e913c70d72e67130a5438ada 100644 |
--- a/chrome/browser/sync/profile_sync_service.cc |
+++ b/chrome/browser/sync/profile_sync_service.cc |
@@ -297,11 +297,21 @@ void ProfileSyncService::TryStart() { |
// but we haven't completed sync setup, we try to start sync anyway, since |
// it's possible we crashed/shutdown after logging in but before the backend |
// finished initializing the last time. |
- if (!HasSyncSetupCompleted() && !setup_in_progress_ && !auto_start_enabled_) |
- return; |
- |
- // All systems Go for launch. |
- StartUp(); |
+ // |
+ // However, the only time we actually need to start sync _immediately_ is if |
+ // we haven't completed sync setup and the user is in the process of setting |
+ // up - either they just signed in (for the first time) on an auto-start |
+ // platform or they explicitly kicked off sync setup, and e.g we need to |
+ // fetch account details like encryption state to populate UI. Otherwise, |
+ // for performance reasons and maximizing parallelism at chrome startup, we |
+ // defer the heavy lifting for sync init until things have calmed down. |
+ if (HasSyncSetupCompleted()) { |
+ StartUp(STARTUP_BACKEND_DEFERRED); |
+ } else if (setup_in_progress_ || auto_start_enabled_) { |
+ // We haven't completed sync setup. Start immediately if the user explicitly |
+ // kicked this off or we're supposed to automatically start syncing. |
+ StartUp(STARTUP_IMMEDIATE); |
+ } |
} |
void ProfileSyncService::StartSyncingWithServer() { |
@@ -467,8 +477,7 @@ void ProfileSyncService::OnSyncConfigureRetry() { |
NotifyObservers(); |
} |
- |
-void ProfileSyncService::StartUp() { |
+void ProfileSyncService::StartUp(StartUpDeferredOption deferred_option) { |
// Don't start up multiple times. |
if (backend_.get()) { |
DVLOG(1) << "Skipping bringing up backend host."; |
@@ -487,6 +496,40 @@ void ProfileSyncService::StartUp() { |
sync_prefs_.GetSpareBootstrapToken()); |
} |
#endif |
+ |
+ if (!sync_global_error_.get()) { |
+#if !defined(OS_ANDROID) |
+ sync_global_error_.reset(new SyncGlobalError(this, signin())); |
+#endif |
+ GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError( |
+ sync_global_error_.get()); |
+ AddObserver(sync_global_error_.get()); |
+ } |
+ |
+ if (deferred_option == STARTUP_BACKEND_DEFERRED && |
+ CommandLine::ForCurrentProcess()-> |
+ HasSwitch(switches::kSyncEnableDeferredStartup)) { |
+ return; |
+ } |
+ DCHECK_EQ(STARTUP_IMMEDIATE, deferred_option); |
+ StartUpSlowBackendComponents(STARTUP_IMMEDIATE); |
+} |
+ |
+void ProfileSyncService::StartUpSlowBackendComponents( |
+ StartUpDeferredOption deferred_option) { |
+ // Don't start up multiple times. |
+ if (backend_.get()) { |
+ DVLOG(1) << "Skipping bringing up backend host."; |
+ return; |
+ } |
+ |
+ DCHECK(!start_up_time_.is_null()); |
+ if (deferred_option == STARTUP_BACKEND_DEFERRED) { |
+ base::TimeDelta time_deferred = base::Time::Now() - start_up_time_; |
+ UMA_HISTOGRAM_TIMES("Sync.Startup.TimeDeferred", time_deferred); |
+ } |
+ |
+ DCHECK(IsSyncEnabledAndLoggedIn()); |
CreateBackend(); |
// Initialize the backend. Every time we start up a new SyncBackendHost, |
@@ -503,15 +546,6 @@ void ProfileSyncService::StartUp() { |
backend_->UpdateRegisteredInvalidationIds( |
invalidator_registrar_->GetAllRegisteredIds()); |
} |
- |
- if (!sync_global_error_.get()) { |
-#if !defined(OS_ANDROID) |
- sync_global_error_.reset(new SyncGlobalError(this, signin())); |
-#endif |
- GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError( |
- sync_global_error_.get()); |
- AddObserver(sync_global_error_.get()); |
- } |
} |
void ProfileSyncService::RegisterInvalidationHandler( |
@@ -1240,6 +1274,15 @@ std::string ProfileSyncService::QuerySyncStatusSummary() { |
} |
} |
+std::string ProfileSyncService::GetBackendInitializationStateString() const { |
+ if (sync_initialized()) |
+ return "Done"; |
+ else if (!start_up_time_.is_null()) |
+ return "Deferred"; |
+ else |
+ return "Not started"; |
+} |
+ |
bool ProfileSyncService::QueryDetailedSyncStatus( |
SyncBackendHost::Status* result) { |
if (backend_.get() && backend_initialized_) { |