| 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 <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "jingle/notifier/listener/push_client.h" | 15 #include "jingle/notifier/listener/push_client.h" |
| 16 #include "sync/notifier/invalidation_notifier.h" | 16 #include "sync/notifier/invalidation_notifier.h" |
| 17 | 17 |
| 18 namespace syncer { | 18 namespace syncer { |
| 19 | 19 |
| 20 class NonBlockingInvalidationNotifier::Core | 20 class NonBlockingInvalidationNotifier::Core |
| 21 : public base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>, | 21 : public base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>, |
| 22 // SyncNotifierObserver to observe the InvalidationNotifier we create. | 22 // InvalidationHandler to observe the InvalidationNotifier we create. |
| 23 public SyncNotifierObserver { | 23 public InvalidationHandler { |
| 24 public: | 24 public: |
| 25 // Called on parent thread. |delegate_observer| should be | 25 // Called on parent thread. |delegate_observer| should be |
| 26 // initialized. | 26 // initialized. |
| 27 explicit Core( | 27 explicit Core( |
| 28 const WeakHandle<SyncNotifierObserver>& delegate_observer); | 28 const WeakHandle<InvalidationHandler>& delegate_observer); |
| 29 | 29 |
| 30 // Helpers called on I/O thread. | 30 // Helpers called on I/O thread. |
| 31 void Initialize( | 31 void Initialize( |
| 32 const notifier::NotifierOptions& notifier_options, | 32 const notifier::NotifierOptions& notifier_options, |
| 33 const InvalidationVersionMap& initial_max_invalidation_versions, | 33 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 34 const std::string& initial_invalidation_state, | 34 const std::string& initial_invalidation_state, |
| 35 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, | 35 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, |
| 36 const std::string& client_info); | 36 const std::string& client_info); |
| 37 void Teardown(); | 37 void Teardown(); |
| 38 void UpdateRegisteredIds(const ObjectIdSet& ids); | 38 void UpdateRegisteredIds(const ObjectIdSet& ids); |
| 39 void SetUniqueId(const std::string& unique_id); | 39 void SetUniqueId(const std::string& unique_id); |
| 40 void SetStateDeprecated(const std::string& state); | 40 void SetStateDeprecated(const std::string& state); |
| 41 void UpdateCredentials(const std::string& email, const std::string& token); | 41 void UpdateCredentials(const std::string& email, const std::string& token); |
| 42 | 42 |
| 43 // SyncNotifierObserver implementation (all called on I/O thread by | 43 // InvalidationHandler implementation (all called on I/O thread by |
| 44 // InvalidationNotifier). | 44 // InvalidationNotifier). |
| 45 virtual void OnNotificationsEnabled() OVERRIDE; | 45 virtual void OnNotificationsEnabled() OVERRIDE; |
| 46 virtual void OnNotificationsDisabled( | 46 virtual void OnNotificationsDisabled( |
| 47 NotificationsDisabledReason reason) OVERRIDE; | 47 NotificationsDisabledReason reason) OVERRIDE; |
| 48 virtual void OnIncomingNotification( | 48 virtual void OnIncomingNotification( |
| 49 const ObjectIdStateMap& id_state_map, | 49 const ObjectIdStateMap& id_state_map, |
| 50 IncomingNotificationSource source) OVERRIDE; | 50 IncomingNotificationSource source) OVERRIDE; |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 friend class | 53 friend class |
| 54 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>; | 54 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>; |
| 55 // Called on parent or I/O thread. | 55 // Called on parent or I/O thread. |
| 56 ~Core(); | 56 ~Core(); |
| 57 | 57 |
| 58 // The variables below should be used only on the I/O thread. | 58 // The variables below should be used only on the I/O thread. |
| 59 const WeakHandle<SyncNotifierObserver> delegate_observer_; | 59 const WeakHandle<InvalidationHandler> delegate_observer_; |
| 60 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | 60 scoped_ptr<InvalidationNotifier> invalidation_notifier_; |
| 61 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | 61 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
| 62 | 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(Core); | 63 DISALLOW_COPY_AND_ASSIGN(Core); |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 NonBlockingInvalidationNotifier::Core::Core( | 66 NonBlockingInvalidationNotifier::Core::Core( |
| 67 const WeakHandle<SyncNotifierObserver>& delegate_observer) | 67 const WeakHandle<InvalidationHandler>& delegate_observer) |
| 68 : delegate_observer_(delegate_observer) { | 68 : delegate_observer_(delegate_observer) { |
| 69 DCHECK(delegate_observer_.IsInitialized()); | 69 DCHECK(delegate_observer_.IsInitialized()); |
| 70 } | 70 } |
| 71 | 71 |
| 72 NonBlockingInvalidationNotifier::Core::~Core() { | 72 NonBlockingInvalidationNotifier::Core::~Core() { |
| 73 } | 73 } |
| 74 | 74 |
| 75 void NonBlockingInvalidationNotifier::Core::Initialize( | 75 void NonBlockingInvalidationNotifier::Core::Initialize( |
| 76 const notifier::NotifierOptions& notifier_options, | 76 const notifier::NotifierOptions& notifier_options, |
| 77 const InvalidationVersionMap& initial_max_invalidation_versions, | 77 const InvalidationVersionMap& initial_max_invalidation_versions, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 | 121 |
| 122 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( | 122 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( |
| 123 const std::string& email, const std::string& token) { | 123 const std::string& email, const std::string& token) { |
| 124 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 124 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 125 invalidation_notifier_->UpdateCredentials(email, token); | 125 invalidation_notifier_->UpdateCredentials(email, token); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { | 128 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { |
| 129 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 129 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 130 delegate_observer_.Call(FROM_HERE, | 130 delegate_observer_.Call(FROM_HERE, |
| 131 &SyncNotifierObserver::OnNotificationsEnabled); | 131 &InvalidationHandler::OnNotificationsEnabled); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( | 134 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( |
| 135 NotificationsDisabledReason reason) { | 135 NotificationsDisabledReason reason) { |
| 136 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 136 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 137 delegate_observer_.Call( | 137 delegate_observer_.Call( |
| 138 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); | 138 FROM_HERE, &InvalidationHandler::OnNotificationsDisabled, reason); |
| 139 } | 139 } |
| 140 | 140 |
| 141 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( | 141 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( |
| 142 const ObjectIdStateMap& id_state_map, IncomingNotificationSource source) { | 142 const ObjectIdStateMap& id_state_map, IncomingNotificationSource source) { |
| 143 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 143 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 144 delegate_observer_.Call(FROM_HERE, | 144 delegate_observer_.Call(FROM_HERE, |
| 145 &SyncNotifierObserver::OnIncomingNotification, | 145 &InvalidationHandler::OnIncomingNotification, |
| 146 id_state_map, | 146 id_state_map, |
| 147 source); | 147 source); |
| 148 } | 148 } |
| 149 | 149 |
| 150 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( | 150 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( |
| 151 const notifier::NotifierOptions& notifier_options, | 151 const notifier::NotifierOptions& notifier_options, |
| 152 const InvalidationVersionMap& initial_max_invalidation_versions, | 152 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 153 const std::string& initial_invalidation_state, | 153 const std::string& initial_invalidation_state, |
| 154 const WeakHandle<InvalidationStateTracker>& | 154 const WeakHandle<InvalidationStateTracker>& |
| 155 invalidation_state_tracker, | 155 invalidation_state_tracker, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 179 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 179 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 180 if (!network_task_runner_->PostTask( | 180 if (!network_task_runner_->PostTask( |
| 181 FROM_HERE, | 181 FROM_HERE, |
| 182 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, | 182 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, |
| 183 core_.get()))) { | 183 core_.get()))) { |
| 184 NOTREACHED(); | 184 NOTREACHED(); |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 | 187 |
| 188 void NonBlockingInvalidationNotifier::RegisterHandler( | 188 void NonBlockingInvalidationNotifier::RegisterHandler( |
| 189 SyncNotifierObserver* handler) { | 189 InvalidationHandler* handler) { |
| 190 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 190 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 191 registrar_.RegisterHandler(handler); | 191 registrar_.RegisterHandler(handler); |
| 192 } | 192 } |
| 193 | 193 |
| 194 void NonBlockingInvalidationNotifier::UpdateRegisteredIds( | 194 void NonBlockingInvalidationNotifier::UpdateRegisteredIds( |
| 195 SyncNotifierObserver* handler, | 195 InvalidationHandler* handler, |
| 196 const ObjectIdSet& ids) { | 196 const ObjectIdSet& ids) { |
| 197 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 197 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 198 registrar_.UpdateRegisteredIds(handler, ids); | 198 registrar_.UpdateRegisteredIds(handler, ids); |
| 199 if (!network_task_runner_->PostTask( | 199 if (!network_task_runner_->PostTask( |
| 200 FROM_HERE, | 200 FROM_HERE, |
| 201 base::Bind( | 201 base::Bind( |
| 202 &NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds, | 202 &NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds, |
| 203 core_.get(), | 203 core_.get(), |
| 204 registrar_.GetAllRegisteredIds()))) { | 204 registrar_.GetAllRegisteredIds()))) { |
| 205 NOTREACHED(); | 205 NOTREACHED(); |
| 206 } | 206 } |
| 207 } | 207 } |
| 208 | 208 |
| 209 void NonBlockingInvalidationNotifier::UnregisterHandler( | 209 void NonBlockingInvalidationNotifier::UnregisterHandler( |
| 210 SyncNotifierObserver* handler) { | 210 InvalidationHandler* handler) { |
| 211 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 211 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 212 registrar_.UnregisterHandler(handler); | 212 registrar_.UnregisterHandler(handler); |
| 213 } | 213 } |
| 214 | 214 |
| 215 void NonBlockingInvalidationNotifier::SetUniqueId( | 215 void NonBlockingInvalidationNotifier::SetUniqueId( |
| 216 const std::string& unique_id) { | 216 const std::string& unique_id) { |
| 217 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 217 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 218 if (!network_task_runner_->PostTask( | 218 if (!network_task_runner_->PostTask( |
| 219 FROM_HERE, | 219 FROM_HERE, |
| 220 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, | 220 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 } | 265 } |
| 266 | 266 |
| 267 void NonBlockingInvalidationNotifier::OnIncomingNotification( | 267 void NonBlockingInvalidationNotifier::OnIncomingNotification( |
| 268 const ObjectIdStateMap& id_state_map, | 268 const ObjectIdStateMap& id_state_map, |
| 269 IncomingNotificationSource source) { | 269 IncomingNotificationSource source) { |
| 270 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 270 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 271 registrar_.DispatchInvalidationsToHandlers(id_state_map, source); | 271 registrar_.DispatchInvalidationsToHandlers(id_state_map, source); |
| 272 } | 272 } |
| 273 | 273 |
| 274 } // namespace syncer | 274 } // namespace syncer |
| OLD | NEW |