| 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/non_blocking_invalidation_notifier.h" | 5 #include "sync/notifier/non_blocking_invalidation_notifier.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "jingle/notifier/listener/push_client.h" | 13 #include "jingle/notifier/listener/push_client.h" |
| 14 #include "sync/notifier/invalidation_notifier.h" | 14 #include "sync/notifier/invalidation_notifier.h" |
| 15 | 15 |
| 16 namespace syncer { | 16 namespace syncer { |
| 17 | 17 |
| 18 class NonBlockingInvalidationNotifier::Core | 18 class NonBlockingInvalidationNotifier::Core |
| 19 : public base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>, | 19 : public base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>, |
| 20 // SyncNotifierObserver to observe the InvalidationNotifier we create. | 20 // SyncNotifierObserver to observe the InvalidationNotifier we create. |
| 21 public SyncNotifierObserver { | 21 public SyncNotifierObserver { |
| 22 public: | 22 public: |
| 23 // Called on parent thread. |delegate_observer| should be | 23 // Called on parent thread. |delegate_observer| should be |
| 24 // initialized. | 24 // initialized. |
| 25 explicit Core( | 25 explicit Core( |
| 26 const syncer::WeakHandle<SyncNotifierObserver>& | 26 const WeakHandle<SyncNotifierObserver>& delegate_observer); |
| 27 delegate_observer); | |
| 28 | 27 |
| 29 // Helpers called on I/O thread. | 28 // Helpers called on I/O thread. |
| 30 void Initialize( | 29 void Initialize( |
| 31 const notifier::NotifierOptions& notifier_options, | 30 const notifier::NotifierOptions& notifier_options, |
| 32 const InvalidationVersionMap& initial_max_invalidation_versions, | 31 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 33 const std::string& initial_invalidation_state, | 32 const std::string& initial_invalidation_state, |
| 34 const syncer::WeakHandle<InvalidationStateTracker>& | 33 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, |
| 35 invalidation_state_tracker, | |
| 36 const std::string& client_info); | 34 const std::string& client_info); |
| 37 void Teardown(); | 35 void Teardown(); |
| 38 void SetUniqueId(const std::string& unique_id); | 36 void SetUniqueId(const std::string& unique_id); |
| 39 void SetStateDeprecated(const std::string& state); | 37 void SetStateDeprecated(const std::string& state); |
| 40 void UpdateCredentials(const std::string& email, const std::string& token); | 38 void UpdateCredentials(const std::string& email, const std::string& token); |
| 41 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types); | 39 void UpdateEnabledTypes(ModelTypeSet enabled_types); |
| 42 | 40 |
| 43 // SyncNotifierObserver implementation (all called on I/O thread by | 41 // SyncNotifierObserver implementation (all called on I/O thread by |
| 44 // InvalidationNotifier). | 42 // InvalidationNotifier). |
| 45 virtual void OnNotificationsEnabled() OVERRIDE; | 43 virtual void OnNotificationsEnabled() OVERRIDE; |
| 46 virtual void OnNotificationsDisabled( | 44 virtual void OnNotificationsDisabled( |
| 47 NotificationsDisabledReason reason) OVERRIDE; | 45 NotificationsDisabledReason reason) OVERRIDE; |
| 48 virtual void OnIncomingNotification( | 46 virtual void OnIncomingNotification( |
| 49 const syncer::ModelTypePayloadMap& type_payloads, | 47 const ModelTypePayloadMap& type_payloads, |
| 50 IncomingNotificationSource source) OVERRIDE; | 48 IncomingNotificationSource source) OVERRIDE; |
| 51 | 49 |
| 52 private: | 50 private: |
| 53 friend class | 51 friend class |
| 54 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>; | 52 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>; |
| 55 // Called on parent or I/O thread. | 53 // Called on parent or I/O thread. |
| 56 ~Core(); | 54 ~Core(); |
| 57 | 55 |
| 58 // The variables below should be used only on the I/O thread. | 56 // The variables below should be used only on the I/O thread. |
| 59 const syncer::WeakHandle<SyncNotifierObserver> delegate_observer_; | 57 const WeakHandle<SyncNotifierObserver> delegate_observer_; |
| 60 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | 58 scoped_ptr<InvalidationNotifier> invalidation_notifier_; |
| 61 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | 59 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
| 62 | 60 |
| 63 DISALLOW_COPY_AND_ASSIGN(Core); | 61 DISALLOW_COPY_AND_ASSIGN(Core); |
| 64 }; | 62 }; |
| 65 | 63 |
| 66 NonBlockingInvalidationNotifier::Core::Core( | 64 NonBlockingInvalidationNotifier::Core::Core( |
| 67 const syncer::WeakHandle<SyncNotifierObserver>& | 65 const WeakHandle<SyncNotifierObserver>& delegate_observer) |
| 68 delegate_observer) | |
| 69 : delegate_observer_(delegate_observer) { | 66 : delegate_observer_(delegate_observer) { |
| 70 DCHECK(delegate_observer_.IsInitialized()); | 67 DCHECK(delegate_observer_.IsInitialized()); |
| 71 } | 68 } |
| 72 | 69 |
| 73 NonBlockingInvalidationNotifier::Core::~Core() { | 70 NonBlockingInvalidationNotifier::Core::~Core() { |
| 74 } | 71 } |
| 75 | 72 |
| 76 void NonBlockingInvalidationNotifier::Core::Initialize( | 73 void NonBlockingInvalidationNotifier::Core::Initialize( |
| 77 const notifier::NotifierOptions& notifier_options, | 74 const notifier::NotifierOptions& notifier_options, |
| 78 const InvalidationVersionMap& initial_max_invalidation_versions, | 75 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 79 const std::string& initial_invalidation_state, | 76 const std::string& initial_invalidation_state, |
| 80 const syncer::WeakHandle<InvalidationStateTracker>& | 77 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, |
| 81 invalidation_state_tracker, | |
| 82 const std::string& client_info) { | 78 const std::string& client_info) { |
| 83 DCHECK(notifier_options.request_context_getter); | 79 DCHECK(notifier_options.request_context_getter); |
| 84 DCHECK_EQ(notifier::NOTIFICATION_SERVER, | 80 DCHECK_EQ(notifier::NOTIFICATION_SERVER, |
| 85 notifier_options.notification_method); | 81 notifier_options.notification_method); |
| 86 network_task_runner_ = notifier_options.request_context_getter-> | 82 network_task_runner_ = notifier_options.request_context_getter-> |
| 87 GetNetworkTaskRunner(); | 83 GetNetworkTaskRunner(); |
| 88 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 84 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 89 invalidation_notifier_.reset( | 85 invalidation_notifier_.reset( |
| 90 new InvalidationNotifier( | 86 new InvalidationNotifier( |
| 91 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), | 87 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 116 invalidation_notifier_->SetStateDeprecated(state); | 112 invalidation_notifier_->SetStateDeprecated(state); |
| 117 } | 113 } |
| 118 | 114 |
| 119 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( | 115 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( |
| 120 const std::string& email, const std::string& token) { | 116 const std::string& email, const std::string& token) { |
| 121 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 117 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 122 invalidation_notifier_->UpdateCredentials(email, token); | 118 invalidation_notifier_->UpdateCredentials(email, token); |
| 123 } | 119 } |
| 124 | 120 |
| 125 void NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes( | 121 void NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes( |
| 126 syncer::ModelTypeSet enabled_types) { | 122 ModelTypeSet enabled_types) { |
| 127 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 123 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 128 invalidation_notifier_->UpdateEnabledTypes(enabled_types); | 124 invalidation_notifier_->UpdateEnabledTypes(enabled_types); |
| 129 } | 125 } |
| 130 | 126 |
| 131 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { | 127 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { |
| 132 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 128 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 133 delegate_observer_.Call(FROM_HERE, | 129 delegate_observer_.Call(FROM_HERE, |
| 134 &SyncNotifierObserver::OnNotificationsEnabled); | 130 &SyncNotifierObserver::OnNotificationsEnabled); |
| 135 } | 131 } |
| 136 | 132 |
| 137 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( | 133 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( |
| 138 NotificationsDisabledReason reason) { | 134 NotificationsDisabledReason reason) { |
| 139 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 135 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 140 delegate_observer_.Call( | 136 delegate_observer_.Call( |
| 141 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); | 137 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); |
| 142 } | 138 } |
| 143 | 139 |
| 144 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( | 140 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( |
| 145 const syncer::ModelTypePayloadMap& type_payloads, | 141 const ModelTypePayloadMap& type_payloads, |
| 146 IncomingNotificationSource source) { | 142 IncomingNotificationSource source) { |
| 147 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 143 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 148 delegate_observer_.Call(FROM_HERE, | 144 delegate_observer_.Call(FROM_HERE, |
| 149 &SyncNotifierObserver::OnIncomingNotification, | 145 &SyncNotifierObserver::OnIncomingNotification, |
| 150 type_payloads, | 146 type_payloads, |
| 151 source); | 147 source); |
| 152 } | 148 } |
| 153 | 149 |
| 154 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( | 150 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( |
| 155 const notifier::NotifierOptions& notifier_options, | 151 const notifier::NotifierOptions& notifier_options, |
| 156 const InvalidationVersionMap& initial_max_invalidation_versions, | 152 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 157 const std::string& initial_invalidation_state, | 153 const std::string& initial_invalidation_state, |
| 158 const syncer::WeakHandle<InvalidationStateTracker>& | 154 const WeakHandle<InvalidationStateTracker>& |
| 159 invalidation_state_tracker, | 155 invalidation_state_tracker, |
| 160 const std::string& client_info) | 156 const std::string& client_info) |
| 161 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 157 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 162 core_( | 158 core_( |
| 163 new Core(syncer::MakeWeakHandle( | 159 new Core(MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()))), |
| 164 weak_ptr_factory_.GetWeakPtr()))), | |
| 165 parent_task_runner_( | 160 parent_task_runner_( |
| 166 base::ThreadTaskRunnerHandle::Get()), | 161 base::ThreadTaskRunnerHandle::Get()), |
| 167 network_task_runner_(notifier_options.request_context_getter-> | 162 network_task_runner_(notifier_options.request_context_getter-> |
| 168 GetNetworkTaskRunner()) { | 163 GetNetworkTaskRunner()) { |
| 169 if (!network_task_runner_->PostTask( | 164 if (!network_task_runner_->PostTask( |
| 170 FROM_HERE, | 165 FROM_HERE, |
| 171 base::Bind( | 166 base::Bind( |
| 172 &NonBlockingInvalidationNotifier::Core::Initialize, | 167 &NonBlockingInvalidationNotifier::Core::Initialize, |
| 173 core_.get(), | 168 core_.get(), |
| 174 notifier_options, | 169 notifier_options, |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 225 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 231 if (!network_task_runner_->PostTask( | 226 if (!network_task_runner_->PostTask( |
| 232 FROM_HERE, | 227 FROM_HERE, |
| 233 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, | 228 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, |
| 234 core_.get(), email, token))) { | 229 core_.get(), email, token))) { |
| 235 NOTREACHED(); | 230 NOTREACHED(); |
| 236 } | 231 } |
| 237 } | 232 } |
| 238 | 233 |
| 239 void NonBlockingInvalidationNotifier::UpdateEnabledTypes( | 234 void NonBlockingInvalidationNotifier::UpdateEnabledTypes( |
| 240 syncer::ModelTypeSet enabled_types) { | 235 ModelTypeSet enabled_types) { |
| 241 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 236 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 242 if (!network_task_runner_->PostTask( | 237 if (!network_task_runner_->PostTask( |
| 243 FROM_HERE, | 238 FROM_HERE, |
| 244 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes, | 239 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes, |
| 245 core_.get(), enabled_types))) { | 240 core_.get(), enabled_types))) { |
| 246 NOTREACHED(); | 241 NOTREACHED(); |
| 247 } | 242 } |
| 248 } | 243 } |
| 249 | 244 |
| 250 void NonBlockingInvalidationNotifier::SendNotification( | 245 void NonBlockingInvalidationNotifier::SendNotification( |
| 251 syncer::ModelTypeSet changed_types) { | 246 ModelTypeSet changed_types) { |
| 252 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 247 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 253 // InvalidationClient doesn't implement SendNotification(), so no | 248 // InvalidationClient doesn't implement SendNotification(), so no |
| 254 // need to forward on the call. | 249 // need to forward on the call. |
| 255 } | 250 } |
| 256 | 251 |
| 257 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { | 252 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { |
| 258 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 253 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 259 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 254 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, |
| 260 OnNotificationsEnabled()); | 255 OnNotificationsEnabled()); |
| 261 } | 256 } |
| 262 | 257 |
| 263 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( | 258 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( |
| 264 NotificationsDisabledReason reason) { | 259 NotificationsDisabledReason reason) { |
| 265 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 260 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 266 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 261 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, |
| 267 OnNotificationsDisabled(reason)); | 262 OnNotificationsDisabled(reason)); |
| 268 } | 263 } |
| 269 | 264 |
| 270 void NonBlockingInvalidationNotifier::OnIncomingNotification( | 265 void NonBlockingInvalidationNotifier::OnIncomingNotification( |
| 271 const syncer::ModelTypePayloadMap& type_payloads, | 266 const ModelTypePayloadMap& type_payloads, |
| 272 IncomingNotificationSource source) { | 267 IncomingNotificationSource source) { |
| 273 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 268 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 274 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 269 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, |
| 275 OnIncomingNotification(type_payloads, source)); | 270 OnIncomingNotification(type_payloads, source)); |
| 276 } | 271 } |
| 277 | 272 |
| 278 } // namespace syncer | 273 } // namespace syncer |
| OLD | NEW |