1 // Copyright (c) 2013 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.
5 #include "chrome/browser/sync/sync_startup_tracker.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/sync/profile_sync_service.h"
9 #include "chrome/browser/sync/profile_sync_service_factory.h"
11 SyncStartupTracker::SyncStartupTracker(Profile
* profile
, Observer
* observer
)
14 ProfileSyncService
* service
= ProfileSyncServiceFactory::GetForProfile(
17 service
->AddObserver(this);
22 SyncStartupTracker::~SyncStartupTracker() {
23 ProfileSyncService
* service
= ProfileSyncServiceFactory::GetForProfile(
26 service
->RemoveObserver(this);
29 void SyncStartupTracker::OnStateChanged() {
33 void SyncStartupTracker::CheckServiceState() {
34 // Note: the observer may free this object so it is not allowed to access
35 // this object after invoking the observer callback below.
36 switch (GetSyncServiceState(profile_
)) {
37 case SYNC_STARTUP_ERROR
:
38 observer_
->SyncStartupFailed();
40 case SYNC_STARTUP_COMPLETE
:
41 observer_
->SyncStartupCompleted();
43 case SYNC_STARTUP_PENDING
:
44 // Do nothing - still waiting for sync to finish starting up.
50 SyncStartupTracker::SyncServiceState
SyncStartupTracker::GetSyncServiceState(
52 // If sync is disabled, treat this as a startup error.
53 if (!profile
->IsSyncAccessible())
54 return SYNC_STARTUP_ERROR
;
56 ProfileSyncService
* service
=
57 ProfileSyncServiceFactory::GetForProfile(profile
);
59 // If no service exists or sync is disabled, treat as a startup error.
60 if (!profile
->IsSyncAccessible() || !service
||
61 !service
->IsSyncEnabledAndLoggedIn()) {
62 return SYNC_STARTUP_ERROR
;
65 // If the sync backend has started up, notify the callback.
66 if (service
->backend_initialized())
67 return SYNC_STARTUP_COMPLETE
;
69 // If the sync service has some kind of error, report to the user.
70 if (service
->HasUnrecoverableError())
71 return SYNC_STARTUP_ERROR
;
73 // If we have an auth error and sync is not still waiting for new auth tokens
74 // to be fetched, exit.
75 if (!service
->waiting_for_auth() &&
76 service
->GetAuthError().state() != GoogleServiceAuthError::NONE
) {
77 return SYNC_STARTUP_ERROR
;
80 // No error detected yet, but the sync backend hasn't started up yet, so
81 // we're in the pending state.
82 return SYNC_STARTUP_PENDING
;