| 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_sync_notifier_state_(NOTIFICATIONS_OFF), |
| 39 push_client_sync_notifier_state_(NOTIFICATIONS_OFF) { |
| 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 (GetSyncNotifierState() == NOTIFICATIONS_ON && |
| 109 registration_manager_.get()) { |
| 122 registration_manager_->SetRegisteredTypes(registered_types_); | 110 registration_manager_->SetRegisteredTypes(registered_types_); |
| 123 } | 111 } |
| 124 // TODO(akalin): Clear invalidation versions for unregistered types. | 112 // TODO(akalin): Clear invalidation versions for unregistered types. |
| 125 } | 113 } |
| 126 | 114 |
| 127 void ChromeInvalidationClient::Ready( | 115 void ChromeInvalidationClient::Ready( |
| 128 invalidation::InvalidationClient* client) { | 116 invalidation::InvalidationClient* client) { |
| 129 ticl_ready_ = true; | 117 ticl_sync_notifier_state_ = NOTIFICATIONS_ON; |
| 130 listener_->OnSessionStatusChanged(true); | 118 EmitStateChange(); |
| 131 registration_manager_->SetRegisteredTypes(registered_types_); | 119 registration_manager_->SetRegisteredTypes(registered_types_); |
| 132 } | 120 } |
| 133 | 121 |
| 134 void ChromeInvalidationClient::Invalidate( | 122 void ChromeInvalidationClient::Invalidate( |
| 135 invalidation::InvalidationClient* client, | 123 invalidation::InvalidationClient* client, |
| 136 const invalidation::Invalidation& invalidation, | 124 const invalidation::Invalidation& invalidation, |
| 137 const invalidation::AckHandle& ack_handle) { | 125 const invalidation::AckHandle& ack_handle) { |
| 138 DCHECK(CalledOnValidThread()); | 126 DCHECK(CalledOnValidThread()); |
| 139 DVLOG(1) << "Invalidate: " << InvalidationToString(invalidation); | 127 DVLOG(1) << "Invalidate: " << InvalidationToString(invalidation); |
| 140 syncable::ModelType model_type; | 128 syncable::ModelType model_type; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 const std::string& prefix, | 267 const std::string& prefix, |
| 280 int prefix_length) { | 268 int prefix_length) { |
| 281 DCHECK(CalledOnValidThread()); | 269 DCHECK(CalledOnValidThread()); |
| 282 DVLOG(1) << "AllRegistrationsLost"; | 270 DVLOG(1) << "AllRegistrationsLost"; |
| 283 registration_manager_->MarkAllRegistrationsLost(); | 271 registration_manager_->MarkAllRegistrationsLost(); |
| 284 } | 272 } |
| 285 | 273 |
| 286 void ChromeInvalidationClient::InformError( | 274 void ChromeInvalidationClient::InformError( |
| 287 invalidation::InvalidationClient* client, | 275 invalidation::InvalidationClient* client, |
| 288 const invalidation::ErrorInfo& error_info) { | 276 const invalidation::ErrorInfo& error_info) { |
| 289 listener_->OnSessionStatusChanged(false); | 277 LOG(ERROR) << "Ticl error " << error_info.error_reason() << ": " |
| 290 LOG(ERROR) << "Invalidation client encountered an error"; | 278 << error_info.error_message() |
| 291 // TODO(ghc): handle the error. | 279 << " (transient = " << error_info.is_transient() << ")"; |
| 280 if (error_info.error_reason() == invalidation::ErrorReason::AUTH_FAILURE) { |
| 281 ticl_sync_notifier_state_ = CREDENTIALS_REJECTED; |
| 282 } else { |
| 283 ticl_sync_notifier_state_ = NOTIFICATIONS_OFF; |
| 284 } |
| 285 EmitStateChange(); |
| 292 } | 286 } |
| 293 | 287 |
| 294 void ChromeInvalidationClient::WriteState(const std::string& state) { | 288 void ChromeInvalidationClient::WriteState(const std::string& state) { |
| 295 DCHECK(CalledOnValidThread()); | 289 DCHECK(CalledOnValidThread()); |
| 296 DVLOG(1) << "WriteState"; | 290 DVLOG(1) << "WriteState"; |
| 297 invalidation_state_tracker_.Call( | 291 invalidation_state_tracker_.Call( |
| 298 FROM_HERE, &InvalidationStateTracker::SetInvalidationState, state); | 292 FROM_HERE, &InvalidationStateTracker::SetInvalidationState, state); |
| 299 } | 293 } |
| 300 | 294 |
| 295 void ChromeInvalidationClient::OnPushClientStateChange( |
| 296 notifier::PushClientState push_client_state) { |
| 297 DCHECK(CalledOnValidThread()); |
| 298 push_client_sync_notifier_state_ = |
| 299 PushClientStateToSyncNotifierState(push_client_state); |
| 300 EmitStateChange(); |
| 301 } |
| 302 |
| 303 SyncNotifierState ChromeInvalidationClient::GetSyncNotifierState() const { |
| 304 DCHECK(CalledOnValidThread()); |
| 305 if (ticl_sync_notifier_state_ == CREDENTIALS_REJECTED || |
| 306 push_client_sync_notifier_state_ == CREDENTIALS_REJECTED) { |
| 307 // If either state is CREDENTIALS_REJECTED, return |
| 308 // CREDENTIALS_REJECTED. |
| 309 return CREDENTIALS_REJECTED; |
| 310 } |
| 311 if (ticl_sync_notifier_state_ == NOTIFICATIONS_ON && |
| 312 push_client_sync_notifier_state_ == NOTIFICATIONS_ON) { |
| 313 // Emit NOTIFICATIONS_ON only when both states are |
| 314 // NOTIFICATIONS_ON. |
| 315 return NOTIFICATIONS_ON; |
| 316 } |
| 317 // Otherwise return NOTIFICATIONS_OFF. |
| 318 return NOTIFICATIONS_OFF; |
| 319 } |
| 320 |
| 321 void ChromeInvalidationClient::Stop() { |
| 322 DCHECK(CalledOnValidThread()); |
| 323 if (!invalidation_client_.get()) { |
| 324 return; |
| 325 } |
| 326 |
| 327 registration_manager_.reset(); |
| 328 chrome_system_resources_.Stop(); |
| 329 invalidation_client_->Stop(); |
| 330 |
| 331 invalidation_client_.reset(); |
| 332 listener_ = NULL; |
| 333 |
| 334 invalidation_state_tracker_.Reset(); |
| 335 max_invalidation_versions_.clear(); |
| 336 ticl_sync_notifier_state_ = NOTIFICATIONS_OFF; |
| 337 push_client_sync_notifier_state_ = NOTIFICATIONS_OFF; |
| 338 } |
| 339 |
| 340 void ChromeInvalidationClient::EmitStateChange() { |
| 341 DCHECK(CalledOnValidThread()); |
| 342 listener_->OnSyncNotifierStateChange(GetSyncNotifierState()); |
| 343 } |
| 344 |
| 345 void ChromeInvalidationClient::OnIncomingNotification( |
| 346 const notifier::Notification& notification) { |
| 347 DCHECK(CalledOnValidThread()); |
| 348 // Do nothing, since this is already handled by |invalidation_client_|. |
| 349 } |
| 350 |
| 301 } // namespace sync_notifier | 351 } // namespace sync_notifier |
| OLD | NEW |