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" | 9 #include "base/prefs/pref_service.h" |
10 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" | 10 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" |
11 #include "chrome/browser/invalidation/invalidation_auth_provider.h" | 11 #include "chrome/browser/invalidation/invalidation_auth_provider.h" |
12 #include "chrome/browser/invalidation/invalidation_logger.h" | 12 #include "chrome/browser/invalidation/invalidation_logger.h" |
13 #include "chrome/browser/invalidation/invalidation_service_util.h" | 13 #include "chrome/browser/invalidation/invalidation_service_util.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 15 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
16 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 16 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
17 #include "chrome/common/chrome_content_client.h" | 17 #include "chrome/common/chrome_content_client.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
20 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 20 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
21 #include "google_apis/gaia/gaia_constants.h" | 21 #include "google_apis/gaia/gaia_constants.h" |
| 22 #include "net/url_request/url_request_context_getter.h" |
22 #include "sync/notifier/gcm_network_channel_delegate.h" | 23 #include "sync/notifier/gcm_network_channel_delegate.h" |
23 #include "sync/notifier/invalidation_util.h" | 24 #include "sync/notifier/invalidation_util.h" |
24 #include "sync/notifier/invalidator.h" | 25 #include "sync/notifier/invalidator.h" |
25 #include "sync/notifier/invalidator_state.h" | 26 #include "sync/notifier/invalidator_state.h" |
26 #include "sync/notifier/non_blocking_invalidator.h" | 27 #include "sync/notifier/non_blocking_invalidator.h" |
27 #include "sync/notifier/object_id_invalidation_map.h" | 28 #include "sync/notifier/object_id_invalidation_map.h" |
28 | 29 |
29 static const char* kOAuth2Scopes[] = { | 30 static const char* kOAuth2Scopes[] = { |
30 GaiaConstants::kGoogleTalkOAuth2Scope | 31 GaiaConstants::kGoogleTalkOAuth2Scope |
31 }; | 32 }; |
(...skipping 23 matching lines...) Expand all Loading... |
55 -1, | 56 -1, |
56 | 57 |
57 // Don't use initial delay unless the last request was an error. | 58 // Don't use initial delay unless the last request was an error. |
58 false, | 59 false, |
59 }; | 60 }; |
60 | 61 |
61 namespace invalidation { | 62 namespace invalidation { |
62 | 63 |
63 TiclInvalidationService::TiclInvalidationService( | 64 TiclInvalidationService::TiclInvalidationService( |
64 scoped_ptr<InvalidationAuthProvider> auth_provider, | 65 scoped_ptr<InvalidationAuthProvider> auth_provider, |
| 66 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 67 scoped_ptr<syncer::InvalidationStateTracker> invalidation_state_tracker, |
65 Profile* profile) | 68 Profile* profile) |
66 : OAuth2TokenService::Consumer("ticl_invalidation"), | 69 : OAuth2TokenService::Consumer("ticl_invalidation"), |
67 profile_(profile), | 70 profile_(profile), |
68 auth_provider_(auth_provider.Pass()), | 71 auth_provider_(auth_provider.Pass()), |
69 invalidator_registrar_(new syncer::InvalidatorRegistrar()), | 72 invalidator_registrar_(new syncer::InvalidatorRegistrar()), |
| 73 invalidation_state_tracker_(invalidation_state_tracker.Pass()), |
70 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy), | 74 request_access_token_backoff_(&kRequestAccessTokenBackoffPolicy), |
71 network_channel_type_(PUSH_CLIENT_CHANNEL), | 75 network_channel_type_(PUSH_CLIENT_CHANNEL), |
| 76 request_context_(request_context), |
72 logger_() {} | 77 logger_() {} |
73 | 78 |
74 TiclInvalidationService::~TiclInvalidationService() { | 79 TiclInvalidationService::~TiclInvalidationService() { |
75 DCHECK(CalledOnValidThread()); | 80 DCHECK(CalledOnValidThread()); |
76 } | 81 } |
77 | 82 |
78 void TiclInvalidationService::Init() { | 83 void TiclInvalidationService::Init() { |
79 DCHECK(CalledOnValidThread()); | 84 DCHECK(CalledOnValidThread()); |
80 | 85 |
81 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 86 if (invalidation_state_tracker_->GetInvalidatorClientId().empty()) { |
82 if (invalidator_storage_->GetInvalidatorClientId().empty()) { | 87 invalidation_state_tracker_->ClearAndSetNewClientId( |
83 // This also clears any existing state. We can't reuse old invalidator | 88 GenerateInvalidatorClientId()); |
84 // state with the new ID anyway. | |
85 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | |
86 } | 89 } |
87 | 90 |
88 pref_change_registrar_.Init(profile_->GetPrefs()); | 91 pref_change_registrar_.Init(profile_->GetPrefs()); |
89 pref_change_registrar_.Add( | 92 pref_change_registrar_.Add( |
90 prefs::kInvalidationServiceUseGCMChannel, | 93 prefs::kInvalidationServiceUseGCMChannel, |
91 base::Bind(&TiclInvalidationService::UpdateInvalidationNetworkChannel, | 94 base::Bind(&TiclInvalidationService::UpdateInvalidationNetworkChannel, |
92 base::Unretained(this))); | 95 base::Unretained(this))); |
93 pref_change_registrar_.Add( | 96 pref_change_registrar_.Add( |
94 prefs::kGCMChannelEnabled, | 97 prefs::kGCMChannelEnabled, |
95 base::Bind(&TiclInvalidationService::UpdateInvalidationNetworkChannel, | 98 base::Bind(&TiclInvalidationService::UpdateInvalidationNetworkChannel, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 << invalidator_->GetInvalidatorState(); | 165 << invalidator_->GetInvalidatorState(); |
163 return invalidator_->GetInvalidatorState(); | 166 return invalidator_->GetInvalidatorState(); |
164 } else { | 167 } else { |
165 DVLOG(2) << "Invalidator currently stopped"; | 168 DVLOG(2) << "Invalidator currently stopped"; |
166 return syncer::TRANSIENT_INVALIDATION_ERROR; | 169 return syncer::TRANSIENT_INVALIDATION_ERROR; |
167 } | 170 } |
168 } | 171 } |
169 | 172 |
170 std::string TiclInvalidationService::GetInvalidatorClientId() const { | 173 std::string TiclInvalidationService::GetInvalidatorClientId() const { |
171 DCHECK(CalledOnValidThread()); | 174 DCHECK(CalledOnValidThread()); |
172 return invalidator_storage_->GetInvalidatorClientId(); | 175 return invalidation_state_tracker_->GetInvalidatorClientId(); |
173 } | 176 } |
174 | 177 |
175 InvalidationLogger* TiclInvalidationService::GetInvalidationLogger() { | 178 InvalidationLogger* TiclInvalidationService::GetInvalidationLogger() { |
176 return &logger_; | 179 return &logger_; |
177 } | 180 } |
178 | 181 |
179 InvalidationAuthProvider* | 182 InvalidationAuthProvider* |
180 TiclInvalidationService::GetInvalidationAuthProvider() { | 183 TiclInvalidationService::GetInvalidationAuthProvider() { |
181 return auth_provider_.get(); | 184 return auth_provider_.get(); |
182 } | 185 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 } | 276 } |
274 | 277 |
275 void TiclInvalidationService::OnInvalidationAuthLogout() { | 278 void TiclInvalidationService::OnInvalidationAuthLogout() { |
276 access_token_request_.reset(); | 279 access_token_request_.reset(); |
277 request_access_token_retry_timer_.Stop(); | 280 request_access_token_retry_timer_.Stop(); |
278 | 281 |
279 if (IsStarted()) { | 282 if (IsStarted()) { |
280 StopInvalidator(); | 283 StopInvalidator(); |
281 } | 284 } |
282 | 285 |
283 // This service always expects to have a valid invalidator storage. | 286 // This service always expects to have a valid invalidation state. Thus, we |
284 // So we must not only clear the old one, but also start a new one. | 287 // must generate a new client ID to replace the existing one. Setting a new |
285 invalidator_storage_->Clear(); | 288 // client ID also clears all other state. |
286 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 289 invalidation_state_tracker_-> |
287 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 290 ClearAndSetNewClientId(GenerateInvalidatorClientId()); |
288 } | 291 } |
289 | 292 |
290 void TiclInvalidationService::OnInvalidatorStateChange( | 293 void TiclInvalidationService::OnInvalidatorStateChange( |
291 syncer::InvalidatorState state) { | 294 syncer::InvalidatorState state) { |
292 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { | 295 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { |
293 // This may be due to normal OAuth access token expiration. If so, we must | 296 // This may be due to normal OAuth access token expiration. If so, we must |
294 // fetch a new one using our refresh token. Resetting the invalidator's | 297 // fetch a new one using our refresh token. Resetting the invalidator's |
295 // access token will not reset the invalidator's exponential backoff, so | 298 // access token will not reset the invalidator's exponential backoff, so |
296 // it's safe to try to update the token every time we receive this signal. | 299 // it's safe to try to update the token every time we receive this signal. |
297 // | 300 // |
(...skipping 20 matching lines...) Expand all Loading... |
318 | 321 |
319 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; } | 322 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; } |
320 | 323 |
321 void TiclInvalidationService::Shutdown() { | 324 void TiclInvalidationService::Shutdown() { |
322 DCHECK(CalledOnValidThread()); | 325 DCHECK(CalledOnValidThread()); |
323 auth_provider_->GetTokenService()->RemoveObserver(this); | 326 auth_provider_->GetTokenService()->RemoveObserver(this); |
324 auth_provider_->RemoveObserver(this); | 327 auth_provider_->RemoveObserver(this); |
325 if (IsStarted()) { | 328 if (IsStarted()) { |
326 StopInvalidator(); | 329 StopInvalidator(); |
327 } | 330 } |
328 invalidator_storage_.reset(); | 331 invalidation_state_tracker_.reset(); |
329 invalidator_registrar_.reset(); | 332 invalidator_registrar_.reset(); |
330 } | 333 } |
331 | 334 |
332 bool TiclInvalidationService::IsReadyToStart() { | 335 bool TiclInvalidationService::IsReadyToStart() { |
333 if (profile_->IsManaged()) { | 336 if (profile_->IsManaged()) { |
334 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; | 337 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; |
335 return false; | 338 return false; |
336 } | 339 } |
337 | 340 |
338 if (auth_provider_->GetAccountId().empty()) { | 341 if (auth_provider_->GetAccountId().empty()) { |
(...skipping 19 matching lines...) Expand all Loading... |
358 } | 361 } |
359 | 362 |
360 bool TiclInvalidationService::IsStarted() const { | 363 bool TiclInvalidationService::IsStarted() const { |
361 return invalidator_.get() != NULL; | 364 return invalidator_.get() != NULL; |
362 } | 365 } |
363 | 366 |
364 void TiclInvalidationService::StartInvalidator( | 367 void TiclInvalidationService::StartInvalidator( |
365 InvalidationNetworkChannel network_channel) { | 368 InvalidationNetworkChannel network_channel) { |
366 DCHECK(CalledOnValidThread()); | 369 DCHECK(CalledOnValidThread()); |
367 DCHECK(!invalidator_); | 370 DCHECK(!invalidator_); |
368 DCHECK(invalidator_storage_); | 371 DCHECK(invalidation_state_tracker_); |
369 DCHECK(!invalidator_storage_->GetInvalidatorClientId().empty()); | 372 DCHECK(!invalidation_state_tracker_->GetInvalidatorClientId().empty()); |
370 | 373 |
371 // Request access token for PushClientChannel. GCMNetworkChannel will request | 374 // Request access token for PushClientChannel. GCMNetworkChannel will request |
372 // access token before sending message to server. | 375 // access token before sending message to server. |
373 if (network_channel == PUSH_CLIENT_CHANNEL && access_token_.empty()) { | 376 if (network_channel == PUSH_CLIENT_CHANNEL && access_token_.empty()) { |
374 DVLOG(1) | 377 DVLOG(1) |
375 << "TiclInvalidationService: " | 378 << "TiclInvalidationService: " |
376 << "Deferring start until we have an access token."; | 379 << "Deferring start until we have an access token."; |
377 RequestAccessToken(); | 380 RequestAccessToken(); |
378 return; | 381 return; |
379 } | 382 } |
380 | 383 |
381 syncer::NetworkChannelCreator network_channel_creator; | 384 syncer::NetworkChannelCreator network_channel_creator; |
382 | 385 |
383 switch (network_channel) { | 386 switch (network_channel) { |
384 case PUSH_CLIENT_CHANNEL: { | 387 case PUSH_CLIENT_CHANNEL: { |
385 notifier::NotifierOptions options = | 388 notifier::NotifierOptions options = |
386 ParseNotifierOptions(*CommandLine::ForCurrentProcess()); | 389 ParseNotifierOptions(*CommandLine::ForCurrentProcess()); |
387 options.request_context_getter = profile_->GetRequestContext(); | 390 options.request_context_getter = request_context_; |
388 options.auth_mechanism = "X-OAUTH2"; | 391 options.auth_mechanism = "X-OAUTH2"; |
389 network_channel_options_.SetString("Options.HostPort", | 392 network_channel_options_.SetString("Options.HostPort", |
390 options.xmpp_host_port.ToString()); | 393 options.xmpp_host_port.ToString()); |
391 network_channel_options_.SetString("Options.AuthMechanism", | 394 network_channel_options_.SetString("Options.AuthMechanism", |
392 options.auth_mechanism); | 395 options.auth_mechanism); |
393 DCHECK_EQ(notifier::NOTIFICATION_SERVER, options.notification_method); | 396 DCHECK_EQ(notifier::NOTIFICATION_SERVER, options.notification_method); |
394 network_channel_creator = | 397 network_channel_creator = |
395 syncer::NonBlockingInvalidator::MakePushClientChannelCreator(options); | 398 syncer::NonBlockingInvalidator::MakePushClientChannelCreator(options); |
396 break; | 399 break; |
397 } | 400 } |
398 case GCM_NETWORK_CHANNEL: { | 401 case GCM_NETWORK_CHANNEL: { |
399 gcm::GCMProfileService* gcm_profile_service = | 402 gcm::GCMProfileService* gcm_profile_service = |
400 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 403 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
401 gcm_invalidation_bridge_.reset( | 404 gcm_invalidation_bridge_.reset( |
402 new GCMInvalidationBridge(gcm_profile_service, auth_provider_.get())); | 405 new GCMInvalidationBridge(gcm_profile_service, auth_provider_.get())); |
403 network_channel_creator = | 406 network_channel_creator = |
404 syncer::NonBlockingInvalidator::MakeGCMNetworkChannelCreator( | 407 syncer::NonBlockingInvalidator::MakeGCMNetworkChannelCreator( |
405 profile_->GetRequestContext(), | 408 request_context_, |
406 gcm_invalidation_bridge_->CreateDelegate().Pass()); | 409 gcm_invalidation_bridge_->CreateDelegate().Pass()); |
407 break; | 410 break; |
408 } | 411 } |
409 default: { | 412 default: { |
410 NOTREACHED(); | 413 NOTREACHED(); |
411 return; | 414 return; |
412 } | 415 } |
413 } | 416 } |
414 invalidator_.reset(new syncer::NonBlockingInvalidator( | 417 invalidator_.reset(new syncer::NonBlockingInvalidator( |
415 network_channel_creator, | 418 network_channel_creator, |
416 invalidator_storage_->GetInvalidatorClientId(), | 419 invalidation_state_tracker_->GetInvalidatorClientId(), |
417 invalidator_storage_->GetSavedInvalidations(), | 420 invalidation_state_tracker_->GetSavedInvalidations(), |
418 invalidator_storage_->GetBootstrapData(), | 421 invalidation_state_tracker_->GetBootstrapData(), |
419 syncer::WeakHandle<syncer::InvalidationStateTracker>( | 422 invalidation_state_tracker_.get(), |
420 invalidator_storage_->AsWeakPtr()), | |
421 GetUserAgent(), | 423 GetUserAgent(), |
422 profile_->GetRequestContext())); | 424 request_context_)); |
423 | 425 |
424 UpdateInvalidatorCredentials(); | 426 UpdateInvalidatorCredentials(); |
425 | 427 |
426 invalidator_->RegisterHandler(this); | 428 invalidator_->RegisterHandler(this); |
427 invalidator_->UpdateRegisteredIds( | 429 invalidator_->UpdateRegisteredIds( |
428 this, | 430 this, |
429 invalidator_registrar_->GetAllRegisteredIds()); | 431 invalidator_registrar_->GetAllRegisteredIds()); |
430 } | 432 } |
431 | 433 |
432 void TiclInvalidationService::UpdateInvalidationNetworkChannel() { | 434 void TiclInvalidationService::UpdateInvalidationNetworkChannel() { |
(...skipping 25 matching lines...) Expand all Loading... |
458 } | 460 } |
459 | 461 |
460 void TiclInvalidationService::StopInvalidator() { | 462 void TiclInvalidationService::StopInvalidator() { |
461 DCHECK(invalidator_); | 463 DCHECK(invalidator_); |
462 gcm_invalidation_bridge_.reset(); | 464 gcm_invalidation_bridge_.reset(); |
463 invalidator_->UnregisterHandler(this); | 465 invalidator_->UnregisterHandler(this); |
464 invalidator_.reset(); | 466 invalidator_.reset(); |
465 } | 467 } |
466 | 468 |
467 } // namespace invalidation | 469 } // namespace invalidation |
OLD | NEW |