Chromium Code Reviews| 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 UpdateRegisteredIds(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 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 syncer::ModelTypePayloadMap& type_payloads, | 49 const ObjectIdPayloadMap& id_payloads, |
| 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 syncer::WeakHandle<SyncNotifierObserver> delegate_observer_; | 59 const syncer::WeakHandle<SyncNotifierObserver> delegate_observer_; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 86 network_task_runner_ = notifier_options.request_context_getter-> | 86 network_task_runner_ = notifier_options.request_context_getter-> |
| 87 GetNetworkTaskRunner(); | 87 GetNetworkTaskRunner(); |
| 88 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 88 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 89 invalidation_notifier_.reset( | 89 invalidation_notifier_.reset( |
| 90 new InvalidationNotifier( | 90 new InvalidationNotifier( |
| 91 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), | 91 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), |
| 92 initial_max_invalidation_versions, | 92 initial_max_invalidation_versions, |
| 93 initial_invalidation_state, | 93 initial_invalidation_state, |
| 94 invalidation_state_tracker, | 94 invalidation_state_tracker, |
| 95 client_info)); | 95 client_info)); |
| 96 invalidation_notifier_->AddObserver(this); | |
| 97 } | 96 } |
| 98 | 97 |
| 99 | 98 |
| 100 void NonBlockingInvalidationNotifier::Core::Teardown() { | 99 void NonBlockingInvalidationNotifier::Core::Teardown() { |
| 101 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 100 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 102 invalidation_notifier_->RemoveObserver(this); | 101 invalidation_notifier_->UpdateRegisteredIds(this, ObjectIdSet()); |
| 103 invalidation_notifier_.reset(); | 102 invalidation_notifier_.reset(); |
| 104 network_task_runner_ = NULL; | 103 network_task_runner_ = NULL; |
| 105 } | 104 } |
| 106 | 105 |
| 106 void NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds( | |
| 107 const ObjectIdSet& ids) { | |
| 108 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
| 109 invalidation_notifier_->UpdateRegisteredIds(this, ids); | |
| 110 } | |
| 111 | |
| 107 void NonBlockingInvalidationNotifier::Core::SetUniqueId( | 112 void NonBlockingInvalidationNotifier::Core::SetUniqueId( |
| 108 const std::string& unique_id) { | 113 const std::string& unique_id) { |
| 109 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 114 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 110 invalidation_notifier_->SetUniqueId(unique_id); | 115 invalidation_notifier_->SetUniqueId(unique_id); |
| 111 } | 116 } |
| 112 | 117 |
| 113 void NonBlockingInvalidationNotifier::Core::SetStateDeprecated( | 118 void NonBlockingInvalidationNotifier::Core::SetStateDeprecated( |
| 114 const std::string& state) { | 119 const std::string& state) { |
| 115 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 120 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 116 invalidation_notifier_->SetStateDeprecated(state); | 121 invalidation_notifier_->SetStateDeprecated(state); |
| 117 } | 122 } |
| 118 | 123 |
| 119 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( | 124 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( |
| 120 const std::string& email, const std::string& token) { | 125 const std::string& email, const std::string& token) { |
| 121 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 126 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 122 invalidation_notifier_->UpdateCredentials(email, token); | 127 invalidation_notifier_->UpdateCredentials(email, token); |
| 123 } | 128 } |
| 124 | 129 |
| 125 void NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes( | |
| 126 syncer::ModelTypeSet enabled_types) { | |
| 127 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
| 128 invalidation_notifier_->UpdateEnabledTypes(enabled_types); | |
| 129 } | |
| 130 | |
| 131 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { | 130 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { |
| 132 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 131 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 133 delegate_observer_.Call(FROM_HERE, | 132 delegate_observer_.Call(FROM_HERE, |
| 134 &SyncNotifierObserver::OnNotificationsEnabled); | 133 &SyncNotifierObserver::OnNotificationsEnabled); |
| 135 } | 134 } |
| 136 | 135 |
| 137 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( | 136 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( |
| 138 NotificationsDisabledReason reason) { | 137 NotificationsDisabledReason reason) { |
| 139 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 138 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 140 delegate_observer_.Call( | 139 delegate_observer_.Call( |
| 141 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); | 140 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); |
| 142 } | 141 } |
| 143 | 142 |
| 144 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( | 143 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( |
| 145 const syncer::ModelTypePayloadMap& type_payloads, | 144 const ObjectIdPayloadMap& id_payloads, IncomingNotificationSource source) { |
| 146 IncomingNotificationSource source) { | |
| 147 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 145 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 148 delegate_observer_.Call(FROM_HERE, | 146 delegate_observer_.Call(FROM_HERE, |
| 149 &SyncNotifierObserver::OnIncomingNotification, | 147 &SyncNotifierObserver::OnIncomingNotification, |
| 150 type_payloads, | 148 id_payloads, |
| 151 source); | 149 source); |
| 152 } | 150 } |
| 153 | 151 |
| 154 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( | 152 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( |
| 155 const notifier::NotifierOptions& notifier_options, | 153 const notifier::NotifierOptions& notifier_options, |
| 156 const InvalidationVersionMap& initial_max_invalidation_versions, | 154 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 157 const std::string& initial_invalidation_state, | 155 const std::string& initial_invalidation_state, |
| 158 const syncer::WeakHandle<InvalidationStateTracker>& | 156 const syncer::WeakHandle<InvalidationStateTracker>& |
| 159 invalidation_state_tracker, | 157 invalidation_state_tracker, |
| 160 const std::string& client_info) | 158 const std::string& client_info) |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 183 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { | 181 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { |
| 184 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 182 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 185 if (!network_task_runner_->PostTask( | 183 if (!network_task_runner_->PostTask( |
| 186 FROM_HERE, | 184 FROM_HERE, |
| 187 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, | 185 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, |
| 188 core_.get()))) { | 186 core_.get()))) { |
| 189 NOTREACHED(); | 187 NOTREACHED(); |
| 190 } | 188 } |
| 191 } | 189 } |
| 192 | 190 |
| 193 void NonBlockingInvalidationNotifier::AddObserver( | 191 void NonBlockingInvalidationNotifier::UpdateRegisteredIds( |
| 194 SyncNotifierObserver* observer) { | 192 SyncNotifierObserver* handler, const ObjectIdSet& ids) { |
| 195 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 193 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 196 observers_.AddObserver(observer); | 194 if (!network_task_runner_->PostTask( |
| 197 } | 195 FROM_HERE, |
| 198 | 196 base::Bind( |
| 199 void NonBlockingInvalidationNotifier::RemoveObserver( | 197 &NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds, |
| 200 SyncNotifierObserver* observer) { | 198 core_.get(), |
| 201 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 199 // SyncNotifierHelper::UpdateRegisteredIds updates its internal |
|
akalin
2012/07/20 19:04:54
Yeah, maybe the comment isn't that helpful. On se
dcheng
2012/07/21 00:09:54
Done.
| |
| 202 observers_.RemoveObserver(observer); | 200 // mappings, then returns all the registered IDs. Pass it to the |
| 201 // Core to update our registrations with the actual | |
| 202 // InvalidationNotifier. | |
| 203 helper_.UpdateRegisteredIds(handler, ids)))) { | |
| 204 NOTREACHED(); | |
| 205 } | |
| 203 } | 206 } |
| 204 | 207 |
| 205 void NonBlockingInvalidationNotifier::SetUniqueId( | 208 void NonBlockingInvalidationNotifier::SetUniqueId( |
| 206 const std::string& unique_id) { | 209 const std::string& unique_id) { |
| 207 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 210 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 208 if (!network_task_runner_->PostTask( | 211 if (!network_task_runner_->PostTask( |
| 209 FROM_HERE, | 212 FROM_HERE, |
| 210 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, | 213 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, |
| 211 core_.get(), unique_id))) { | 214 core_.get(), unique_id))) { |
| 212 NOTREACHED(); | 215 NOTREACHED(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 229 const std::string& email, const std::string& token) { | 232 const std::string& email, const std::string& token) { |
| 230 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 233 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 231 if (!network_task_runner_->PostTask( | 234 if (!network_task_runner_->PostTask( |
| 232 FROM_HERE, | 235 FROM_HERE, |
| 233 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, | 236 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, |
| 234 core_.get(), email, token))) { | 237 core_.get(), email, token))) { |
| 235 NOTREACHED(); | 238 NOTREACHED(); |
| 236 } | 239 } |
| 237 } | 240 } |
| 238 | 241 |
| 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( | 242 void NonBlockingInvalidationNotifier::SendNotification( |
| 251 syncer::ModelTypeSet changed_types) { | 243 syncer::ModelTypeSet changed_types) { |
| 252 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 244 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 253 // InvalidationClient doesn't implement SendNotification(), so no | 245 // InvalidationClient doesn't implement SendNotification(), so no |
| 254 // need to forward on the call. | 246 // need to forward on the call. |
| 255 } | 247 } |
| 256 | 248 |
| 257 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { | 249 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { |
| 258 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 250 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 259 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 251 helper_.OnNotificationsEnabled(); |
| 260 OnNotificationsEnabled()); | |
| 261 } | 252 } |
| 262 | 253 |
| 263 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( | 254 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( |
| 264 NotificationsDisabledReason reason) { | 255 NotificationsDisabledReason reason) { |
| 265 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 256 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 266 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 257 helper_.OnNotificationsDisabled(reason); |
| 267 OnNotificationsDisabled(reason)); | |
| 268 } | 258 } |
| 269 | 259 |
| 270 void NonBlockingInvalidationNotifier::OnIncomingNotification( | 260 void NonBlockingInvalidationNotifier::OnIncomingNotification( |
| 271 const syncer::ModelTypePayloadMap& type_payloads, | 261 const ObjectIdPayloadMap& id_payloads, |
| 272 IncomingNotificationSource source) { | 262 IncomingNotificationSource source) { |
| 273 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 263 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 274 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 264 helper_.DispatchInvalidationsToHandlers(id_payloads, source); |
| 275 OnIncomingNotification(type_payloads, source)); | |
| 276 } | 265 } |
| 277 | 266 |
| 278 } // namespace syncer | 267 } // namespace syncer |
| OLD | NEW |