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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 DCHECK(CalledOnValidThread()); | 73 DCHECK(CalledOnValidThread()); |
74 | 74 |
75 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 75 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); |
76 if (invalidator_storage_->GetInvalidatorClientId().empty()) { | 76 if (invalidator_storage_->GetInvalidatorClientId().empty()) { |
77 // 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 |
78 // state with the new ID anyway. | 78 // state with the new ID anyway. |
79 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 79 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); |
80 } | 80 } |
81 | 81 |
82 if (IsReadyToStart()) { | 82 if (IsReadyToStart()) { |
83 StartInvalidator(); | 83 StartInvalidator(PUSH_CLIENT_CHANNEL); |
84 } | 84 } |
85 | 85 |
86 notification_registrar_.Add(this, | 86 notification_registrar_.Add(this, |
87 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | 87 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, |
88 content::Source<Profile>(profile_)); | 88 content::Source<Profile>(profile_)); |
89 oauth2_token_service_->AddObserver(this); | 89 oauth2_token_service_->AddObserver(this); |
90 } | 90 } |
91 | 91 |
92 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { | 92 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { |
93 // Here we perform the equivalent of Init() and StartInvalidator(), but with | 93 // Here we perform the equivalent of Init() and StartInvalidator(), but with |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 void TiclInvalidationService::OnGetTokenSuccess( | 182 void TiclInvalidationService::OnGetTokenSuccess( |
183 const OAuth2TokenService::Request* request, | 183 const OAuth2TokenService::Request* request, |
184 const std::string& access_token, | 184 const std::string& access_token, |
185 const base::Time& expiration_time) { | 185 const base::Time& expiration_time) { |
186 DCHECK_EQ(access_token_request_, request); | 186 DCHECK_EQ(access_token_request_, request); |
187 access_token_request_.reset(); | 187 access_token_request_.reset(); |
188 // Reset backoff time after successful response. | 188 // Reset backoff time after successful response. |
189 request_access_token_backoff_.Reset(); | 189 request_access_token_backoff_.Reset(); |
190 access_token_ = access_token; | 190 access_token_ = access_token; |
191 if (!IsStarted() && IsReadyToStart()) { | 191 if (!IsStarted() && IsReadyToStart()) { |
192 StartInvalidator(); | 192 StartInvalidator(PUSH_CLIENT_CHANNEL); |
193 } else { | 193 } else { |
194 UpdateInvalidatorCredentials(); | 194 UpdateInvalidatorCredentials(); |
195 } | 195 } |
196 } | 196 } |
197 | 197 |
198 void TiclInvalidationService::OnGetTokenFailure( | 198 void TiclInvalidationService::OnGetTokenFailure( |
199 const OAuth2TokenService::Request* request, | 199 const OAuth2TokenService::Request* request, |
200 const GoogleServiceAuthError& error) { | 200 const GoogleServiceAuthError& error) { |
201 DCHECK_EQ(access_token_request_, request); | 201 DCHECK_EQ(access_token_request_, request); |
202 DCHECK_NE(error.state(), GoogleServiceAuthError::NONE); | 202 DCHECK_NE(error.state(), GoogleServiceAuthError::NONE); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 default: { | 240 default: { |
241 // We have no way to notify the user of this. Do nothing. | 241 // We have no way to notify the user of this. Do nothing. |
242 } | 242 } |
243 } | 243 } |
244 } | 244 } |
245 | 245 |
246 void TiclInvalidationService::OnRefreshTokenAvailable( | 246 void TiclInvalidationService::OnRefreshTokenAvailable( |
247 const std::string& account_id) { | 247 const std::string& account_id) { |
248 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) { | 248 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) { |
249 if (!IsStarted() && IsReadyToStart()) { | 249 if (!IsStarted() && IsReadyToStart()) { |
250 StartInvalidator(); | 250 StartInvalidator(PUSH_CLIENT_CHANNEL); |
251 } | 251 } |
252 } | 252 } |
253 } | 253 } |
254 | 254 |
255 void TiclInvalidationService::OnRefreshTokenRevoked( | 255 void TiclInvalidationService::OnRefreshTokenRevoked( |
256 const std::string& account_id) { | 256 const std::string& account_id) { |
257 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) { | 257 if (oauth2_token_service_->GetPrimaryAccountId() == account_id) { |
258 access_token_.clear(); | 258 access_token_.clear(); |
259 if (IsStarted()) { | 259 if (IsStarted()) { |
260 UpdateInvalidatorCredentials(); | 260 UpdateInvalidatorCredentials(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 return false; | 323 return false; |
324 } | 324 } |
325 | 325 |
326 return true; | 326 return true; |
327 } | 327 } |
328 | 328 |
329 bool TiclInvalidationService::IsStarted() { | 329 bool TiclInvalidationService::IsStarted() { |
330 return invalidator_.get() != NULL; | 330 return invalidator_.get() != NULL; |
331 } | 331 } |
332 | 332 |
333 void TiclInvalidationService::StartInvalidator() { | 333 void TiclInvalidationService::StartInvalidator( |
| 334 InvalidationNetworkChannel network_channel) { |
334 DCHECK(CalledOnValidThread()); | 335 DCHECK(CalledOnValidThread()); |
335 DCHECK(!invalidator_); | 336 DCHECK(!invalidator_); |
336 DCHECK(invalidator_storage_); | 337 DCHECK(invalidator_storage_); |
337 DCHECK(!invalidator_storage_->GetInvalidatorClientId().empty()); | 338 DCHECK(!invalidator_storage_->GetInvalidatorClientId().empty()); |
338 | 339 |
339 if (access_token_.empty()) { | 340 if (access_token_.empty()) { |
340 DVLOG(1) | 341 DVLOG(1) |
341 << "TiclInvalidationService: " | 342 << "TiclInvalidationService: " |
342 << "Deferring start until we have an access token."; | 343 << "Deferring start until we have an access token."; |
343 RequestAccessToken(); | 344 RequestAccessToken(); |
344 return; | 345 return; |
345 } | 346 } |
346 | 347 |
347 notifier::NotifierOptions options = | 348 syncer::NetworkChannelCreator network_channel_creator; |
348 ParseNotifierOptions(*CommandLine::ForCurrentProcess()); | 349 |
349 options.request_context_getter = profile_->GetRequestContext(); | 350 switch (network_channel) { |
350 options.auth_mechanism = "X-OAUTH2"; | 351 case PUSH_CLIENT_CHANNEL: { |
| 352 notifier::NotifierOptions options = |
| 353 ParseNotifierOptions(*CommandLine::ForCurrentProcess()); |
| 354 options.request_context_getter = profile_->GetRequestContext(); |
| 355 options.auth_mechanism = "X-OAUTH2"; |
| 356 DCHECK_EQ(notifier::NOTIFICATION_SERVER, options.notification_method); |
| 357 network_channel_creator = |
| 358 syncer::NonBlockingInvalidator::MakePushClientChannelCreator(options); |
| 359 break; |
| 360 } |
| 361 case GCM_NETWORK_CHANNEL: { |
| 362 network_channel_creator = |
| 363 syncer::NonBlockingInvalidator::MakeGCMNetworkChannelCreator(); |
| 364 break; |
| 365 } |
| 366 default: { |
| 367 NOTREACHED(); |
| 368 return; |
| 369 } |
| 370 } |
351 invalidator_.reset(new syncer::NonBlockingInvalidator( | 371 invalidator_.reset(new syncer::NonBlockingInvalidator( |
352 options, | 372 network_channel_creator, |
353 invalidator_storage_->GetInvalidatorClientId(), | 373 invalidator_storage_->GetInvalidatorClientId(), |
354 invalidator_storage_->GetSavedInvalidations(), | 374 invalidator_storage_->GetSavedInvalidations(), |
355 invalidator_storage_->GetBootstrapData(), | 375 invalidator_storage_->GetBootstrapData(), |
356 syncer::WeakHandle<syncer::InvalidationStateTracker>( | 376 syncer::WeakHandle<syncer::InvalidationStateTracker>( |
357 invalidator_storage_->AsWeakPtr()), | 377 invalidator_storage_->AsWeakPtr()), |
358 content::GetUserAgent(GURL()))); | 378 content::GetUserAgent(GURL()), |
| 379 profile_->GetRequestContext())); |
359 | 380 |
360 UpdateInvalidatorCredentials(); | 381 UpdateInvalidatorCredentials(); |
361 | 382 |
362 invalidator_->RegisterHandler(this); | 383 invalidator_->RegisterHandler(this); |
363 invalidator_->UpdateRegisteredIds( | 384 invalidator_->UpdateRegisteredIds( |
364 this, | 385 this, |
365 invalidator_registrar_->GetAllRegisteredIds()); | 386 invalidator_registrar_->GetAllRegisteredIds()); |
366 } | 387 } |
367 | 388 |
368 void TiclInvalidationService::UpdateInvalidatorCredentials() { | 389 void TiclInvalidationService::UpdateInvalidatorCredentials() { |
(...skipping 20 matching lines...) Expand all Loading... |
389 } | 410 } |
390 | 411 |
391 // This service always expects to have a valid invalidator storage. | 412 // This service always expects to have a valid invalidator storage. |
392 // So we must not only clear the old one, but also start a new one. | 413 // So we must not only clear the old one, but also start a new one. |
393 invalidator_storage_->Clear(); | 414 invalidator_storage_->Clear(); |
394 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | 415 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); |
395 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | 416 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); |
396 } | 417 } |
397 | 418 |
398 } // namespace invalidation | 419 } // namespace invalidation |
OLD | NEW |