1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/notifier/non_blocking_invalidation_notifier.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/observer_list_threadsafe.h"
#include "base/threading/thread.h"
#include "chrome/browser/sync/notifier/invalidation_notifier.h"
#include "chrome/browser/sync/notifier/sync_notifier_observer.h"
namespace sync_notifier {
class NonBlockingInvalidationNotifier::Core
: public base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>,
public SyncNotifierObserver {
public:
// Called on parent thread.
Core();
// Called on parent thread.
void AddObserver(SyncNotifierObserver* observer);
void RemoveObserver(SyncNotifierObserver* observer);
// Helpers called on I/O thread.
void Initialize(
const notifier::NotifierOptions& notifier_options,
const InvalidationVersionMap& initial_max_invalidation_versions,
const browser_sync::WeakHandle<InvalidationVersionTracker>&
invalidation_version_tracker,
const std::string& client_info);
void Teardown();
void SetUniqueId(const std::string& unique_id);
void SetState(const std::string& state);
void UpdateCredentials(const std::string& email, const std::string& token);
void UpdateEnabledTypes(const syncable::ModelTypeSet& enabled_types);
// SyncNotifierObserver implementation (all called on I/O thread).
virtual void OnIncomingNotification(
const syncable::ModelTypePayloadMap& type_payloads);
virtual void OnNotificationStateChange(bool notifications_enabled);
virtual void StoreState(const std::string& state);
private:
friend class
base::RefCountedThreadSafe<NonBlockingInvalidationNotifier::Core>;
// Called on parent or I/O thread.
~Core();
scoped_ptr<InvalidationNotifier> invalidation_notifier_;
scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
scoped_refptr<ObserverListThreadSafe<SyncNotifierObserver> > observers_;
DISALLOW_COPY_AND_ASSIGN(Core);
};
NonBlockingInvalidationNotifier::Core::Core()
: observers_(new ObserverListThreadSafe<SyncNotifierObserver>()) {
}
NonBlockingInvalidationNotifier::Core::~Core() {
}
void NonBlockingInvalidationNotifier::Core::Initialize(
const notifier::NotifierOptions& notifier_options,
const InvalidationVersionMap& initial_max_invalidation_versions,
const browser_sync::WeakHandle<InvalidationVersionTracker>&
invalidation_version_tracker,
const std::string& client_info) {
DCHECK(notifier_options.request_context_getter);
DCHECK_EQ(notifier::NOTIFICATION_SERVER,
notifier_options.notification_method);
io_message_loop_proxy_ = notifier_options.request_context_getter->
GetIOMessageLoopProxy();
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
invalidation_notifier_.reset(
new InvalidationNotifier(
notifier_options,
initial_max_invalidation_versions,
invalidation_version_tracker,
client_info));
invalidation_notifier_->AddObserver(this);
}
void NonBlockingInvalidationNotifier::Core::Teardown() {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
invalidation_notifier_->RemoveObserver(this);
invalidation_notifier_.reset();
io_message_loop_proxy_ = NULL;
}
void NonBlockingInvalidationNotifier::Core::AddObserver(
SyncNotifierObserver* observer) {
observers_->AddObserver(observer);
}
void NonBlockingInvalidationNotifier::Core::RemoveObserver(
SyncNotifierObserver* observer) {
observers_->RemoveObserver(observer);
}
void NonBlockingInvalidationNotifier::Core::SetUniqueId(
const std::string& unique_id) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
invalidation_notifier_->SetUniqueId(unique_id);
}
void NonBlockingInvalidationNotifier::Core::SetState(
const std::string& state) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
invalidation_notifier_->SetState(state);
}
void NonBlockingInvalidationNotifier::Core::UpdateCredentials(
const std::string& email, const std::string& token) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
invalidation_notifier_->UpdateCredentials(email, token);
}
void NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes(
const syncable::ModelTypeSet& enabled_types) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
invalidation_notifier_->UpdateEnabledTypes(enabled_types);
}
void NonBlockingInvalidationNotifier::Core::OnIncomingNotification(
const syncable::ModelTypePayloadMap& type_payloads) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
observers_->Notify(&SyncNotifierObserver::OnIncomingNotification,
type_payloads);
}
void NonBlockingInvalidationNotifier::Core::OnNotificationStateChange(
bool notifications_enabled) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
observers_->Notify(&SyncNotifierObserver::OnNotificationStateChange,
notifications_enabled);
}
void NonBlockingInvalidationNotifier::Core::StoreState(
const std::string& state) {
DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
observers_->Notify(&SyncNotifierObserver::StoreState, state);
}
NonBlockingInvalidationNotifier::NonBlockingInvalidationNotifier(
const notifier::NotifierOptions& notifier_options,
const InvalidationVersionMap& initial_max_invalidation_versions,
const browser_sync::WeakHandle<InvalidationVersionTracker>&
invalidation_version_tracker,
const std::string& client_info)
: core_(new Core),
parent_message_loop_proxy_(
base::MessageLoopProxy::current()),
io_message_loop_proxy_(notifier_options.request_context_getter->
GetIOMessageLoopProxy()) {
if (!io_message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(
&NonBlockingInvalidationNotifier::Core::Initialize,
core_.get(),
notifier_options,
initial_max_invalidation_versions,
invalidation_version_tracker,
client_info))) {
NOTREACHED();
}
}
NonBlockingInvalidationNotifier::~NonBlockingInvalidationNotifier() {
DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
if (!io_message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&NonBlockingInvalidationNotifier::Core::Teardown,
core_.get()))) {
NOTREACHED();
}
}
void NonBlockingInvalidationNotifier::AddObserver(
SyncNotifierObserver* observer) {
DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
core_->AddObserver(observer);
}
void NonBlockingInvalidationNotifier::RemoveObserver(
SyncNotifierObserver* observer) {
DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
core_->RemoveObserver(observer);
}
void NonBlockingInvalidationNotifier::SetUniqueId(
const std::string& unique_id) {
DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
if (!io_message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&NonBlockingInvalidationNotifier::Core::SetUniqueId,
core_.get(), unique_id))) {
NOTREACHED();
}
}
void NonBlockingInvalidationNotifier::SetState(const std::string& state) {
DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
if (!io_message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&NonBlockingInvalidationNotifier::Core::SetState,
core_.get(), state))) {
NOTREACHED();
}
}
void NonBlockingInvalidationNotifier::UpdateCredentials(
const std::string& email, const std::string& token) {
DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
if (!io_message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateCredentials,
core_.get(), email, token))) {
NOTREACHED();
}
}
void NonBlockingInvalidationNotifier::UpdateEnabledTypes(
const syncable::ModelTypeSet& enabled_types) {
DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
if (!io_message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&NonBlockingInvalidationNotifier::Core::UpdateEnabledTypes,
core_.get(), enabled_types))) {
NOTREACHED();
}
}
void NonBlockingInvalidationNotifier::SendNotification(
const syncable::ModelTypeSet& changed_types) {
DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread());
// InvalidationClient doesn't implement SendNotification(), so no
// need to forward on the call.
}
} // namespace sync_notifier
|