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 15 matching lines...) Expand all Loading... | |
| 26 const WeakHandle<SyncNotifierObserver>& delegate_observer); | 26 const WeakHandle<SyncNotifierObserver>& delegate_observer); |
| 27 | 27 |
| 28 // Helpers called on I/O thread. | 28 // Helpers called on I/O thread. |
| 29 void Initialize( | 29 void Initialize( |
| 30 const notifier::NotifierOptions& notifier_options, | 30 const notifier::NotifierOptions& notifier_options, |
| 31 const InvalidationVersionMap& initial_max_invalidation_versions, | 31 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 32 const std::string& initial_invalidation_state, | 32 const std::string& initial_invalidation_state, |
| 33 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, | 33 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, |
| 34 const std::string& client_info); | 34 const std::string& client_info); |
| 35 void Teardown(); | 35 void Teardown(); |
| 36 void UpdateRegisteredIds(const ObjectIdSet& ids); | |
| 36 void SetUniqueId(const std::string& unique_id); | 37 void SetUniqueId(const std::string& unique_id); |
| 37 void SetStateDeprecated(const std::string& state); | 38 void SetStateDeprecated(const std::string& state); |
| 38 void UpdateCredentials(const std::string& email, const std::string& token); | 39 void UpdateCredentials(const std::string& email, const std::string& token); |
| 39 void UpdateEnabledTypes(ModelTypeSet enabled_types); | |
| 40 | 40 |
| 41 // SyncNotifierObserver implementation (all called on I/O thread by | 41 // SyncNotifierObserver implementation (all called on I/O thread by |
| 42 // InvalidationNotifier). | 42 // InvalidationNotifier). |
| 43 virtual void OnNotificationsEnabled() OVERRIDE; | 43 virtual void OnNotificationsEnabled() OVERRIDE; |
| 44 virtual void OnNotificationsDisabled( | 44 virtual void OnNotificationsDisabled( |
| 45 NotificationsDisabledReason reason) OVERRIDE; | 45 NotificationsDisabledReason reason) OVERRIDE; |
| 46 virtual void OnIncomingNotification( | 46 virtual void OnIncomingNotification( |
| 47 const ModelTypePayloadMap& type_payloads, | 47 const ObjectIdPayloadMap& id_payloads, |
| 48 IncomingNotificationSource source) OVERRIDE; | 48 IncomingNotificationSource source) OVERRIDE; |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 friend class | 51 friend class |
| 52 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>; | 52 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>; |
| 53 // Called on parent or I/O thread. | 53 // Called on parent or I/O thread. |
| 54 ~Core(); | 54 ~Core(); |
| 55 | 55 |
| 56 // 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. |
| 57 const WeakHandle<SyncNotifierObserver> delegate_observer_; | 57 const WeakHandle<SyncNotifierObserver> delegate_observer_; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 82 network_task_runner_ = notifier_options.request_context_getter-> | 82 network_task_runner_ = notifier_options.request_context_getter-> |
| 83 GetNetworkTaskRunner(); | 83 GetNetworkTaskRunner(); |
| 84 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 84 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 85 invalidation_notifier_.reset( | 85 invalidation_notifier_.reset( |
| 86 new InvalidationNotifier( | 86 new InvalidationNotifier( |
| 87 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), | 87 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), |
| 88 initial_max_invalidation_versions, | 88 initial_max_invalidation_versions, |
| 89 initial_invalidation_state, | 89 initial_invalidation_state, |
| 90 invalidation_state_tracker, | 90 invalidation_state_tracker, |
| 91 client_info)); | 91 client_info)); |
| 92 invalidation_notifier_->AddObserver(this); | |
| 93 } | 92 } |
| 94 | 93 |
| 95 | 94 |
| 96 void NonBlockingInvalidationNotifier::Core::Teardown() { | 95 void NonBlockingInvalidationNotifier::Core::Teardown() { |
| 97 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 96 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 98 invalidation_notifier_->RemoveObserver(this); | 97 invalidation_notifier_->UpdateRegisteredIds(this, ObjectIdSet()); |
| 99 invalidation_notifier_.reset(); | 98 invalidation_notifier_.reset(); |
| 100 network_task_runner_ = NULL; | 99 network_task_runner_ = NULL; |
| 101 } | 100 } |
| 102 | 101 |
| 102 void NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds( | |
| 103 const ObjectIdSet& ids) { | |
| 104 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
| 105 invalidation_notifier_->UpdateRegisteredIds(this, ids); | |
| 106 } | |
| 107 | |
| 103 void NonBlockingInvalidationNotifier::Core::SetUniqueId( | 108 void NonBlockingInvalidationNotifier::Core::SetUniqueId( |
| 104 const std::string& unique_id) { | 109 const std::string& unique_id) { |
| 105 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 110 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 106 invalidation_notifier_->SetUniqueId(unique_id); | 111 invalidation_notifier_->SetUniqueId(unique_id); |
| 107 } | 112 } |
| 108 | 113 |
| 109 void NonBlockingInvalidationNotifier::Core::SetStateDeprecated( | 114 void NonBlockingInvalidationNotifier::Core::SetStateDeprecated( |
| 110 const std::string& state) { | 115 const std::string& state) { |
| 111 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 116 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 112 invalidation_notifier_->SetStateDeprecated(state); | 117 invalidation_notifier_->SetStateDeprecated(state); |
| 113 } | 118 } |
| 114 | 119 |
| 115 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( | 120 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( |
| 116 const std::string& email, const std::string& token) { | 121 const std::string& email, const std::string& token) { |
| 117 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 122 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 118 invalidation_notifier_->UpdateCredentials(email, token); | 123 invalidation_notifier_->UpdateCredentials(email, token); |
| 119 } | 124 } |
| 120 | 125 |
| 121 void NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes( | |
| 122 ModelTypeSet enabled_types) { | |
| 123 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
| 124 invalidation_notifier_->UpdateEnabledTypes(enabled_types); | |
| 125 } | |
| 126 | |
| 127 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { | 126 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { |
| 128 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 127 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 129 delegate_observer_.Call(FROM_HERE, | 128 delegate_observer_.Call(FROM_HERE, |
| 130 &SyncNotifierObserver::OnNotificationsEnabled); | 129 &SyncNotifierObserver::OnNotificationsEnabled); |
| 131 } | 130 } |
| 132 | 131 |
| 133 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( | 132 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( |
| 134 NotificationsDisabledReason reason) { | 133 NotificationsDisabledReason reason) { |
| 135 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 134 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 136 delegate_observer_.Call( | 135 delegate_observer_.Call( |
| 137 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); | 136 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); |
| 138 } | 137 } |
| 139 | 138 |
| 140 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( | 139 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( |
| 141 const ModelTypePayloadMap& type_payloads, | 140 const ObjectIdPayloadMap& id_payloads, IncomingNotificationSource source) { |
| 142 IncomingNotificationSource source) { | |
| 143 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 141 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 144 delegate_observer_.Call(FROM_HERE, | 142 delegate_observer_.Call(FROM_HERE, |
| 145 &SyncNotifierObserver::OnIncomingNotification, | 143 &SyncNotifierObserver::OnIncomingNotification, |
| 146 type_payloads, | 144 id_payloads, |
| 147 source); | 145 source); |
| 148 } | 146 } |
| 149 | 147 |
| 150 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( | 148 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( |
| 151 const notifier::NotifierOptions& notifier_options, | 149 const notifier::NotifierOptions& notifier_options, |
| 152 const InvalidationVersionMap& initial_max_invalidation_versions, | 150 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 153 const std::string& initial_invalidation_state, | 151 const std::string& initial_invalidation_state, |
| 154 const WeakHandle<InvalidationStateTracker>& | 152 const WeakHandle<InvalidationStateTracker>& |
| 155 invalidation_state_tracker, | 153 invalidation_state_tracker, |
| 156 const std::string& client_info) | 154 const std::string& client_info) |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 178 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { | 176 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { |
| 179 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 177 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 180 if (!network_task_runner_->PostTask( | 178 if (!network_task_runner_->PostTask( |
| 181 FROM_HERE, | 179 FROM_HERE, |
| 182 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, | 180 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, |
| 183 core_.get()))) { | 181 core_.get()))) { |
| 184 NOTREACHED(); | 182 NOTREACHED(); |
| 185 } | 183 } |
| 186 } | 184 } |
| 187 | 185 |
| 188 void NonBlockingInvalidationNotifier::AddObserver( | 186 void NonBlockingInvalidationNotifier::UpdateRegisteredIds( |
| 189 SyncNotifierObserver* observer) { | 187 SyncNotifierObserver* handler, const ObjectIdSet& ids) { |
| 190 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 188 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 191 observers_.AddObserver(observer); | 189 const ObjectIdSet& registered_ids = helper_.UpdateRegisteredIds(handler, ids); |
|
akalin
2012/07/21 01:14:10
actually, rename this to "all_registered_ids"
dcheng
2012/07/21 14:06:53
Done.
| |
| 192 } | 190 if (!network_task_runner_->PostTask( |
| 193 | 191 FROM_HERE, |
| 194 void NonBlockingInvalidationNotifier::RemoveObserver( | 192 base::Bind( |
| 195 SyncNotifierObserver* observer) { | 193 &NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds, |
| 196 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 194 core_.get(), |
| 197 observers_.RemoveObserver(observer); | 195 registered_ids))) { |
| 196 NOTREACHED(); | |
| 197 } | |
| 198 } | 198 } |
| 199 | 199 |
| 200 void NonBlockingInvalidationNotifier::SetUniqueId( | 200 void NonBlockingInvalidationNotifier::SetUniqueId( |
| 201 const std::string& unique_id) { | 201 const std::string& unique_id) { |
| 202 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 202 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 203 if (!network_task_runner_->PostTask( | 203 if (!network_task_runner_->PostTask( |
| 204 FROM_HERE, | 204 FROM_HERE, |
| 205 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, | 205 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, |
| 206 core_.get(), unique_id))) { | 206 core_.get(), unique_id))) { |
| 207 NOTREACHED(); | 207 NOTREACHED(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 224 const std::string& email, const std::string& token) { | 224 const std::string& email, const std::string& token) { |
| 225 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 225 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 226 if (!network_task_runner_->PostTask( | 226 if (!network_task_runner_->PostTask( |
| 227 FROM_HERE, | 227 FROM_HERE, |
| 228 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, | 228 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, |
| 229 core_.get(), email, token))) { | 229 core_.get(), email, token))) { |
| 230 NOTREACHED(); | 230 NOTREACHED(); |
| 231 } | 231 } |
| 232 } | 232 } |
| 233 | 233 |
| 234 void NonBlockingInvalidationNotifier::UpdateEnabledTypes( | |
| 235 ModelTypeSet enabled_types) { | |
| 236 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
| 237 if (!network_task_runner_->PostTask( | |
| 238 FROM_HERE, | |
| 239 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes, | |
| 240 core_.get(), enabled_types))) { | |
| 241 NOTREACHED(); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 void NonBlockingInvalidationNotifier::SendNotification( | 234 void NonBlockingInvalidationNotifier::SendNotification( |
| 246 ModelTypeSet changed_types) { | 235 ModelTypeSet changed_types) { |
| 247 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 236 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 248 // InvalidationClient doesn't implement SendNotification(), so no | 237 // InvalidationClient doesn't implement SendNotification(), so no |
| 249 // need to forward on the call. | 238 // need to forward on the call. |
| 250 } | 239 } |
| 251 | 240 |
| 252 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { | 241 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { |
| 253 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 242 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 254 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 243 helper_.EmitOnNotificationsEnabled(); |
| 255 OnNotificationsEnabled()); | |
| 256 } | 244 } |
| 257 | 245 |
| 258 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( | 246 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( |
| 259 NotificationsDisabledReason reason) { | 247 NotificationsDisabledReason reason) { |
| 260 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 248 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 261 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 249 helper_.EmitOnNotificationsDisabled(reason); |
| 262 OnNotificationsDisabled(reason)); | |
| 263 } | 250 } |
| 264 | 251 |
| 265 void NonBlockingInvalidationNotifier::OnIncomingNotification( | 252 void NonBlockingInvalidationNotifier::OnIncomingNotification( |
| 266 const ModelTypePayloadMap& type_payloads, | 253 const ObjectIdPayloadMap& id_payloads, |
| 267 IncomingNotificationSource source) { | 254 IncomingNotificationSource source) { |
| 268 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 255 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
| 269 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 256 helper_.DispatchInvalidationsToHandlers(id_payloads, source); |
| 270 OnIncomingNotification(type_payloads, source)); | |
| 271 } | 257 } |
| 272 | 258 |
| 273 } // namespace syncer | 259 } // namespace syncer |
| OLD | NEW |