| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "sync/notifier/chrome_invalidation_client.h" | 5 #include "sync/notifier/chrome_invalidation_client.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 const char kApplicationName[] = "chrome-sync"; | 24 const char kApplicationName[] = "chrome-sync"; |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 namespace sync_notifier { | 28 namespace sync_notifier { |
| 29 | 29 |
| 30 ChromeInvalidationClient::Listener::~Listener() {} | 30 ChromeInvalidationClient::Listener::~Listener() {} |
| 31 | 31 |
| 32 ChromeInvalidationClient::ChromeInvalidationClient( | 32 ChromeInvalidationClient::ChromeInvalidationClient( |
| 33 scoped_ptr<notifier::PushClient> push_client) | 33 scoped_ptr<notifier::PushClient> push_client) |
| 34 : chrome_system_resources_(push_client.Pass(), | 34 : push_client_(push_client.get()), |
| 35 chrome_system_resources_(push_client.Pass(), |
| 35 ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 36 ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 36 listener_(NULL), | 37 listener_(NULL), |
| 37 ticl_ready_(false) { | 38 ticl_state_(DEFAULT_NOTIFICATION_ERROR), |
| 39 push_client_state_(DEFAULT_NOTIFICATION_ERROR) { |
| 38 DCHECK(CalledOnValidThread()); | 40 DCHECK(CalledOnValidThread()); |
| 41 push_client_->AddObserver(this); |
| 39 } | 42 } |
| 40 | 43 |
| 41 ChromeInvalidationClient::~ChromeInvalidationClient() { | 44 ChromeInvalidationClient::~ChromeInvalidationClient() { |
| 42 DCHECK(CalledOnValidThread()); | 45 DCHECK(CalledOnValidThread()); |
| 46 push_client_->RemoveObserver(this); |
| 43 Stop(); | 47 Stop(); |
| 44 DCHECK(!listener_); | 48 DCHECK(!listener_); |
| 45 } | 49 } |
| 46 | 50 |
| 47 void ChromeInvalidationClient::Start( | 51 void ChromeInvalidationClient::Start( |
| 48 const std::string& client_id, const std::string& client_info, | 52 const std::string& client_id, const std::string& client_info, |
| 49 const std::string& state, | 53 const std::string& state, |
| 50 const InvalidationVersionMap& initial_max_invalidation_versions, | 54 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 51 const browser_sync::WeakHandle<InvalidationStateTracker>& | 55 const browser_sync::WeakHandle<InvalidationStateTracker>& |
| 52 invalidation_state_tracker, | 56 invalidation_state_tracker, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 registration_manager_.reset( | 95 registration_manager_.reset( |
| 92 new RegistrationManager(invalidation_client_.get())); | 96 new RegistrationManager(invalidation_client_.get())); |
| 93 } | 97 } |
| 94 | 98 |
| 95 void ChromeInvalidationClient::UpdateCredentials( | 99 void ChromeInvalidationClient::UpdateCredentials( |
| 96 const std::string& email, const std::string& token) { | 100 const std::string& email, const std::string& token) { |
| 97 DCHECK(CalledOnValidThread()); | 101 DCHECK(CalledOnValidThread()); |
| 98 chrome_system_resources_.network()->UpdateCredentials(email, token); | 102 chrome_system_resources_.network()->UpdateCredentials(email, token); |
| 99 } | 103 } |
| 100 | 104 |
| 101 void ChromeInvalidationClient::Stop() { | |
| 102 DCHECK(CalledOnValidThread()); | |
| 103 if (!invalidation_client_.get()) { | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 registration_manager_.reset(); | |
| 108 chrome_system_resources_.Stop(); | |
| 109 invalidation_client_->Stop(); | |
| 110 | |
| 111 invalidation_client_.reset(); | |
| 112 listener_ = NULL; | |
| 113 | |
| 114 invalidation_state_tracker_.Reset(); | |
| 115 max_invalidation_versions_.clear(); | |
| 116 } | |
| 117 | |
| 118 void ChromeInvalidationClient::RegisterTypes(syncable::ModelTypeSet types) { | 105 void ChromeInvalidationClient::RegisterTypes(syncable::ModelTypeSet types) { |
| 119 DCHECK(CalledOnValidThread()); | 106 DCHECK(CalledOnValidThread()); |
| 120 registered_types_ = types; | 107 registered_types_ = types; |
| 121 if (ticl_ready_ && registration_manager_.get()) { | 108 if (GetState() == NO_NOTIFICATION_ERROR && registration_manager_.get()) { |
| 122 registration_manager_->SetRegisteredTypes(registered_types_); | 109 registration_manager_->SetRegisteredTypes(registered_types_); |
| 123 } | 110 } |
| 124 // TODO(akalin): Clear invalidation versions for unregistered types. | 111 // TODO(akalin): Clear invalidation versions for unregistered types. |
| 125 } | 112 } |
| 126 | 113 |
| 127 void ChromeInvalidationClient::Ready( | 114 void ChromeInvalidationClient::Ready( |
| 128 invalidation::InvalidationClient* client) { | 115 invalidation::InvalidationClient* client) { |
| 129 ticl_ready_ = true; | 116 ticl_state_ = NO_NOTIFICATION_ERROR; |
| 130 listener_->OnSessionStatusChanged(true); | 117 EmitStateChange(); |
| 131 registration_manager_->SetRegisteredTypes(registered_types_); | 118 registration_manager_->SetRegisteredTypes(registered_types_); |
| 132 } | 119 } |
| 133 | 120 |
| 134 void ChromeInvalidationClient::Invalidate( | 121 void ChromeInvalidationClient::Invalidate( |
| 135 invalidation::InvalidationClient* client, | 122 invalidation::InvalidationClient* client, |
| 136 const invalidation::Invalidation& invalidation, | 123 const invalidation::Invalidation& invalidation, |
| 137 const invalidation::AckHandle& ack_handle) { | 124 const invalidation::AckHandle& ack_handle) { |
| 138 DCHECK(CalledOnValidThread()); | 125 DCHECK(CalledOnValidThread()); |
| 139 DVLOG(1) << "Invalidate: " << InvalidationToString(invalidation); | 126 DVLOG(1) << "Invalidate: " << InvalidationToString(invalidation); |
| 140 syncable::ModelType model_type; | 127 syncable::ModelType model_type; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 const std::string& prefix, | 266 const std::string& prefix, |
| 280 int prefix_length) { | 267 int prefix_length) { |
| 281 DCHECK(CalledOnValidThread()); | 268 DCHECK(CalledOnValidThread()); |
| 282 DVLOG(1) << "AllRegistrationsLost"; | 269 DVLOG(1) << "AllRegistrationsLost"; |
| 283 registration_manager_->MarkAllRegistrationsLost(); | 270 registration_manager_->MarkAllRegistrationsLost(); |
| 284 } | 271 } |
| 285 | 272 |
| 286 void ChromeInvalidationClient::InformError( | 273 void ChromeInvalidationClient::InformError( |
| 287 invalidation::InvalidationClient* client, | 274 invalidation::InvalidationClient* client, |
| 288 const invalidation::ErrorInfo& error_info) { | 275 const invalidation::ErrorInfo& error_info) { |
| 289 listener_->OnSessionStatusChanged(false); | 276 LOG(ERROR) << "Ticl error " << error_info.error_reason() << ": " |
| 290 LOG(ERROR) << "Invalidation client encountered an error"; | 277 << error_info.error_message() |
| 291 // TODO(ghc): handle the error. | 278 << " (transient = " << error_info.is_transient() << ")"; |
| 279 if (error_info.error_reason() == invalidation::ErrorReason::AUTH_FAILURE) { |
| 280 ticl_state_ = NOTIFICATION_CREDENTIALS_REJECTED; |
| 281 } else { |
| 282 ticl_state_ = TRANSIENT_NOTIFICATION_ERROR; |
| 283 } |
| 284 EmitStateChange(); |
| 292 } | 285 } |
| 293 | 286 |
| 294 void ChromeInvalidationClient::WriteState(const std::string& state) { | 287 void ChromeInvalidationClient::WriteState(const std::string& state) { |
| 295 DCHECK(CalledOnValidThread()); | 288 DCHECK(CalledOnValidThread()); |
| 296 DVLOG(1) << "WriteState"; | 289 DVLOG(1) << "WriteState"; |
| 297 invalidation_state_tracker_.Call( | 290 invalidation_state_tracker_.Call( |
| 298 FROM_HERE, &InvalidationStateTracker::SetInvalidationState, state); | 291 FROM_HERE, &InvalidationStateTracker::SetInvalidationState, state); |
| 299 } | 292 } |
| 300 | 293 |
| 294 void ChromeInvalidationClient::Stop() { |
| 295 DCHECK(CalledOnValidThread()); |
| 296 if (!invalidation_client_.get()) { |
| 297 return; |
| 298 } |
| 299 |
| 300 registration_manager_.reset(); |
| 301 chrome_system_resources_.Stop(); |
| 302 invalidation_client_->Stop(); |
| 303 |
| 304 invalidation_client_.reset(); |
| 305 listener_ = NULL; |
| 306 |
| 307 invalidation_state_tracker_.Reset(); |
| 308 max_invalidation_versions_.clear(); |
| 309 ticl_state_ = DEFAULT_NOTIFICATION_ERROR; |
| 310 push_client_state_ = DEFAULT_NOTIFICATION_ERROR; |
| 311 } |
| 312 |
| 313 NotificationsDisabledReason ChromeInvalidationClient::GetState() const { |
| 314 DCHECK(CalledOnValidThread()); |
| 315 if (ticl_state_ == NOTIFICATION_CREDENTIALS_REJECTED || |
| 316 push_client_state_ == NOTIFICATION_CREDENTIALS_REJECTED) { |
| 317 // If either the ticl or the push client rejected our credentials, |
| 318 // return NOTIFICATION_CREDENTIALS_REJECTED. |
| 319 return NOTIFICATION_CREDENTIALS_REJECTED; |
| 320 } |
| 321 if (ticl_state_ == NO_NOTIFICATION_ERROR && |
| 322 push_client_state_ == NO_NOTIFICATION_ERROR) { |
| 323 // If the ticl is ready and the push client notifications are |
| 324 // enabled, return NO_NOTIFICATION_ERROR. |
| 325 return NO_NOTIFICATION_ERROR; |
| 326 } |
| 327 // Otherwise, we have a transient error. |
| 328 return TRANSIENT_NOTIFICATION_ERROR; |
| 329 } |
| 330 |
| 331 void ChromeInvalidationClient::EmitStateChange() { |
| 332 DCHECK(CalledOnValidThread()); |
| 333 if (GetState() == NO_NOTIFICATION_ERROR) { |
| 334 listener_->OnNotificationsEnabled(); |
| 335 } else { |
| 336 listener_->OnNotificationsDisabled(GetState()); |
| 337 } |
| 338 } |
| 339 |
| 340 void ChromeInvalidationClient::OnNotificationsEnabled() { |
| 341 DCHECK(CalledOnValidThread()); |
| 342 push_client_state_ = NO_NOTIFICATION_ERROR; |
| 343 EmitStateChange(); |
| 344 } |
| 345 |
| 346 void ChromeInvalidationClient::OnNotificationsDisabled( |
| 347 notifier::NotificationsDisabledReason reason) { |
| 348 DCHECK(CalledOnValidThread()); |
| 349 push_client_state_ = FromNotifierReason(reason); |
| 350 EmitStateChange(); |
| 351 } |
| 352 |
| 353 void ChromeInvalidationClient::OnIncomingNotification( |
| 354 const notifier::Notification& notification) { |
| 355 DCHECK(CalledOnValidThread()); |
| 356 // Do nothing, since this is already handled by |invalidation_client_|. |
| 357 } |
| 358 |
| 301 } // namespace sync_notifier | 359 } // namespace sync_notifier |
| OLD | NEW |