Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / sync / sync_startup_tracker.cc
blobb33732c56539edd0d5ece06dd7ea83988414fcc8
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)
12 : profile_(profile),
13 observer_(observer) {
14 ProfileSyncService* service = ProfileSyncServiceFactory::GetForProfile(
15 profile_);
16 if (service)
17 service->AddObserver(this);
19 CheckServiceState();
22 SyncStartupTracker::~SyncStartupTracker() {
23 ProfileSyncService* service = ProfileSyncServiceFactory::GetForProfile(
24 profile_);
25 if (service)
26 service->RemoveObserver(this);
29 void SyncStartupTracker::OnStateChanged() {
30 CheckServiceState();
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();
39 break;
40 case SYNC_STARTUP_COMPLETE:
41 observer_->SyncStartupCompleted();
42 break;
43 case SYNC_STARTUP_PENDING:
44 // Do nothing - still waiting for sync to finish starting up.
45 break;
49 // static
50 SyncStartupTracker::SyncServiceState SyncStartupTracker::GetSyncServiceState(
51 Profile* profile) {
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;