blob: ac0f76a2cb37fcaec0419add599e046f6adab1e3 [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
[email protected]4969b0122012-06-16 01:58:287#include "base/location.h"
[email protected]0a80fed2011-03-24 22:31:488#include "base/logging.h"
[email protected]942e2022011-04-07 22:27:439#include "base/memory/scoped_ptr.h"
[email protected]4969b0122012-06-16 01:58:2810#include "base/single_thread_task_runner.h"
11#include "base/thread_task_runner_handle.h"
[email protected]942e2022011-04-07 22:27:4312#include "base/threading/thread.h"
[email protected]1f7eba72012-05-30 09:10:1713#include "jingle/notifier/listener/push_client.h"
[email protected]b7fe4b52012-04-19 14:21:0914#include "sync/notifier/invalidation_notifier.h"
[email protected]0a80fed2011-03-24 22:31:4815
[email protected]65f173552012-06-28 22:43:5816namespace syncer {
[email protected]0a80fed2011-03-24 22:31:4817
[email protected]942e2022011-04-07 22:27:4318class NonBlockingInvalidationNotifier::Core
19 : public base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>,
[email protected]7800d792012-05-30 02:34:4820 // SyncNotifierObserver to observe the InvalidationNotifier we create.
[email protected]942e2022011-04-07 22:27:4321 public SyncNotifierObserver {
22 public:
[email protected]b9908a242011-11-19 09:31:3223 // Called on parent thread. |delegate_observer| should be
24 // initialized.
25 explicit Core(
[email protected]d45f0d92012-07-20 17:25:4126 const WeakHandle<SyncNotifierObserver>& delegate_observer);
[email protected]942e2022011-04-07 22:27:4327
28 // Helpers called on I/O thread.
[email protected]e3b0ee52011-10-12 03:04:1329 void Initialize(
30 const notifier::NotifierOptions& notifier_options,
31 const InvalidationVersionMap& initial_max_invalidation_versions,
[email protected]0857ae0b2012-05-31 00:39:2632 const std::string& initial_invalidation_state,
[email protected]d45f0d92012-07-20 17:25:4133 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker,
[email protected]e3b0ee52011-10-12 03:04:1334 const std::string& client_info);
[email protected]942e2022011-04-07 22:27:4335 void Teardown();
[email protected]d914f022012-07-27 02:02:0036 void UpdateRegisteredIds(const ObjectIdSet& ids);
[email protected]0a5612052011-06-29 03:29:1837 void SetUniqueId(const std::string& unique_id);
[email protected]0857ae0b2012-05-31 00:39:2638 void SetStateDeprecated(const std::string& state);
[email protected]942e2022011-04-07 22:27:4339 void UpdateCredentials(const std::string& email, const std::string& token);
[email protected]942e2022011-04-07 22:27:4340
[email protected]7800d792012-05-30 02:34:4841 // SyncNotifierObserver implementation (all called on I/O thread by
42 // InvalidationNotifier).
[email protected]2d3d1d12012-06-18 20:50:2843 virtual void OnNotificationsEnabled() OVERRIDE;
44 virtual void OnNotificationsDisabled(
45 NotificationsDisabledReason reason) OVERRIDE;
[email protected]942e2022011-04-07 22:27:4346 virtual void OnIncomingNotification(
[email protected]d914f022012-07-27 02:02:0047 const ObjectIdPayloadMap& id_payloads,
[email protected]2d3d1d12012-06-18 20:50:2848 IncomingNotificationSource source) OVERRIDE;
[email protected]942e2022011-04-07 22:27:4349
50 private:
51 friend class
52 base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>;
53 // Called on parent or I/O thread.
54 ~Core();
55
[email protected]b9908a242011-11-19 09:31:3256 // The variables below should be used only on the I/O thread.
[email protected]d45f0d92012-07-20 17:25:4157 const WeakHandle<SyncNotifierObserver> delegate_observer_;
[email protected]942e2022011-04-07 22:27:4358 scoped_ptr<InvalidationNotifier> invalidation_notifier_;
[email protected]4969b0122012-06-16 01:58:2859 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
[email protected]b9908a242011-11-19 09:31:3260
[email protected]942e2022011-04-07 22:27:4361 DISALLOW_COPY_AND_ASSIGN(Core);
62};
63
[email protected]b9908a242011-11-19 09:31:3264NonBlockingInvalidationNotifier::Core::Core(
[email protected]d45f0d92012-07-20 17:25:4165 const WeakHandle<SyncNotifierObserver>& delegate_observer)
[email protected]b9908a242011-11-19 09:31:3266 : delegate_observer_(delegate_observer) {
67 DCHECK(delegate_observer_.IsInitialized());
[email protected]942e2022011-04-07 22:27:4368}
69
70NonBlockingInvalidationNotifier::Core::~Core() {
71}
72
73void NonBlockingInvalidationNotifier::Core::Initialize(
74 const notifier::NotifierOptions& notifier_options,
[email protected]e3b0ee52011-10-12 03:04:1375 const InvalidationVersionMap& initial_max_invalidation_versions,
[email protected]0857ae0b2012-05-31 00:39:2676 const std::string& initial_invalidation_state,
[email protected]d45f0d92012-07-20 17:25:4177 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker,
[email protected]942e2022011-04-07 22:27:4378 const std::string& client_info) {
79 DCHECK(notifier_options.request_context_getter);
80 DCHECK_EQ(notifier::NOTIFICATION_SERVER,
81 notifier_options.notification_method);
[email protected]4969b0122012-06-16 01:58:2882 network_task_runner_ = notifier_options.request_context_getter->
83 GetNetworkTaskRunner();
84 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]942e2022011-04-07 22:27:4385 invalidation_notifier_.reset(
[email protected]e3b0ee52011-10-12 03:04:1386 new InvalidationNotifier(
[email protected]1f7eba72012-05-30 09:10:1787 notifier::PushClient::CreateDefaultOnIOThread(notifier_options),
[email protected]e3b0ee52011-10-12 03:04:1388 initial_max_invalidation_versions,
[email protected]0857ae0b2012-05-31 00:39:2689 initial_invalidation_state,
[email protected]46e43ee2012-05-18 19:24:4190 invalidation_state_tracker,
[email protected]e3b0ee52011-10-12 03:04:1391 client_info));
[email protected]942e2022011-04-07 22:27:4392}
93
94
95void NonBlockingInvalidationNotifier::Core::Teardown() {
[email protected]4969b0122012-06-16 01:58:2896 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]d914f022012-07-27 02:02:0097 invalidation_notifier_->UpdateRegisteredIds(this, ObjectIdSet());
[email protected]942e2022011-04-07 22:27:4398 invalidation_notifier_.reset();
[email protected]4969b0122012-06-16 01:58:2899 network_task_runner_ = NULL;
[email protected]942e2022011-04-07 22:27:43100}
101
[email protected]d914f022012-07-27 02:02:00102void NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds(
103 const ObjectIdSet& ids) {
104 DCHECK(network_task_runner_->BelongsToCurrentThread());
105 invalidation_notifier_->UpdateRegisteredIds(this, ids);
106}
107
[email protected]0a5612052011-06-29 03:29:18108void NonBlockingInvalidationNotifier::Core::SetUniqueId(
109 const std::string& unique_id) {
[email protected]4969b0122012-06-16 01:58:28110 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]0a5612052011-06-29 03:29:18111 invalidation_notifier_->SetUniqueId(unique_id);
112}
113
[email protected]0857ae0b2012-05-31 00:39:26114void NonBlockingInvalidationNotifier::Core::SetStateDeprecated(
[email protected]942e2022011-04-07 22:27:43115 const std::string& state) {
[email protected]4969b0122012-06-16 01:58:28116 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]0857ae0b2012-05-31 00:39:26117 invalidation_notifier_->SetStateDeprecated(state);
[email protected]942e2022011-04-07 22:27:43118}
119
120void NonBlockingInvalidationNotifier::Core::UpdateCredentials(
121 const std::string& email, const std::string& token) {
[email protected]4969b0122012-06-16 01:58:28122 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]942e2022011-04-07 22:27:43123 invalidation_notifier_->UpdateCredentials(email, token);
124}
125
[email protected]2d3d1d12012-06-18 20:50:28126void NonBlockingInvalidationNotifier::Core::OnNotificationsEnabled() {
127 DCHECK(network_task_runner_->BelongsToCurrentThread());
128 delegate_observer_.Call(FROM_HERE,
129 &SyncNotifierObserver::OnNotificationsEnabled);
130}
131
132void NonBlockingInvalidationNotifier::Core::OnNotificationsDisabled(
133 NotificationsDisabledReason reason) {
134 DCHECK(network_task_runner_->BelongsToCurrentThread());
135 delegate_observer_.Call(
136 FROM_HERE, &SyncNotifierObserver::OnNotificationsDisabled, reason);
137}
138
[email protected]942e2022011-04-07 22:27:43139void NonBlockingInvalidationNotifier::Core::OnIncomingNotification(
[email protected]d914f022012-07-27 02:02:00140 const ObjectIdPayloadMap& id_payloads, IncomingNotificationSource source) {
[email protected]4969b0122012-06-16 01:58:28141 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]b9908a242011-11-19 09:31:32142 delegate_observer_.Call(FROM_HERE,
143 &SyncNotifierObserver::OnIncomingNotification,
[email protected]d914f022012-07-27 02:02:00144 id_payloads,
[email protected]6990e4e02012-01-26 00:44:53145 source);
[email protected]942e2022011-04-07 22:27:43146}
147
[email protected]0a80fed2011-03-24 22:31:48148NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier(
149 const notifier::NotifierOptions& notifier_options,
[email protected]e3b0ee52011-10-12 03:04:13150 const InvalidationVersionMap& initial_max_invalidation_versions,
[email protected]0857ae0b2012-05-31 00:39:26151 const std::string& initial_invalidation_state,
[email protected]d45f0d92012-07-20 17:25:41152 const WeakHandle<InvalidationStateTracker>&
[email protected]46e43ee2012-05-18 19:24:41153 invalidation_state_tracker,
[email protected]0a80fed2011-03-24 22:31:48154 const std::string& client_info)
[email protected]b9908a242011-11-19 09:31:32155 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
156 core_(
[email protected]d45f0d92012-07-20 17:25:41157 new Core(MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()))),
[email protected]4969b0122012-06-16 01:58:28158 parent_task_runner_(
159 base::ThreadTaskRunnerHandle::Get()),
160 network_task_runner_(notifier_options.request_context_getter->
161 GetNetworkTaskRunner()) {
162 if (!network_task_runner_->PostTask(
[email protected]daf4e102011-04-26 08:30:28163 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02164 base::Bind(
[email protected]daf4e102011-04-26 08:30:28165 &NonBlockingInvalidationNotifier::Core::Initialize,
[email protected]09e170f2011-10-28 23:22:02166 core_.get(),
[email protected]e3b0ee52011-10-12 03:04:13167 notifier_options,
168 initial_max_invalidation_versions,
[email protected]0857ae0b2012-05-31 00:39:26169 initial_invalidation_state,
[email protected]46e43ee2012-05-18 19:24:41170 invalidation_state_tracker,
[email protected]e3b0ee52011-10-12 03:04:13171 client_info))) {
[email protected]daf4e102011-04-26 08:30:28172 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18173 }
[email protected]0a80fed2011-03-24 22:31:48174}
175
176NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() {
[email protected]4969b0122012-06-16 01:58:28177 DCHECK(parent_task_runner_->BelongsToCurrentThread());
178 if (!network_task_runner_->PostTask(
[email protected]daf4e102011-04-26 08:30:28179 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02180 base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown,
181 core_.get()))) {
[email protected]daf4e102011-04-26 08:30:28182 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18183 }
[email protected]0a80fed2011-03-24 22:31:48184}
185
[email protected]d914f022012-07-27 02:02:00186void NonBlockingInvalidationNotifier::UpdateRegisteredIds(
187 SyncNotifierObserver* handler, const ObjectIdSet& ids) {
[email protected]4969b0122012-06-16 01:58:28188 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]d914f022012-07-27 02:02:00189 const ObjectIdSet& all_registered_ids =
190 helper_.UpdateRegisteredIds(handler, ids);
191 if (!network_task_runner_->PostTask(
192 FROM_HERE,
193 base::Bind(
194 &NonBlockingInvalidationNotifier::Core::UpdateRegisteredIds,
195 core_.get(),
196 all_registered_ids))) {
197 NOTREACHED();
198 }
[email protected]0a80fed2011-03-24 22:31:48199}
200
[email protected]0a5612052011-06-29 03:29:18201void NonBlockingInvalidationNotifier::SetUniqueId(
202 const std::string& unique_id) {
[email protected]4969b0122012-06-16 01:58:28203 DCHECK(parent_task_runner_->BelongsToCurrentThread());
204 if (!network_task_runner_->PostTask(
[email protected]0a5612052011-06-29 03:29:18205 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02206 base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId,
207 core_.get(), unique_id))) {
[email protected]0a5612052011-06-29 03:29:18208 NOTREACHED();
209 }
210}
211
[email protected]0857ae0b2012-05-31 00:39:26212void NonBlockingInvalidationNotifier::SetStateDeprecated(
213 const std::string& state) {
[email protected]4969b0122012-06-16 01:58:28214 DCHECK(parent_task_runner_->BelongsToCurrentThread());
215 if (!network_task_runner_->PostTask(
[email protected]daf4e102011-04-26 08:30:28216 FROM_HERE,
[email protected]0857ae0b2012-05-31 00:39:26217 base::Bind(
218 &NonBlockingInvalidationNotifier::Core::SetStateDeprecated,
219 core_.get(), state))) {
[email protected]daf4e102011-04-26 08:30:28220 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18221 }
[email protected]0a80fed2011-03-24 22:31:48222}
223
224void NonBlockingInvalidationNotifier::UpdateCredentials(
225 const std::string& email, const std::string& token) {
[email protected]4969b0122012-06-16 01:58:28226 DCHECK(parent_task_runner_->BelongsToCurrentThread());
227 if (!network_task_runner_->PostTask(
[email protected]daf4e102011-04-26 08:30:28228 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02229 base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials,
230 core_.get(), email, token))) {
[email protected]daf4e102011-04-26 08:30:28231 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18232 }
[email protected]0a80fed2011-03-24 22:31:48233}
234
[email protected]2f15fd0e2011-08-27 05:29:09235void NonBlockingInvalidationNotifier::SendNotification(
[email protected]d45f0d92012-07-20 17:25:41236 ModelTypeSet changed_types) {
[email protected]4969b0122012-06-16 01:58:28237 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]0a80fed2011-03-24 22:31:48238 // InvalidationClient doesn't implement SendNotification(), so no
239 // need to forward on the call.
240}
241
[email protected]2d3d1d12012-06-18 20:50:28242void NonBlockingInvalidationNotifier::OnNotificationsEnabled() {
243 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]d914f022012-07-27 02:02:00244 helper_.EmitOnNotificationsEnabled();
[email protected]2d3d1d12012-06-18 20:50:28245}
246
247void NonBlockingInvalidationNotifier::OnNotificationsDisabled(
248 NotificationsDisabledReason reason) {
249 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]d914f022012-07-27 02:02:00250 helper_.EmitOnNotificationsDisabled(reason);
[email protected]2d3d1d12012-06-18 20:50:28251}
252
[email protected]b9908a242011-11-19 09:31:32253void NonBlockingInvalidationNotifier::OnIncomingNotification(
[email protected]d914f022012-07-27 02:02:00254 const ObjectIdPayloadMap& id_payloads,
[email protected]6990e4e02012-01-26 00:44:53255 IncomingNotificationSource source) {
[email protected]4969b0122012-06-16 01:58:28256 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]d914f022012-07-27 02:02:00257 helper_.DispatchInvalidationsToHandlers(id_payloads, source);
[email protected]b9908a242011-11-19 09:31:32258}
259
[email protected]65f173552012-06-28 22:43:58260} // namespace syncer