| 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 "base/prefs/pref_service.h" | |
| 10 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" | 9 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" |
| 11 #include "chrome/browser/invalidation/invalidation_service_util.h" | 10 #include "chrome/browser/invalidation/invalidation_service_util.h" |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/services/gcm/gcm_profile_service.h" | |
| 14 #include "chrome/browser/services/gcm/gcm_service.h" | 11 #include "chrome/browser/services/gcm/gcm_service.h" |
| 15 #include "chrome/common/chrome_content_client.h" | 12 #include "chrome/common/chrome_content_client.h" |
| 16 #include "chrome/common/chrome_switches.h" | |
| 17 #include "chrome/common/pref_names.h" | |
| 18 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 19 #include "google_apis/gaia/gaia_constants.h" | 13 #include "google_apis/gaia/gaia_constants.h" |
| 20 #include "net/url_request/url_request_context_getter.h" | 14 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "sync/notifier/gcm_network_channel_delegate.h" | 15 #include "sync/notifier/gcm_network_channel_delegate.h" |
| 22 #include "sync/notifier/invalidation_util.h" | 16 #include "sync/notifier/invalidation_util.h" |
| 23 #include "sync/notifier/invalidator.h" | 17 #include "sync/notifier/invalidator.h" |
| 24 #include "sync/notifier/invalidator_state.h" | 18 #include "sync/notifier/invalidator_state.h" |
| 25 #include "sync/notifier/non_blocking_invalidator.h" | 19 #include "sync/notifier/non_blocking_invalidator.h" |
| 26 #include "sync/notifier/object_id_invalidation_map.h" | 20 #include "sync/notifier/object_id_invalidation_map.h" |
| 27 | 21 |
| 28 static const char* kOAuth2Scopes[] = { | 22 static const char* kOAuth2Scopes[] = { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 54 -1, | 48 -1, |
| 55 | 49 |
| 56 // Don't use initial delay unless the last request was an error. | 50 // Don't use initial delay unless the last request was an error. |
| 57 false, | 51 false, |
| 58 }; | 52 }; |
| 59 | 53 |
| 60 namespace invalidation { | 54 namespace invalidation { |
| 61 | 55 |
| 62 TiclInvalidationService::TiclInvalidationService( | 56 TiclInvalidationService::TiclInvalidationService( |
| 63 scoped_ptr<IdentityProvider> identity_provider, | 57 scoped_ptr<IdentityProvider> identity_provider, |
| 58 scoped_ptr<TiclSettingsProvider> settings_provider, |
| 64 gcm::GCMService* gcm_service, | 59 gcm::GCMService* gcm_service, |
| 65 const scoped_refptr<net::URLRequestContextGetter>& request_context, | 60 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 66 Profile* profile) | |
| 67 : OAuth2TokenService::Consumer("ticl_invalidation"), | 61 : OAuth2TokenService::Consumer("ticl_invalidation"), |
| 68 profile_(profile), | |
| 69 identity_provider_(identity_provider.Pass()), | 62 identity_provider_(identity_provider.Pass()), |
| 63 settings_provider_(settings_provider.Pass()), |
| 70 invalidator_registrar_(new syncer::InvalidatorRegistrar()), | 64 invalidator_registrar_(new syncer::InvalidatorRegistrar()), |
| 71 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy), | 65 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy), |
| 72 network_channel_type_(PUSH_CLIENT_CHANNEL), | 66 network_channel_type_(PUSH_CLIENT_CHANNEL), |
| 73 gcm_service_(gcm_service), | 67 gcm_service_(gcm_service), |
| 74 request_context_(request_context), | 68 request_context_(request_context), |
| 75 logger_() {} | 69 logger_() {} |
| 76 | 70 |
| 77 TiclInvalidationService::~TiclInvalidationService() { | 71 TiclInvalidationService::~TiclInvalidationService() { |
| 78 DCHECK(CalledOnValidThread()); | 72 DCHECK(CalledOnValidThread()); |
| 79 } | 73 } |
| 80 | 74 |
| 81 void TiclInvalidationService::Init( | 75 void TiclInvalidationService::Init( |
| 82 scoped_ptr<syncer::InvalidationStateTracker> invalidation_state_tracker) { | 76 scoped_ptr<syncer::InvalidationStateTracker> invalidation_state_tracker) { |
| 83 DCHECK(CalledOnValidThread()); | 77 DCHECK(CalledOnValidThread()); |
| 84 invalidation_state_tracker_ = invalidation_state_tracker.Pass(); | 78 invalidation_state_tracker_ = invalidation_state_tracker.Pass(); |
| 85 | 79 |
| 86 if (invalidation_state_tracker_->GetInvalidatorClientId().empty()) { | 80 if (invalidation_state_tracker_->GetInvalidatorClientId().empty()) { |
| 87 invalidation_state_tracker_->ClearAndSetNewClientId( | 81 invalidation_state_tracker_->ClearAndSetNewClientId( |
| 88 GenerateInvalidatorClientId()); | 82 GenerateInvalidatorClientId()); |
| 89 } | 83 } |
| 90 | 84 |
| 91 pref_change_registrar_.Init(profile_->GetPrefs()); | |
| 92 pref_change_registrar_.Add( | |
| 93 prefs::kInvalidationServiceUseGCMChannel, | |
| 94 base::Bind(&TiclInvalidationService::UpdateInvalidationNetworkChannel, | |
| 95 base::Unretained(this))); | |
| 96 pref_change_registrar_.Add( | |
| 97 prefs::kGCMChannelEnabled, | |
| 98 base::Bind(&TiclInvalidationService::UpdateInvalidationNetworkChannel, | |
| 99 base::Unretained(this))); | |
| 100 | |
| 101 UpdateInvalidationNetworkChannel(); | 85 UpdateInvalidationNetworkChannel(); |
| 102 UMA_HISTOGRAM_ENUMERATION("Invalidations.NetworkChannel", | 86 UMA_HISTOGRAM_ENUMERATION("Invalidations.NetworkChannel", |
| 103 network_channel_type_, | 87 network_channel_type_, |
| 104 NETWORK_CHANNELS_COUNT); | 88 NETWORK_CHANNELS_COUNT); |
| 105 | 89 |
| 106 if (IsReadyToStart()) { | 90 if (IsReadyToStart()) { |
| 107 StartInvalidator(network_channel_type_); | 91 StartInvalidator(network_channel_type_); |
| 108 } | 92 } |
| 109 | 93 |
| 110 identity_provider_->AddObserver(this); | 94 identity_provider_->AddObserver(this); |
| 111 identity_provider_->AddActiveAccountRefreshTokenObserver(this); | 95 identity_provider_->AddActiveAccountRefreshTokenObserver(this); |
| 96 settings_provider_->AddObserver(this); |
| 112 } | 97 } |
| 113 | 98 |
| 114 void TiclInvalidationService::InitForTest( | 99 void TiclInvalidationService::InitForTest( |
| 115 scoped_ptr<syncer::InvalidationStateTracker> invalidation_state_tracker, | 100 scoped_ptr<syncer::InvalidationStateTracker> invalidation_state_tracker, |
| 116 syncer::Invalidator* invalidator) { | 101 syncer::Invalidator* invalidator) { |
| 117 // Here we perform the equivalent of Init() and StartInvalidator(), but with | 102 // Here we perform the equivalent of Init() and StartInvalidator(), but with |
| 118 // some minor changes to account for the fact that we're injecting the | 103 // some minor changes to account for the fact that we're injecting the |
| 119 // invalidator. | 104 // invalidator. |
| 120 invalidation_state_tracker_ = invalidation_state_tracker.Pass(); | 105 invalidation_state_tracker_ = invalidation_state_tracker.Pass(); |
| 121 invalidator_.reset(invalidator); | 106 invalidator_.reset(invalidator); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 StopInvalidator(); | 264 StopInvalidator(); |
| 280 } | 265 } |
| 281 | 266 |
| 282 // This service always expects to have a valid invalidation state. Thus, we | 267 // This service always expects to have a valid invalidation state. Thus, we |
| 283 // must generate a new client ID to replace the existing one. Setting a new | 268 // must generate a new client ID to replace the existing one. Setting a new |
| 284 // client ID also clears all other state. | 269 // client ID also clears all other state. |
| 285 invalidation_state_tracker_-> | 270 invalidation_state_tracker_-> |
| 286 ClearAndSetNewClientId(GenerateInvalidatorClientId()); | 271 ClearAndSetNewClientId(GenerateInvalidatorClientId()); |
| 287 } | 272 } |
| 288 | 273 |
| 274 void TiclInvalidationService::OnUseGCMChannelChanged() { |
| 275 UpdateInvalidationNetworkChannel(); |
| 276 } |
| 277 |
| 289 void TiclInvalidationService::OnInvalidatorStateChange( | 278 void TiclInvalidationService::OnInvalidatorStateChange( |
| 290 syncer::InvalidatorState state) { | 279 syncer::InvalidatorState state) { |
| 291 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { | 280 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { |
| 292 // This may be due to normal OAuth access token expiration. If so, we must | 281 // This may be due to normal OAuth access token expiration. If so, we must |
| 293 // fetch a new one using our refresh token. Resetting the invalidator's | 282 // fetch a new one using our refresh token. Resetting the invalidator's |
| 294 // access token will not reset the invalidator's exponential backoff, so | 283 // access token will not reset the invalidator's exponential backoff, so |
| 295 // it's safe to try to update the token every time we receive this signal. | 284 // it's safe to try to update the token every time we receive this signal. |
| 296 // | 285 // |
| 297 // We won't be receiving any invalidations while the refresh is in progress, | 286 // We won't be receiving any invalidations while the refresh is in progress, |
| 298 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials | 287 // we set our state to TRANSIENT_INVALIDATION_ERROR. If the credentials |
| (...skipping 13 matching lines...) Expand all Loading... |
| 312 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 301 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 313 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map); | 302 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map); |
| 314 | 303 |
| 315 logger_.OnInvalidation(invalidation_map); | 304 logger_.OnInvalidation(invalidation_map); |
| 316 } | 305 } |
| 317 | 306 |
| 318 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; } | 307 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; } |
| 319 | 308 |
| 320 void TiclInvalidationService::Shutdown() { | 309 void TiclInvalidationService::Shutdown() { |
| 321 DCHECK(CalledOnValidThread()); | 310 DCHECK(CalledOnValidThread()); |
| 311 settings_provider_->RemoveObserver(this); |
| 322 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); | 312 identity_provider_->RemoveActiveAccountRefreshTokenObserver(this); |
| 323 identity_provider_->RemoveObserver(this); | 313 identity_provider_->RemoveObserver(this); |
| 324 if (IsStarted()) { | 314 if (IsStarted()) { |
| 325 StopInvalidator(); | 315 StopInvalidator(); |
| 326 } | 316 } |
| 327 invalidation_state_tracker_.reset(); | 317 invalidation_state_tracker_.reset(); |
| 328 invalidator_registrar_.reset(); | 318 invalidator_registrar_.reset(); |
| 329 } | 319 } |
| 330 | 320 |
| 331 bool TiclInvalidationService::IsReadyToStart() { | 321 bool TiclInvalidationService::IsReadyToStart() { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 | 405 |
| 416 UpdateInvalidatorCredentials(); | 406 UpdateInvalidatorCredentials(); |
| 417 | 407 |
| 418 invalidator_->RegisterHandler(this); | 408 invalidator_->RegisterHandler(this); |
| 419 invalidator_->UpdateRegisteredIds( | 409 invalidator_->UpdateRegisteredIds( |
| 420 this, | 410 this, |
| 421 invalidator_registrar_->GetAllRegisteredIds()); | 411 invalidator_registrar_->GetAllRegisteredIds()); |
| 422 } | 412 } |
| 423 | 413 |
| 424 void TiclInvalidationService::UpdateInvalidationNetworkChannel() { | 414 void TiclInvalidationService::UpdateInvalidationNetworkChannel() { |
| 425 InvalidationNetworkChannel network_channel_type = PUSH_CLIENT_CHANNEL; | 415 const InvalidationNetworkChannel network_channel_type = |
| 426 if (gcm::GCMProfileService::GetGCMEnabledState(profile_) == | 416 settings_provider_->UseGCMChannel() ? GCM_NETWORK_CHANNEL |
| 427 gcm::GCMProfileService::ALWAYS_ENABLED && | 417 : PUSH_CLIENT_CHANNEL; |
| 428 (profile_->GetPrefs()->GetBoolean( | |
| 429 prefs::kInvalidationServiceUseGCMChannel) || | |
| 430 CommandLine::ForCurrentProcess()->HasSwitch( | |
| 431 switches::kInvalidationUseGCMChannel))) { | |
| 432 network_channel_type = GCM_NETWORK_CHANNEL; | |
| 433 } | |
| 434 if (network_channel_type_ == network_channel_type) | 418 if (network_channel_type_ == network_channel_type) |
| 435 return; | 419 return; |
| 436 network_channel_type_ = network_channel_type; | 420 network_channel_type_ = network_channel_type; |
| 437 if (IsStarted()) { | 421 if (IsStarted()) { |
| 438 StopInvalidator(); | 422 StopInvalidator(); |
| 439 StartInvalidator(network_channel_type_); | 423 StartInvalidator(network_channel_type_); |
| 440 } | 424 } |
| 441 } | 425 } |
| 442 | 426 |
| 443 void TiclInvalidationService::UpdateInvalidatorCredentials() { | 427 void TiclInvalidationService::UpdateInvalidatorCredentials() { |
| 444 std::string email = identity_provider_->GetActiveAccountId(); | 428 std::string email = identity_provider_->GetActiveAccountId(); |
| 445 | 429 |
| 446 DCHECK(!email.empty()) << "Expected user to be signed in."; | 430 DCHECK(!email.empty()) << "Expected user to be signed in."; |
| 447 | 431 |
| 448 DVLOG(2) << "UpdateCredentials: " << email; | 432 DVLOG(2) << "UpdateCredentials: " << email; |
| 449 invalidator_->UpdateCredentials(email, access_token_); | 433 invalidator_->UpdateCredentials(email, access_token_); |
| 450 } | 434 } |
| 451 | 435 |
| 452 void TiclInvalidationService::StopInvalidator() { | 436 void TiclInvalidationService::StopInvalidator() { |
| 453 DCHECK(invalidator_); | 437 DCHECK(invalidator_); |
| 454 gcm_invalidation_bridge_.reset(); | 438 gcm_invalidation_bridge_.reset(); |
| 455 invalidator_->UnregisterHandler(this); | 439 invalidator_->UnregisterHandler(this); |
| 456 invalidator_.reset(); | 440 invalidator_.reset(); |
| 457 } | 441 } |
| 458 | 442 |
| 459 } // namespace invalidation | 443 } // namespace invalidation |
| OLD | NEW |