This source file includes following definitions.
- observer_
- OnStateChanged
- CheckServiceState
- GetSyncServiceState
#include "chrome/browser/sync/sync_startup_tracker.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
SyncStartupTracker::SyncStartupTracker(Profile* profile, Observer* observer)
: profile_(profile),
observer_(observer) {
ProfileSyncService* service = ProfileSyncServiceFactory::GetForProfile(
profile_);
if (service)
service->AddObserver(this);
CheckServiceState();
}
SyncStartupTracker::~SyncStartupTracker() {
ProfileSyncService* service = ProfileSyncServiceFactory::GetForProfile(
profile_);
if (service)
service->RemoveObserver(this);
}
void SyncStartupTracker::OnStateChanged() {
CheckServiceState();
}
void SyncStartupTracker::CheckServiceState() {
switch (GetSyncServiceState(profile_)) {
case SYNC_STARTUP_ERROR:
observer_->SyncStartupFailed();
break;
case SYNC_STARTUP_COMPLETE:
observer_->SyncStartupCompleted();
break;
case SYNC_STARTUP_PENDING:
break;
}
}
SyncStartupTracker::SyncServiceState SyncStartupTracker::GetSyncServiceState(
Profile* profile) {
if (!profile->IsSyncAccessible())
return SYNC_STARTUP_ERROR;
ProfileSyncService* service =
ProfileSyncServiceFactory::GetForProfile(profile);
if (!profile->IsSyncAccessible() || !service ||
!service->IsSyncEnabledAndLoggedIn()) {
return SYNC_STARTUP_ERROR;
}
if (service->sync_initialized())
return SYNC_STARTUP_COMPLETE;
if (service->HasUnrecoverableError())
return SYNC_STARTUP_ERROR;
if (!service->waiting_for_auth() &&
service->GetAuthError().state() != GoogleServiceAuthError::NONE) {
return SYNC_STARTUP_ERROR;
}
return SYNC_STARTUP_PENDING;
}