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/p2p_invalidator.h" | 5 #include "sync/notifier/p2p_invalidator.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 if (target_str == kNotifyOthers) { | 54 if (target_str == kNotifyOthers) { |
55 return NOTIFY_OTHERS; | 55 return NOTIFY_OTHERS; |
56 } | 56 } |
57 if (target_str == kNotifyAll) { | 57 if (target_str == kNotifyAll) { |
58 return NOTIFY_ALL; | 58 return NOTIFY_ALL; |
59 } | 59 } |
60 LOG(WARNING) << "Could not parse " << target_str; | 60 LOG(WARNING) << "Could not parse " << target_str; |
61 return NOTIFY_SELF; | 61 return NOTIFY_SELF; |
62 } | 62 } |
63 | 63 |
64 IncomingNotificationSource P2PNotificationSourceFromInteger(int source_num) { | 64 IncomingInvalidationSource P2PNotificationSourceFromInteger(int source_num) { |
65 if (source_num == LOCAL_NOTIFICATION) { | 65 if (source_num == LOCAL_INVALIDATION) { |
66 return LOCAL_NOTIFICATION; | 66 return LOCAL_INVALIDATION; |
67 } | 67 } |
68 return REMOTE_NOTIFICATION; | 68 return REMOTE_INVALIDATION; |
69 } | 69 } |
70 | 70 |
71 P2PNotificationData::P2PNotificationData() | 71 P2PNotificationData::P2PNotificationData() |
72 : target_(NOTIFY_SELF), source_(REMOTE_NOTIFICATION) {} | 72 : target_(NOTIFY_SELF), source_(REMOTE_INVALIDATION) {} |
73 | 73 |
74 P2PNotificationData::P2PNotificationData( | 74 P2PNotificationData::P2PNotificationData( |
75 const std::string& sender_id, | 75 const std::string& sender_id, |
76 P2PNotificationTarget target, | 76 P2PNotificationTarget target, |
77 const ObjectIdStateMap& id_state_map, | 77 const ObjectIdStateMap& id_state_map, |
78 IncomingNotificationSource source) | 78 IncomingInvalidationSource source) |
79 : sender_id_(sender_id), | 79 : sender_id_(sender_id), |
80 target_(target), | 80 target_(target), |
81 id_state_map_(id_state_map), | 81 id_state_map_(id_state_map), |
82 source_(source) {} | 82 source_(source) {} |
83 | 83 |
84 P2PNotificationData::~P2PNotificationData() {} | 84 P2PNotificationData::~P2PNotificationData() {} |
85 | 85 |
86 bool P2PNotificationData::IsTargeted(const std::string& id) const { | 86 bool P2PNotificationData::IsTargeted(const std::string& id) const { |
87 switch (target_) { | 87 switch (target_) { |
88 case NOTIFY_SELF: | 88 case NOTIFY_SELF: |
89 return sender_id_ == id; | 89 return sender_id_ == id; |
90 case NOTIFY_OTHERS: | 90 case NOTIFY_OTHERS: |
91 return sender_id_ != id; | 91 return sender_id_ != id; |
92 case NOTIFY_ALL: | 92 case NOTIFY_ALL: |
93 return true; | 93 return true; |
94 default: | 94 default: |
95 NOTREACHED(); | 95 NOTREACHED(); |
96 return false; | 96 return false; |
97 } | 97 } |
98 } | 98 } |
99 | 99 |
100 const ObjectIdStateMap& P2PNotificationData::GetIdStateMap() const { | 100 const ObjectIdStateMap& P2PNotificationData::GetIdStateMap() const { |
101 return id_state_map_; | 101 return id_state_map_; |
102 } | 102 } |
103 | 103 |
104 IncomingNotificationSource P2PNotificationData::GetSource() const { | 104 IncomingInvalidationSource P2PNotificationData::GetSource() const { |
105 return source_; | 105 return source_; |
106 } | 106 } |
107 | 107 |
108 bool P2PNotificationData::Equals(const P2PNotificationData& other) const { | 108 bool P2PNotificationData::Equals(const P2PNotificationData& other) const { |
109 return | 109 return |
110 (sender_id_ == other.sender_id_) && | 110 (sender_id_ == other.sender_id_) && |
111 (target_ == other.target_) && | 111 (target_ == other.target_) && |
112 ObjectIdStateMapEquals(id_state_map_, other.id_state_map_) && | 112 ObjectIdStateMapEquals(id_state_map_, other.id_state_map_) && |
113 (source_ == other.source_); | 113 (source_ == other.source_); |
114 } | 114 } |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 DCHECK(thread_checker_.CalledOnValidThread()); | 181 DCHECK(thread_checker_.CalledOnValidThread()); |
182 ObjectIdSet new_ids; | 182 ObjectIdSet new_ids; |
183 const ObjectIdSet& old_ids = registrar_.GetRegisteredIds(handler); | 183 const ObjectIdSet& old_ids = registrar_.GetRegisteredIds(handler); |
184 std::set_difference(ids.begin(), ids.end(), | 184 std::set_difference(ids.begin(), ids.end(), |
185 old_ids.begin(), old_ids.end(), | 185 old_ids.begin(), old_ids.end(), |
186 std::inserter(new_ids, new_ids.end()), | 186 std::inserter(new_ids, new_ids.end()), |
187 ObjectIdLessThan()); | 187 ObjectIdLessThan()); |
188 registrar_.UpdateRegisteredIds(handler, ids); | 188 registrar_.UpdateRegisteredIds(handler, ids); |
189 const P2PNotificationData notification_data( | 189 const P2PNotificationData notification_data( |
190 unique_id_, NOTIFY_SELF, ObjectIdSetToStateMap(new_ids, ""), | 190 unique_id_, NOTIFY_SELF, ObjectIdSetToStateMap(new_ids, ""), |
191 REMOTE_NOTIFICATION); | 191 REMOTE_INVALIDATION); |
192 SendNotificationData(notification_data); | 192 SendNotificationData(notification_data); |
193 } | 193 } |
194 | 194 |
195 void P2PInvalidator::UnregisterHandler(InvalidationHandler* handler) { | 195 void P2PInvalidator::UnregisterHandler(InvalidationHandler* handler) { |
196 DCHECK(thread_checker_.CalledOnValidThread()); | 196 DCHECK(thread_checker_.CalledOnValidThread()); |
197 registrar_.UnregisterHandler(handler); | 197 registrar_.UnregisterHandler(handler); |
198 } | 198 } |
199 | 199 |
| 200 InvalidatorState P2PInvalidator::GetInvalidatorState() const { |
| 201 DCHECK(thread_checker_.CalledOnValidThread()); |
| 202 return registrar_.GetInvalidatorState(); |
| 203 } |
| 204 |
200 void P2PInvalidator::SetUniqueId(const std::string& unique_id) { | 205 void P2PInvalidator::SetUniqueId(const std::string& unique_id) { |
201 DCHECK(thread_checker_.CalledOnValidThread()); | 206 DCHECK(thread_checker_.CalledOnValidThread()); |
202 unique_id_ = unique_id; | 207 unique_id_ = unique_id; |
203 } | 208 } |
204 | 209 |
205 void P2PInvalidator::SetStateDeprecated(const std::string& state) { | 210 void P2PInvalidator::SetStateDeprecated(const std::string& state) { |
206 DCHECK(thread_checker_.CalledOnValidThread()); | 211 DCHECK(thread_checker_.CalledOnValidThread()); |
207 // Do nothing. | 212 // Do nothing. |
208 } | 213 } |
209 | 214 |
210 void P2PInvalidator::UpdateCredentials( | 215 void P2PInvalidator::UpdateCredentials( |
211 const std::string& email, const std::string& token) { | 216 const std::string& email, const std::string& token) { |
212 DCHECK(thread_checker_.CalledOnValidThread()); | 217 DCHECK(thread_checker_.CalledOnValidThread()); |
213 notifier::Subscription subscription; | 218 notifier::Subscription subscription; |
214 subscription.channel = kSyncP2PNotificationChannel; | 219 subscription.channel = kSyncP2PNotificationChannel; |
215 // There may be some subtle issues around case sensitivity of the | 220 // There may be some subtle issues around case sensitivity of the |
216 // from field, but it doesn't matter too much since this is only | 221 // from field, but it doesn't matter too much since this is only |
217 // used in p2p mode (which is only used in testing). | 222 // used in p2p mode (which is only used in testing). |
218 subscription.from = email; | 223 subscription.from = email; |
219 push_client_->UpdateSubscriptions( | 224 push_client_->UpdateSubscriptions( |
220 notifier::SubscriptionList(1, subscription)); | 225 notifier::SubscriptionList(1, subscription)); |
221 // If already logged in, the new credentials will take effect on the | 226 // If already logged in, the new credentials will take effect on the |
222 // next reconnection. | 227 // next reconnection. |
223 push_client_->UpdateCredentials(email, token); | 228 push_client_->UpdateCredentials(email, token); |
224 logged_in_ = true; | 229 logged_in_ = true; |
225 } | 230 } |
226 | 231 |
227 void P2PInvalidator::SendNotification(const ObjectIdStateMap& id_state_map) { | 232 void P2PInvalidator::SendInvalidation(const ObjectIdStateMap& id_state_map) { |
228 DCHECK(thread_checker_.CalledOnValidThread()); | 233 DCHECK(thread_checker_.CalledOnValidThread()); |
229 const P2PNotificationData notification_data( | 234 const P2PNotificationData notification_data( |
230 unique_id_, send_notification_target_, id_state_map, | 235 unique_id_, send_notification_target_, id_state_map, |
231 REMOTE_NOTIFICATION); | 236 REMOTE_INVALIDATION); |
232 SendNotificationData(notification_data); | 237 SendNotificationData(notification_data); |
233 } | 238 } |
234 | 239 |
235 void P2PInvalidator::OnNotificationsEnabled() { | 240 void P2PInvalidator::OnNotificationsEnabled() { |
236 DCHECK(thread_checker_.CalledOnValidThread()); | 241 DCHECK(thread_checker_.CalledOnValidThread()); |
237 bool just_turned_on = (notifications_enabled_ == false); | 242 bool just_turned_on = (notifications_enabled_ == false); |
238 notifications_enabled_ = true; | 243 notifications_enabled_ = true; |
239 registrar_.EmitOnNotificationsEnabled(); | 244 registrar_.UpdateInvalidatorState(INVALIDATIONS_ENABLED); |
240 if (just_turned_on) { | 245 if (just_turned_on) { |
241 const P2PNotificationData notification_data( | 246 const P2PNotificationData notification_data( |
242 unique_id_, NOTIFY_SELF, | 247 unique_id_, NOTIFY_SELF, |
243 ObjectIdSetToStateMap(registrar_.GetAllRegisteredIds(), ""), | 248 ObjectIdSetToStateMap(registrar_.GetAllRegisteredIds(), ""), |
244 REMOTE_NOTIFICATION); | 249 REMOTE_INVALIDATION); |
245 SendNotificationData(notification_data); | 250 SendNotificationData(notification_data); |
246 } | 251 } |
247 } | 252 } |
248 | 253 |
249 void P2PInvalidator::OnNotificationsDisabled( | 254 void P2PInvalidator::OnNotificationsDisabled( |
250 notifier::NotificationsDisabledReason reason) { | 255 notifier::NotificationsDisabledReason reason) { |
251 DCHECK(thread_checker_.CalledOnValidThread()); | 256 DCHECK(thread_checker_.CalledOnValidThread()); |
252 registrar_.EmitOnNotificationsDisabled(FromNotifierReason(reason)); | 257 registrar_.UpdateInvalidatorState(FromNotifierReason(reason)); |
253 } | 258 } |
254 | 259 |
255 void P2PInvalidator::OnIncomingNotification( | 260 void P2PInvalidator::OnIncomingNotification( |
256 const notifier::Notification& notification) { | 261 const notifier::Notification& notification) { |
257 DCHECK(thread_checker_.CalledOnValidThread()); | 262 DCHECK(thread_checker_.CalledOnValidThread()); |
258 DVLOG(1) << "Received notification " << notification.ToString(); | 263 DVLOG(1) << "Received notification " << notification.ToString(); |
259 if (!logged_in_) { | 264 if (!logged_in_) { |
260 DVLOG(1) << "Not logged in yet -- not emitting notification"; | 265 DVLOG(1) << "Not logged in yet -- not emitting notification"; |
261 return; | 266 return; |
262 } | 267 } |
263 if (!notifications_enabled_) { | 268 if (!notifications_enabled_) { |
264 DVLOG(1) << "Notifications not on -- not emitting notification"; | 269 DVLOG(1) << "Notifications not on -- not emitting notification"; |
265 return; | 270 return; |
266 } | 271 } |
267 if (notification.channel != kSyncP2PNotificationChannel) { | 272 if (notification.channel != kSyncP2PNotificationChannel) { |
268 LOG(WARNING) << "Notification from unexpected source " | 273 LOG(WARNING) << "Notification from unexpected source " |
269 << notification.channel; | 274 << notification.channel; |
270 } | 275 } |
271 P2PNotificationData notification_data; | 276 P2PNotificationData notification_data; |
272 if (!notification_data.ResetFromString(notification.data)) { | 277 if (!notification_data.ResetFromString(notification.data)) { |
273 LOG(WARNING) << "Could not parse notification data from " | 278 LOG(WARNING) << "Could not parse notification data from " |
274 << notification.data; | 279 << notification.data; |
275 notification_data = | 280 notification_data = |
276 P2PNotificationData( | 281 P2PNotificationData( |
277 unique_id_, NOTIFY_ALL, | 282 unique_id_, NOTIFY_ALL, |
278 ObjectIdSetToStateMap(registrar_.GetAllRegisteredIds(), ""), | 283 ObjectIdSetToStateMap(registrar_.GetAllRegisteredIds(), ""), |
279 REMOTE_NOTIFICATION); | 284 REMOTE_INVALIDATION); |
280 } | 285 } |
281 if (!notification_data.IsTargeted(unique_id_)) { | 286 if (!notification_data.IsTargeted(unique_id_)) { |
282 DVLOG(1) << "Not a target of the notification -- " | 287 DVLOG(1) << "Not a target of the notification -- " |
283 << "not emitting notification"; | 288 << "not emitting notification"; |
284 return; | 289 return; |
285 } | 290 } |
286 registrar_.DispatchInvalidationsToHandlers( | 291 registrar_.DispatchInvalidationsToHandlers( |
287 notification_data.GetIdStateMap(), | 292 notification_data.GetIdStateMap(), |
288 REMOTE_NOTIFICATION); | 293 REMOTE_INVALIDATION); |
289 } | 294 } |
290 | 295 |
291 void P2PInvalidator::SendNotificationDataForTest( | 296 void P2PInvalidator::SendNotificationDataForTest( |
292 const P2PNotificationData& notification_data) { | 297 const P2PNotificationData& notification_data) { |
293 DCHECK(thread_checker_.CalledOnValidThread()); | 298 DCHECK(thread_checker_.CalledOnValidThread()); |
294 SendNotificationData(notification_data); | 299 SendNotificationData(notification_data); |
295 } | 300 } |
296 | 301 |
297 void P2PInvalidator::SendNotificationData( | 302 void P2PInvalidator::SendNotificationData( |
298 const P2PNotificationData& notification_data) { | 303 const P2PNotificationData& notification_data) { |
299 DCHECK(thread_checker_.CalledOnValidThread()); | 304 DCHECK(thread_checker_.CalledOnValidThread()); |
300 if (notification_data.GetIdStateMap().empty()) { | 305 if (notification_data.GetIdStateMap().empty()) { |
301 DVLOG(1) << "Not sending XMPP notification with empty state map: " | 306 DVLOG(1) << "Not sending XMPP notification with empty state map: " |
302 << notification_data.ToString(); | 307 << notification_data.ToString(); |
303 return; | 308 return; |
304 } | 309 } |
305 notifier::Notification notification; | 310 notifier::Notification notification; |
306 notification.channel = kSyncP2PNotificationChannel; | 311 notification.channel = kSyncP2PNotificationChannel; |
307 notification.data = notification_data.ToString(); | 312 notification.data = notification_data.ToString(); |
308 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 313 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
309 push_client_->SendNotification(notification); | 314 push_client_->SendNotification(notification); |
310 } | 315 } |
311 | 316 |
312 } // namespace syncer | 317 } // namespace syncer |
OLD | NEW |