OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/notifier/chrome_invalidation_client.h" | 5 #include "sync/notifier/chrome_invalidation_client.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
13 #include "google/cacheinvalidation/include/invalidation-client-factory.h" | |
14 #include "google/cacheinvalidation/include/invalidation-client.h" | 14 #include "google/cacheinvalidation/include/invalidation-client.h" |
15 #include "google/cacheinvalidation/include/types.h" | 15 #include "google/cacheinvalidation/include/types.h" |
16 #include "google/cacheinvalidation/types.pb.h" | 16 #include "google/cacheinvalidation/types.pb.h" |
17 #include "jingle/notifier/listener/push_client.h" | 17 #include "jingle/notifier/listener/push_client.h" |
18 #include "sync/notifier/invalidation_util.h" | 18 #include "sync/notifier/invalidation_util.h" |
19 #include "sync/notifier/registration_manager.h" | 19 #include "sync/notifier/registration_manager.h" |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 const char kApplicationName[] = "chrome-sync"; | 23 const char kApplicationName[] = "chrome-sync"; |
(...skipping 17 matching lines...) Expand all Loading... |
41 } | 41 } |
42 | 42 |
43 ChromeInvalidationClient::~ChromeInvalidationClient() { | 43 ChromeInvalidationClient::~ChromeInvalidationClient() { |
44 DCHECK(CalledOnValidThread()); | 44 DCHECK(CalledOnValidThread()); |
45 push_client_->RemoveObserver(this); | 45 push_client_->RemoveObserver(this); |
46 Stop(); | 46 Stop(); |
47 DCHECK(!listener_); | 47 DCHECK(!listener_); |
48 } | 48 } |
49 | 49 |
50 void ChromeInvalidationClient::Start( | 50 void ChromeInvalidationClient::Start( |
| 51 const CreateInvalidationClientCallback& |
| 52 create_invalidation_client_callback, |
51 const std::string& client_id, const std::string& client_info, | 53 const std::string& client_id, const std::string& client_info, |
52 const std::string& state, | 54 const std::string& state, |
53 const InvalidationVersionMap& initial_max_invalidation_versions, | 55 const InvalidationVersionMap& initial_max_invalidation_versions, |
54 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, | 56 const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker, |
55 Listener* listener) { | 57 Listener* listener) { |
56 DCHECK(CalledOnValidThread()); | 58 DCHECK(CalledOnValidThread()); |
57 Stop(); | 59 Stop(); |
58 | 60 |
59 chrome_system_resources_.set_platform(client_info); | 61 chrome_system_resources_.set_platform(client_info); |
60 chrome_system_resources_.Start(); | 62 chrome_system_resources_.Start(); |
(...skipping 17 matching lines...) Expand all Loading... |
78 } | 80 } |
79 invalidation_state_tracker_ = invalidation_state_tracker; | 81 invalidation_state_tracker_ = invalidation_state_tracker; |
80 DCHECK(invalidation_state_tracker_.IsInitialized()); | 82 DCHECK(invalidation_state_tracker_.IsInitialized()); |
81 | 83 |
82 DCHECK(!listener_); | 84 DCHECK(!listener_); |
83 DCHECK(listener); | 85 DCHECK(listener); |
84 listener_ = listener; | 86 listener_ = listener; |
85 | 87 |
86 int client_type = ipc::invalidation::ClientType::CHROME_SYNC; | 88 int client_type = ipc::invalidation::ClientType::CHROME_SYNC; |
87 invalidation_client_.reset( | 89 invalidation_client_.reset( |
88 invalidation::CreateInvalidationClient( | 90 create_invalidation_client_callback.Run( |
89 &chrome_system_resources_, client_type, client_id, | 91 &chrome_system_resources_, client_type, client_id, |
90 kApplicationName, this)); | 92 kApplicationName, this)); |
91 invalidation_client_->Start(); | 93 invalidation_client_->Start(); |
92 | 94 |
93 registration_manager_.reset( | 95 registration_manager_.reset( |
94 new RegistrationManager(invalidation_client_.get())); | 96 new RegistrationManager(invalidation_client_.get())); |
95 } | 97 } |
96 | 98 |
97 void ChromeInvalidationClient::UpdateCredentials( | 99 void ChromeInvalidationClient::UpdateCredentials( |
98 const std::string& email, const std::string& token) { | 100 const std::string& email, const std::string& token) { |
99 DCHECK(CalledOnValidThread()); | 101 DCHECK(CalledOnValidThread()); |
100 chrome_system_resources_.network()->UpdateCredentials(email, token); | 102 chrome_system_resources_.network()->UpdateCredentials(email, token); |
101 } | 103 } |
102 | 104 |
103 void ChromeInvalidationClient::RegisterIds(const ObjectIdSet& ids) { | 105 void ChromeInvalidationClient::UpdateRegisteredIds(const ObjectIdSet& ids) { |
104 DCHECK(CalledOnValidThread()); | 106 DCHECK(CalledOnValidThread()); |
105 registered_ids_ = ids; | 107 registered_ids_ = ids; |
106 // |ticl_state_| can go to NO_NOTIFICATION_ERROR even without a | 108 // |ticl_state_| can go to NO_NOTIFICATION_ERROR even without a |
107 // working XMPP connection (as observed by us), so check it instead | 109 // working XMPP connection (as observed by us), so check it instead |
108 // of GetState() (see http://crbug.com/139424). | 110 // of GetState() (see http://crbug.com/139424). |
109 if (ticl_state_ == NO_NOTIFICATION_ERROR && registration_manager_.get()) { | 111 if (ticl_state_ == NO_NOTIFICATION_ERROR && registration_manager_.get()) { |
110 registration_manager_->SetRegisteredIds(registered_ids_); | 112 registration_manager_->UpdateRegisteredIds(registered_ids_); |
111 } | 113 } |
112 // TODO(akalin): Clear invalidation versions for unregistered types. | 114 // TODO(akalin): Clear invalidation versions for unregistered types. |
113 } | 115 } |
114 | 116 |
115 void ChromeInvalidationClient::Ready( | 117 void ChromeInvalidationClient::Ready( |
116 invalidation::InvalidationClient* client) { | 118 invalidation::InvalidationClient* client) { |
| 119 DCHECK(CalledOnValidThread()); |
| 120 DCHECK_EQ(client, invalidation_client_.get()); |
117 ticl_state_ = NO_NOTIFICATION_ERROR; | 121 ticl_state_ = NO_NOTIFICATION_ERROR; |
118 EmitStateChange(); | 122 EmitStateChange(); |
119 registration_manager_->SetRegisteredIds(registered_ids_); | 123 registration_manager_->UpdateRegisteredIds(registered_ids_); |
120 } | 124 } |
121 | 125 |
122 void ChromeInvalidationClient::Invalidate( | 126 void ChromeInvalidationClient::Invalidate( |
123 invalidation::InvalidationClient* client, | 127 invalidation::InvalidationClient* client, |
124 const invalidation::Invalidation& invalidation, | 128 const invalidation::Invalidation& invalidation, |
125 const invalidation::AckHandle& ack_handle) { | 129 const invalidation::AckHandle& ack_handle) { |
126 DCHECK(CalledOnValidThread()); | 130 DCHECK(CalledOnValidThread()); |
| 131 DCHECK_EQ(client, invalidation_client_.get()); |
127 DVLOG(1) << "Invalidate: " << InvalidationToString(invalidation); | 132 DVLOG(1) << "Invalidate: " << InvalidationToString(invalidation); |
128 | 133 |
129 const invalidation::ObjectId& id = invalidation.object_id(); | 134 const invalidation::ObjectId& id = invalidation.object_id(); |
130 | 135 |
131 // The invalidation API spec allows for the possibility of redundant | 136 // The invalidation API spec allows for the possibility of redundant |
132 // invalidations, so keep track of the max versions and drop | 137 // invalidations, so keep track of the max versions and drop |
133 // invalidations with old versions. | 138 // invalidations with old versions. |
134 // | 139 // |
135 // TODO(akalin): Now that we keep track of registered ids, we | 140 // TODO(akalin): Now that we keep track of registered ids, we |
136 // should drop invalidations for unregistered ids. We may also | 141 // should drop invalidations for unregistered ids. We may also |
(...skipping 26 matching lines...) Expand all Loading... |
163 // TODO(akalin): We should really acknowledge only after we get the | 168 // TODO(akalin): We should really acknowledge only after we get the |
164 // updates from the sync server. (see http://crbug.com/78462). | 169 // updates from the sync server. (see http://crbug.com/78462). |
165 client->Acknowledge(ack_handle); | 170 client->Acknowledge(ack_handle); |
166 } | 171 } |
167 | 172 |
168 void ChromeInvalidationClient::InvalidateUnknownVersion( | 173 void ChromeInvalidationClient::InvalidateUnknownVersion( |
169 invalidation::InvalidationClient* client, | 174 invalidation::InvalidationClient* client, |
170 const invalidation::ObjectId& object_id, | 175 const invalidation::ObjectId& object_id, |
171 const invalidation::AckHandle& ack_handle) { | 176 const invalidation::AckHandle& ack_handle) { |
172 DCHECK(CalledOnValidThread()); | 177 DCHECK(CalledOnValidThread()); |
| 178 DCHECK_EQ(client, invalidation_client_.get()); |
173 DVLOG(1) << "InvalidateUnknownVersion"; | 179 DVLOG(1) << "InvalidateUnknownVersion"; |
174 | 180 |
175 ObjectIdPayloadMap id_payloads; | 181 ObjectIdPayloadMap id_payloads; |
176 id_payloads[object_id] = std::string(); | 182 id_payloads[object_id] = std::string(); |
177 EmitInvalidation(id_payloads); | 183 EmitInvalidation(id_payloads); |
178 // TODO(akalin): We should really acknowledge only after we get the | 184 // TODO(akalin): We should really acknowledge only after we get the |
179 // updates from the sync server. (see http://crbug.com/78462). | 185 // updates from the sync server. (see http://crbug.com/78462). |
180 client->Acknowledge(ack_handle); | 186 client->Acknowledge(ack_handle); |
181 } | 187 } |
182 | 188 |
183 // This should behave as if we got an invalidation with version | 189 // This should behave as if we got an invalidation with version |
184 // UNKNOWN_OBJECT_VERSION for all known data types. | 190 // UNKNOWN_OBJECT_VERSION for all known data types. |
185 void ChromeInvalidationClient::InvalidateAll( | 191 void ChromeInvalidationClient::InvalidateAll( |
186 invalidation::InvalidationClient* client, | 192 invalidation::InvalidationClient* client, |
187 const invalidation::AckHandle& ack_handle) { | 193 const invalidation::AckHandle& ack_handle) { |
188 DCHECK(CalledOnValidThread()); | 194 DCHECK(CalledOnValidThread()); |
| 195 DCHECK_EQ(client, invalidation_client_.get()); |
189 DVLOG(1) << "InvalidateAll"; | 196 DVLOG(1) << "InvalidateAll"; |
190 | 197 |
191 ObjectIdPayloadMap id_payloads; | 198 ObjectIdPayloadMap id_payloads; |
192 for (ObjectIdSet::const_iterator it = registered_ids_.begin(); | 199 for (ObjectIdSet::const_iterator it = registered_ids_.begin(); |
193 it != registered_ids_.end(); ++it) { | 200 it != registered_ids_.end(); ++it) { |
194 id_payloads[*it] = std::string(); | 201 id_payloads[*it] = std::string(); |
195 } | 202 } |
196 EmitInvalidation(id_payloads); | 203 EmitInvalidation(id_payloads); |
197 // TODO(akalin): We should really acknowledge only after we get the | 204 // TODO(akalin): We should really acknowledge only after we get the |
198 // updates from the sync server. (see http://crbug.com/76482). | 205 // updates from the sync server. (see http://crbug.com/76482). |
199 client->Acknowledge(ack_handle); | 206 client->Acknowledge(ack_handle); |
200 } | 207 } |
201 | 208 |
202 void ChromeInvalidationClient::EmitInvalidation( | 209 void ChromeInvalidationClient::EmitInvalidation( |
203 const ObjectIdPayloadMap& id_payloads) { | 210 const ObjectIdPayloadMap& id_payloads) { |
204 DCHECK(CalledOnValidThread()); | 211 DCHECK(CalledOnValidThread()); |
205 listener_->OnInvalidate(id_payloads); | 212 listener_->OnInvalidate(id_payloads); |
206 } | 213 } |
207 | 214 |
208 void ChromeInvalidationClient::InformRegistrationStatus( | 215 void ChromeInvalidationClient::InformRegistrationStatus( |
209 invalidation::InvalidationClient* client, | 216 invalidation::InvalidationClient* client, |
210 const invalidation::ObjectId& object_id, | 217 const invalidation::ObjectId& object_id, |
211 InvalidationListener::RegistrationState new_state) { | 218 InvalidationListener::RegistrationState new_state) { |
212 DCHECK(CalledOnValidThread()); | 219 DCHECK(CalledOnValidThread()); |
| 220 DCHECK_EQ(client, invalidation_client_.get()); |
213 DVLOG(1) << "InformRegistrationStatus: " | 221 DVLOG(1) << "InformRegistrationStatus: " |
214 << ObjectIdToString(object_id) << " " << new_state; | 222 << ObjectIdToString(object_id) << " " << new_state; |
215 | 223 |
216 if (new_state != InvalidationListener::REGISTERED) { | 224 if (new_state != InvalidationListener::REGISTERED) { |
217 // Let |registration_manager_| handle the registration backoff policy. | 225 // Let |registration_manager_| handle the registration backoff policy. |
218 registration_manager_->MarkRegistrationLost(object_id); | 226 registration_manager_->MarkRegistrationLost(object_id); |
219 } | 227 } |
220 } | 228 } |
221 | 229 |
222 void ChromeInvalidationClient::InformRegistrationFailure( | 230 void ChromeInvalidationClient::InformRegistrationFailure( |
223 invalidation::InvalidationClient* client, | 231 invalidation::InvalidationClient* client, |
224 const invalidation::ObjectId& object_id, | 232 const invalidation::ObjectId& object_id, |
225 bool is_transient, | 233 bool is_transient, |
226 const std::string& error_message) { | 234 const std::string& error_message) { |
227 DCHECK(CalledOnValidThread()); | 235 DCHECK(CalledOnValidThread()); |
| 236 DCHECK_EQ(client, invalidation_client_.get()); |
228 DVLOG(1) << "InformRegistrationFailure: " | 237 DVLOG(1) << "InformRegistrationFailure: " |
229 << ObjectIdToString(object_id) | 238 << ObjectIdToString(object_id) |
230 << "is_transient=" << is_transient | 239 << "is_transient=" << is_transient |
231 << ", message=" << error_message; | 240 << ", message=" << error_message; |
232 | 241 |
233 if (is_transient) { | 242 if (is_transient) { |
234 // We don't care about |unknown_hint|; we let | 243 // We don't care about |unknown_hint|; we let |
235 // |registration_manager_| handle the registration backoff policy. | 244 // |registration_manager_| handle the registration backoff policy. |
236 registration_manager_->MarkRegistrationLost(object_id); | 245 registration_manager_->MarkRegistrationLost(object_id); |
237 } else { | 246 } else { |
238 // Non-transient failures are permanent, so block any future | 247 // Non-transient failures are permanent, so block any future |
239 // registration requests for |model_type|. (This happens if the | 248 // registration requests for |model_type|. (This happens if the |
240 // server doesn't recognize the data type, which could happen for | 249 // server doesn't recognize the data type, which could happen for |
241 // brand-new data types.) | 250 // brand-new data types.) |
242 registration_manager_->DisableId(object_id); | 251 registration_manager_->DisableId(object_id); |
243 } | 252 } |
244 } | 253 } |
245 | 254 |
246 void ChromeInvalidationClient::ReissueRegistrations( | 255 void ChromeInvalidationClient::ReissueRegistrations( |
247 invalidation::InvalidationClient* client, | 256 invalidation::InvalidationClient* client, |
248 const std::string& prefix, | 257 const std::string& prefix, |
249 int prefix_length) { | 258 int prefix_length) { |
250 DCHECK(CalledOnValidThread()); | 259 DCHECK(CalledOnValidThread()); |
| 260 DCHECK_EQ(client, invalidation_client_.get()); |
251 DVLOG(1) << "AllRegistrationsLost"; | 261 DVLOG(1) << "AllRegistrationsLost"; |
252 registration_manager_->MarkAllRegistrationsLost(); | 262 registration_manager_->MarkAllRegistrationsLost(); |
253 } | 263 } |
254 | 264 |
255 void ChromeInvalidationClient::InformError( | 265 void ChromeInvalidationClient::InformError( |
256 invalidation::InvalidationClient* client, | 266 invalidation::InvalidationClient* client, |
257 const invalidation::ErrorInfo& error_info) { | 267 const invalidation::ErrorInfo& error_info) { |
| 268 DCHECK(CalledOnValidThread()); |
| 269 DCHECK_EQ(client, invalidation_client_.get()); |
258 LOG(ERROR) << "Ticl error " << error_info.error_reason() << ": " | 270 LOG(ERROR) << "Ticl error " << error_info.error_reason() << ": " |
259 << error_info.error_message() | 271 << error_info.error_message() |
260 << " (transient = " << error_info.is_transient() << ")"; | 272 << " (transient = " << error_info.is_transient() << ")"; |
261 if (error_info.error_reason() == invalidation::ErrorReason::AUTH_FAILURE) { | 273 if (error_info.error_reason() == invalidation::ErrorReason::AUTH_FAILURE) { |
262 ticl_state_ = NOTIFICATION_CREDENTIALS_REJECTED; | 274 ticl_state_ = NOTIFICATION_CREDENTIALS_REJECTED; |
263 } else { | 275 } else { |
264 ticl_state_ = TRANSIENT_NOTIFICATION_ERROR; | 276 ticl_state_ = TRANSIENT_NOTIFICATION_ERROR; |
265 } | 277 } |
266 EmitStateChange(); | 278 EmitStateChange(); |
267 } | 279 } |
268 | 280 |
269 void ChromeInvalidationClient::WriteState(const std::string& state) { | 281 void ChromeInvalidationClient::WriteState(const std::string& state) { |
270 DCHECK(CalledOnValidThread()); | 282 DCHECK(CalledOnValidThread()); |
271 DVLOG(1) << "WriteState"; | 283 DVLOG(1) << "WriteState"; |
272 invalidation_state_tracker_.Call( | 284 invalidation_state_tracker_.Call( |
273 FROM_HERE, &InvalidationStateTracker::SetInvalidationState, state); | 285 FROM_HERE, &InvalidationStateTracker::SetInvalidationState, state); |
274 } | 286 } |
275 | 287 |
| 288 void ChromeInvalidationClient::StopForTest() { |
| 289 DCHECK(CalledOnValidThread()); |
| 290 Stop(); |
| 291 } |
| 292 |
276 void ChromeInvalidationClient::Stop() { | 293 void ChromeInvalidationClient::Stop() { |
277 DCHECK(CalledOnValidThread()); | 294 DCHECK(CalledOnValidThread()); |
278 if (!invalidation_client_.get()) { | 295 if (!invalidation_client_.get()) { |
279 return; | 296 return; |
280 } | 297 } |
281 | 298 |
282 registration_manager_.reset(); | 299 registration_manager_.reset(); |
283 chrome_system_resources_.Stop(); | 300 chrome_system_resources_.Stop(); |
284 invalidation_client_->Stop(); | 301 invalidation_client_->Stop(); |
285 | 302 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 EmitStateChange(); | 349 EmitStateChange(); |
333 } | 350 } |
334 | 351 |
335 void ChromeInvalidationClient::OnIncomingNotification( | 352 void ChromeInvalidationClient::OnIncomingNotification( |
336 const notifier::Notification& notification) { | 353 const notifier::Notification& notification) { |
337 DCHECK(CalledOnValidThread()); | 354 DCHECK(CalledOnValidThread()); |
338 // Do nothing, since this is already handled by |invalidation_client_|. | 355 // Do nothing, since this is already handled by |invalidation_client_|. |
339 } | 356 } |
340 | 357 |
341 } // namespace syncer | 358 } // namespace syncer |
OLD | NEW |