OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "sync/notifier/non_blocking_invalidation_notifier.h" | |
6 | |
7 #include <cstddef> | |
8 | |
9 #include "base/location.h" | |
10 #include "base/logging.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/single_thread_task_runner.h" | |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "base/threading/thread.h" | |
15 #include "jingle/notifier/listener/push_client.h" | |
16 #include "sync/notifier/invalidation_notifier.h" | |
17 | |
18 namespace syncer { | |
19 | |
20 class NonBlockingInvalidationNotifier::Core | |
21 : public base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>, | |
22 // SyncNotifierObserver to observe the InvalidationNotifier we create. | |
23 public SyncNotifierObserver { | |
24 public: | |
25 // Called on parent thread. |delegate_observer| should be | |
26 // initialized. | |
27 explicit Core( | |
28 const WeakHandle<SyncNotifierObserver>& delegate_observer); | |
29 | |
30 // Helpers called on I/O thread. | |
31 void Initialize( | |
32 const notifier::NotifierOptions& notifier_options, | |
33 const InvalidationVersionMap& initial_max_invalidation_versions, | |
34 const std::string& initial_invalidation_state, | |
35 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, | |
36 const std::string& client_info); | |
37 void Teardown(); | |
38 void UpdateRegisteredIds(const ObjectIdSet& ids); | |
39 void SetUniqueId(const std::string& unique_id); | |
40 void SetStateDeprecated(const std::string& state); | |
41 void UpdateCredentials(const std::string& email, const std::string& token); | |
42 | |
43 // SyncNotifierObserver implementation (all called on I/O thread by | |
44 // InvalidationNotifier). | |
45 virtual void OnNotificationsEnabled() OVERRIDE; | |
46 virtual void OnNotificationsDisabled( | |
47 NotificationsDisabledReason reason) OVERRIDE; | |
48 virtual void OnIncomingNotification( | |
49 const ObjectIdStateMap& id_state_map, | |
50 IncomingNotificationSource source) OVERRIDE; | |
51 | |
52 private: | |
53 friend class | |
54 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>; | |
55 // Called on parent or I/O thread. | |
56 ~Core(); | |
57 | |
58 // The variables below should be used only on the I/O thread. | |
59 const WeakHandle<SyncNotifierObserver> delegate_observer_; | |
60 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | |
61 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(Core); | |
64 }; | |
65 | |
66 NonBlockingInvalidationNotifier::Core::Core( | |
67 const WeakHandle<SyncNotifierObserver>& delegate_observer) | |
68 : delegate_observer_(delegate_observer) { | |
69 DCHECK(delegate_observer_.IsInitialized()); | |
70 } | |
71 | |
72 NonBlockingInvalidationNotifier::Core::~Core() { | |
73 } | |
74 | |
75 void NonBlockingInvalidationNotifier::Core::Initialize( | |
76 const notifier::NotifierOptions& notifier_options, | |
77 const InvalidationVersionMap& initial_max_invalidation_versions, | |
78 const std::string& initial_invalidation_state, | |
79 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, | |
80 const std::string& client_info) { | |
81 DCHECK(notifier_options.request_context_getter); | |
82 DCHECK_EQ(notifier::NOTIFICATION_SERVER, | |
83 notifier_options.notification_method); | |
84 network_task_runner_ = notifier_options.request_context_getter-> | |
85 GetNetworkTaskRunner(); | |
86 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
87 invalidation_notifier_.reset( | |
88 new InvalidationNotifier( | |
89 notifier::PushClient::CreateDefaultOnIOThread(notifier_options), | |
90 initial_max_invalidation_versions, | |
91 initial_invalidation_state, | |
92 invalidation_state_tracker, | |
93 client_info)); | |
94 invalidation_notifier_->RegisterHandler(this); | |
95 } | |
96 | |
97 void NonBlockingInvalidationNotifier::Core::Teardown() { | |
98 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
99 invalidation_notifier_->UnregisterHandler(this); | |
100 invalidation_notifier_.reset(); | |
101 network_task_runner_ = NULL; | |
102 } | |
103 | |
104 void NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds( | |
105 const ObjectIdSet& ids) { | |
106 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
107 invalidation_notifier_->UpdateRegisteredIds(this, ids); | |
108 } | |
109 | |
110 void NonBlockingInvalidationNotifier::Core::SetUniqueId( | |
111 const std::string& unique_id) { | |
112 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
113 invalidation_notifier_->SetUniqueId(unique_id); | |
114 } | |
115 | |
116 void NonBlockingInvalidationNotifier::Core::SetStateDeprecated( | |
117 const std::string& state) { | |
118 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
119 invalidation_notifier_->SetStateDeprecated(state); | |
120 } | |
121 | |
122 void NonBlockingInvalidationNotifier::Core::UpdateCredentials( | |
123 const std::string& email, const std::string& token) { | |
124 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
125 invalidation_notifier_->UpdateCredentials(email, token); | |
126 } | |
127 | |
128 void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() { | |
129 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
130 delegate_observer_.Call(FROM_HERE, | |
131 &SyncNotifierObserver::OnNotificationsEnabled); | |
132 } | |
133 | |
134 void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled( | |
135 NotificationsDisabledReason reason) { | |
136 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
137 delegate_observer_.Call( | |
138 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason); | |
139 } | |
140 | |
141 void NonBlockingInvalidationNotifier::Core::OnIncomingNotification( | |
142 const ObjectIdStateMap& id_state_map, IncomingNotificationSource source) { | |
143 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
144 delegate_observer_.Call(FROM_HERE, | |
145 &SyncNotifierObserver::OnIncomingNotification, | |
146 id_state_map, | |
147 source); | |
148 } | |
149 | |
150 NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier( | |
151 const notifier::NotifierOptions& notifier_options, | |
152 const InvalidationVersionMap& initial_max_invalidation_versions, | |
153 const std::string& initial_invalidation_state, | |
154 const WeakHandle<InvalidationStateTracker>& | |
155 invalidation_state_tracker, | |
156 const std::string& client_info) | |
157 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
158 core_( | |
159 new Core(MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()))), | |
160 parent_task_runner_( | |
161 base::ThreadTaskRunnerHandle::Get()), | |
162 network_task_runner_(notifier_options.request_context_getter-> | |
163 GetNetworkTaskRunner()) { | |
164 if (!network_task_runner_->PostTask( | |
165 FROM_HERE, | |
166 base::Bind( | |
167 &NonBlockingInvalidationNotifier::Core::Initialize, | |
168 core_.get(), | |
169 notifier_options, | |
170 initial_max_invalidation_versions, | |
171 initial_invalidation_state, | |
172 invalidation_state_tracker, | |
173 client_info))) { | |
174 NOTREACHED(); | |
175 } | |
176 } | |
177 | |
178 NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() { | |
179 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
180 if (!network_task_runner_->PostTask( | |
181 FROM_HERE, | |
182 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown, | |
183 core_.get()))) { | |
184 NOTREACHED(); | |
185 } | |
186 } | |
187 | |
188 void NonBlockingInvalidationNotifier::RegisterHandler( | |
189 SyncNotifierObserver* handler) { | |
190 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
191 registrar_.RegisterHandler(handler); | |
192 } | |
193 | |
194 void NonBlockingInvalidationNotifier::UpdateRegisteredIds( | |
195 SyncNotifierObserver* handler, | |
196 const ObjectIdSet& ids) { | |
197 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
198 registrar_.UpdateRegisteredIds(handler, ids); | |
199 if (!network_task_runner_->PostTask( | |
200 FROM_HERE, | |
201 base::Bind( | |
202 &NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds, | |
203 core_.get(), | |
204 registrar_.GetAllRegisteredIds()))) { | |
205 NOTREACHED(); | |
206 } | |
207 } | |
208 | |
209 void NonBlockingInvalidationNotifier::UnregisterHandler( | |
210 SyncNotifierObserver* handler) { | |
211 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
212 registrar_.UnregisterHandler(handler); | |
213 } | |
214 | |
215 void NonBlockingInvalidationNotifier::SetUniqueId( | |
216 const std::string& unique_id) { | |
217 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
218 if (!network_task_runner_->PostTask( | |
219 FROM_HERE, | |
220 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId, | |
221 core_.get(), unique_id))) { | |
222 NOTREACHED(); | |
223 } | |
224 } | |
225 | |
226 void NonBlockingInvalidationNotifier::SetStateDeprecated( | |
227 const std::string& state) { | |
228 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
229 if (!network_task_runner_->PostTask( | |
230 FROM_HERE, | |
231 base::Bind( | |
232 &NonBlockingInvalidationNotifier::Core::SetStateDeprecated, | |
233 core_.get(), state))) { | |
234 NOTREACHED(); | |
235 } | |
236 } | |
237 | |
238 void NonBlockingInvalidationNotifier::UpdateCredentials( | |
239 const std::string& email, const std::string& token) { | |
240 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
241 if (!network_task_runner_->PostTask( | |
242 FROM_HERE, | |
243 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials, | |
244 core_.get(), email, token))) { | |
245 NOTREACHED(); | |
246 } | |
247 } | |
248 | |
249 void NonBlockingInvalidationNotifier::SendNotification( | |
250 ModelTypeSet changed_types) { | |
251 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
252 // InvalidationClient doesn't implement SendNotification(), so no | |
253 // need to forward on the call. | |
254 } | |
255 | |
256 void NonBlockingInvalidationNotifier::OnNotificationsEnabled() { | |
257 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
258 registrar_.EmitOnNotificationsEnabled(); | |
259 } | |
260 | |
261 void NonBlockingInvalidationNotifier::OnNotificationsDisabled( | |
262 NotificationsDisabledReason reason) { | |
263 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
264 registrar_.EmitOnNotificationsDisabled(reason); | |
265 } | |
266 | |
267 void NonBlockingInvalidationNotifier::OnIncomingNotification( | |
268 const ObjectIdStateMap& id_state_map, | |
269 IncomingNotificationSource source) { | |
270 DCHECK(parent_task_runner_->BelongsToCurrentThread()); | |
271 registrar_.DispatchInvalidationsToHandlers(id_state_map, source); | |
272 } | |
273 | |
274 } // namespace syncer | |
OLD | NEW |