| 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" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 // Helpers called on I/O thread. | 29 // Helpers called on I/O thread. |
| 30 void Initialize( | 30 void Initialize( |
| 31 const notifier::NotifierOptions& notifier_options, | 31 const notifier::NotifierOptions& notifier_options, |
| 32 const InvalidationVersionMap& initial_max_invalidation_versions, | 32 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 33 const std::string& initial_invalidation_state, | 33 const std::string& initial_invalidation_state, |
| 34 const syncer::WeakHandle<InvalidationStateTracker>& | 34 const syncer::WeakHandle<InvalidationStateTracker>& |
| 35 invalidation_state_tracker, | 35 invalidation_state_tracker, |
| 36 const std::string& client_info); | 36 const std::string& client_info); |
| 37 void Teardown(); | 37 void Teardown(); |
| 38 void ReloadHandlers(const ObjectIdSet& ids); |
| 38 void SetUniqueId(const std::string& unique_id); | 39 void SetUniqueId(const std::string& unique_id); |
| 39 void SetStateDeprecated(const std::string& state); | 40 void SetStateDeprecated(const std::string& state); |
| 40 void UpdateCredentials(const std::string& email, const std::string& token); | 41 void UpdateCredentials(const std::string& email, const std::string& token); |
| 41 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types); | |
| 42 | 42 |
| 43 // SyncNotifierObserver implementation (all called on I/O thread by | 43 // SyncNotifierObserver implementation (all called on I/O thread by |
| 44 // InvalidationNotifier). | 44 // InvalidationNotifier). |
| 45 virtual ObjectIdSet GetHandledIds() OVERRIDE; |
| 45 virtual void OnNotificationsEnabled() OVERRIDE; | 46 virtual void OnNotificationsEnabled() OVERRIDE; |
| 46 virtual void OnNotificationsDisabled( | 47 virtual void OnNotificationsDisabled( |
| 47 NotificationsDisabledReason reason) OVERRIDE; | 48 NotificationsDisabledReason reason) OVERRIDE; |
| 48 virtual void OnIncomingNotification( | 49 virtual void OnIncomingNotification( |
| 49 const syncer::ModelTypePayloadMap& type_payloads, | 50 const ObjectIdPayloadMap& id_payloads, |
| 50 IncomingNotificationSource source) OVERRIDE; | 51 IncomingNotificationSource source) OVERRIDE; |
| 51 | 52 |
| 52 private: | 53 private: |
| 53 friend class | 54 friend class |
| 54 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>; | 55 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>; |
| 55 // Called on parent or I/O thread. | 56 // Called on parent or I/O thread. |
| 56 ~Core(); | 57 ~Core(); |
| 57 | 58 |
| 58 // The variables below should be used only on the I/O thread. | 59 // The variables below should be used only on the I/O thread. |
| 59 const syncer::WeakHandle<SyncNotifierObserver> delegate_observer_; | 60 const syncer::WeakHandle<SyncNotifierObserver> delegate_observer_; |
| 60 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | 61 scoped_ptr<InvalidationNotifier> invalidation_notifier_; |
| 61 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | 62 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
| 63 ObjectIdSet registered_ids_; |
| 62 | 64 |
| 63 DISALLOW_COPY_AND_ASSIGN(Core); | 65 DISALLOW_COPY_AND_ASSIGN(Core); |
| 64 }; | 66 }; |
| 65 | 67 |
| 66 NonBlockingInvalidationNotifier::Core::Core( | 68 NonBlockingInvalidationNotifier::Core::Core( |
| 67 const syncer::WeakHandle<SyncNotifierObserver>& | 69 const syncer::WeakHandle<SyncNotifierObserver>& |
| 68 delegate_observer) | 70 delegate_observer) |
| 69 : delegate_observer_(delegate_observer) { | 71 : delegate_observer_(delegate_observer) { |
| 70 DCHECK(delegate_observer_.IsInitialized()); | 72 DCHECK(delegate_observer_.IsInitialized()); |
| 71 } | 73 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 86 network_task_runner_ = notifier_options.request_context_getter-> | 88 network_task_runner_ = notifier_options.request_context_getter-> |
| 87 GetNetworkTaskRunner(); | 89 GetNetworkTaskRunner(); |
| 88 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 90 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 89 invalidation_notifier_.reset( | 91 invalidation_notifier_.reset( |
| 90 new InvalidationNotifier( | 92 new InvalidationNotifier( |
| 91 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), | 93 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), |
| 92 initial_max_invalidation_versions, | 94 initial_max_invalidation_versions, |
| 93 initial_invalidation_state, | 95 initial_invalidation_state, |
| 94 invalidation_state_tracker, | 96 invalidation_state_tracker, |
| 95 client_info)); | 97 client_info)); |
| 96 invalidation_notifier_->AddObserver(this); | 98 invalidation_notifier_->AddHandler(this); |
| 97 } | 99 } |
| 98 | 100 |
| 99 | 101 |
| 100 void NonBlockingInvalidationNotifier::Core::Teardown() { | 102 void NonBlockingInvalidationNotifier::Core::Teardown() { |
| 101 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 103 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 102 invalidation_notifier_->RemoveObserver(this); | 104 invalidation_notifier_->RemoveHandler(this); |
| 103 invalidation_notifier_.reset(); | 105 invalidation_notifier_.reset(); |
| 104 network_task_runner_ = NULL; | 106 network_task_runner_ = NULL; |
| 105 } | 107 } |
| 106 | 108 |
| 109 void NonBlockingInvalidationNotifier::Core::ReloadHandlers( |
| 110 const ObjectIdSet& ids) { |
| 111 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 112 registered_ids_ = ids; |
| 113 invalidation_notifier_->ReloadHandlers(); |
| 114 } |
| 115 |
| 107 void NonBlockingInvalidationNotifier::Core::SetUniqueId( | 116 void NonBlockingInvalidationNotifier::Core::SetUniqueId( |
| 108 const std::string& unique_id) { | 117 const std::string& unique_id) { |
| 109 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 118 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 110 invalidation_notifier_->SetUniqueId(unique_id); | 119 invalidation_notifier_->SetUniqueId(unique_id); |
| 111 } | 120 } |
| 112 | 121 |
| 113 void NonBlockingInvalidationNotifier::Core::SetStateDeprecated( | 122 void NonBlockingInvalidationNotifier::Core::SetStateDeprecated( |
| 114 const std::string& state) { | 123 const std::string& state) { |
| 115 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 124 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 116 invalidation_notifier_->SetStateDeprecated(state); | 125 invalidation_notifier_->SetStateDeprecated(state); |
| 117 } | 126 } |
| 118 | 127 |
| 119 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( | 128 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( |
| 120 const std::string& email, const std::string& token) { | 129 const std::string& email, const std::string& token) { |
| 121 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 130 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 122 invalidation_notifier_->UpdateCredentials(email, token); | 131 invalidation_notifier_->UpdateCredentials(email, token); |
| 123 } | 132 } |
| 124 | 133 |
| 125 void NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes( | 134 ObjectIdSet NonBlockingInvalidationNotifier::Core::GetHandledIds() { |
| 126 syncer::ModelTypeSet enabled_types) { | |
| 127 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 135 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 128 invalidation_notifier_->UpdateEnabledTypes(enabled_types); | 136 return registered_ids_; |
| 129 } | 137 } |
| 130 | 138 |
| 131 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { | 139 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { |
| 132 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 140 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 133 delegate_observer_.Call(FROM_HERE, | 141 delegate_observer_.Call(FROM_HERE, |
| 134 &SyncNotifierObserver::OnNotificationsEnabled); | 142 &SyncNotifierObserver::OnNotificationsEnabled); |
| 135 } | 143 } |
| 136 | 144 |
| 137 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( | 145 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( |
| 138 NotificationsDisabledReason reason) { | 146 NotificationsDisabledReason reason) { |
| 139 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 147 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 140 delegate_observer_.Call( | 148 delegate_observer_.Call( |
| 141 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); | 149 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); |
| 142 } | 150 } |
| 143 | 151 |
| 144 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( | 152 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( |
| 145 const syncer::ModelTypePayloadMap& type_payloads, | 153 const ObjectIdPayloadMap& id_payloads, IncomingNotificationSource source) { |
| 146 IncomingNotificationSource source) { | |
| 147 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 154 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 148 delegate_observer_.Call(FROM_HERE, | 155 delegate_observer_.Call(FROM_HERE, |
| 149 &SyncNotifierObserver::OnIncomingNotification, | 156 &SyncNotifierObserver::OnIncomingNotification, |
| 150 type_payloads, | 157 id_payloads, |
| 151 source); | 158 source); |
| 152 } | 159 } |
| 153 | 160 |
| 154 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( | 161 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( |
| 155 const notifier::NotifierOptions& notifier_options, | 162 const notifier::NotifierOptions& notifier_options, |
| 156 const InvalidationVersionMap& initial_max_invalidation_versions, | 163 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 157 const std::string& initial_invalidation_state, | 164 const std::string& initial_invalidation_state, |
| 158 const syncer::WeakHandle<InvalidationStateTracker>& | 165 const syncer::WeakHandle<InvalidationStateTracker>& |
| 159 invalidation_state_tracker, | 166 invalidation_state_tracker, |
| 160 const std::string& client_info) | 167 const std::string& client_info) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 183 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { | 190 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { |
| 184 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 191 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 185 if (!network_task_runner_->PostTask( | 192 if (!network_task_runner_->PostTask( |
| 186 FROM_HERE, | 193 FROM_HERE, |
| 187 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, | 194 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, |
| 188 core_.get()))) { | 195 core_.get()))) { |
| 189 NOTREACHED(); | 196 NOTREACHED(); |
| 190 } | 197 } |
| 191 } | 198 } |
| 192 | 199 |
| 193 void NonBlockingInvalidationNotifier::AddObserver( | 200 void NonBlockingInvalidationNotifier::AddHandler( |
| 194 SyncNotifierObserver* observer) { | 201 SyncNotifierObserver* observer) { |
| 195 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 202 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 196 observers_.AddObserver(observer); | 203 InvalidationNotifierBase::AddHandler(observer); |
| 197 } | 204 } |
| 198 | 205 |
| 199 void NonBlockingInvalidationNotifier::RemoveObserver( | 206 void NonBlockingInvalidationNotifier::RemoveHandler( |
| 200 SyncNotifierObserver* observer) { | 207 SyncNotifierObserver* observer) { |
| 201 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 208 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 202 observers_.RemoveObserver(observer); | 209 InvalidationNotifierBase::RemoveHandler(observer); |
| 210 } |
| 211 |
| 212 void NonBlockingInvalidationNotifier::ReloadHandlers() { |
| 213 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 214 const ObjectIdSet& ids_to_register = UpdateObjectIdObserverMap(); |
| 215 if (!network_task_runner_->PostTask( |
| 216 FROM_HERE, |
| 217 base::Bind(&NonBlockingInvalidationNotifier::Core::ReloadHandlers, |
| 218 core_.get(), ids_to_register))) { |
| 219 NOTREACHED(); |
| 220 } |
| 203 } | 221 } |
| 204 | 222 |
| 205 void NonBlockingInvalidationNotifier::SetUniqueId( | 223 void NonBlockingInvalidationNotifier::SetUniqueId( |
| 206 const std::string& unique_id) { | 224 const std::string& unique_id) { |
| 207 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 225 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 208 if (!network_task_runner_->PostTask( | 226 if (!network_task_runner_->PostTask( |
| 209 FROM_HERE, | 227 FROM_HERE, |
| 210 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, | 228 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, |
| 211 core_.get(), unique_id))) { | 229 core_.get(), unique_id))) { |
| 212 NOTREACHED(); | 230 NOTREACHED(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 229 const std::string& email, const std::string& token) { | 247 const std::string& email, const std::string& token) { |
| 230 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 248 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 231 if (!network_task_runner_->PostTask( | 249 if (!network_task_runner_->PostTask( |
| 232 FROM_HERE, | 250 FROM_HERE, |
| 233 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, | 251 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, |
| 234 core_.get(), email, token))) { | 252 core_.get(), email, token))) { |
| 235 NOTREACHED(); | 253 NOTREACHED(); |
| 236 } | 254 } |
| 237 } | 255 } |
| 238 | 256 |
| 239 void NonBlockingInvalidationNotifier::UpdateEnabledTypes( | |
| 240 syncer::ModelTypeSet enabled_types) { | |
| 241 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
| 242 if (!network_task_runner_->PostTask( | |
| 243 FROM_HERE, | |
| 244 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes, | |
| 245 core_.get(), enabled_types))) { | |
| 246 NOTREACHED(); | |
| 247 } | |
| 248 } | |
| 249 | |
| 250 void NonBlockingInvalidationNotifier::SendNotification( | 257 void NonBlockingInvalidationNotifier::SendNotification( |
| 251 syncer::ModelTypeSet changed_types) { | 258 syncer::ModelTypeSet changed_types) { |
| 252 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 259 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 253 // InvalidationClient doesn't implement SendNotification(), so no | 260 // InvalidationClient doesn't implement SendNotification(), so no |
| 254 // need to forward on the call. | 261 // need to forward on the call. |
| 255 } | 262 } |
| 256 | 263 |
| 264 ObjectIdSet NonBlockingInvalidationNotifier::GetHandledIds() { |
| 265 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 266 return ObjectIdSet(); |
| 267 } |
| 268 |
| 257 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { | 269 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { |
| 258 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 270 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 259 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 271 FOR_EACH_OBSERVER(SyncNotifierObserver, observers(), |
| 260 OnNotificationsEnabled()); | 272 OnNotificationsEnabled()); |
| 261 } | 273 } |
| 262 | 274 |
| 263 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( | 275 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( |
| 264 NotificationsDisabledReason reason) { | 276 NotificationsDisabledReason reason) { |
| 265 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 277 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 266 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 278 FOR_EACH_OBSERVER(SyncNotifierObserver, observers(), |
| 267 OnNotificationsDisabled(reason)); | 279 OnNotificationsDisabled(reason)); |
| 268 } | 280 } |
| 269 | 281 |
| 270 void NonBlockingInvalidationNotifier::OnIncomingNotification( | 282 void NonBlockingInvalidationNotifier::OnIncomingNotification( |
| 271 const syncer::ModelTypePayloadMap& type_payloads, | 283 const ObjectIdPayloadMap& id_payloads, |
| 272 IncomingNotificationSource source) { | 284 IncomingNotificationSource source) { |
| 273 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 285 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 274 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 286 DispatchInvalidationsToObservers(id_payloads, source); |
| 275 OnIncomingNotification(type_payloads, source)); | |
| 276 } | 287 } |
| 277 | 288 |
| 278 } // namespace syncer | 289 } // namespace syncer |
| OLD | NEW |