1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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() {
// Note: the observer may free this object so it is not allowed to access
// this object after invoking the observer callback below.
switch (GetSyncServiceState(profile_)) {
case SYNC_STARTUP_ERROR:
observer_->SyncStartupFailed();
break;
case SYNC_STARTUP_COMPLETE:
observer_->SyncStartupCompleted();
break;
case SYNC_STARTUP_PENDING:
// Do nothing - still waiting for sync to finish starting up.
break;
}
}
// static
SyncStartupTracker::SyncServiceState SyncStartupTracker::GetSyncServiceState(
Profile* profile) {
// If sync is disabled, treat this as a startup error.
if (!profile->IsSyncAccessible())
return SYNC_STARTUP_ERROR;
ProfileSyncService* service =
ProfileSyncServiceFactory::GetForProfile(profile);
// If no service exists or sync is disabled, treat as a startup error.
if (!profile->IsSyncAccessible() || !service ||
!service->IsSyncEnabledAndLoggedIn()) {
return SYNC_STARTUP_ERROR;
}
// If the sync backend has started up, notify the callback.
if (service->backend_initialized())
return SYNC_STARTUP_COMPLETE;
// If the sync service has some kind of error, report to the user.
if (service->HasUnrecoverableError())
return SYNC_STARTUP_ERROR;
// If we have an auth error and sync is not still waiting for new auth tokens
// to be fetched, exit.
if (!service->waiting_for_auth() &&
service->GetAuthError().state() != GoogleServiceAuthError::NONE) {
return SYNC_STARTUP_ERROR;
}
// No error detected yet, but the sync backend hasn't started up yet, so
// we're in the pending state.
return SYNC_STARTUP_PENDING;
}
|