| 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/invalidation/gcm_invalidation_bridge.h" | 9 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" |
| 10 #include "chrome/browser/invalidation/invalidation_auth_provider.h" |
| 10 #include "chrome/browser/invalidation/invalidation_logger.h" | 11 #include "chrome/browser/invalidation/invalidation_logger.h" |
| 11 #include "chrome/browser/invalidation/invalidation_service_util.h" | 12 #include "chrome/browser/invalidation/invalidation_service_util.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/signin/about_signin_internals.h" | 14 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
| 14 #include "chrome/browser/signin/about_signin_internals_factory.h" | |
| 15 #include "chrome/browser/signin/profile_oauth2_token_service.h" | |
| 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 17 #include "google_apis/gaia/gaia_constants.h" | 15 #include "google_apis/gaia/gaia_constants.h" |
| 18 #include "sync/notifier/gcm_network_channel_delegate.h" | 16 #include "sync/notifier/gcm_network_channel_delegate.h" |
| 19 #include "sync/notifier/invalidation_util.h" | 17 #include "sync/notifier/invalidation_util.h" |
| 20 #include "sync/notifier/invalidator.h" | 18 #include "sync/notifier/invalidator.h" |
| 21 #include "sync/notifier/invalidator_state.h" | 19 #include "sync/notifier/invalidator_state.h" |
| 22 #include "sync/notifier/non_blocking_invalidator.h" | 20 #include "sync/notifier/non_blocking_invalidator.h" |
| 23 #include "sync/notifier/object_id_invalidation_map.h" | 21 #include "sync/notifier/object_id_invalidation_map.h" |
| 24 | 22 |
| 25 static const char* kOAuth2Scopes[] = { | 23 static const char* kOAuth2Scopes[] = { |
| 26 GaiaConstants::kGoogleTalkOAuth2Scope | 24 GaiaConstants::kGoogleTalkOAuth2Scope |
| (...skipping 23 matching lines...) Expand all Loading... |
| 50 // has no significant state, -1 to never discard. | 48 // has no significant state, -1 to never discard. |
| 51 -1, | 49 -1, |
| 52 | 50 |
| 53 // 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. |
| 54 false, | 52 false, |
| 55 }; | 53 }; |
| 56 | 54 |
| 57 namespace invalidation { | 55 namespace invalidation { |
| 58 | 56 |
| 59 TiclInvalidationService::TiclInvalidationService( | 57 TiclInvalidationService::TiclInvalidationService( |
| 60 SigninManagerBase* signin, | 58 scoped_ptr<InvalidationAuthProvider> auth_provider, |
| 61 ProfileOAuth2TokenService* oauth2_token_service, | |
| 62 Profile* profile) | 59 Profile* profile) |
| 63 : OAuth2TokenService::Consumer("ticl_invalidation"), | 60 : OAuth2TokenService::Consumer("ticl_invalidation"), |
| 64 profile_(profile), | 61 profile_(profile), |
| 65 signin_manager_(signin), | 62 auth_provider_(auth_provider.Pass()), |
| 66 oauth2_token_service_(oauth2_token_service), | |
| 67 invalidator_registrar_(new syncer::InvalidatorRegistrar()), | 63 invalidator_registrar_(new syncer::InvalidatorRegistrar()), |
| 68 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy), | 64 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy), |
| 69 logger_() {} | 65 logger_() {} |
| 70 | 66 |
| 71 TiclInvalidationService::~TiclInvalidationService() { | 67 TiclInvalidationService::~TiclInvalidationService() { |
| 72 DCHECK(CalledOnValidThread()); | 68 DCHECK(CalledOnValidThread()); |
| 73 } | 69 } |
| 74 | 70 |
| 75 void TiclInvalidationService::Init() { | 71 void TiclInvalidationService::Init() { |
| 76 DCHECK(CalledOnValidThread()); | 72 DCHECK(CalledOnValidThread()); |
| 77 | 73 |
| 78 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 74 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); |
| 79 if (invalidator_storage_->GetInvalidatorClientId().empty()) { | 75 if (invalidator_storage_->GetInvalidatorClientId().empty()) { |
| 80 // This also clears any existing state. We can't reuse old invalidator | 76 // This also clears any existing state. We can't reuse old invalidator |
| 81 // state with the new ID anyway. | 77 // state with the new ID anyway. |
| 82 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 78 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); |
| 83 } | 79 } |
| 84 | 80 |
| 85 if (IsReadyToStart()) { | 81 if (IsReadyToStart()) { |
| 86 StartInvalidator(PUSH_CLIENT_CHANNEL); | 82 StartInvalidator(PUSH_CLIENT_CHANNEL); |
| 87 } | 83 } |
| 88 | 84 |
| 89 SigninManagerBase* signin_manager = | 85 auth_provider_->AddObserver(this); |
| 90 SigninManagerFactory::GetForProfile(profile_); | 86 auth_provider_->GetTokenService()->AddObserver(this); |
| 91 signin_manager->AddObserver(this); | |
| 92 | |
| 93 oauth2_token_service_->AddObserver(this); | |
| 94 } | 87 } |
| 95 | 88 |
| 96 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { | 89 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { |
| 97 // Here we perform the equivalent of Init() and StartInvalidator(), but with | 90 // Here we perform the equivalent of Init() and StartInvalidator(), but with |
| 98 // some minor changes to account for the fact that we're injecting the | 91 // some minor changes to account for the fact that we're injecting the |
| 99 // invalidator. | 92 // invalidator. |
| 100 invalidator_.reset(invalidator); | 93 invalidator_.reset(invalidator); |
| 101 | 94 |
| 102 invalidator_->RegisterHandler(this); | 95 invalidator_->RegisterHandler(this); |
| 103 invalidator_->UpdateRegisteredIds( | 96 invalidator_->UpdateRegisteredIds( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 147 |
| 155 std::string TiclInvalidationService::GetInvalidatorClientId() const { | 148 std::string TiclInvalidationService::GetInvalidatorClientId() const { |
| 156 DCHECK(CalledOnValidThread()); | 149 DCHECK(CalledOnValidThread()); |
| 157 return invalidator_storage_->GetInvalidatorClientId(); | 150 return invalidator_storage_->GetInvalidatorClientId(); |
| 158 } | 151 } |
| 159 | 152 |
| 160 InvalidationLogger* TiclInvalidationService::GetInvalidationLogger() { | 153 InvalidationLogger* TiclInvalidationService::GetInvalidationLogger() { |
| 161 return &logger_; | 154 return &logger_; |
| 162 } | 155 } |
| 163 | 156 |
| 164 void TiclInvalidationService::GoogleSignedOut(const std::string& username) { | 157 InvalidationAuthProvider* |
| 165 DCHECK(CalledOnValidThread()); | 158 TiclInvalidationService::GetInvalidationAuthProvider() { |
| 166 Logout(); | 159 return auth_provider_.get(); |
| 167 } | 160 } |
| 168 | 161 |
| 169 void TiclInvalidationService::RequestAccessToken() { | 162 void TiclInvalidationService::RequestAccessToken() { |
| 170 // Only one active request at a time. | 163 // Only one active request at a time. |
| 171 if (access_token_request_ != NULL) | 164 if (access_token_request_ != NULL) |
| 172 return; | 165 return; |
| 173 request_access_token_retry_timer_.Stop(); | 166 request_access_token_retry_timer_.Stop(); |
| 174 OAuth2TokenService::ScopeSet oauth2_scopes; | 167 OAuth2TokenService::ScopeSet oauth2_scopes; |
| 175 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) | 168 for (size_t i = 0; i < arraysize(kOAuth2Scopes); i++) |
| 176 oauth2_scopes.insert(kOAuth2Scopes[i]); | 169 oauth2_scopes.insert(kOAuth2Scopes[i]); |
| 177 // Invalidate previous token, otherwise token service will return the same | 170 // Invalidate previous token, otherwise token service will return the same |
| 178 // token again. | 171 // token again. |
| 179 const std::string& account_id = signin_manager_->GetAuthenticatedAccountId(); | 172 const std::string& account_id = auth_provider_->GetAccountId(); |
| 180 oauth2_token_service_->InvalidateToken(account_id, | 173 OAuth2TokenService* token_service = auth_provider_->GetTokenService(); |
| 181 oauth2_scopes, | 174 token_service->InvalidateToken(account_id, oauth2_scopes, access_token_); |
| 182 access_token_); | |
| 183 access_token_.clear(); | 175 access_token_.clear(); |
| 184 access_token_request_ = oauth2_token_service_->StartRequest(account_id, | 176 access_token_request_ = |
| 185 oauth2_scopes, | 177 token_service->StartRequest(account_id, oauth2_scopes, this); |
| 186 this); | |
| 187 } | 178 } |
| 188 | 179 |
| 189 void TiclInvalidationService::OnGetTokenSuccess( | 180 void TiclInvalidationService::OnGetTokenSuccess( |
| 190 const OAuth2TokenService::Request* request, | 181 const OAuth2TokenService::Request* request, |
| 191 const std::string& access_token, | 182 const std::string& access_token, |
| 192 const base::Time& expiration_time) { | 183 const base::Time& expiration_time) { |
| 193 DCHECK_EQ(access_token_request_, request); | 184 DCHECK_EQ(access_token_request_, request); |
| 194 access_token_request_.reset(); | 185 access_token_request_.reset(); |
| 195 // Reset backoff time after successful response. | 186 // Reset backoff time after successful response. |
| 196 request_access_token_backoff_.Reset(); | 187 request_access_token_backoff_.Reset(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 227 break; | 218 break; |
| 228 } | 219 } |
| 229 default: { | 220 default: { |
| 230 // We have no way to notify the user of this. Do nothing. | 221 // We have no way to notify the user of this. Do nothing. |
| 231 } | 222 } |
| 232 } | 223 } |
| 233 } | 224 } |
| 234 | 225 |
| 235 void TiclInvalidationService::OnRefreshTokenAvailable( | 226 void TiclInvalidationService::OnRefreshTokenAvailable( |
| 236 const std::string& account_id) { | 227 const std::string& account_id) { |
| 237 if (signin_manager_->GetAuthenticatedAccountId() == account_id) { | 228 if (auth_provider_->GetAccountId() == account_id) { |
| 238 if (!IsStarted() && IsReadyToStart()) { | 229 if (!IsStarted() && IsReadyToStart()) { |
| 239 StartInvalidator(PUSH_CLIENT_CHANNEL); | 230 StartInvalidator(PUSH_CLIENT_CHANNEL); |
| 240 } | 231 } |
| 241 } | 232 } |
| 242 } | 233 } |
| 243 | 234 |
| 244 void TiclInvalidationService::OnRefreshTokenRevoked( | 235 void TiclInvalidationService::OnRefreshTokenRevoked( |
| 245 const std::string& account_id) { | 236 const std::string& account_id) { |
| 246 if (signin_manager_->GetAuthenticatedAccountId() == account_id) { | 237 if (auth_provider_->GetAccountId() == account_id) { |
| 247 access_token_.clear(); | 238 access_token_.clear(); |
| 248 if (IsStarted()) { | 239 if (IsStarted()) { |
| 249 UpdateInvalidatorCredentials(); | 240 UpdateInvalidatorCredentials(); |
| 250 } | 241 } |
| 251 } | 242 } |
| 252 } | 243 } |
| 253 | 244 |
| 245 void TiclInvalidationService::OnInvalidationAuthLogout() { |
| 246 access_token_request_.reset(); |
| 247 request_access_token_retry_timer_.Stop(); |
| 248 |
| 249 if (IsStarted()) { |
| 250 StopInvalidator(); |
| 251 } |
| 252 |
| 253 // This service always expects to have a valid invalidator storage. |
| 254 // So we must not only clear the old one, but also start a new one. |
| 255 invalidator_storage_->Clear(); |
| 256 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); |
| 257 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); |
| 258 } |
| 259 |
| 254 void TiclInvalidationService::OnInvalidatorStateChange( | 260 void TiclInvalidationService::OnInvalidatorStateChange( |
| 255 syncer::InvalidatorState state) { | 261 syncer::InvalidatorState state) { |
| 256 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { | 262 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { |
| 257 // This may be due to normal OAuth access token expiration. If so, we must | 263 // This may be due to normal OAuth access token expiration. If so, we must |
| 258 // fetch a new one using our refresh token. Resetting the invalidator's | 264 // fetch a new one using our refresh token. Resetting the invalidator's |
| 259 // access token will not reset the invalidator's exponential backoff, so | 265 // access token will not reset the invalidator's exponential backoff, so |
| 260 // it's safe to try to update the token every time we receive this signal. | 266 // it's safe to try to update the token every time we receive this signal. |
| 261 // | 267 // |
| 262 // We won't be receiving any invalidations while the refresh is in progress, | 268 // We won't be receiving any invalidations while the refresh is in progress, |
| 263 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials | 269 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials |
| (...skipping 13 matching lines...) Expand all Loading... |
| 277 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 283 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 278 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map); | 284 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map); |
| 279 | 285 |
| 280 logger_.OnInvalidation(invalidation_map); | 286 logger_.OnInvalidation(invalidation_map); |
| 281 } | 287 } |
| 282 | 288 |
| 283 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; } | 289 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; } |
| 284 | 290 |
| 285 void TiclInvalidationService::Shutdown() { | 291 void TiclInvalidationService::Shutdown() { |
| 286 DCHECK(CalledOnValidThread()); | 292 DCHECK(CalledOnValidThread()); |
| 287 SigninManagerBase* signin_manager = | 293 auth_provider_->GetTokenService()->RemoveObserver(this); |
| 288 SigninManagerFactory::GetForProfile(profile_); | 294 auth_provider_->RemoveObserver(this); |
| 289 signin_manager->RemoveObserver(this); | |
| 290 oauth2_token_service_->RemoveObserver(this); | |
| 291 if (IsStarted()) { | 295 if (IsStarted()) { |
| 292 StopInvalidator(); | 296 StopInvalidator(); |
| 293 } | 297 } |
| 294 invalidator_storage_.reset(); | 298 invalidator_storage_.reset(); |
| 295 invalidator_registrar_.reset(); | 299 invalidator_registrar_.reset(); |
| 296 } | 300 } |
| 297 | 301 |
| 298 bool TiclInvalidationService::IsReadyToStart() { | 302 bool TiclInvalidationService::IsReadyToStart() { |
| 299 if (profile_->IsManaged()) { | 303 if (profile_->IsManaged()) { |
| 300 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; | 304 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; |
| 301 return false; | 305 return false; |
| 302 } | 306 } |
| 303 | 307 |
| 304 if (signin_manager_->GetAuthenticatedUsername().empty()) { | 308 if (auth_provider_->GetAccountId().empty()) { |
| 305 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; | 309 DVLOG(2) << "Not starting TiclInvalidationService: User is not signed in."; |
| 306 return false; | 310 return false; |
| 307 } | 311 } |
| 308 | 312 |
| 309 if (!oauth2_token_service_) { | 313 OAuth2TokenService* token_service = auth_provider_->GetTokenService(); |
| 314 if (!token_service) { |
| 310 DVLOG(2) | 315 DVLOG(2) |
| 311 << "Not starting TiclInvalidationService: " | 316 << "Not starting TiclInvalidationService: " |
| 312 << "OAuth2TokenService unavailable."; | 317 << "OAuth2TokenService unavailable."; |
| 313 return false; | 318 return false; |
| 314 } | 319 } |
| 315 | 320 |
| 316 if (!oauth2_token_service_->RefreshTokenIsAvailable( | 321 if (!token_service->RefreshTokenIsAvailable(auth_provider_->GetAccountId())) { |
| 317 signin_manager_->GetAuthenticatedAccountId())) { | |
| 318 DVLOG(2) | 322 DVLOG(2) |
| 319 << "Not starting TiclInvalidationServce: Waiting for refresh token."; | 323 << "Not starting TiclInvalidationServce: Waiting for refresh token."; |
| 320 return false; | 324 return false; |
| 321 } | 325 } |
| 322 | 326 |
| 323 return true; | 327 return true; |
| 324 } | 328 } |
| 325 | 329 |
| 326 bool TiclInvalidationService::IsStarted() { | 330 bool TiclInvalidationService::IsStarted() { |
| 327 return invalidator_.get() != NULL; | 331 return invalidator_.get() != NULL; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 349 notifier::NotifierOptions options = | 353 notifier::NotifierOptions options = |
| 350 ParseNotifierOptions(*CommandLine::ForCurrentProcess()); | 354 ParseNotifierOptions(*CommandLine::ForCurrentProcess()); |
| 351 options.request_context_getter = profile_->GetRequestContext(); | 355 options.request_context_getter = profile_->GetRequestContext(); |
| 352 options.auth_mechanism = "X-OAUTH2"; | 356 options.auth_mechanism = "X-OAUTH2"; |
| 353 DCHECK_EQ(notifier::NOTIFICATION_SERVER, options.notification_method); | 357 DCHECK_EQ(notifier::NOTIFICATION_SERVER, options.notification_method); |
| 354 network_channel_creator = | 358 network_channel_creator = |
| 355 syncer::NonBlockingInvalidator::MakePushClientChannelCreator(options); | 359 syncer::NonBlockingInvalidator::MakePushClientChannelCreator(options); |
| 356 break; | 360 break; |
| 357 } | 361 } |
| 358 case GCM_NETWORK_CHANNEL: { | 362 case GCM_NETWORK_CHANNEL: { |
| 359 gcm_invalidation_bridge_.reset(new GCMInvalidationBridge(profile_)); | 363 gcm::GCMProfileService* gcm_profile_service = |
| 364 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
| 365 gcm_invalidation_bridge_.reset( |
| 366 new GCMInvalidationBridge(gcm_profile_service, auth_provider_.get())); |
| 360 network_channel_creator = | 367 network_channel_creator = |
| 361 syncer::NonBlockingInvalidator::MakeGCMNetworkChannelCreator( | 368 syncer::NonBlockingInvalidator::MakeGCMNetworkChannelCreator( |
| 362 profile_->GetRequestContext(), | 369 profile_->GetRequestContext(), |
| 363 gcm_invalidation_bridge_->CreateDelegate().Pass()); | 370 gcm_invalidation_bridge_->CreateDelegate().Pass()); |
| 364 break; | 371 break; |
| 365 } | 372 } |
| 366 default: { | 373 default: { |
| 367 NOTREACHED(); | 374 NOTREACHED(); |
| 368 return; | 375 return; |
| 369 } | 376 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 380 | 387 |
| 381 UpdateInvalidatorCredentials(); | 388 UpdateInvalidatorCredentials(); |
| 382 | 389 |
| 383 invalidator_->RegisterHandler(this); | 390 invalidator_->RegisterHandler(this); |
| 384 invalidator_->UpdateRegisteredIds( | 391 invalidator_->UpdateRegisteredIds( |
| 385 this, | 392 this, |
| 386 invalidator_registrar_->GetAllRegisteredIds()); | 393 invalidator_registrar_->GetAllRegisteredIds()); |
| 387 } | 394 } |
| 388 | 395 |
| 389 void TiclInvalidationService::UpdateInvalidatorCredentials() { | 396 void TiclInvalidationService::UpdateInvalidatorCredentials() { |
| 390 std::string email = signin_manager_->GetAuthenticatedUsername(); | 397 std::string email = auth_provider_->GetAccountId(); |
| 391 | 398 |
| 392 DCHECK(!email.empty()) << "Expected user to be signed in."; | 399 DCHECK(!email.empty()) << "Expected user to be signed in."; |
| 393 | 400 |
| 394 DVLOG(2) << "UpdateCredentials: " << email; | 401 DVLOG(2) << "UpdateCredentials: " << email; |
| 395 invalidator_->UpdateCredentials(email, access_token_); | 402 invalidator_->UpdateCredentials(email, access_token_); |
| 396 } | 403 } |
| 397 | 404 |
| 398 void TiclInvalidationService::StopInvalidator() { | 405 void TiclInvalidationService::StopInvalidator() { |
| 399 DCHECK(invalidator_); | 406 DCHECK(invalidator_); |
| 400 gcm_invalidation_bridge_.reset(); | 407 gcm_invalidation_bridge_.reset(); |
| 401 invalidator_->UnregisterHandler(this); | 408 invalidator_->UnregisterHandler(this); |
| 402 invalidator_.reset(); | 409 invalidator_.reset(); |
| 403 } | 410 } |
| 404 | 411 |
| 405 void TiclInvalidationService::Logout() { | |
| 406 access_token_request_.reset(); | |
| 407 request_access_token_retry_timer_.Stop(); | |
| 408 | |
| 409 if (IsStarted()) { | |
| 410 StopInvalidator(); | |
| 411 } | |
| 412 | |
| 413 // This service always expects to have a valid invalidator storage. | |
| 414 // So we must not only clear the old one, but also start a new one. | |
| 415 invalidator_storage_->Clear(); | |
| 416 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | |
| 417 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | |
| 418 } | |
| 419 | |
| 420 } // namespace invalidation | 412 } // namespace invalidation |
| OLD | NEW |