OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/signin/signin_tracker.h" | 5 #include "chrome/browser/signin/signin_tracker.h" |
6 | 6 |
7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/signin/signin_manager.h" | 8 #include "chrome/browser/signin/signin_manager.h" |
9 #include "chrome/browser/signin/signin_manager_factory.h" | 9 #include "chrome/browser/signin/signin_manager_factory.h" |
10 #include "chrome/browser/signin/token_service.h" | 10 #include "chrome/browser/signin/token_service.h" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 void SigninTracker::OnStateChanged() { | 122 void SigninTracker::OnStateChanged() { |
123 HandleServiceStateChange(); | 123 HandleServiceStateChange(); |
124 } | 124 } |
125 | 125 |
126 void SigninTracker::HandleServiceStateChange() { | 126 void SigninTracker::HandleServiceStateChange() { |
127 if (state_ != SERVICES_INITIALIZING) { | 127 if (state_ != SERVICES_INITIALIZING) { |
128 // Ignore service updates until after our GAIA credentials are validated. | 128 // Ignore service updates until after our GAIA credentials are validated. |
129 return; | 129 return; |
130 } | 130 } |
131 | 131 |
132 if (SigninManagerFactory::GetForProfile(profile_)-> | 132 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); |
133 GetAuthenticatedUsername().empty()) { | 133 if (signin->GetAuthenticatedUsername().empty()) { |
134 // User is signed out, trigger a signin failure. | 134 // User is signed out, trigger a signin failure. |
135 state_ = WAITING_FOR_GAIA_VALIDATION; | 135 state_ = WAITING_FOR_GAIA_VALIDATION; |
136 observer_->SigninFailed( | 136 observer_->SigninFailed( |
137 GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED)); | 137 GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED)); |
138 return; | 138 return; |
139 } | 139 } |
140 | 140 |
141 // Wait until all of our services are logged in. For now this just means sync. | 141 // Wait until all of our services are logged in. For now this just means sync. |
142 // Long term, we should separate out service auth failures from the signin | 142 // Long term, we should separate out service auth failures from the signin |
143 // process, but for the current UI flow we'll validate service signin status | 143 // process, but for the current UI flow we'll validate service signin status |
144 // also. | 144 // also. |
145 // TODO(atwilson): Move the code to wait for app notification oauth tokens out | 145 ProfileSyncService* service = profile_->IsSyncAccessible() ? |
146 // of ProfileSyncService and over to here (http://crbug.com/114209). | 146 ProfileSyncServiceFactory::GetForProfile(profile_) : NULL; |
147 ProfileSyncService* service = | 147 if (service && service->waiting_for_auth()) { |
148 ProfileSyncServiceFactory::GetForProfile(profile_); | |
149 if (service->waiting_for_auth()) { | |
150 // Still waiting for an auth token to come in so stay in the INITIALIZING | 148 // Still waiting for an auth token to come in so stay in the INITIALIZING |
151 // state (we do this to avoid triggering an early signin error in the case | 149 // state (we do this to avoid triggering an early signin error in the case |
152 // where there's a previous auth error in the sync service that hasn't | 150 // where there's a previous auth error in the sync service that hasn't |
153 // been cleared yet). | 151 // been cleared yet). |
154 return; | 152 return; |
155 } | 153 } |
156 | 154 |
157 // If we haven't loaded all our service tokens yet, just exit (we'll be called | 155 // If we haven't loaded all our service tokens yet, just exit (we'll be called |
158 // again when another token is loaded, or will transition to SigninFailed if | 156 // again when another token is loaded, or will transition to SigninFailed if |
159 // the loading fails). | 157 // the loading fails). |
160 if (!AreServiceTokensLoaded(profile_)) | 158 if (!AreServiceTokensLoaded(profile_)) |
161 return; | 159 return; |
162 if (!AreServicesSignedIn(profile_)) { | 160 if (!AreServicesSignedIn(profile_)) { |
163 state_ = WAITING_FOR_GAIA_VALIDATION; | 161 state_ = WAITING_FOR_GAIA_VALIDATION; |
164 observer_->SigninFailed(service->GetAuthError()); | 162 observer_->SigninFailed(signin->signin_global_error()->GetLastAuthError()); |
165 } else if (service->sync_initialized()) { | 163 } else if (!service || service->sync_initialized()) { |
166 state_ = SIGNIN_COMPLETE; | 164 state_ = SIGNIN_COMPLETE; |
167 observer_->SigninSuccess(); | 165 observer_->SigninSuccess(); |
168 } | 166 } |
169 } | 167 } |
170 | 168 |
171 // static | 169 // static |
172 bool SigninTracker::AreServiceTokensLoaded(Profile* profile) { | 170 bool SigninTracker::AreServiceTokensLoaded(Profile* profile) { |
173 // See if we have all of the tokens required. | 171 // See if we have all of the tokens required. |
174 TokenService* token_service = TokenServiceFactory::GetForProfile(profile); | 172 TokenService* token_service = TokenServiceFactory::GetForProfile(profile); |
175 for (int i = 0; i < kNumSignedInServices; ++i) { | 173 for (int i = 0; i < kNumSignedInServices; ++i) { |
176 if (!token_service->HasTokenForService(kSignedInServices[i])) { | 174 if (!token_service->HasTokenForService(kSignedInServices[i])) { |
177 // Don't have a token for one of our signed-in services. | 175 // Don't have a token for one of our signed-in services. |
178 return false; | 176 return false; |
179 } | 177 } |
180 } | 178 } |
181 return true; | 179 return true; |
182 } | 180 } |
183 | 181 |
184 // static | 182 // static |
185 bool SigninTracker::AreServicesSignedIn(Profile* profile) { | 183 bool SigninTracker::AreServicesSignedIn(Profile* profile) { |
186 if (!AreServiceTokensLoaded(profile)) | 184 if (!AreServiceTokensLoaded(profile)) |
187 return false; | 185 return false; |
| 186 // Don't care about the sync state if sync is disabled by policy. |
| 187 if (!profile->IsSyncAccessible()) |
| 188 return true; |
188 ProfileSyncService* service = | 189 ProfileSyncService* service = |
189 ProfileSyncServiceFactory::GetForProfile(profile); | 190 ProfileSyncServiceFactory::GetForProfile(profile); |
190 return (service->IsSyncEnabledAndLoggedIn() && | 191 return (service->IsSyncEnabledAndLoggedIn() && |
191 service->IsSyncTokenAvailable() && | 192 service->IsSyncTokenAvailable() && |
192 service->GetAuthError().state() == GoogleServiceAuthError::NONE && | 193 service->GetAuthError().state() == GoogleServiceAuthError::NONE && |
193 !service->HasUnrecoverableError()); | 194 !service->HasUnrecoverableError()); |
194 } | 195 } |
OLD | NEW |