| 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 const base::ListValue* invalidation_map_list = NULL; | 130 const base::ListValue* invalidation_map_list = NULL; |
| 131 if (!data_dict->GetList(kIdInvalidationMapKey, &invalidation_map_list) || | 131 if (!data_dict->GetList(kIdInvalidationMapKey, &invalidation_map_list) || |
| 132 !ObjectIdInvalidationMapFromValue(*invalidation_map_list, | 132 !ObjectIdInvalidationMapFromValue(*invalidation_map_list, |
| 133 &invalidation_map_)) { | 133 &invalidation_map_)) { |
| 134 LOG(WARNING) << "Could not parse " << kIdInvalidationMapKey; | 134 LOG(WARNING) << "Could not parse " << kIdInvalidationMapKey; |
| 135 } | 135 } |
| 136 return true; | 136 return true; |
| 137 } | 137 } |
| 138 | 138 |
| 139 P2PInvalidator::P2PInvalidator(scoped_ptr<notifier::PushClient> push_client, | 139 P2PInvalidator::P2PInvalidator(scoped_ptr<notifier::PushClient> push_client, |
| 140 const std::string& invalidator_client_id, |
| 140 P2PNotificationTarget send_notification_target) | 141 P2PNotificationTarget send_notification_target) |
| 141 : push_client_(push_client.Pass()), | 142 : push_client_(push_client.Pass()), |
| 143 invalidator_client_id_(invalidator_client_id), |
| 142 logged_in_(false), | 144 logged_in_(false), |
| 143 notifications_enabled_(false), | 145 notifications_enabled_(false), |
| 144 send_notification_target_(send_notification_target) { | 146 send_notification_target_(send_notification_target) { |
| 145 DCHECK(send_notification_target_ == NOTIFY_OTHERS || | 147 DCHECK(send_notification_target_ == NOTIFY_OTHERS || |
| 146 send_notification_target_ == NOTIFY_ALL); | 148 send_notification_target_ == NOTIFY_ALL); |
| 147 push_client_->AddObserver(this); | 149 push_client_->AddObserver(this); |
| 148 } | 150 } |
| 149 | 151 |
| 150 P2PInvalidator::~P2PInvalidator() { | 152 P2PInvalidator::~P2PInvalidator() { |
| 151 DCHECK(thread_checker_.CalledOnValidThread()); | 153 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 162 // TODO(akalin): Handle arbitrary object IDs (http://crbug.com/140411). | 164 // TODO(akalin): Handle arbitrary object IDs (http://crbug.com/140411). |
| 163 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
| 164 ObjectIdSet new_ids; | 166 ObjectIdSet new_ids; |
| 165 const ObjectIdSet& old_ids = registrar_.GetRegisteredIds(handler); | 167 const ObjectIdSet& old_ids = registrar_.GetRegisteredIds(handler); |
| 166 std::set_difference(ids.begin(), ids.end(), | 168 std::set_difference(ids.begin(), ids.end(), |
| 167 old_ids.begin(), old_ids.end(), | 169 old_ids.begin(), old_ids.end(), |
| 168 std::inserter(new_ids, new_ids.end()), | 170 std::inserter(new_ids, new_ids.end()), |
| 169 ObjectIdLessThan()); | 171 ObjectIdLessThan()); |
| 170 registrar_.UpdateRegisteredIds(handler, ids); | 172 registrar_.UpdateRegisteredIds(handler, ids); |
| 171 const P2PNotificationData notification_data( | 173 const P2PNotificationData notification_data( |
| 172 unique_id_, NOTIFY_SELF, ObjectIdSetToInvalidationMap(new_ids, "")); | 174 invalidator_client_id_, NOTIFY_SELF, |
| 175 ObjectIdSetToInvalidationMap(new_ids, "")); |
| 173 SendNotificationData(notification_data); | 176 SendNotificationData(notification_data); |
| 174 } | 177 } |
| 175 | 178 |
| 176 void P2PInvalidator::UnregisterHandler(InvalidationHandler* handler) { | 179 void P2PInvalidator::UnregisterHandler(InvalidationHandler* handler) { |
| 177 DCHECK(thread_checker_.CalledOnValidThread()); | 180 DCHECK(thread_checker_.CalledOnValidThread()); |
| 178 registrar_.UnregisterHandler(handler); | 181 registrar_.UnregisterHandler(handler); |
| 179 } | 182 } |
| 180 | 183 |
| 181 void P2PInvalidator::Acknowledge(const invalidation::ObjectId& id, | 184 void P2PInvalidator::Acknowledge(const invalidation::ObjectId& id, |
| 182 const AckHandle& ack_handle) { | 185 const AckHandle& ack_handle) { |
| 183 DCHECK(thread_checker_.CalledOnValidThread()); | 186 DCHECK(thread_checker_.CalledOnValidThread()); |
| 184 // Do nothing for the P2P implementation. | 187 // Do nothing for the P2P implementation. |
| 185 } | 188 } |
| 186 | 189 |
| 187 InvalidatorState P2PInvalidator::GetInvalidatorState() const { | 190 InvalidatorState P2PInvalidator::GetInvalidatorState() const { |
| 188 DCHECK(thread_checker_.CalledOnValidThread()); | 191 DCHECK(thread_checker_.CalledOnValidThread()); |
| 189 return registrar_.GetInvalidatorState(); | 192 return registrar_.GetInvalidatorState(); |
| 190 } | 193 } |
| 191 | 194 |
| 192 void P2PInvalidator::SetUniqueId(const std::string& unique_id) { | |
| 193 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 194 unique_id_ = unique_id; | |
| 195 } | |
| 196 | |
| 197 void P2PInvalidator::UpdateCredentials( | 195 void P2PInvalidator::UpdateCredentials( |
| 198 const std::string& email, const std::string& token) { | 196 const std::string& email, const std::string& token) { |
| 199 DCHECK(thread_checker_.CalledOnValidThread()); | 197 DCHECK(thread_checker_.CalledOnValidThread()); |
| 200 notifier::Subscription subscription; | 198 notifier::Subscription subscription; |
| 201 subscription.channel = kSyncP2PNotificationChannel; | 199 subscription.channel = kSyncP2PNotificationChannel; |
| 202 // There may be some subtle issues around case sensitivity of the | 200 // There may be some subtle issues around case sensitivity of the |
| 203 // from field, but it doesn't matter too much since this is only | 201 // from field, but it doesn't matter too much since this is only |
| 204 // used in p2p mode (which is only used in testing). | 202 // used in p2p mode (which is only used in testing). |
| 205 subscription.from = email; | 203 subscription.from = email; |
| 206 push_client_->UpdateSubscriptions( | 204 push_client_->UpdateSubscriptions( |
| 207 notifier::SubscriptionList(1, subscription)); | 205 notifier::SubscriptionList(1, subscription)); |
| 208 // If already logged in, the new credentials will take effect on the | 206 // If already logged in, the new credentials will take effect on the |
| 209 // next reconnection. | 207 // next reconnection. |
| 210 push_client_->UpdateCredentials(email, token); | 208 push_client_->UpdateCredentials(email, token); |
| 211 logged_in_ = true; | 209 logged_in_ = true; |
| 212 } | 210 } |
| 213 | 211 |
| 214 void P2PInvalidator::SendInvalidation( | 212 void P2PInvalidator::SendInvalidation( |
| 215 const ObjectIdInvalidationMap& invalidation_map) { | 213 const ObjectIdInvalidationMap& invalidation_map) { |
| 216 DCHECK(thread_checker_.CalledOnValidThread()); | 214 DCHECK(thread_checker_.CalledOnValidThread()); |
| 217 const P2PNotificationData notification_data( | 215 const P2PNotificationData notification_data( |
| 218 unique_id_, send_notification_target_, invalidation_map); | 216 invalidator_client_id_, send_notification_target_, invalidation_map); |
| 219 SendNotificationData(notification_data); | 217 SendNotificationData(notification_data); |
| 220 } | 218 } |
| 221 | 219 |
| 222 void P2PInvalidator::OnNotificationsEnabled() { | 220 void P2PInvalidator::OnNotificationsEnabled() { |
| 223 DCHECK(thread_checker_.CalledOnValidThread()); | 221 DCHECK(thread_checker_.CalledOnValidThread()); |
| 224 bool just_turned_on = (notifications_enabled_ == false); | 222 bool just_turned_on = (notifications_enabled_ == false); |
| 225 notifications_enabled_ = true; | 223 notifications_enabled_ = true; |
| 226 registrar_.UpdateInvalidatorState(INVALIDATIONS_ENABLED); | 224 registrar_.UpdateInvalidatorState(INVALIDATIONS_ENABLED); |
| 227 if (just_turned_on) { | 225 if (just_turned_on) { |
| 228 const P2PNotificationData notification_data( | 226 const P2PNotificationData notification_data( |
| 229 unique_id_, NOTIFY_SELF, | 227 invalidator_client_id_, NOTIFY_SELF, |
| 230 ObjectIdSetToInvalidationMap(registrar_.GetAllRegisteredIds(), "")); | 228 ObjectIdSetToInvalidationMap(registrar_.GetAllRegisteredIds(), "")); |
| 231 SendNotificationData(notification_data); | 229 SendNotificationData(notification_data); |
| 232 } | 230 } |
| 233 } | 231 } |
| 234 | 232 |
| 235 void P2PInvalidator::OnNotificationsDisabled( | 233 void P2PInvalidator::OnNotificationsDisabled( |
| 236 notifier::NotificationsDisabledReason reason) { | 234 notifier::NotificationsDisabledReason reason) { |
| 237 DCHECK(thread_checker_.CalledOnValidThread()); | 235 DCHECK(thread_checker_.CalledOnValidThread()); |
| 238 registrar_.UpdateInvalidatorState(FromNotifierReason(reason)); | 236 registrar_.UpdateInvalidatorState(FromNotifierReason(reason)); |
| 239 } | 237 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 253 if (notification.channel != kSyncP2PNotificationChannel) { | 251 if (notification.channel != kSyncP2PNotificationChannel) { |
| 254 LOG(WARNING) << "Notification from unexpected source " | 252 LOG(WARNING) << "Notification from unexpected source " |
| 255 << notification.channel; | 253 << notification.channel; |
| 256 } | 254 } |
| 257 P2PNotificationData notification_data; | 255 P2PNotificationData notification_data; |
| 258 if (!notification_data.ResetFromString(notification.data)) { | 256 if (!notification_data.ResetFromString(notification.data)) { |
| 259 LOG(WARNING) << "Could not parse notification data from " | 257 LOG(WARNING) << "Could not parse notification data from " |
| 260 << notification.data; | 258 << notification.data; |
| 261 notification_data = | 259 notification_data = |
| 262 P2PNotificationData( | 260 P2PNotificationData( |
| 263 unique_id_, NOTIFY_ALL, | 261 invalidator_client_id_, NOTIFY_ALL, |
| 264 ObjectIdSetToInvalidationMap(registrar_.GetAllRegisteredIds(), "")); | 262 ObjectIdSetToInvalidationMap(registrar_.GetAllRegisteredIds(), "")); |
| 265 } | 263 } |
| 266 if (!notification_data.IsTargeted(unique_id_)) { | 264 if (!notification_data.IsTargeted(invalidator_client_id_)) { |
| 267 DVLOG(1) << "Not a target of the notification -- " | 265 DVLOG(1) << "Not a target of the notification -- " |
| 268 << "not emitting notification"; | 266 << "not emitting notification"; |
| 269 return; | 267 return; |
| 270 } | 268 } |
| 271 registrar_.DispatchInvalidationsToHandlers( | 269 registrar_.DispatchInvalidationsToHandlers( |
| 272 notification_data.GetIdInvalidationMap()); | 270 notification_data.GetIdInvalidationMap()); |
| 273 } | 271 } |
| 274 | 272 |
| 275 void P2PInvalidator::SendNotificationDataForTest( | 273 void P2PInvalidator::SendNotificationDataForTest( |
| 276 const P2PNotificationData& notification_data) { | 274 const P2PNotificationData& notification_data) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 287 return; | 285 return; |
| 288 } | 286 } |
| 289 notifier::Notification notification; | 287 notifier::Notification notification; |
| 290 notification.channel = kSyncP2PNotificationChannel; | 288 notification.channel = kSyncP2PNotificationChannel; |
| 291 notification.data = notification_data.ToString(); | 289 notification.data = notification_data.ToString(); |
| 292 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 290 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
| 293 push_client_->SendNotification(notification); | 291 push_client_->SendNotification(notification); |
| 294 } | 292 } |
| 295 | 293 |
| 296 } // namespace syncer | 294 } // namespace syncer |
| OLD | NEW |