OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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. |
| 4 |
| 5 #include "chrome/browser/ui/webui/signin/signin_tracker.h" |
| 6 |
| 7 #include "chrome/browser/sync/profile_sync_service.h" |
| 8 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 9 #include "chrome/common/chrome_notification_types.h" |
| 10 #include "content/public/browser/notification_details.h" |
| 11 #include "content/public/browser/notification_source.h" |
| 12 |
| 13 SigninTracker::SigninTracker(Profile* profile, Observer* observer) |
| 14 : state_(WAITING_FOR_GAIA_VALIDATION), |
| 15 profile_(profile), |
| 16 observer_(observer), |
| 17 credentials_valid_(false) { |
| 18 DCHECK(observer_); |
| 19 // Register for notifications from the SigninManager. |
| 20 registrar_.Add(this, |
| 21 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 22 content::Source<Profile>(profile_)); |
| 23 registrar_.Add(this, |
| 24 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
| 25 content::Source<Profile>(profile_)); |
| 26 |
| 27 // Also listen for notifications from the various signed in services (only |
| 28 // sync for now). |
| 29 ProfileSyncService* service = |
| 30 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 31 service->AddObserver(this); |
| 32 } |
| 33 |
| 34 SigninTracker::~SigninTracker() { |
| 35 ProfileSyncService* service = |
| 36 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 37 service->RemoveObserver(this); |
| 38 } |
| 39 |
| 40 void SigninTracker::Observe(int type, |
| 41 const content::NotificationSource& source, |
| 42 const content::NotificationDetails& details) { |
| 43 // We should not get more than one of these notifications. |
| 44 DCHECK_EQ(state_, WAITING_FOR_GAIA_VALIDATION); |
| 45 switch (type) { |
| 46 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: |
| 47 state_ = SERVICES_INITIALIZING; |
| 48 observer_->GaiaCredentialsValid(); |
| 49 break; |
| 50 case chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED: |
| 51 state_ = WAITING_FOR_GAIA_VALIDATION; |
| 52 observer_->SigninFailed(); |
| 53 break; |
| 54 default: |
| 55 NOTREACHED(); |
| 56 } |
| 57 } |
| 58 |
| 59 // Called when the ProfileSyncService state changes. |
| 60 void SigninTracker::OnStateChanged() { |
| 61 if (state_ != SERVICES_INITIALIZING) { |
| 62 // Ignore service updates until after our GAIA credentials are validated. |
| 63 return; |
| 64 } |
| 65 // Wait until all of our services are logged in. For now this just means sync. |
| 66 // Long term, we should separate out service auth failures from the signin |
| 67 // process, but for the current UI flow we'll validate service signin status |
| 68 // also. |
| 69 // TODO(atwilson): Move the code to wait for app notification oauth tokens out |
| 70 // of ProfileSyncService and over to here. |
| 71 ProfileSyncService* service = |
| 72 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 73 if (service->waiting_for_auth()) { |
| 74 // Still waiting for an auth token to come in so stay in the INITIALIZING |
| 75 // state (we do this to avoid triggering an early signin error in the case |
| 76 // where there's a previous auth error in the sync service that hasn't |
| 77 // been cleared yet). |
| 78 return; |
| 79 } |
| 80 |
| 81 if (!AreServicesSignedIn(profile_)) { |
| 82 state_ = WAITING_FOR_GAIA_VALIDATION; |
| 83 observer_->SigninFailed(); |
| 84 } else if (service->sync_initialized()) { |
| 85 state_ = SIGNIN_COMPLETE; |
| 86 observer_->SigninSuccess(); |
| 87 } |
| 88 } |
| 89 |
| 90 // static |
| 91 bool SigninTracker::AreServicesSignedIn(Profile* profile) { |
| 92 ProfileSyncService* service = |
| 93 ProfileSyncServiceFactory::GetForProfile(profile); |
| 94 return (service->AreCredentialsAvailable() && |
| 95 service->GetAuthError().state() == GoogleServiceAuthError::NONE && |
| 96 !service->unrecoverable_error_detected()); |
| 97 } |
| 98 |
OLD | NEW |