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 not allowed, treat this as a startup error.
53 if (!profile
->IsSyncAllowed())
54 return SYNC_STARTUP_ERROR
;
56 ProfileSyncService
* service
=
57 ProfileSyncServiceFactory::GetForProfile(profile
);
59 // If no service exists or it can't be started, treat as a startup error.
60 if (!service
|| !service
->CanSyncStart()) {
61 return SYNC_STARTUP_ERROR
;
64 // If the sync backend has started up, notify the callback.
65 if (service
->backend_initialized())
66 return SYNC_STARTUP_COMPLETE
;
68 // If the sync service has some kind of error, report to the user.
69 if (service
->HasUnrecoverableError())
70 return SYNC_STARTUP_ERROR
;
72 // If we have an auth error and sync is not still waiting for new auth tokens
73 // to be fetched, exit.
74 if (!service
->waiting_for_auth() &&
75 service
->GetAuthError().state() != GoogleServiceAuthError::NONE
) {
76 return SYNC_STARTUP_ERROR
;
79 // No error detected yet, but the sync backend hasn't started up yet, so
80 // we're in the pending state.
81 return SYNC_STARTUP_PENDING
;