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()) { | |
83 // This also clears any existing state. We can't reuse old invalidator | 87 // This also clears any existing state. We can't reuse old invalidator |
84 // state with the new ID anyway. | 88 // state with the new ID anyway. |
85 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 89 invalidation_state_tracker_->SetInvalidatorClientId( |
90 GenerateInvalidatorClientId()); | |
86 } | 91 } |
87 | 92 |
88 pref_change_registrar_.Init(profile_->GetPrefs()); | 93 pref_change_registrar_.Init(profile_->GetPrefs()); |
89 pref_change_registrar_.Add( | 94 pref_change_registrar_.Add( |
90 prefs::kInvalidationServiceUseGCMChannel, | 95 prefs::kInvalidationServiceUseGCMChannel, |
91 base::Bind(&TiclInvalidationService::UpdateInvalidationNetworkChannel, | 96 base::Bind(&TiclInvalidationService::UpdateInvalidationNetworkChannel, |
92 base::Unretained(this))); | 97 base::Unretained(this))); |
93 pref_change_registrar_.Add( | 98 pref_change_registrar_.Add( |
94 prefs::kGCMChannelEnabled, | 99 prefs::kGCMChannelEnabled, |
95 base::Bind(&TiclInvalidationService::UpdateInvalidationNetworkChannel, | 100 base::Bind(&TiclInvalidationService::UpdateInvalidationNetworkChannel, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 << invalidator_->GetInvalidatorState(); | 167 << invalidator_->GetInvalidatorState(); |
163 return invalidator_->GetInvalidatorState(); | 168 return invalidator_->GetInvalidatorState(); |
164 } else { | 169 } else { |
165 DVLOG(2) << "Invalidator currently stopped"; | 170 DVLOG(2) << "Invalidator currently stopped"; |
166 return syncer::TRANSIENT_INVALIDATION_ERROR; | 171 return syncer::TRANSIENT_INVALIDATION_ERROR; |
167 } | 172 } |
168 } | 173 } |
169 | 174 |
170 std::string TiclInvalidationService::GetInvalidatorClientId() const { | 175 std::string TiclInvalidationService::GetInvalidatorClientId() const { |
171 DCHECK(CalledOnValidThread()); | 176 DCHECK(CalledOnValidThread()); |
172 return invalidator_storage_->GetInvalidatorClientId(); | 177 return invalidation_state_tracker_->GetInvalidatorClientId(); |
173 } | 178 } |
174 | 179 |
175 InvalidationLogger* TiclInvalidationService::GetInvalidationLogger() { | 180 InvalidationLogger* TiclInvalidationService::GetInvalidationLogger() { |
176 return &logger_; | 181 return &logger_; |
177 } | 182 } |
178 | 183 |
179 InvalidationAuthProvider* | 184 InvalidationAuthProvider* |
180 TiclInvalidationService::GetInvalidationAuthProvider() { | 185 TiclInvalidationService::GetInvalidationAuthProvider() { |
181 return auth_provider_.get(); | 186 return auth_provider_.get(); |
182 } | 187 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 } | 278 } |
274 | 279 |
275 void TiclInvalidationService::OnInvalidationAuthLogout() { | 280 void TiclInvalidationService::OnInvalidationAuthLogout() { |
276 access_token_request_.reset(); | 281 access_token_request_.reset(); |
277 request_access_token_retry_timer_.Stop(); | 282 request_access_token_retry_timer_.Stop(); |
278 | 283 |
279 if (IsStarted()) { | 284 if (IsStarted()) { |
280 StopInvalidator(); | 285 StopInvalidator(); |
281 } | 286 } |
282 | 287 |
283 // This service always expects to have a valid invalidator storage. | 288 // 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. | 289 // must generate a new client ID to replace the existing one. Setting a new |
285 invalidator_storage_->Clear(); | 290 // client ID also clears all other state. |
286 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 291 invalidation_state_tracker_-> |
287 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 292 SetInvalidatorClientId(GenerateInvalidatorClientId()); |
dcheng
2014/04/11 19:54:00
It might be nice to rename this method to make it
bartfab (slow)
2014/04/14 09:20:08
Done.
| |
288 } | 293 } |
289 | 294 |
290 void TiclInvalidationService::OnInvalidatorStateChange( | 295 void TiclInvalidationService::OnInvalidatorStateChange( |
291 syncer::InvalidatorState state) { | 296 syncer::InvalidatorState state) { |
292 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { | 297 if (state == syncer::INVALIDATION_CREDENTIALS_REJECTED) { |
293 // This may be due to normal OAuth access token expiration. If so, we must | 298 // 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 | 299 // fetch a new one using our refresh token. Resetting the invalidator's |
295 // access token will not reset the invalidator's exponential backoff, so | 300 // 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. | 301 // it's safe to try to update the token every time we receive this signal. |
297 // | 302 // |
(...skipping 20 matching lines...) Expand all Loading... | |
318 | 323 |
319 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; } | 324 std::string TiclInvalidationService::GetOwnerName() const { return "TICL"; } |
320 | 325 |
321 void TiclInvalidationService::Shutdown() { | 326 void TiclInvalidationService::Shutdown() { |
322 DCHECK(CalledOnValidThread()); | 327 DCHECK(CalledOnValidThread()); |
323 auth_provider_->GetTokenService()->RemoveObserver(this); | 328 auth_provider_->GetTokenService()->RemoveObserver(this); |
324 auth_provider_->RemoveObserver(this); | 329 auth_provider_->RemoveObserver(this); |
325 if (IsStarted()) { | 330 if (IsStarted()) { |
326 StopInvalidator(); | 331 StopInvalidator(); |
327 } | 332 } |
328 invalidator_storage_.reset(); | 333 invalidation_state_tracker_.reset(); |
329 invalidator_registrar_.reset(); | 334 invalidator_registrar_.reset(); |
330 } | 335 } |
331 | 336 |
332 bool TiclInvalidationService::IsReadyToStart() { | 337 bool TiclInvalidationService::IsReadyToStart() { |
333 if (profile_->IsManaged()) { | 338 if (profile_->IsManaged()) { |
334 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; | 339 DVLOG(2) << "Not starting TiclInvalidationService: User is managed."; |
335 return false; | 340 return false; |
336 } | 341 } |
337 | 342 |
338 if (auth_provider_->GetAccountId().empty()) { | 343 if (auth_provider_->GetAccountId().empty()) { |
(...skipping 19 matching lines...) Expand all Loading... | |
358 } | 363 } |
359 | 364 |
360 bool TiclInvalidationService::IsStarted() const { | 365 bool TiclInvalidationService::IsStarted() const { |
361 return invalidator_.get() != NULL; | 366 return invalidator_.get() != NULL; |
362 } | 367 } |
363 | 368 |
364 void TiclInvalidationService::StartInvalidator( | 369 void TiclInvalidationService::StartInvalidator( |
365 InvalidationNetworkChannel network_channel) { | 370 InvalidationNetworkChannel network_channel) { |
366 DCHECK(CalledOnValidThread()); | 371 DCHECK(CalledOnValidThread()); |
367 DCHECK(!invalidator_); | 372 DCHECK(!invalidator_); |
368 DCHECK(invalidator_storage_); | 373 DCHECK(invalidation_state_tracker_); |
369 DCHECK(!invalidator_storage_->GetInvalidatorClientId().empty()); | 374 DCHECK(!invalidation_state_tracker_->GetInvalidatorClientId().empty()); |
370 | 375 |
371 // Request access token for PushClientChannel. GCMNetworkChannel will request | 376 // Request access token for PushClientChannel. GCMNetworkChannel will request |
372 // access token before sending message to server. | 377 // access token before sending message to server. |
373 if (network_channel == PUSH_CLIENT_CHANNEL && access_token_.empty()) { | 378 if (network_channel == PUSH_CLIENT_CHANNEL && access_token_.empty()) { |
374 DVLOG(1) | 379 DVLOG(1) |
375 << "TiclInvalidationService: " | 380 << "TiclInvalidationService: " |
376 << "Deferring start until we have an access token."; | 381 << "Deferring start until we have an access token."; |
377 RequestAccessToken(); | 382 RequestAccessToken(); |
378 return; | 383 return; |
379 } | 384 } |
380 | 385 |
381 syncer::NetworkChannelCreator network_channel_creator; | 386 syncer::NetworkChannelCreator network_channel_creator; |
382 | 387 |
383 switch (network_channel) { | 388 switch (network_channel) { |
384 case PUSH_CLIENT_CHANNEL: { | 389 case PUSH_CLIENT_CHANNEL: { |
385 notifier::NotifierOptions options = | 390 notifier::NotifierOptions options = |
386 ParseNotifierOptions(*CommandLine::ForCurrentProcess()); | 391 ParseNotifierOptions(*CommandLine::ForCurrentProcess()); |
387 options.request_context_getter = profile_->GetRequestContext(); | 392 options.request_context_getter = request_context_; |
388 options.auth_mechanism = "X-OAUTH2"; | 393 options.auth_mechanism = "X-OAUTH2"; |
389 network_channel_options_.SetString("Options.HostPort", | 394 network_channel_options_.SetString("Options.HostPort", |
390 options.xmpp_host_port.ToString()); | 395 options.xmpp_host_port.ToString()); |
391 network_channel_options_.SetString("Options.AuthMechanism", | 396 network_channel_options_.SetString("Options.AuthMechanism", |
392 options.auth_mechanism); | 397 options.auth_mechanism); |
393 DCHECK_EQ(notifier::NOTIFICATION_SERVER, options.notification_method); | 398 DCHECK_EQ(notifier::NOTIFICATION_SERVER, options.notification_method); |
394 network_channel_creator = | 399 network_channel_creator = |
395 syncer::NonBlockingInvalidator::MakePushClientChannelCreator(options); | 400 syncer::NonBlockingInvalidator::MakePushClientChannelCreator(options); |
396 break; | 401 break; |
397 } | 402 } |
398 case GCM_NETWORK_CHANNEL: { | 403 case GCM_NETWORK_CHANNEL: { |
399 gcm::GCMProfileService* gcm_profile_service = | 404 gcm::GCMProfileService* gcm_profile_service = |
400 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | 405 gcm::GCMProfileServiceFactory::GetForProfile(profile_); |
401 gcm_invalidation_bridge_.reset( | 406 gcm_invalidation_bridge_.reset( |
402 new GCMInvalidationBridge(gcm_profile_service, auth_provider_.get())); | 407 new GCMInvalidationBridge(gcm_profile_service, auth_provider_.get())); |
403 network_channel_creator = | 408 network_channel_creator = |
404 syncer::NonBlockingInvalidator::MakeGCMNetworkChannelCreator( | 409 syncer::NonBlockingInvalidator::MakeGCMNetworkChannelCreator( |
405 profile_->GetRequestContext(), | 410 request_context_, |
406 gcm_invalidation_bridge_->CreateDelegate().Pass()); | 411 gcm_invalidation_bridge_->CreateDelegate().Pass()); |
407 break; | 412 break; |
408 } | 413 } |
409 default: { | 414 default: { |
410 NOTREACHED(); | 415 NOTREACHED(); |
411 return; | 416 return; |
412 } | 417 } |
413 } | 418 } |
414 invalidator_.reset(new syncer::NonBlockingInvalidator( | 419 invalidator_.reset(new syncer::NonBlockingInvalidator( |
415 network_channel_creator, | 420 network_channel_creator, |
416 invalidator_storage_->GetInvalidatorClientId(), | 421 invalidation_state_tracker_->GetInvalidatorClientId(), |
417 invalidator_storage_->GetSavedInvalidations(), | 422 invalidation_state_tracker_->GetSavedInvalidations(), |
418 invalidator_storage_->GetBootstrapData(), | 423 invalidation_state_tracker_->GetBootstrapData(), |
419 syncer::WeakHandle<syncer::InvalidationStateTracker>( | 424 syncer::WeakHandle<syncer::InvalidationStateTracker>( |
420 invalidator_storage_->AsWeakPtr()), | 425 invalidation_state_tracker_->GetWeakPtr()), |
421 GetUserAgent(), | 426 GetUserAgent(), |
422 profile_->GetRequestContext())); | 427 request_context_)); |
423 | 428 |
424 UpdateInvalidatorCredentials(); | 429 UpdateInvalidatorCredentials(); |
425 | 430 |
426 invalidator_->RegisterHandler(this); | 431 invalidator_->RegisterHandler(this); |
427 invalidator_->UpdateRegisteredIds( | 432 invalidator_->UpdateRegisteredIds( |
428 this, | 433 this, |
429 invalidator_registrar_->GetAllRegisteredIds()); | 434 invalidator_registrar_->GetAllRegisteredIds()); |
430 } | 435 } |
431 | 436 |
432 void TiclInvalidationService::UpdateInvalidationNetworkChannel() { | 437 void TiclInvalidationService::UpdateInvalidationNetworkChannel() { |
(...skipping 25 matching lines...) Expand all Loading... | |
458 } | 463 } |
459 | 464 |
460 void TiclInvalidationService::StopInvalidator() { | 465 void TiclInvalidationService::StopInvalidator() { |
461 DCHECK(invalidator_); | 466 DCHECK(invalidator_); |
462 gcm_invalidation_bridge_.reset(); | 467 gcm_invalidation_bridge_.reset(); |
463 invalidator_->UnregisterHandler(this); | 468 invalidator_->UnregisterHandler(this); |
464 invalidator_.reset(); | 469 invalidator_.reset(); |
465 } | 470 } |
466 | 471 |
467 } // namespace invalidation | 472 } // namespace invalidation |
OLD | NEW |