blob: ffb93618f61c4e0a75be3b305f92a71e8176b076 [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]df006cbc2014-01-22 18:36:2016#include "sync/notifier/gcm_network_channel_delegate.h"
[email protected]b7fe4b52012-04-19 14:21:0917#include "sync/notifier/invalidation_notifier.h"
[email protected]a7b16392013-11-26 22:46:2618#include "sync/notifier/object_id_invalidation_map.h"
[email protected]04a830a2014-01-04 02:48:5119#include "sync/notifier/sync_system_resources.h"
[email protected]0a80fed2011-03-24 22:31:4820
[email protected]65f173552012-06-28 22:43:5821namespace syncer {
[email protected]0a80fed2011-03-24 22:31:4822
[email protected]04a830a2014-01-04 02:48:5123struct NonBlockingInvalidator::InitializeOptions {
24 InitializeOptions(
25 NetworkChannelCreator network_channel_creator,
26 const std::string& invalidator_client_id,
27 const UnackedInvalidationsMap& saved_invalidations,
28 const std::string& invalidation_bootstrap_data,
29 const WeakHandle<InvalidationStateTracker>&
30 invalidation_state_tracker,
31 const std::string& client_info,
32 scoped_refptr<net::URLRequestContextGetter> request_context_getter)
33 : network_channel_creator(network_channel_creator),
34 invalidator_client_id(invalidator_client_id),
35 saved_invalidations(saved_invalidations),
36 invalidation_bootstrap_data(invalidation_bootstrap_data),
37 invalidation_state_tracker(invalidation_state_tracker),
38 client_info(client_info),
39 request_context_getter(request_context_getter) {
40 }
41
42 NetworkChannelCreator network_channel_creator;
43 std::string invalidator_client_id;
44 UnackedInvalidationsMap saved_invalidations;
45 std::string invalidation_bootstrap_data;
46 WeakHandle<InvalidationStateTracker> invalidation_state_tracker;
47 std::string client_info;
48 scoped_refptr<net::URLRequestContextGetter> request_context_getter;
49};
50
51
[email protected]a329cb82012-08-28 03:17:5852class NonBlockingInvalidator::Core
53 : public base::RefCountedThreadSafe<NonBlockingInvalidator::Core>,
54 // InvalidationHandler to observe the InvalidationNotifier we create.
55 public InvalidationHandler {
[email protected]942e2022011-04-07 22:27:4356 public:
[email protected]b9908a242011-11-19 09:31:3257 // Called on parent thread. |delegate_observer| should be
58 // initialized.
59 explicit Core(
[email protected]a329cb82012-08-28 03:17:5860 const WeakHandle<InvalidationHandler>& delegate_observer);
[email protected]942e2022011-04-07 22:27:4361
62 // Helpers called on I/O thread.
[email protected]e3b0ee52011-10-12 03:04:1363 void Initialize(
[email protected]04a830a2014-01-04 02:48:5164 const NonBlockingInvalidator::InitializeOptions& initialize_options);
[email protected]942e2022011-04-07 22:27:4365 void Teardown();
[email protected]d914f022012-07-27 02:02:0066 void UpdateRegisteredIds(const ObjectIdSet& ids);
[email protected]942e2022011-04-07 22:27:4367 void UpdateCredentials(const std::string& email, const std::string& token);
[email protected]942e2022011-04-07 22:27:4368
[email protected]a329cb82012-08-28 03:17:5869 // InvalidationHandler implementation (all called on I/O thread by
[email protected]7800d792012-05-30 02:34:4870 // InvalidationNotifier).
[email protected]08a6f9992012-09-07 19:19:1671 virtual void OnInvalidatorStateChange(InvalidatorState reason) OVERRIDE;
72 virtual void OnIncomingInvalidation(
[email protected]0d0b59d2013-02-02 00:24:4773 const ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
[email protected]9b0d66b2014-02-20 13:16:0174 virtual std::string GetOwnerName() const OVERRIDE;
[email protected]942e2022011-04-07 22:27:4375
76 private:
77 friend class
[email protected]a329cb82012-08-28 03:17:5878 base::RefCountedThreadSafe<NonBlockingInvalidator::Core>;
[email protected]942e2022011-04-07 22:27:4379 // Called on parent or I/O thread.
[email protected]6d44a14e2013-02-07 22:19:5680 virtual ~Core();
[email protected]942e2022011-04-07 22:27:4381
[email protected]b9908a242011-11-19 09:31:3282 // The variables below should be used only on the I/O thread.
[email protected]a329cb82012-08-28 03:17:5883 const WeakHandle<InvalidationHandler> delegate_observer_;
[email protected]942e2022011-04-07 22:27:4384 scoped_ptr<InvalidationNotifier> invalidation_notifier_;
[email protected]4969b0122012-06-16 01:58:2885 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
[email protected]b9908a242011-11-19 09:31:3286
[email protected]942e2022011-04-07 22:27:4387 DISALLOW_COPY_AND_ASSIGN(Core);
88};
89
[email protected]a329cb82012-08-28 03:17:5890NonBlockingInvalidator::Core::Core(
91 const WeakHandle<InvalidationHandler>& delegate_observer)
[email protected]b9908a242011-11-19 09:31:3292 : delegate_observer_(delegate_observer) {
93 DCHECK(delegate_observer_.IsInitialized());
[email protected]942e2022011-04-07 22:27:4394}
95
[email protected]a329cb82012-08-28 03:17:5896NonBlockingInvalidator::Core::~Core() {
[email protected]942e2022011-04-07 22:27:4397}
98
[email protected]a329cb82012-08-28 03:17:5899void NonBlockingInvalidator::Core::Initialize(
[email protected]04a830a2014-01-04 02:48:51100 const NonBlockingInvalidator::InitializeOptions& initialize_options) {
101 DCHECK(initialize_options.request_context_getter.get());
102 network_task_runner_ =
103 initialize_options.request_context_getter->GetNetworkTaskRunner();
[email protected]4969b0122012-06-16 01:58:28104 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]04a830a2014-01-04 02:48:51105 scoped_ptr<SyncNetworkChannel> network_channel =
106 initialize_options.network_channel_creator.Run();
[email protected]942e2022011-04-07 22:27:43107 invalidation_notifier_.reset(
[email protected]e3b0ee52011-10-12 03:04:13108 new InvalidationNotifier(
[email protected]04a830a2014-01-04 02:48:51109 network_channel.Pass(),
110 initialize_options.invalidator_client_id,
111 initialize_options.saved_invalidations,
112 initialize_options.invalidation_bootstrap_data,
113 initialize_options.invalidation_state_tracker,
114 initialize_options.client_info));
[email protected]85b25eb2012-08-10 19:32:08115 invalidation_notifier_->RegisterHandler(this);
[email protected]942e2022011-04-07 22:27:43116}
117
[email protected]a329cb82012-08-28 03:17:58118void NonBlockingInvalidator::Core::Teardown() {
[email protected]4969b0122012-06-16 01:58:28119 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]85b25eb2012-08-10 19:32:08120 invalidation_notifier_->UnregisterHandler(this);
[email protected]942e2022011-04-07 22:27:43121 invalidation_notifier_.reset();
[email protected]4969b0122012-06-16 01:58:28122 network_task_runner_ = NULL;
[email protected]942e2022011-04-07 22:27:43123}
124
[email protected]a329cb82012-08-28 03:17:58125void NonBlockingInvalidator::Core::UpdateRegisteredIds(const ObjectIdSet& ids) {
[email protected]d914f022012-07-27 02:02:00126 DCHECK(network_task_runner_->BelongsToCurrentThread());
127 invalidation_notifier_->UpdateRegisteredIds(this, ids);
128}
129
[email protected]a329cb82012-08-28 03:17:58130void NonBlockingInvalidator::Core::UpdateCredentials(const std::string& email,
131 const std::string& token) {
[email protected]4969b0122012-06-16 01:58:28132 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]942e2022011-04-07 22:27:43133 invalidation_notifier_->UpdateCredentials(email, token);
134}
135
[email protected]08a6f9992012-09-07 19:19:16136void NonBlockingInvalidator::Core::OnInvalidatorStateChange(
137 InvalidatorState reason) {
[email protected]2d3d1d12012-06-18 20:50:28138 DCHECK(network_task_runner_->BelongsToCurrentThread());
139 delegate_observer_.Call(
[email protected]08a6f9992012-09-07 19:19:16140 FROM_HERE, &InvalidationHandler::OnInvalidatorStateChange, reason);
[email protected]2d3d1d12012-06-18 20:50:28141}
142
[email protected]08a6f9992012-09-07 19:19:16143void NonBlockingInvalidator::Core::OnIncomingInvalidation(
[email protected]0d0b59d2013-02-02 00:24:47144 const ObjectIdInvalidationMap& invalidation_map) {
[email protected]4969b0122012-06-16 01:58:28145 DCHECK(network_task_runner_->BelongsToCurrentThread());
[email protected]b9908a242011-11-19 09:31:32146 delegate_observer_.Call(FROM_HERE,
[email protected]08a6f9992012-09-07 19:19:16147 &InvalidationHandler::OnIncomingInvalidation,
[email protected]0d0b59d2013-02-02 00:24:47148 invalidation_map);
[email protected]942e2022011-04-07 22:27:43149}
150
[email protected]9b0d66b2014-02-20 13:16:01151std::string NonBlockingInvalidator::Core::GetOwnerName() const {
152 return "Sync";
153}
154
[email protected]a329cb82012-08-28 03:17:58155NonBlockingInvalidator::NonBlockingInvalidator(
[email protected]04a830a2014-01-04 02:48:51156 NetworkChannelCreator network_channel_creator,
[email protected]d5511232013-03-28 01:34:54157 const std::string& invalidator_client_id,
[email protected]a7b16392013-11-26 22:46:26158 const UnackedInvalidationsMap& saved_invalidations,
[email protected]8cdb6892012-10-03 05:54:40159 const std::string& invalidation_bootstrap_data,
[email protected]d45f0d92012-07-20 17:25:41160 const WeakHandle<InvalidationStateTracker>&
[email protected]46e43ee2012-05-18 19:24:41161 invalidation_state_tracker,
[email protected]04a830a2014-01-04 02:48:51162 const std::string& client_info,
163 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter)
[email protected]37d5b3472013-10-10 16:20:36164 : parent_task_runner_(base::ThreadTaskRunnerHandle::Get()),
[email protected]04a830a2014-01-04 02:48:51165 network_task_runner_(request_context_getter->GetNetworkTaskRunner()),
[email protected]37d5b3472013-10-10 16:20:36166 weak_ptr_factory_(this) {
167 core_ = new Core(MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()));
168
[email protected]04a830a2014-01-04 02:48:51169 InitializeOptions initialize_options(
170 network_channel_creator,
171 invalidator_client_id,
172 saved_invalidations,
173 invalidation_bootstrap_data,
174 invalidation_state_tracker,
175 client_info,
176 request_context_getter);
177
[email protected]4969b0122012-06-16 01:58:28178 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(
[email protected]a329cb82012-08-28 03:17:58181 &NonBlockingInvalidator::Core::Initialize,
[email protected]09e170f2011-10-28 23:22:02182 core_.get(),
[email protected]04a830a2014-01-04 02:48:51183 initialize_options))) {
[email protected]daf4e102011-04-26 08:30:28184 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18185 }
[email protected]0a80fed2011-03-24 22:31:48186}
187
[email protected]a329cb82012-08-28 03:17:58188NonBlockingInvalidator::~NonBlockingInvalidator() {
[email protected]4969b0122012-06-16 01:58:28189 DCHECK(parent_task_runner_->BelongsToCurrentThread());
190 if (!network_task_runner_->PostTask(
[email protected]daf4e102011-04-26 08:30:28191 FROM_HERE,
[email protected]a329cb82012-08-28 03:17:58192 base::Bind(&NonBlockingInvalidator::Core::Teardown,
[email protected]09e170f2011-10-28 23:22:02193 core_.get()))) {
[email protected]93dcabfb2013-07-31 01:31:55194 DVLOG(1) << "Network thread stopped before invalidator is destroyed.";
[email protected]0a5612052011-06-29 03:29:18195 }
[email protected]0a80fed2011-03-24 22:31:48196}
197
[email protected]a329cb82012-08-28 03:17:58198void NonBlockingInvalidator::RegisterHandler(InvalidationHandler* handler) {
[email protected]65293822012-08-10 04:07:19199 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]85b25eb2012-08-10 19:32:08200 registrar_.RegisterHandler(handler);
201}
202
[email protected]a329cb82012-08-28 03:17:58203void NonBlockingInvalidator::UpdateRegisteredIds(InvalidationHandler* handler,
204 const ObjectIdSet& ids) {
[email protected]85b25eb2012-08-10 19:32:08205 DCHECK(parent_task_runner_->BelongsToCurrentThread());
206 registrar_.UpdateRegisteredIds(handler, ids);
[email protected]d914f022012-07-27 02:02:00207 if (!network_task_runner_->PostTask(
208 FROM_HERE,
209 base::Bind(
[email protected]a329cb82012-08-28 03:17:58210 &NonBlockingInvalidator::Core::UpdateRegisteredIds,
[email protected]d914f022012-07-27 02:02:00211 core_.get(),
[email protected]85b25eb2012-08-10 19:32:08212 registrar_.GetAllRegisteredIds()))) {
[email protected]d914f022012-07-27 02:02:00213 NOTREACHED();
214 }
[email protected]0a80fed2011-03-24 22:31:48215}
216
[email protected]a329cb82012-08-28 03:17:58217void NonBlockingInvalidator::UnregisterHandler(InvalidationHandler* handler) {
[email protected]85b25eb2012-08-10 19:32:08218 DCHECK(parent_task_runner_->BelongsToCurrentThread());
219 registrar_.UnregisterHandler(handler);
220}
221
[email protected]08a6f9992012-09-07 19:19:16222InvalidatorState NonBlockingInvalidator::GetInvalidatorState() const {
223 DCHECK(parent_task_runner_->BelongsToCurrentThread());
224 return registrar_.GetInvalidatorState();
225}
226
[email protected]a329cb82012-08-28 03:17:58227void NonBlockingInvalidator::UpdateCredentials(const std::string& email,
228 const std::string& token) {
[email protected]4969b0122012-06-16 01:58:28229 DCHECK(parent_task_runner_->BelongsToCurrentThread());
230 if (!network_task_runner_->PostTask(
[email protected]daf4e102011-04-26 08:30:28231 FROM_HERE,
[email protected]a329cb82012-08-28 03:17:58232 base::Bind(&NonBlockingInvalidator::Core::UpdateCredentials,
[email protected]09e170f2011-10-28 23:22:02233 core_.get(), email, token))) {
[email protected]daf4e102011-04-26 08:30:28234 NOTREACHED();
[email protected]0a5612052011-06-29 03:29:18235 }
[email protected]0a80fed2011-03-24 22:31:48236}
237
[email protected]08a6f9992012-09-07 19:19:16238void NonBlockingInvalidator::OnInvalidatorStateChange(InvalidatorState state) {
[email protected]2d3d1d12012-06-18 20:50:28239 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]08a6f9992012-09-07 19:19:16240 registrar_.UpdateInvalidatorState(state);
[email protected]2d3d1d12012-06-18 20:50:28241}
242
[email protected]08a6f9992012-09-07 19:19:16243void NonBlockingInvalidator::OnIncomingInvalidation(
[email protected]0d0b59d2013-02-02 00:24:47244 const ObjectIdInvalidationMap& invalidation_map) {
[email protected]4969b0122012-06-16 01:58:28245 DCHECK(parent_task_runner_->BelongsToCurrentThread());
[email protected]0d0b59d2013-02-02 00:24:47246 registrar_.DispatchInvalidationsToHandlers(invalidation_map);
[email protected]b9908a242011-11-19 09:31:32247}
248
[email protected]9b0d66b2014-02-20 13:16:01249std::string NonBlockingInvalidator::GetOwnerName() const { return "Sync"; }
250
[email protected]04a830a2014-01-04 02:48:51251NetworkChannelCreator
252 NonBlockingInvalidator::MakePushClientChannelCreator(
253 const notifier::NotifierOptions& notifier_options) {
254 return base::Bind(SyncNetworkChannel::CreatePushClientChannel,
255 notifier_options);
256}
257
[email protected]df006cbc2014-01-22 18:36:20258NetworkChannelCreator NonBlockingInvalidator::MakeGCMNetworkChannelCreator(
259 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
260 scoped_ptr<GCMNetworkChannelDelegate> delegate) {
261 return base::Bind(&SyncNetworkChannel::CreateGCMNetworkChannel,
262 request_context_getter,
263 base::Passed(&delegate));
[email protected]04a830a2014-01-04 02:48:51264}
265
[email protected]65f173552012-06-28 22:43:58266} // namespace syncer