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_invalidator.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 NonBlockingInvalidator::Core |
21 : public base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>, | 21 : public base::RefCountedThreadSafe<NonBlockingInvalidator::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<NonBlockingInvalidator::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 NonBlockingInvalidator::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 NonBlockingInvalidator::Core::~Core() { |
73 } | 73 } |
74 | 74 |
75 void NonBlockingInvalidationNotifier::Core::Initialize( | 75 void NonBlockingInvalidator::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, |
78 const std::string& initial_invalidation_state, | 78 const std::string& initial_invalidation_state, |
79 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, | 79 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, |
80 const std::string& client_info) { | 80 const std::string& client_info) { |
81 DCHECK(notifier_options.request_context_getter); | 81 DCHECK(notifier_options.request_context_getter); |
82 DCHECK_EQ(notifier::NOTIFICATION_SERVER, | 82 DCHECK_EQ(notifier::NOTIFICATION_SERVER, |
83 notifier_options.notification_method); | 83 notifier_options.notification_method); |
84 network_task_runner_ = notifier_options.request_context_getter-> | 84 network_task_runner_ = notifier_options.request_context_getter-> |
85 GetNetworkTaskRunner(); | 85 GetNetworkTaskRunner(); |
86 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 86 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
87 invalidation_notifier_.reset( | 87 invalidation_notifier_.reset( |
88 new InvalidationNotifier( | 88 new InvalidationNotifier( |
89 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), | 89 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), |
90 initial_max_invalidation_versions, | 90 initial_max_invalidation_versions, |
91 initial_invalidation_state, | 91 initial_invalidation_state, |
92 invalidation_state_tracker, | 92 invalidation_state_tracker, |
93 client_info)); | 93 client_info)); |
94 invalidation_notifier_->RegisterHandler(this); | 94 invalidation_notifier_->RegisterHandler(this); |
95 } | 95 } |
96 | 96 |
97 void NonBlockingInvalidationNotifier::Core::Teardown() { | 97 void NonBlockingInvalidator::Core::Teardown() { |
98 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 98 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
99 invalidation_notifier_->UnregisterHandler(this); | 99 invalidation_notifier_->UnregisterHandler(this); |
100 invalidation_notifier_.reset(); | 100 invalidation_notifier_.reset(); |
101 network_task_runner_ = NULL; | 101 network_task_runner_ = NULL; |
102 } | 102 } |
103 | 103 |
104 void NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds( | 104 void NonBlockingInvalidator::Core::UpdateRegisteredIds(const ObjectIdSet& ids) { |
105 const ObjectIdSet& ids) { | |
106 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 105 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
107 invalidation_notifier_->UpdateRegisteredIds(this, ids); | 106 invalidation_notifier_->UpdateRegisteredIds(this, ids); |
108 } | 107 } |
109 | 108 |
110 void NonBlockingInvalidationNotifier::Core::SetUniqueId( | 109 void NonBlockingInvalidator::Core::SetUniqueId(const std::string& unique_id) { |
111 const std::string& unique_id) { | |
112 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 110 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
113 invalidation_notifier_->SetUniqueId(unique_id); | 111 invalidation_notifier_->SetUniqueId(unique_id); |
114 } | 112 } |
115 | 113 |
116 void NonBlockingInvalidationNotifier::Core::SetStateDeprecated( | 114 void NonBlockingInvalidator::Core::SetStateDeprecated( |
117 const std::string& state) { | 115 const std::string& state) { |
118 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 116 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
119 invalidation_notifier_->SetStateDeprecated(state); | 117 invalidation_notifier_->SetStateDeprecated(state); |
120 } | 118 } |
121 | 119 |
122 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( | 120 void NonBlockingInvalidator::Core::UpdateCredentials(const std::string& email, |
123 const std::string& email, const std::string& token) { | 121 const std::string& token) { |
124 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 122 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
125 invalidation_notifier_->UpdateCredentials(email, token); | 123 invalidation_notifier_->UpdateCredentials(email, token); |
126 } | 124 } |
127 | 125 |
128 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { | 126 void NonBlockingInvalidator::Core::OnNotificationsEnabled() { |
129 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 127 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
130 delegate_observer_.Call(FROM_HERE, | 128 delegate_observer_.Call(FROM_HERE, |
131 &SyncNotifierObserver::OnNotificationsEnabled); | 129 &InvalidationHandler::OnNotificationsEnabled); |
132 } | 130 } |
133 | 131 |
134 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( | 132 void NonBlockingInvalidator::Core::OnNotificationsDisabled( |
135 NotificationsDisabledReason reason) { | 133 NotificationsDisabledReason reason) { |
136 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 134 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
137 delegate_observer_.Call( | 135 delegate_observer_.Call( |
138 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); | 136 FROM_HERE, &InvalidationHandler::OnNotificationsDisabled, reason); |
139 } | 137 } |
140 | 138 |
141 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( | 139 void NonBlockingInvalidator::Core::OnIncomingNotification( |
142 const ObjectIdStateMap& id_state_map, IncomingNotificationSource source) { | 140 const ObjectIdStateMap& id_state_map, 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 &InvalidationHandler::OnIncomingNotification, |
146 id_state_map, | 144 id_state_map, |
147 source); | 145 source); |
148 } | 146 } |
149 | 147 |
150 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( | 148 NonBlockingInvalidator::NonBlockingInvalidator( |
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) |
157 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 155 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
158 core_( | 156 core_( |
159 new Core(MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()))), | 157 new Core(MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()))), |
160 parent_task_runner_( | 158 parent_task_runner_( |
161 base::ThreadTaskRunnerHandle::Get()), | 159 base::ThreadTaskRunnerHandle::Get()), |
162 network_task_runner_(notifier_options.request_context_getter-> | 160 network_task_runner_(notifier_options.request_context_getter-> |
163 GetNetworkTaskRunner()) { | 161 GetNetworkTaskRunner()) { |
164 if (!network_task_runner_->PostTask( | 162 if (!network_task_runner_->PostTask( |
165 FROM_HERE, | 163 FROM_HERE, |
166 base::Bind( | 164 base::Bind( |
167 &NonBlockingInvalidationNotifier::Core::Initialize, | 165 &NonBlockingInvalidator::Core::Initialize, |
168 core_.get(), | 166 core_.get(), |
169 notifier_options, | 167 notifier_options, |
170 initial_max_invalidation_versions, | 168 initial_max_invalidation_versions, |
171 initial_invalidation_state, | 169 initial_invalidation_state, |
172 invalidation_state_tracker, | 170 invalidation_state_tracker, |
173 client_info))) { | 171 client_info))) { |
174 NOTREACHED(); | 172 NOTREACHED(); |
175 } | 173 } |
176 } | 174 } |
177 | 175 |
178 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { | 176 NonBlockingInvalidator::~NonBlockingInvalidator() { |
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(&NonBlockingInvalidator::Core::Teardown, |
183 core_.get()))) { | 181 core_.get()))) { |
184 NOTREACHED(); | 182 NOTREACHED(); |
185 } | 183 } |
186 } | 184 } |
187 | 185 |
188 void NonBlockingInvalidationNotifier::RegisterHandler( | 186 void NonBlockingInvalidator::RegisterHandler(InvalidationHandler* handler) { |
189 SyncNotifierObserver* handler) { | |
190 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 187 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
191 registrar_.RegisterHandler(handler); | 188 registrar_.RegisterHandler(handler); |
192 } | 189 } |
193 | 190 |
194 void NonBlockingInvalidationNotifier::UpdateRegisteredIds( | 191 void NonBlockingInvalidator::UpdateRegisteredIds(InvalidationHandler* handler, |
195 SyncNotifierObserver* handler, | 192 const ObjectIdSet& ids) { |
196 const ObjectIdSet& ids) { | |
197 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 193 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
198 registrar_.UpdateRegisteredIds(handler, ids); | 194 registrar_.UpdateRegisteredIds(handler, ids); |
199 if (!network_task_runner_->PostTask( | 195 if (!network_task_runner_->PostTask( |
200 FROM_HERE, | 196 FROM_HERE, |
201 base::Bind( | 197 base::Bind( |
202 &NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds, | 198 &NonBlockingInvalidator::Core::UpdateRegisteredIds, |
203 core_.get(), | 199 core_.get(), |
204 registrar_.GetAllRegisteredIds()))) { | 200 registrar_.GetAllRegisteredIds()))) { |
205 NOTREACHED(); | 201 NOTREACHED(); |
206 } | 202 } |
207 } | 203 } |
208 | 204 |
209 void NonBlockingInvalidationNotifier::UnregisterHandler( | 205 void NonBlockingInvalidator::UnregisterHandler(InvalidationHandler* handler) { |
210 SyncNotifierObserver* handler) { | |
211 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 206 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
212 registrar_.UnregisterHandler(handler); | 207 registrar_.UnregisterHandler(handler); |
213 } | 208 } |
214 | 209 |
215 void NonBlockingInvalidationNotifier::SetUniqueId( | 210 void NonBlockingInvalidator::SetUniqueId(const std::string& unique_id) { |
216 const std::string& unique_id) { | |
217 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 211 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
218 if (!network_task_runner_->PostTask( | 212 if (!network_task_runner_->PostTask( |
219 FROM_HERE, | 213 FROM_HERE, |
220 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, | 214 base::Bind(&NonBlockingInvalidator::Core::SetUniqueId, |
221 core_.get(), unique_id))) { | 215 core_.get(), unique_id))) { |
222 NOTREACHED(); | 216 NOTREACHED(); |
223 } | 217 } |
224 } | 218 } |
225 | 219 |
226 void NonBlockingInvalidationNotifier::SetStateDeprecated( | 220 void NonBlockingInvalidator::SetStateDeprecated(const std::string& state) { |
227 const std::string& state) { | |
228 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 221 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
229 if (!network_task_runner_->PostTask( | 222 if (!network_task_runner_->PostTask( |
230 FROM_HERE, | 223 FROM_HERE, |
231 base::Bind( | 224 base::Bind( |
232 &NonBlockingInvalidationNotifier::Core::SetStateDeprecated, | 225 &NonBlockingInvalidator::Core::SetStateDeprecated, |
233 core_.get(), state))) { | 226 core_.get(), state))) { |
234 NOTREACHED(); | 227 NOTREACHED(); |
235 } | 228 } |
236 } | 229 } |
237 | 230 |
238 void NonBlockingInvalidationNotifier::UpdateCredentials( | 231 void NonBlockingInvalidator::UpdateCredentials(const std::string& email, |
239 const std::string& email, const std::string& token) { | 232 const std::string& token) { |
240 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 233 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
241 if (!network_task_runner_->PostTask( | 234 if (!network_task_runner_->PostTask( |
242 FROM_HERE, | 235 FROM_HERE, |
243 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, | 236 base::Bind(&NonBlockingInvalidator::Core::UpdateCredentials, |
244 core_.get(), email, token))) { | 237 core_.get(), email, token))) { |
245 NOTREACHED(); | 238 NOTREACHED(); |
246 } | 239 } |
247 } | 240 } |
248 | 241 |
249 void NonBlockingInvalidationNotifier::SendNotification( | 242 void NonBlockingInvalidator::SendNotification(ModelTypeSet changed_types) { |
250 ModelTypeSet changed_types) { | |
251 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 243 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
252 // InvalidationClient doesn't implement SendNotification(), so no | 244 // InvalidationClient doesn't implement SendNotification(), so no |
253 // need to forward on the call. | 245 // need to forward on the call. |
254 } | 246 } |
255 | 247 |
256 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { | 248 void NonBlockingInvalidator::OnNotificationsEnabled() { |
257 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 249 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
258 registrar_.EmitOnNotificationsEnabled(); | 250 registrar_.EmitOnNotificationsEnabled(); |
259 } | 251 } |
260 | 252 |
261 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( | 253 void NonBlockingInvalidator::OnNotificationsDisabled( |
262 NotificationsDisabledReason reason) { | 254 NotificationsDisabledReason reason) { |
263 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 255 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
264 registrar_.EmitOnNotificationsDisabled(reason); | 256 registrar_.EmitOnNotificationsDisabled(reason); |
265 } | 257 } |
266 | 258 |
267 void NonBlockingInvalidationNotifier::OnIncomingNotification( | 259 void NonBlockingInvalidator::OnIncomingNotification( |
268 const ObjectIdStateMap& id_state_map, | 260 const ObjectIdStateMap& id_state_map, |
269 IncomingNotificationSource source) { | 261 IncomingNotificationSource source) { |
270 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | 262 DCHECK(parent_task_runner_->BelongsToCurrentThread()); |
271 registrar_.DispatchInvalidationsToHandlers(id_state_map, source); | 263 registrar_.DispatchInvalidationsToHandlers(id_state_map, source); |
272 } | 264 } |
273 | 265 |
274 } // namespace syncer | 266 } // namespace syncer |
OLD | NEW |