blob: 3c4fb13e4488e8885b291797e5d415175a867670 [file] [log] [blame]
[email protected]6990e4e02012-01-26 00:44:531// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0a80fed2011-03-24 22:31:482// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b7fe4b52012-04-19 14:21:095#include "sync/notifier/non_blocking_invalidation_notifier.h"
[email protected]0a80fed2011-03-24 22:31:486
7#include "base/logging.h"
[email protected]942e2022011-04-07 22:27:438#include "base/memory/scoped_ptr.h"
[email protected]0a80fed2011-03-24 22:31:489#include "base/message_loop.h"
[email protected]942e2022011-04-07 22:27:4310#include "base/threading/thread.h"
[email protected]1f7eba72012-05-30 09:10:1711#include "jingle/notifier/listener/push_client.h"
[email protected]b7fe4b52012-04-19 14:21:0912#include "sync/notifier/invalidation_notifier.h"
[email protected]0a80fed2011-03-24 22:31:4813
14namespace sync_notifier {
15
[email protected]942e2022011-04-07 22:27:4316class NonBlockingInvalidationNotifier::Core
17 : public base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>,
[email protected]7800d792012-05-30 02:34:4818 // SyncNotifierObserver to observe the InvalidationNotifier we create.
[email protected]942e2022011-04-07 22:27:4319 public SyncNotifierObserver {
20 public:
[email protected]b9908a242011-11-19 09:31:3221 // Called on parent thread. |delegate_observer| should be
22 // initialized.
23 explicit Core(
24 const browser_sync::WeakHandle<SyncNotifierObserver>&
25 delegate_observer);
[email protected]942e2022011-04-07 22:27:4326
27 // Helpers called on I/O thread.
[email protected]e3b0ee52011-10-12 03:04:1328 void Initialize(
29 const notifier::NotifierOptions& notifier_options,
30 const InvalidationVersionMap& initial_max_invalidation_versions,
[email protected]46e43ee2012-05-18 19:24:4131 const browser_sync::WeakHandle<InvalidationStateTracker>&
32 invalidation_state_tracker,
[email protected]e3b0ee52011-10-12 03:04:1333 const std::string& client_info);
[email protected]942e2022011-04-07 22:27:4334 void Teardown();
[email protected]0a5612052011-06-29 03:29:1835 void SetUniqueId(const std::string& unique_id);
[email protected]942e2022011-04-07 22:27:4336 void SetState(const std::string& state);
37 void UpdateCredentials(const std::string& email, const std::string& token);
[email protected]400f521a2011-12-13 01:50:2338 void UpdateEnabledTypes(syncable::ModelTypeSet enabled_types);
[email protected]942e2022011-04-07 22:27:4339
[email protected]7800d792012-05-30 02:34:4840 // SyncNotifierObserver implementation (all called on I/O thread by
41 // InvalidationNotifier).
[email protected]942e2022011-04-07 22:27:4342 virtual void OnIncomingNotification(
[email protected]6990e4e02012-01-26 00:44:5343 const syncable::ModelTypePayloadMap& type_payloads,
44 IncomingNotificationSource source);
[email protected]942e2022011-04-07 22:27:4345 virtual void OnNotificationStateChange(bool notifications_enabled);
46 virtual void StoreState(const std::string& state);
47
48 private:
49 friend class
50 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>;
51 // Called on parent or I/O thread.
52 ~Core();
53
[email protected]b9908a242011-11-19 09:31:3254 // The variables below should be used only on the I/O thread.
55 const browser_sync::WeakHandle<SyncNotifierObserver> delegate_observer_;
[email protected]942e2022011-04-07 22:27:4356 scoped_ptr<InvalidationNotifier> invalidation_notifier_;
57 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
[email protected]b9908a242011-11-19 09:31:3258
[email protected]942e2022011-04-07 22:27:4359 DISALLOW_COPY_AND_ASSIGN(Core);
60};
61
[email protected]b9908a242011-11-19 09:31:3262NonBlockingInvalidationNotifier::Core::Core(
63 const browser_sync::WeakHandle<SyncNotifierObserver>&
64 delegate_observer)
65 : delegate_observer_(delegate_observer) {
66 DCHECK(delegate_observer_.IsInitialized());
[email protected]942e2022011-04-07 22:27:4367}
68
69NonBlockingInvalidationNotifier::Core::~Core() {
70}
71
72void NonBlockingInvalidationNotifier::Core::Initialize(
73 const notifier::NotifierOptions& notifier_options,
[email protected]e3b0ee52011-10-12 03:04:1374 const InvalidationVersionMap& initial_max_invalidation_versions,
[email protected]46e43ee2012-05-18 19:24:4175 const browser_sync::WeakHandle<InvalidationStateTracker>&
76 invalidation_state_tracker,
[email protected]942e2022011-04-07 22:27:4377 const std::string& client_info) {
78 DCHECK(notifier_options.request_context_getter);
79 DCHECK_EQ(notifier::NOTIFICATION_SERVER,
80 notifier_options.notification_method);
81 io_message_loop_proxy_ = notifier_options.request_context_getter->
82 GetIOMessageLoopProxy();
83 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
84 invalidation_notifier_.reset(
[email protected]e3b0ee52011-10-12 03:04:1385 new InvalidationNotifier(
[email protected]1f7eba72012-05-30 09:10:1786 notifier::PushClient::CreateDefaultOnIOThread(notifier_options),
[email protected]e3b0ee52011-10-12 03:04:1387 initial_max_invalidation_versions,
[email protected]46e43ee2012-05-18 19:24:4188 invalidation_state_tracker,
[email protected]e3b0ee52011-10-12 03:04:1389 client_info));
[email protected]942e2022011-04-07 22:27:4390 invalidation_notifier_->AddObserver(this);
91}
92
93
94void NonBlockingInvalidationNotifier::Core::Teardown() {
95 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
96 invalidation_notifier_->RemoveObserver(this);
97 invalidation_notifier_.reset();
98 io_message_loop_proxy_ = NULL;
99}
100
[email protected]0a5612052011-06-29 03:29:18101void NonBlockingInvalidationNotifier::Core::SetUniqueId(
102 const std::string& unique_id) {
103 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
104 invalidation_notifier_->SetUniqueId(unique_id);
105}
106
[email protected]942e2022011-04-07 22:27:43107void NonBlockingInvalidationNotifier::Core::SetState(
108 const std::string& state) {
109 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
110 invalidation_notifier_->SetState(state);
111}
112
113void NonBlockingInvalidationNotifier::Core::UpdateCredentials(
114 const std::string& email, const std::string& token) {
115 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
116 invalidation_notifier_->UpdateCredentials(email, token);
117}
118
119void NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes(
[email protected]400f521a2011-12-13 01:50:23120 syncable::ModelTypeSet enabled_types) {
[email protected]942e2022011-04-07 22:27:43121 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
[email protected]2f15fd0e2011-08-27 05:29:09122 invalidation_notifier_->UpdateEnabledTypes(enabled_types);
[email protected]942e2022011-04-07 22:27:43123}
124
125void NonBlockingInvalidationNotifier::Core::OnIncomingNotification(
[email protected]6990e4e02012-01-26 00:44:53126 const syncable::ModelTypePayloadMap& type_payloads,
127 IncomingNotificationSource source) {
[email protected]942e2022011-04-07 22:27:43128 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
[email protected]b9908a242011-11-19 09:31:32129 delegate_observer_.Call(FROM_HERE,
130 &SyncNotifierObserver::OnIncomingNotification,
[email protected]6990e4e02012-01-26 00:44:53131 type_payloads,
132 source);
[email protected]942e2022011-04-07 22:27:43133}
134
135void NonBlockingInvalidationNotifier::Core::OnNotificationStateChange(
136 bool notifications_enabled) {
137 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
[email protected]b9908a242011-11-19 09:31:32138 delegate_observer_.Call(FROM_HERE,
139 &SyncNotifierObserver::OnNotificationStateChange,
140 notifications_enabled);
[email protected]942e2022011-04-07 22:27:43141}
142
143void NonBlockingInvalidationNotifier::Core::StoreState(
144 const std::string& state) {
145 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
[email protected]b9908a242011-11-19 09:31:32146 delegate_observer_.Call(FROM_HERE,
147 &SyncNotifierObserver::StoreState, state);
[email protected]942e2022011-04-07 22:27:43148}
149
[email protected]0a80fed2011-03-24 22:31:48150NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier(
151 const notifier::NotifierOptions& notifier_options,
[email protected]e3b0ee52011-10-12 03:04:13152 const InvalidationVersionMap& initial_max_invalidation_versions,
[email protected]46e43ee2012-05-18 19:24:41153 const browser_sync::WeakHandle<InvalidationStateTracker>&
154 invalidation_state_tracker,
[email protected]0a80fed2011-03-24 22:31:48155 const std::string& client_info)
[email protected]b9908a242011-11-19 09:31:32156 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
157 core_(
158 new Core(browser_sync::MakeWeakHandle(
159 weak_ptr_factory_.GetWeakPtr()))),
[email protected]f9682402011-06-28 22:34:27160 parent_message_loop_proxy_(
[email protected]edd685f2011-08-15 20:33:46161 base::MessageLoopProxy::current()),
[email protected]942e2022011-04-07 22:27:43162 io_message_loop_proxy_(notifier_options.request_context_getter->
163 GetIOMessageLoopProxy()) {
[email protected]daf4e102011-04-26 08:30:28164 if (!io_message_loop_proxy_->PostTask(
165 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02166 base::Bind(
[email protected]daf4e102011-04-26 08:30:28167 &NonBlockingInvalidationNotifier::Core::Initialize,
[email protected]09e170f2011-10-28 23:22:02168 core_.get(),
[email protected]e3b0ee52011-10-12 03:04:13169 notifier_options,
170 initial_max_invalidation_versions,
[email protected]46e43ee2012-05-18 19:24:41171 invalidation_state_tracker,
[email protected]e3b0ee52011-10-12 03:04:13172 client_info))) {
[email protected]daf4e102011-04-26 08:30:28173 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18174 }
[email protected]0a80fed2011-03-24 22:31:48175}
176
177NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() {
[email protected]f9682402011-06-28 22:34:27178 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
[email protected]daf4e102011-04-26 08:30:28179 if (!io_message_loop_proxy_->PostTask(
180 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02181 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown,
182 core_.get()))) {
[email protected]daf4e102011-04-26 08:30:28183 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18184 }
[email protected]0a80fed2011-03-24 22:31:48185}
186
187void NonBlockingInvalidationNotifier::AddObserver(
188 SyncNotifierObserver* observer) {
[email protected]f9682402011-06-28 22:34:27189 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
[email protected]b9908a242011-11-19 09:31:32190 observers_.AddObserver(observer);
[email protected]0a80fed2011-03-24 22:31:48191}
192
193void NonBlockingInvalidationNotifier::RemoveObserver(
194 SyncNotifierObserver* observer) {
[email protected]f9682402011-06-28 22:34:27195 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
[email protected]b9908a242011-11-19 09:31:32196 observers_.RemoveObserver(observer);
[email protected]0a80fed2011-03-24 22:31:48197}
198
[email protected]0a5612052011-06-29 03:29:18199void NonBlockingInvalidationNotifier::SetUniqueId(
200 const std::string& unique_id) {
201 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
202 if (!io_message_loop_proxy_->PostTask(
203 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02204 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId,
205 core_.get(), unique_id))) {
[email protected]0a5612052011-06-29 03:29:18206 NOTREACHED();
207 }
208}
209
[email protected]0a80fed2011-03-24 22:31:48210void NonBlockingInvalidationNotifier::SetState(const std::string& state) {
[email protected]f9682402011-06-28 22:34:27211 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
[email protected]daf4e102011-04-26 08:30:28212 if (!io_message_loop_proxy_->PostTask(
213 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02214 base::Bind(&NonBlockingInvalidationNotifier::Core::SetState,
215 core_.get(), state))) {
[email protected]daf4e102011-04-26 08:30:28216 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18217 }
[email protected]0a80fed2011-03-24 22:31:48218}
219
220void NonBlockingInvalidationNotifier::UpdateCredentials(
221 const std::string& email, const std::string& token) {
[email protected]f9682402011-06-28 22:34:27222 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
[email protected]daf4e102011-04-26 08:30:28223 if (!io_message_loop_proxy_->PostTask(
224 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02225 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials,
226 core_.get(), email, token))) {
[email protected]daf4e102011-04-26 08:30:28227 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18228 }
[email protected]0a80fed2011-03-24 22:31:48229}
230
231void NonBlockingInvalidationNotifier::UpdateEnabledTypes(
[email protected]400f521a2011-12-13 01:50:23232 syncable::ModelTypeSet enabled_types) {
[email protected]f9682402011-06-28 22:34:27233 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
[email protected]daf4e102011-04-26 08:30:28234 if (!io_message_loop_proxy_->PostTask(
235 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02236 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes,
237 core_.get(), enabled_types))) {
[email protected]daf4e102011-04-26 08:30:28238 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18239 }
[email protected]0a80fed2011-03-24 22:31:48240}
241
[email protected]2f15fd0e2011-08-27 05:29:09242void NonBlockingInvalidationNotifier::SendNotification(
[email protected]400f521a2011-12-13 01:50:23243 syncable::ModelTypeSet changed_types) {
[email protected]f9682402011-06-28 22:34:27244 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
[email protected]0a80fed2011-03-24 22:31:48245 // InvalidationClient doesn't implement SendNotification(), so no
246 // need to forward on the call.
247}
248
[email protected]b9908a242011-11-19 09:31:32249void NonBlockingInvalidationNotifier::OnIncomingNotification(
[email protected]6990e4e02012-01-26 00:44:53250 const syncable::ModelTypePayloadMap& type_payloads,
251 IncomingNotificationSource source) {
[email protected]b9908a242011-11-19 09:31:32252 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
253 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_,
[email protected]6990e4e02012-01-26 00:44:53254 OnIncomingNotification(type_payloads, source));
[email protected]b9908a242011-11-19 09:31:32255}
256
257void NonBlockingInvalidationNotifier::OnNotificationStateChange(
258 bool notifications_enabled) {
259 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
260 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_,
261 OnNotificationStateChange(notifications_enabled));
262}
263
264void NonBlockingInvalidationNotifier::StoreState(
265 const std::string& state) {
266 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
267 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_,
268 StoreState(state));
269}
270
[email protected]0a80fed2011-03-24 22:31:48271} // namespace sync_notifier