blob: bd0596722b0e155b7edf6e2bbac20924c1f1e1d0 [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]a329cb82012-08-28 03:17:585#include "sync/notifier/non_blocking_invalidator.h"
[email protected]0a80fed2011-03-24 22:31:486
[email protected]85b25eb2012-08-10 19:32:087#include <cstddef>
8
[email protected]4969b0122012-06-16 01:58:289#include "base/location.h"
[email protected]0a80fed2011-03-24 22:31:4810#include "base/logging.h"
[email protected]942e2022011-04-07 22:27:4311#include "base/memory/scoped_ptr.h"
[email protected]4969b0122012-06-16 01:58:2812#include "base/single_thread_task_runner.h"
13#include "base/thread_task_runner_handle.h"
[email protected]942e2022011-04-07 22:27:4314#include "base/threading/thread.h"
[email protected]1f7eba72012-05-30 09:10:1715#include "jingle/notifier/listener/push_client.h"
[email protected]b7fe4b52012-04-19 14:21:0916#include "sync/notifier/invalidation_notifier.h"
[email protected]a7b16392013-11-26 22:46:2617#include "sync/notifier/object_id_invalidation_map.h"
[email protected]0a80fed2011-03-24 22:31:4818
[email protected]65f173552012-06-28 22:43:5819namespace syncer {
[email protected]0a80fed2011-03-24 22:31:4820
[email protected]a329cb82012-08-28 03:17:5821class NonBlockingInvalidator::Core
22 : public base::RefCountedThreadSafe<NonBlockingInvalidator::Core>,
23 // InvalidationHandler to observe the InvalidationNotifier we create.
24 public InvalidationHandler {
[email protected]942e2022011-04-07 22:27:4325 public:
[email protected]b9908a242011-11-19 09:31:3226 // Called on parent thread. |delegate_observer| should be
27 // initialized.
28 explicit Core(
[email protected]a329cb82012-08-28 03:17:5829 const WeakHandle<InvalidationHandler>& delegate_observer);
[email protected]942e2022011-04-07 22:27:4330
31 // Helpers called on I/O thread.
[email protected]e3b0ee52011-10-12 03:04:1332 void Initialize(
33 const notifier::NotifierOptions& notifier_options,
[email protected]d5511232013-03-28 01:34:5434 const std::string& invalidator_client_id,
[email protected]a7b16392013-11-26 22:46:2635 const UnackedInvalidationsMap& saved_invalidations,
[email protected]8cdb6892012-10-03 05:54:4036 const std::string& invalidation_bootstrap_data,
[email protected]d45f0d92012-07-20 17:25:4137 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker,
[email protected]e3b0ee52011-10-12 03:04:1338 const std::string& client_info);
[email protected]942e2022011-04-07 22:27:4339 void Teardown();
[email protected]d914f022012-07-27 02:02:0040 void UpdateRegisteredIds(const ObjectIdSet& ids);
[email protected]942e2022011-04-07 22:27:4341 void UpdateCredentials(const std::string& email, const std::string& token);
[email protected]942e2022011-04-07 22:27:4342
[email protected]a329cb82012-08-28 03:17:5843 // InvalidationHandler implementation (all called on I/O thread by
[email protected]7800d792012-05-30 02:34:4844 // InvalidationNotifier).
[email protected]08a6f9992012-09-07 19:19:1645 virtual void OnInvalidatorStateChange(InvalidatorState reason) OVERRIDE;
46 virtual void OnIncomingInvalidation(
[email protected]0d0b59d2013-02-02 00:24:4747 const ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
[email protected]942e2022011-04-07 22:27:4348
49 private:
50 friend class
[email protected]a329cb82012-08-28 03:17:5851 base::RefCountedThreadSafe<NonBlockingInvalidator::Core>;
[email protected]942e2022011-04-07 22:27:4352 // Called on parent or I/O thread.
[email protected]6d44a14e2013-02-07 22:19:5653 virtual ~Core();
[email protected]942e2022011-04-07 22:27:4354
[email protected]b9908a242011-11-19 09:31:3255 // The variables below should be used only on the I/O thread.
[email protected]a329cb82012-08-28 03:17:5856 const WeakHandle<InvalidationHandler> delegate_observer_;
[email protected]942e2022011-04-07 22:27:4357 scoped_ptr<InvalidationNotifier> invalidation_notifier_;
[email protected]4969b0122012-06-16 01:58:2858 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
[email protected]b9908a242011-11-19 09:31:3259
[email protected]942e2022011-04-07 22:27:4360 DISALLOW_COPY_AND_ASSIGN(Core);
61};
62
[email protected]a329cb82012-08-28 03:17:5863NonBlockingInvalidator::Core::Core(
64 const WeakHandle<InvalidationHandler>& delegate_observer)
[email protected]b9908a242011-11-19 09:31:3265 : delegate_observer_(delegate_observer) {
66 DCHECK(delegate_observer_.IsInitialized());
[email protected]942e2022011-04-07 22:27:4367}
68
[email protected]a329cb82012-08-28 03:17:5869NonBlockingInvalidator::Core::~Core() {
[email protected]942e2022011-04-07 22:27:4370}
71
[email protected]a329cb82012-08-28 03:17:5872void NonBlockingInvalidator::Core::Initialize(
[email protected]942e2022011-04-07 22:27:4373 const notifier::NotifierOptions& notifier_options,
[email protected]d5511232013-03-28 01:34:5474 const std::string& invalidator_client_id,
[email protected]a7b16392013-11-26 22:46:2675 const UnackedInvalidationsMap& saved_invalidations,
[email protected]8cdb6892012-10-03 05:54:4076 const std::string& invalidation_bootstrap_data,
[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) {
[email protected]71318802013-06-03 00:20:0579 DCHECK(notifier_options.request_context_getter.get());
[email protected]942e2022011-04-07 22:27:4380 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]d5511232013-03-28 01:34:5488 invalidator_client_id,
[email protected]a7b16392013-11-26 22:46:2689 saved_invalidations,
[email protected]8cdb6892012-10-03 05:54:4090 invalidation_bootstrap_data,
[email protected]46e43ee2012-05-18 19:24:4191 invalidation_state_tracker,
[email protected]e3b0ee52011-10-12 03:04:1392 client_info));
[email protected]85b25eb2012-08-10 19:32:0893 invalidation_notifier_->RegisterHandler(this);
[email protected]942e2022011-04-07 22:27:4394}
95
[email protected]a329cb82012-08-28 03:17:5896void NonBlockingInvalidator::Core::Teardown() {
[email protected]4969b0122012-06-16 01:58:2897 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]85b25eb2012-08-10 19:32:0898 invalidation_notifier_->UnregisterHandler(this);
[email protected]942e2022011-04-07 22:27:4399 invalidation_notifier_.reset();
[email protected]4969b0122012-06-16 01:58:28100 network_task_runner_ = NULL;
[email protected]942e2022011-04-07 22:27:43101}
102
[email protected]a329cb82012-08-28 03:17:58103void NonBlockingInvalidator::Core::UpdateRegisteredIds(const ObjectIdSet& ids) {
[email protected]d914f022012-07-27 02:02:00104 DCHECK(network_task_runner_->BelongsToCurrentThread());
105 invalidation_notifier_->UpdateRegisteredIds(this, ids);
106}
107
[email protected]a329cb82012-08-28 03:17:58108void NonBlockingInvalidator::Core::UpdateCredentials(const std::string& email,
109 const std::string& token) {
[email protected]4969b0122012-06-16 01:58:28110 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]942e2022011-04-07 22:27:43111 invalidation_notifier_->UpdateCredentials(email, token);
112}
113
[email protected]08a6f9992012-09-07 19:19:16114void NonBlockingInvalidator::Core::OnInvalidatorStateChange(
115 InvalidatorState reason) {
[email protected]2d3d1d12012-06-18 20:50:28116 DCHECK(network_task_runner_->BelongsToCurrentThread());
117 delegate_observer_.Call(
[email protected]08a6f9992012-09-07 19:19:16118 FROM_HERE, &InvalidationHandler::OnInvalidatorStateChange, reason);
[email protected]2d3d1d12012-06-18 20:50:28119}
120
[email protected]08a6f9992012-09-07 19:19:16121void NonBlockingInvalidator::Core::OnIncomingInvalidation(
[email protected]0d0b59d2013-02-02 00:24:47122 const ObjectIdInvalidationMap& invalidation_map) {
[email protected]4969b0122012-06-16 01:58:28123 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]b9908a242011-11-19 09:31:32124 delegate_observer_.Call(FROM_HERE,
[email protected]08a6f9992012-09-07 19:19:16125 &InvalidationHandler::OnIncomingInvalidation,
[email protected]0d0b59d2013-02-02 00:24:47126 invalidation_map);
[email protected]942e2022011-04-07 22:27:43127}
128
[email protected]a329cb82012-08-28 03:17:58129NonBlockingInvalidator::NonBlockingInvalidator(
[email protected]0a80fed2011-03-24 22:31:48130 const notifier::NotifierOptions& notifier_options,
[email protected]d5511232013-03-28 01:34:54131 const std::string& invalidator_client_id,
[email protected]a7b16392013-11-26 22:46:26132 const UnackedInvalidationsMap& saved_invalidations,
[email protected]8cdb6892012-10-03 05:54:40133 const std::string& invalidation_bootstrap_data,
[email protected]d45f0d92012-07-20 17:25:41134 const WeakHandle<InvalidationStateTracker>&
[email protected]46e43ee2012-05-18 19:24:41135 invalidation_state_tracker,
[email protected]0a80fed2011-03-24 22:31:48136 const std::string& client_info)
[email protected]37d5b3472013-10-10 16:20:36137 : parent_task_runner_(base::ThreadTaskRunnerHandle::Get()),
138 network_task_runner_(
139 notifier_options.request_context_getter->GetNetworkTaskRunner()),
140 weak_ptr_factory_(this) {
141 core_ = new Core(MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()));
142
[email protected]4969b0122012-06-16 01:58:28143 if (!network_task_runner_->PostTask(
[email protected]daf4e102011-04-26 08:30:28144 FROM_HERE,
[email protected]09e170f2011-10-28 23:22:02145 base::Bind(
[email protected]a329cb82012-08-28 03:17:58146 &NonBlockingInvalidator::Core::Initialize,
[email protected]09e170f2011-10-28 23:22:02147 core_.get(),
[email protected]e3b0ee52011-10-12 03:04:13148 notifier_options,
[email protected]d5511232013-03-28 01:34:54149 invalidator_client_id,
[email protected]a7b16392013-11-26 22:46:26150 saved_invalidations,
[email protected]8cdb6892012-10-03 05:54:40151 invalidation_bootstrap_data,
[email protected]46e43ee2012-05-18 19:24:41152 invalidation_state_tracker,
[email protected]e3b0ee52011-10-12 03:04:13153 client_info))) {
[email protected]daf4e102011-04-26 08:30:28154 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18155 }
[email protected]0a80fed2011-03-24 22:31:48156}
157
[email protected]a329cb82012-08-28 03:17:58158NonBlockingInvalidator::~NonBlockingInvalidator() {
[email protected]4969b0122012-06-16 01:58:28159 DCHECK(parent_task_runner_->BelongsToCurrentThread());
160 if (!network_task_runner_->PostTask(
[email protected]daf4e102011-04-26 08:30:28161 FROM_HERE,
[email protected]a329cb82012-08-28 03:17:58162 base::Bind(&NonBlockingInvalidator::Core::Teardown,
[email protected]09e170f2011-10-28 23:22:02163 core_.get()))) {
[email protected]93dcabfb2013-07-31 01:31:55164 DVLOG(1) << "Network thread stopped before invalidator is destroyed.";
[email protected]0a5612052011-06-29 03:29:18165 }
[email protected]0a80fed2011-03-24 22:31:48166}
167
[email protected]a329cb82012-08-28 03:17:58168void NonBlockingInvalidator::RegisterHandler(InvalidationHandler* handler) {
[email protected]65293822012-08-10 04:07:19169 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]85b25eb2012-08-10 19:32:08170 registrar_.RegisterHandler(handler);
171}
172
[email protected]a329cb82012-08-28 03:17:58173void NonBlockingInvalidator::UpdateRegisteredIds(InvalidationHandler* handler,
174 const ObjectIdSet& ids) {
[email protected]85b25eb2012-08-10 19:32:08175 DCHECK(parent_task_runner_->BelongsToCurrentThread());
176 registrar_.UpdateRegisteredIds(handler, ids);
[email protected]d914f022012-07-27 02:02:00177 if (!network_task_runner_->PostTask(
178 FROM_HERE,
179 base::Bind(
[email protected]a329cb82012-08-28 03:17:58180 &NonBlockingInvalidator::Core::UpdateRegisteredIds,
[email protected]d914f022012-07-27 02:02:00181 core_.get(),
[email protected]85b25eb2012-08-10 19:32:08182 registrar_.GetAllRegisteredIds()))) {
[email protected]d914f022012-07-27 02:02:00183 NOTREACHED();
184 }
[email protected]0a80fed2011-03-24 22:31:48185}
186
[email protected]a329cb82012-08-28 03:17:58187void NonBlockingInvalidator::UnregisterHandler(InvalidationHandler* handler) {
[email protected]85b25eb2012-08-10 19:32:08188 DCHECK(parent_task_runner_->BelongsToCurrentThread());
189 registrar_.UnregisterHandler(handler);
190}
191
[email protected]08a6f9992012-09-07 19:19:16192InvalidatorState NonBlockingInvalidator::GetInvalidatorState() const {
193 DCHECK(parent_task_runner_->BelongsToCurrentThread());
194 return registrar_.GetInvalidatorState();
195}
196
[email protected]a329cb82012-08-28 03:17:58197void NonBlockingInvalidator::UpdateCredentials(const std::string& email,
198 const std::string& token) {
[email protected]4969b0122012-06-16 01:58:28199 DCHECK(parent_task_runner_->BelongsToCurrentThread());
200 if (!network_task_runner_->PostTask(
[email protected]daf4e102011-04-26 08:30:28201 FROM_HERE,
[email protected]a329cb82012-08-28 03:17:58202 base::Bind(&NonBlockingInvalidator::Core::UpdateCredentials,
[email protected]09e170f2011-10-28 23:22:02203 core_.get(), email, token))) {
[email protected]daf4e102011-04-26 08:30:28204 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18205 }
[email protected]0a80fed2011-03-24 22:31:48206}
207
[email protected]08a6f9992012-09-07 19:19:16208void NonBlockingInvalidator::OnInvalidatorStateChange(InvalidatorState state) {
[email protected]2d3d1d12012-06-18 20:50:28209 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]08a6f9992012-09-07 19:19:16210 registrar_.UpdateInvalidatorState(state);
[email protected]2d3d1d12012-06-18 20:50:28211}
212
[email protected]08a6f9992012-09-07 19:19:16213void NonBlockingInvalidator::OnIncomingInvalidation(
[email protected]0d0b59d2013-02-02 00:24:47214 const ObjectIdInvalidationMap& invalidation_map) {
[email protected]4969b0122012-06-16 01:58:28215 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]0d0b59d2013-02-02 00:24:47216 registrar_.DispatchInvalidationsToHandlers(invalidation_map);
[email protected]b9908a242011-11-19 09:31:32217}
218
[email protected]65f173552012-06-28 22:43:58219} // namespace syncer