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); | |
37 void SetUniqueId(const std::string& unique_id); | 36 void SetUniqueId(const std::string& unique_id); |
38 void SetStateDeprecated(const std::string& state); | 37 void SetStateDeprecated(const std::string& state); |
39 void UpdateCredentials(const std::string& email, const std::string& token); | 38 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 ObjectIdPayloadMap& id_payloads, | 47 const ModelTypePayloadMap& type_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); |
92 } | 93 } |
93 | 94 |
94 | 95 |
95 void NonBlockingInvalidationNotifier::Core::Teardown() { | 96 void NonBlockingInvalidationNotifier::Core::Teardown() { |
96 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 97 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
97 invalidation_notifier_->UpdateRegisteredIds(this, ObjectIdSet()); | 98 invalidation_notifier_->RemoveObserver(this); |
98 invalidation_notifier_.reset(); | 99 invalidation_notifier_.reset(); |
99 network_task_runner_ = NULL; | 100 network_task_runner_ = NULL; |
100 } | 101 } |
101 | 102 |
102 void NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds( | |
103 const ObjectIdSet& ids) { | |
104 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
105 invalidation_notifier_->UpdateRegisteredIds(this, ids); | |
106 } | |
107 | |
108 void NonBlockingInvalidationNotifier::Core::SetUniqueId( | 103 void NonBlockingInvalidationNotifier::Core::SetUniqueId( |
109 const std::string& unique_id) { | 104 const std::string& unique_id) { |
110 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 105 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
111 invalidation_notifier_->SetUniqueId(unique_id); | 106 invalidation_notifier_->SetUniqueId(unique_id); |
112 } | 107 } |
113 | 108 |
114 void NonBlockingInvalidationNotifier::Core::SetStateDeprecated( | 109 void NonBlockingInvalidationNotifier::Core::SetStateDeprecated( |
115 const std::string& state) { | 110 const std::string& state) { |
116 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 111 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
117 invalidation_notifier_->SetStateDeprecated(state); | 112 invalidation_notifier_->SetStateDeprecated(state); |
118 } | 113 } |
119 | 114 |
120 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( | 115 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( |
121 const std::string& email, const std::string& token) { | 116 const std::string& email, const std::string& token) { |
122 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 117 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
123 invalidation_notifier_->UpdateCredentials(email, token); | 118 invalidation_notifier_->UpdateCredentials(email, token); |
124 } | 119 } |
125 | 120 |
| 121 void NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes( |
| 122 ModelTypeSet enabled_types) { |
| 123 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 124 invalidation_notifier_->UpdateEnabledTypes(enabled_types); |
| 125 } |
| 126 |
126 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { | 127 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { |
127 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 128 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
128 delegate_observer_.Call(FROM_HERE, | 129 delegate_observer_.Call(FROM_HERE, |
129 &SyncNotifierObserver::OnNotificationsEnabled); | 130 &SyncNotifierObserver::OnNotificationsEnabled); |
130 } | 131 } |
131 | 132 |
132 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( | 133 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( |
133 NotificationsDisabledReason reason) { | 134 NotificationsDisabledReason reason) { |
134 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 135 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
135 delegate_observer_.Call( | 136 delegate_observer_.Call( |
136 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); | 137 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); |
137 } | 138 } |
138 | 139 |
139 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( | 140 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( |
140 const ObjectIdPayloadMap& id_payloads, IncomingNotificationSource source) { | 141 const ModelTypePayloadMap& type_payloads, |
| 142 IncomingNotificationSource source) { |
141 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 143 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
142 delegate_observer_.Call(FROM_HERE, | 144 delegate_observer_.Call(FROM_HERE, |
143 &SyncNotifierObserver::OnIncomingNotification, | 145 &SyncNotifierObserver::OnIncomingNotification, |
144 id_payloads, | 146 type_payloads, |
145 source); | 147 source); |
146 } | 148 } |
147 | 149 |
148 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( | 150 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( |
149 const notifier::NotifierOptions& notifier_options, | 151 const notifier::NotifierOptions& notifier_options, |
150 const InvalidationVersionMap& initial_max_invalidation_versions, | 152 const InvalidationVersionMap& initial_max_invalidation_versions, |
151 const std::string& initial_invalidation_state, | 153 const std::string& initial_invalidation_state, |
152 const WeakHandle<InvalidationStateTracker>& | 154 const WeakHandle<InvalidationStateTracker>& |
153 invalidation_state_tracker, | 155 invalidation_state_tracker, |
154 const std::string& client_info) | 156 const std::string& client_info) |
(...skipping 21 matching lines...) Expand all Loading... |
176 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { | 178 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { |
177 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 179 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
178 if (!network_task_runner_->PostTask( | 180 if (!network_task_runner_->PostTask( |
179 FROM_HERE, | 181 FROM_HERE, |
180 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, | 182 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, |
181 core_.get()))) { | 183 core_.get()))) { |
182 NOTREACHED(); | 184 NOTREACHED(); |
183 } | 185 } |
184 } | 186 } |
185 | 187 |
186 void NonBlockingInvalidationNotifier::UpdateRegisteredIds( | 188 void NonBlockingInvalidationNotifier::AddObserver( |
187 SyncNotifierObserver* handler, const ObjectIdSet& ids) { | 189 SyncNotifierObserver* observer) { |
188 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 190 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
189 const ObjectIdSet& all_registered_ids = | 191 observers_.AddObserver(observer); |
190 helper_.UpdateRegisteredIds(handler, ids); | 192 } |
191 if (!network_task_runner_->PostTask( | 193 |
192 FROM_HERE, | 194 void NonBlockingInvalidationNotifier::RemoveObserver( |
193 base::Bind( | 195 SyncNotifierObserver* observer) { |
194 &NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds, | 196 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
195 core_.get(), | 197 observers_.RemoveObserver(observer); |
196 all_registered_ids))) { | |
197 NOTREACHED(); | |
198 } | |
199 } | 198 } |
200 | 199 |
201 void NonBlockingInvalidationNotifier::SetUniqueId( | 200 void NonBlockingInvalidationNotifier::SetUniqueId( |
202 const std::string& unique_id) { | 201 const std::string& unique_id) { |
203 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 202 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
204 if (!network_task_runner_->PostTask( | 203 if (!network_task_runner_->PostTask( |
205 FROM_HERE, | 204 FROM_HERE, |
206 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, | 205 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, |
207 core_.get(), unique_id))) { | 206 core_.get(), unique_id))) { |
208 NOTREACHED(); | 207 NOTREACHED(); |
(...skipping 16 matching lines...) Expand all Loading... |
225 const std::string& email, const std::string& token) { | 224 const std::string& email, const std::string& token) { |
226 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 225 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
227 if (!network_task_runner_->PostTask( | 226 if (!network_task_runner_->PostTask( |
228 FROM_HERE, | 227 FROM_HERE, |
229 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, | 228 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, |
230 core_.get(), email, token))) { | 229 core_.get(), email, token))) { |
231 NOTREACHED(); | 230 NOTREACHED(); |
232 } | 231 } |
233 } | 232 } |
234 | 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 |
235 void NonBlockingInvalidationNotifier::SendNotification( | 245 void NonBlockingInvalidationNotifier::SendNotification( |
236 ModelTypeSet changed_types) { | 246 ModelTypeSet changed_types) { |
237 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 247 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
238 // InvalidationClient doesn't implement SendNotification(), so no | 248 // InvalidationClient doesn't implement SendNotification(), so no |
239 // need to forward on the call. | 249 // need to forward on the call. |
240 } | 250 } |
241 | 251 |
242 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { | 252 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { |
243 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 253 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
244 helper_.EmitOnNotificationsEnabled(); | 254 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, |
| 255 OnNotificationsEnabled()); |
245 } | 256 } |
246 | 257 |
247 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( | 258 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( |
248 NotificationsDisabledReason reason) { | 259 NotificationsDisabledReason reason) { |
249 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 260 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
250 helper_.EmitOnNotificationsDisabled(reason); | 261 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, |
| 262 OnNotificationsDisabled(reason)); |
251 } | 263 } |
252 | 264 |
253 void NonBlockingInvalidationNotifier::OnIncomingNotification( | 265 void NonBlockingInvalidationNotifier::OnIncomingNotification( |
254 const ObjectIdPayloadMap& id_payloads, | 266 const ModelTypePayloadMap& type_payloads, |
255 IncomingNotificationSource source) { | 267 IncomingNotificationSource source) { |
256 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 268 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
257 helper_.DispatchInvalidationsToHandlers(id_payloads, source); | 269 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, |
| 270 OnIncomingNotification(type_payloads, source)); |
258 } | 271 } |
259 | 272 |
260 } // namespace syncer | 273 } // namespace syncer |
OLD | NEW |