| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/invalidation/ticl_invalidation_service.h" | 5 #include "chrome/browser/invalidation/ticl_invalidation_service.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/invalidation/invalidation_service_util.h" | 10 #include "chrome/browser/invalidation/invalidation_service_util.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 -1, | 49 -1, |
| 50 | 50 |
| 51 // Don't use initial delay unless the last request was an error. | 51 // Don't use initial delay unless the last request was an error. |
| 52 false, | 52 false, |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 namespace invalidation { | 55 namespace invalidation { |
| 56 | 56 |
| 57 TiclInvalidationService::TiclInvalidationService( | 57 TiclInvalidationService::TiclInvalidationService( |
| 58 SigninManagerBase* signin, | 58 SigninManagerBase* signin, |
| 59 TokenService* token_service, | 59 ProfileOAuth2TokenService* oauth2_token_service, |
| 60 OAuth2TokenService* oauth2_token_service, | |
| 61 Profile* profile) | 60 Profile* profile) |
| 62 : profile_(profile), | 61 : profile_(profile), |
| 63 signin_manager_(signin), | 62 signin_manager_(signin), |
| 64 token_service_(token_service), | |
| 65 oauth2_token_service_(oauth2_token_service), | 63 oauth2_token_service_(oauth2_token_service), |
| 66 invalidator_registrar_(new syncer::InvalidatorRegistrar()), | 64 invalidator_registrar_(new syncer::InvalidatorRegistrar()), |
| 67 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy) { | 65 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy) { |
| 68 } | 66 } |
| 69 | 67 |
| 70 TiclInvalidationService::~TiclInvalidationService() { | 68 TiclInvalidationService::~TiclInvalidationService() { |
| 71 DCHECK(CalledOnValidThread()); | 69 DCHECK(CalledOnValidThread()); |
| 72 } | 70 } |
| 73 | 71 |
| 74 void TiclInvalidationService::Init() { | 72 void TiclInvalidationService::Init() { |
| 75 DCHECK(CalledOnValidThread()); | 73 DCHECK(CalledOnValidThread()); |
| 76 | 74 |
| 77 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 75 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); |
| 78 if (invalidator_storage_->GetInvalidatorClientId().empty()) { | 76 if (invalidator_storage_->GetInvalidatorClientId().empty()) { |
| 79 // This also clears any existing state. We can't reuse old invalidator | 77 // This also clears any existing state. We can't reuse old invalidator |
| 80 // state with the new ID anyway. | 78 // state with the new ID anyway. |
| 81 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 79 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); |
| 82 } | 80 } |
| 83 | 81 |
| 84 if (IsReadyToStart()) { | 82 if (IsReadyToStart()) { |
| 85 StartInvalidator(); | 83 StartInvalidator(); |
| 86 } | 84 } |
| 87 | 85 |
| 88 notification_registrar_.Add(this, | 86 notification_registrar_.Add(this, |
| 89 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | 87 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
| 90 content::Source<Profile>(profile_)); | 88 content::Source<Profile>(profile_)); |
| 91 notification_registrar_.Add(this, | 89 oauth2_token_service_->AddObserver(this); |
| 92 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
| 93 content::Source<TokenService>(token_service_)); | |
| 94 notification_registrar_.Add(this, | |
| 95 chrome::NOTIFICATION_TOKENS_CLEARED, | |
| 96 content::Source<TokenService>(token_service_)); | |
| 97 } | 90 } |
| 98 | 91 |
| 99 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { | 92 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { |
| 100 // Here we perform the equivalent of Init() and StartInvalidator(), but with | 93 // Here we perform the equivalent of Init() and StartInvalidator(), but with |
| 101 // some minor changes to account for the fact that we're injecting the | 94 // some minor changes to account for the fact that we're injecting the |
| 102 // invalidator. | 95 // invalidator. |
| 103 invalidator_.reset(invalidator); | 96 invalidator_.reset(invalidator); |
| 104 | 97 |
| 105 invalidator_->RegisterHandler(this); | 98 invalidator_->RegisterHandler(this); |
| 106 invalidator_->UpdateRegisteredIds( | 99 invalidator_->UpdateRegisteredIds( |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 std::string TiclInvalidationService::GetInvalidatorClientId() const { | 157 std::string TiclInvalidationService::GetInvalidatorClientId() const { |
| 165 DCHECK(CalledOnValidThread()); | 158 DCHECK(CalledOnValidThread()); |
| 166 return invalidator_storage_->GetInvalidatorClientId(); | 159 return invalidator_storage_->GetInvalidatorClientId(); |
| 167 } | 160 } |
| 168 | 161 |
| 169 void TiclInvalidationService::Observe( | 162 void TiclInvalidationService::Observe( |
| 170 int type, | 163 int type, |
| 171 const content::NotificationSource& source, | 164 const content::NotificationSource& source, |
| 172 const content::NotificationDetails& details) { | 165 const content::NotificationDetails& details) { |
| 173 DCHECK(CalledOnValidThread()); | 166 DCHECK(CalledOnValidThread()); |
| 174 | 167 DCHECK_EQ(type, chrome::NOTIFICATION_GOOGLE_SIGNED_OUT); |
| 175 switch (type) { | 168 Logout(); |
| 176 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { | |
| 177 if (!IsStarted() && IsReadyToStart()) { | |
| 178 StartInvalidator(); | |
| 179 } | |
| 180 break; | |
| 181 } | |
| 182 case chrome::NOTIFICATION_TOKENS_CLEARED: { | |
| 183 access_token_.clear(); | |
| 184 if (IsStarted()) { | |
| 185 UpdateInvalidatorCredentials(); | |
| 186 } | |
| 187 break; | |
| 188 } | |
| 189 case chrome::NOTIFICATION_GOOGLE_SIGNED_OUT: { | |
| 190 Logout(); | |
| 191 break; | |
| 192 } | |
| 193 default: { | |
| 194 NOTREACHED(); | |
| 195 } | |
| 196 } | |
| 197 } | 169 } |
| 198 | 170 |
| 199 void TiclInvalidationService::RequestAccessToken() { | 171 void TiclInvalidationService::RequestAccessToken() { |
| 200 // Only one active request at a time. | 172 // Only one active request at a time. |
| 201 if (access_token_request_ != NULL) | 173 if (access_token_request_ != NULL) |
| 202 return; | 174 return; |
| 203 request_access_token_retry_timer_.Stop(); | 175 request_access_token_retry_timer_.Stop(); |
| 204 OAuth2TokenService::ScopeSet oauth2_scopes; | 176 OAuth2TokenService::ScopeSet oauth2_scopes; |
| 205 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) | 177 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) |
| 206 oauth2_scopes.insert(kOAuth2Scopes[i]); | 178 oauth2_scopes.insert(kOAuth2Scopes[i]); |
| 207 // Invalidate previous token, otherwise token service will return the same | 179 // Invalidate previous token, otherwise token service will return the same |
| 208 // token again. | 180 // token again. |
| 209 oauth2_token_service_->InvalidateToken(oauth2_scopes, access_token_); | 181 oauth2_token_service_->InvalidateToken(oauth2_scopes, access_token_); |
| 210 access_token_.clear(); | 182 access_token_.clear(); |
| 211 access_token_request_ = | 183 access_token_request_ = oauth2_token_service_->StartRequest( |
| 212 oauth2_token_service_->StartRequest(oauth2_scopes, this); | 184 oauth2_token_service_->GetPrimaryAccountId(), oauth2_scopes, this); |
| 213 } | 185 } |
| 214 | 186 |
| 215 void TiclInvalidationService::OnGetTokenSuccess( | 187 void TiclInvalidationService::OnGetTokenSuccess( |
| 216 const OAuth2TokenService::Request* request, | 188 const OAuth2TokenService::Request* request, |
| 217 const std::string& access_token, | 189 const std::string& access_token, |
| 218 const base::Time& expiration_time) { | 190 const base::Time& expiration_time) { |
| 219 DCHECK_EQ(access_token_request_, request); | 191 DCHECK_EQ(access_token_request_, request); |
| 220 access_token_request_.reset(); | 192 access_token_request_.reset(); |
| 221 // Reset backoff time after successful response. | 193 // Reset backoff time after successful response. |
| 222 request_access_token_backoff_.Reset(); | 194 request_access_token_backoff_.Reset(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 invalidator_registrar_->UpdateInvalidatorState( | 240 invalidator_registrar_->UpdateInvalidatorState( |
| 269 syncer::INVALIDATION_CREDENTIALS_REJECTED); | 241 syncer::INVALIDATION_CREDENTIALS_REJECTED); |
| 270 break; | 242 break; |
| 271 } | 243 } |
| 272 default: { | 244 default: { |
| 273 // We have no way to notify the user of this. Do nothing. | 245 // We have no way to notify the user of this. Do nothing. |
| 274 } | 246 } |
| 275 } | 247 } |
| 276 } | 248 } |
| 277 | 249 |
| 250 void TiclInvalidationService::OnRefreshTokenAvailable( |
| 251 const std::string& account_id) { |
| 252 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) { |
| 253 if (!IsStarted() && IsReadyToStart()) { |
| 254 StartInvalidator(); |
| 255 } |
| 256 } |
| 257 } |
| 258 |
| 259 void TiclInvalidationService::OnRefreshTokenRevoked( |
| 260 const std::string& account_id) { |
| 261 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) { |
| 262 access_token_.clear(); |
| 263 if (IsStarted()) { |
| 264 UpdateInvalidatorCredentials(); |
| 265 } |
| 266 } |
| 267 } |
| 268 |
| 278 void TiclInvalidationService::OnInvalidatorStateChange( | 269 void TiclInvalidationService::OnInvalidatorStateChange( |
| 279 syncer::InvalidatorState state) { | 270 syncer::InvalidatorState state) { |
| 280 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { | 271 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { |
| 281 // This may be due to normal OAuth access token expiration. If so, we must | 272 // This may be due to normal OAuth access token expiration. If so, we must |
| 282 // fetch a new one using our refresh token. Resetting the invalidator's | 273 // fetch a new one using our refresh token. Resetting the invalidator's |
| 283 // access token will not reset the invalidator's exponential backoff, so | 274 // access token will not reset the invalidator's exponential backoff, so |
| 284 // it's safe to try to update the token every time we receive this signal. | 275 // it's safe to try to update the token every time we receive this signal. |
| 285 // | 276 // |
| 286 // We won't be receiving any invalidations while the refresh is in progress, | 277 // We won't be receiving any invalidations while the refresh is in progress, |
| 287 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials | 278 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials |
| 288 // really are invalid, the refresh request should fail and | 279 // really are invalid, the refresh request should fail and |
| 289 // OnGetTokenFailure() will put us into a INVALIDATION_CREDENTIALS_REJECTED | 280 // OnGetTokenFailure() will put us into a INVALIDATION_CREDENTIALS_REJECTED |
| 290 // state. | 281 // state. |
| 291 invalidator_registrar_->UpdateInvalidatorState( | 282 invalidator_registrar_->UpdateInvalidatorState( |
| 292 syncer::TRANSIENT_INVALIDATION_ERROR); | 283 syncer::TRANSIENT_INVALIDATION_ERROR); |
| 293 RequestAccessToken(); | 284 RequestAccessToken(); |
| 294 } else { | 285 } else { |
| 295 invalidator_registrar_->UpdateInvalidatorState(state); | 286 invalidator_registrar_->UpdateInvalidatorState(state); |
| 296 } | 287 } |
| 297 } | 288 } |
| 298 | 289 |
| 299 void TiclInvalidationService::OnIncomingInvalidation( | 290 void TiclInvalidationService::OnIncomingInvalidation( |
| 300 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 291 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 301 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map); | 292 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map); |
| 302 } | 293 } |
| 303 | 294 |
| 304 void TiclInvalidationService::Shutdown() { | 295 void TiclInvalidationService::Shutdown() { |
| 305 DCHECK(CalledOnValidThread()); | 296 DCHECK(CalledOnValidThread()); |
| 297 oauth2_token_service_->RemoveObserver(this); |
| 306 if (IsStarted()) { | 298 if (IsStarted()) { |
| 307 StopInvalidator(); | 299 StopInvalidator(); |
| 308 } | 300 } |
| 309 invalidator_storage_.reset(); | 301 invalidator_storage_.reset(); |
| 310 invalidator_registrar_.reset(); | 302 invalidator_registrar_.reset(); |
| 311 } | 303 } |
| 312 | 304 |
| 313 bool TiclInvalidationService::IsReadyToStart() { | 305 bool TiclInvalidationService::IsReadyToStart() { |
| 314 if (profile_->IsManaged()) { | 306 if (profile_->IsManaged()) { |
| 315 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; | 307 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; |
| 316 return false; | 308 return false; |
| 317 } | 309 } |
| 318 | 310 |
| 319 if (signin_manager_->GetAuthenticatedUsername().empty()) { | 311 if (signin_manager_->GetAuthenticatedUsername().empty()) { |
| 320 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; | 312 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; |
| 321 return false; | 313 return false; |
| 322 } | 314 } |
| 323 | 315 |
| 324 if (!oauth2_token_service_) { | 316 if (!oauth2_token_service_) { |
| 325 DVLOG(2) | 317 DVLOG(2) |
| 326 << "Not starting TiclInvalidationService: TokenService unavailable."; | 318 << "Not starting TiclInvalidationService: TokenService unavailable."; |
| 327 return false; | 319 return false; |
| 328 } | 320 } |
| 329 | 321 |
| 330 if (!oauth2_token_service_->RefreshTokenIsAvailable()) { | 322 if (!oauth2_token_service_->RefreshTokenIsAvailable( |
| 323 oauth2_token_service_->GetPrimaryAccountId())) { |
| 331 DVLOG(2) | 324 DVLOG(2) |
| 332 << "Not starting TiclInvalidationServce: Waiting for refresh token."; | 325 << "Not starting TiclInvalidationServce: Waiting for refresh token."; |
| 333 return false; | 326 return false; |
| 334 } | 327 } |
| 335 | 328 |
| 336 return true; | 329 return true; |
| 337 } | 330 } |
| 338 | 331 |
| 339 bool TiclInvalidationService::IsStarted() { | 332 bool TiclInvalidationService::IsStarted() { |
| 340 return invalidator_.get() != NULL; | 333 return invalidator_.get() != NULL; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 } | 392 } |
| 400 | 393 |
| 401 // This service always expects to have a valid invalidator storage. | 394 // This service always expects to have a valid invalidator storage. |
| 402 // So we must not only clear the old one, but also start a new one. | 395 // So we must not only clear the old one, but also start a new one. |
| 403 invalidator_storage_->Clear(); | 396 invalidator_storage_->Clear(); |
| 404 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 397 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); |
| 405 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 398 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); |
| 406 } | 399 } |
| 407 | 400 |
| 408 } // namespace invalidation | 401 } // namespace invalidation |
| OLD | NEW |