| 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 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "jingle/notifier/listener/push_client.h" | 14 #include "jingle/notifier/listener/push_client.h" |
| 15 #include "sync/notifier/invalidation_handler.h" | 15 #include "sync/notifier/invalidation_handler.h" |
| 16 #include "sync/notifier/invalidation_util.h" | 16 #include "sync/notifier/invalidation_util.h" |
| 17 #include "sync/notifier/object_id_invalidation_map.h" |
| 17 | 18 |
| 18 namespace syncer { | 19 namespace syncer { |
| 19 | 20 |
| 20 const char kSyncP2PNotificationChannel[] = "http://www.google.com/chrome/sync"; | 21 const char kSyncP2PNotificationChannel[] = "http://www.google.com/chrome/sync"; |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 const char kNotifySelf[] = "notifySelf"; | 25 const char kNotifySelf[] = "notifySelf"; |
| 25 const char kNotifyOthers[] = "notifyOthers"; | 26 const char kNotifyOthers[] = "notifyOthers"; |
| 26 const char kNotifyAll[] = "notifyAll"; | 27 const char kNotifyAll[] = "notifyAll"; |
| 27 | 28 |
| 28 const char kSenderIdKey[] = "senderId"; | 29 const char kSenderIdKey[] = "senderId"; |
| 29 const char kNotificationTypeKey[] = "notificationType"; | 30 const char kNotificationTypeKey[] = "notificationType"; |
| 30 const char kIdInvalidationMapKey[] = "idInvalidationMap"; | 31 const char kInvalidationsKey[] = "invalidations"; |
| 31 | 32 |
| 32 } // namespace | 33 } // namespace |
| 33 | 34 |
| 34 std::string P2PNotificationTargetToString(P2PNotificationTarget target) { | 35 std::string P2PNotificationTargetToString(P2PNotificationTarget target) { |
| 35 switch (target) { | 36 switch (target) { |
| 36 case NOTIFY_SELF: | 37 case NOTIFY_SELF: |
| 37 return kNotifySelf; | 38 return kNotifySelf; |
| 38 case NOTIFY_OTHERS: | 39 case NOTIFY_OTHERS: |
| 39 return kNotifyOthers; | 40 return kNotifyOthers; |
| 40 case NOTIFY_ALL: | 41 case NOTIFY_ALL: |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 | 90 |
| 90 const ObjectIdInvalidationMap& | 91 const ObjectIdInvalidationMap& |
| 91 P2PNotificationData::GetIdInvalidationMap() const { | 92 P2PNotificationData::GetIdInvalidationMap() const { |
| 92 return invalidation_map_; | 93 return invalidation_map_; |
| 93 } | 94 } |
| 94 | 95 |
| 95 bool P2PNotificationData::Equals(const P2PNotificationData& other) const { | 96 bool P2PNotificationData::Equals(const P2PNotificationData& other) const { |
| 96 return | 97 return |
| 97 (sender_id_ == other.sender_id_) && | 98 (sender_id_ == other.sender_id_) && |
| 98 (target_ == other.target_) && | 99 (target_ == other.target_) && |
| 99 ObjectIdInvalidationMapEquals(invalidation_map_, | 100 (invalidation_map_ == other.invalidation_map_); |
| 100 other.invalidation_map_); | |
| 101 } | 101 } |
| 102 | 102 |
| 103 std::string P2PNotificationData::ToString() const { | 103 std::string P2PNotificationData::ToString() const { |
| 104 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 104 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 105 dict->SetString(kSenderIdKey, sender_id_); | 105 dict->SetString(kSenderIdKey, sender_id_); |
| 106 dict->SetString(kNotificationTypeKey, | 106 dict->SetString(kNotificationTypeKey, |
| 107 P2PNotificationTargetToString(target_)); | 107 P2PNotificationTargetToString(target_)); |
| 108 dict->Set(kIdInvalidationMapKey, | 108 dict->Set(kInvalidationsKey, invalidation_map_.ToValue().release()); |
| 109 ObjectIdInvalidationMapToValue(invalidation_map_).release()); | |
| 110 std::string json; | 109 std::string json; |
| 111 base::JSONWriter::Write(dict.get(), &json); | 110 base::JSONWriter::Write(dict.get(), &json); |
| 112 return json; | 111 return json; |
| 113 } | 112 } |
| 114 | 113 |
| 115 bool P2PNotificationData::ResetFromString(const std::string& str) { | 114 bool P2PNotificationData::ResetFromString(const std::string& str) { |
| 116 scoped_ptr<base::Value> data_value(base::JSONReader::Read(str)); | 115 scoped_ptr<base::Value> data_value(base::JSONReader::Read(str)); |
| 117 const base::DictionaryValue* data_dict = NULL; | 116 const base::DictionaryValue* data_dict = NULL; |
| 118 if (!data_value.get() || !data_value->GetAsDictionary(&data_dict)) { | 117 if (!data_value.get() || !data_value->GetAsDictionary(&data_dict)) { |
| 119 LOG(WARNING) << "Could not parse " << str << " as a dictionary"; | 118 LOG(WARNING) << "Could not parse " << str << " as a dictionary"; |
| 120 return false; | 119 return false; |
| 121 } | 120 } |
| 122 if (!data_dict->GetString(kSenderIdKey, &sender_id_)) { | 121 if (!data_dict->GetString(kSenderIdKey, &sender_id_)) { |
| 123 LOG(WARNING) << "Could not find string value for " << kSenderIdKey; | 122 LOG(WARNING) << "Could not find string value for " << kSenderIdKey; |
| 124 } | 123 } |
| 125 std::string target_str; | 124 std::string target_str; |
| 126 if (!data_dict->GetString(kNotificationTypeKey, &target_str)) { | 125 if (!data_dict->GetString(kNotificationTypeKey, &target_str)) { |
| 127 LOG(WARNING) << "Could not find string value for " | 126 LOG(WARNING) << "Could not find string value for " |
| 128 << kNotificationTypeKey; | 127 << kNotificationTypeKey; |
| 129 } | 128 } |
| 130 target_ = P2PNotificationTargetFromString(target_str); | 129 target_ = P2PNotificationTargetFromString(target_str); |
| 131 const base::ListValue* invalidation_map_list = NULL; | 130 const base::ListValue* invalidation_map_list = NULL; |
| 132 if (!data_dict->GetList(kIdInvalidationMapKey, &invalidation_map_list) || | 131 if (!data_dict->GetList(kInvalidationsKey, &invalidation_map_list) || |
| 133 !ObjectIdInvalidationMapFromValue(*invalidation_map_list, | 132 !invalidation_map_.ResetFromValue(*invalidation_map_list)) { |
| 134 &invalidation_map_)) { | 133 LOG(WARNING) << "Could not parse " << kInvalidationsKey; |
| 135 LOG(WARNING) << "Could not parse " << kIdInvalidationMapKey; | |
| 136 } | 134 } |
| 137 return true; | 135 return true; |
| 138 } | 136 } |
| 139 | 137 |
| 140 P2PInvalidator::P2PInvalidator(scoped_ptr<notifier::PushClient> push_client, | 138 P2PInvalidator::P2PInvalidator(scoped_ptr<notifier::PushClient> push_client, |
| 141 const std::string& invalidator_client_id, | 139 const std::string& invalidator_client_id, |
| 142 P2PNotificationTarget send_notification_target) | 140 P2PNotificationTarget send_notification_target) |
| 143 : push_client_(push_client.Pass()), | 141 : push_client_(push_client.Pass()), |
| 144 invalidator_client_id_(invalidator_client_id), | 142 invalidator_client_id_(invalidator_client_id), |
| 145 logged_in_(false), | 143 logged_in_(false), |
| 146 notifications_enabled_(false), | 144 notifications_enabled_(false), |
| 147 send_notification_target_(send_notification_target) { | 145 send_notification_target_(send_notification_target) { |
| 148 DCHECK(send_notification_target_ == NOTIFY_OTHERS || | 146 DCHECK(send_notification_target_ == NOTIFY_OTHERS || |
| 149 send_notification_target_ == NOTIFY_ALL); | 147 send_notification_target_ == NOTIFY_ALL); |
| 150 push_client_->AddObserver(this); | 148 push_client_->AddObserver(this); |
| 151 } | 149 } |
| 152 | 150 |
| 153 P2PInvalidator::~P2PInvalidator() { | 151 P2PInvalidator::~P2PInvalidator() { |
| 154 DCHECK(thread_checker_.CalledOnValidThread()); | 152 DCHECK(thread_checker_.CalledOnValidThread()); |
| 155 push_client_->RemoveObserver(this); | 153 push_client_->RemoveObserver(this); |
| 156 } | 154 } |
| 157 | 155 |
| 158 void P2PInvalidator::RegisterHandler(InvalidationHandler* handler) { | 156 void P2PInvalidator::RegisterHandler(InvalidationHandler* handler) { |
| 159 DCHECK(thread_checker_.CalledOnValidThread()); | 157 DCHECK(thread_checker_.CalledOnValidThread()); |
| 160 registrar_.RegisterHandler(handler); | 158 registrar_.RegisterHandler(handler); |
| 161 } | 159 } |
| 162 | 160 |
| 163 void P2PInvalidator::UpdateRegisteredIds(InvalidationHandler* handler, | 161 void P2PInvalidator::UpdateRegisteredIds(InvalidationHandler* handler, |
| 164 const ObjectIdSet& ids) { | 162 const ObjectIdSet& ids) { |
| 165 // TODO(akalin): Handle arbitrary object IDs (http://crbug.com/140411). | |
| 166 DCHECK(thread_checker_.CalledOnValidThread()); | 163 DCHECK(thread_checker_.CalledOnValidThread()); |
| 167 ObjectIdSet new_ids; | 164 ObjectIdSet new_ids; |
| 168 const ObjectIdSet& old_ids = registrar_.GetRegisteredIds(handler); | 165 const ObjectIdSet& old_ids = registrar_.GetRegisteredIds(handler); |
| 169 std::set_difference(ids.begin(), ids.end(), | 166 std::set_difference(ids.begin(), ids.end(), |
| 170 old_ids.begin(), old_ids.end(), | 167 old_ids.begin(), old_ids.end(), |
| 171 std::inserter(new_ids, new_ids.end()), | 168 std::inserter(new_ids, new_ids.end()), |
| 172 ObjectIdLessThan()); | 169 ObjectIdLessThan()); |
| 173 registrar_.UpdateRegisteredIds(handler, ids); | 170 registrar_.UpdateRegisteredIds(handler, ids); |
| 174 const P2PNotificationData notification_data( | 171 const P2PNotificationData notification_data( |
| 175 invalidator_client_id_, | 172 invalidator_client_id_, |
| 176 NOTIFY_SELF, | 173 send_notification_target_, |
| 177 ObjectIdSetToInvalidationMap(new_ids, | 174 ObjectIdInvalidationMap::InvalidateAll(ids)); |
| 178 Invalidation::kUnknownVersion, | |
| 179 std::string())); | |
| 180 SendNotificationData(notification_data); | 175 SendNotificationData(notification_data); |
| 181 } | 176 } |
| 182 | 177 |
| 183 void P2PInvalidator::UnregisterHandler(InvalidationHandler* handler) { | 178 void P2PInvalidator::UnregisterHandler(InvalidationHandler* handler) { |
| 184 DCHECK(thread_checker_.CalledOnValidThread()); | 179 DCHECK(thread_checker_.CalledOnValidThread()); |
| 185 registrar_.UnregisterHandler(handler); | 180 registrar_.UnregisterHandler(handler); |
| 186 } | 181 } |
| 187 | 182 |
| 188 void P2PInvalidator::Acknowledge(const invalidation::ObjectId& id, | |
| 189 const AckHandle& ack_handle) { | |
| 190 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 191 // Do nothing for the P2P implementation. | |
| 192 } | |
| 193 | |
| 194 InvalidatorState P2PInvalidator::GetInvalidatorState() const { | 183 InvalidatorState P2PInvalidator::GetInvalidatorState() const { |
| 195 DCHECK(thread_checker_.CalledOnValidThread()); | 184 DCHECK(thread_checker_.CalledOnValidThread()); |
| 196 return registrar_.GetInvalidatorState(); | 185 return registrar_.GetInvalidatorState(); |
| 197 } | 186 } |
| 198 | 187 |
| 199 void P2PInvalidator::UpdateCredentials( | 188 void P2PInvalidator::UpdateCredentials( |
| 200 const std::string& email, const std::string& token) { | 189 const std::string& email, const std::string& token) { |
| 201 DCHECK(thread_checker_.CalledOnValidThread()); | 190 DCHECK(thread_checker_.CalledOnValidThread()); |
| 202 notifier::Subscription subscription; | 191 notifier::Subscription subscription; |
| 203 subscription.channel = kSyncP2PNotificationChannel; | 192 subscription.channel = kSyncP2PNotificationChannel; |
| 204 // There may be some subtle issues around case sensitivity of the | 193 // There may be some subtle issues around case sensitivity of the |
| 205 // from field, but it doesn't matter too much since this is only | 194 // from field, but it doesn't matter too much since this is only |
| 206 // used in p2p mode (which is only used in testing). | 195 // used in p2p mode (which is only used in testing). |
| 207 subscription.from = email; | 196 subscription.from = email; |
| 208 push_client_->UpdateSubscriptions( | 197 push_client_->UpdateSubscriptions( |
| 209 notifier::SubscriptionList(1, subscription)); | 198 notifier::SubscriptionList(1, subscription)); |
| 210 // If already logged in, the new credentials will take effect on the | 199 // If already logged in, the new credentials will take effect on the |
| 211 // next reconnection. | 200 // next reconnection. |
| 212 push_client_->UpdateCredentials(email, token); | 201 push_client_->UpdateCredentials(email, token); |
| 213 logged_in_ = true; | 202 logged_in_ = true; |
| 214 } | 203 } |
| 215 | 204 |
| 216 void P2PInvalidator::SendInvalidation( | 205 void P2PInvalidator::SendInvalidation(const ObjectIdSet& ids) { |
| 217 const ObjectIdInvalidationMap& invalidation_map) { | |
| 218 DCHECK(thread_checker_.CalledOnValidThread()); | 206 DCHECK(thread_checker_.CalledOnValidThread()); |
| 207 ObjectIdInvalidationMap invalidation_map = |
| 208 ObjectIdInvalidationMap::InvalidateAll(ids); |
| 219 const P2PNotificationData notification_data( | 209 const P2PNotificationData notification_data( |
| 220 invalidator_client_id_, send_notification_target_, invalidation_map); | 210 invalidator_client_id_, send_notification_target_, invalidation_map); |
| 221 SendNotificationData(notification_data); | 211 SendNotificationData(notification_data); |
| 222 } | 212 } |
| 223 | 213 |
| 224 void P2PInvalidator::OnNotificationsEnabled() { | 214 void P2PInvalidator::OnNotificationsEnabled() { |
| 225 DCHECK(thread_checker_.CalledOnValidThread()); | 215 DCHECK(thread_checker_.CalledOnValidThread()); |
| 226 bool just_turned_on = (notifications_enabled_ == false); | 216 bool just_turned_on = (notifications_enabled_ == false); |
| 227 notifications_enabled_ = true; | 217 notifications_enabled_ = true; |
| 228 registrar_.UpdateInvalidatorState(INVALIDATIONS_ENABLED); | 218 registrar_.UpdateInvalidatorState(INVALIDATIONS_ENABLED); |
| 229 if (just_turned_on) { | 219 if (just_turned_on) { |
| 230 const P2PNotificationData notification_data( | 220 const P2PNotificationData notification_data( |
| 231 invalidator_client_id_, | 221 invalidator_client_id_, |
| 232 NOTIFY_SELF, | 222 NOTIFY_SELF, |
| 233 ObjectIdSetToInvalidationMap(registrar_.GetAllRegisteredIds(), | 223 ObjectIdInvalidationMap::InvalidateAll( |
| 234 Invalidation::kUnknownVersion, | 224 registrar_.GetAllRegisteredIds())); |
| 235 std::string())); | |
| 236 SendNotificationData(notification_data); | 225 SendNotificationData(notification_data); |
| 237 } | 226 } |
| 238 } | 227 } |
| 239 | 228 |
| 240 void P2PInvalidator::OnNotificationsDisabled( | 229 void P2PInvalidator::OnNotificationsDisabled( |
| 241 notifier::NotificationsDisabledReason reason) { | 230 notifier::NotificationsDisabledReason reason) { |
| 242 DCHECK(thread_checker_.CalledOnValidThread()); | 231 DCHECK(thread_checker_.CalledOnValidThread()); |
| 243 registrar_.UpdateInvalidatorState(FromNotifierReason(reason)); | 232 registrar_.UpdateInvalidatorState(FromNotifierReason(reason)); |
| 244 } | 233 } |
| 245 | 234 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 259 LOG(WARNING) << "Notification from unexpected source " | 248 LOG(WARNING) << "Notification from unexpected source " |
| 260 << notification.channel; | 249 << notification.channel; |
| 261 } | 250 } |
| 262 P2PNotificationData notification_data; | 251 P2PNotificationData notification_data; |
| 263 if (!notification_data.ResetFromString(notification.data)) { | 252 if (!notification_data.ResetFromString(notification.data)) { |
| 264 LOG(WARNING) << "Could not parse notification data from " | 253 LOG(WARNING) << "Could not parse notification data from " |
| 265 << notification.data; | 254 << notification.data; |
| 266 notification_data = P2PNotificationData( | 255 notification_data = P2PNotificationData( |
| 267 invalidator_client_id_, | 256 invalidator_client_id_, |
| 268 NOTIFY_ALL, | 257 NOTIFY_ALL, |
| 269 ObjectIdSetToInvalidationMap(registrar_.GetAllRegisteredIds(), | 258 ObjectIdInvalidationMap::InvalidateAll( |
| 270 Invalidation::kUnknownVersion, | 259 registrar_.GetAllRegisteredIds())); |
| 271 std::string())); | |
| 272 } | 260 } |
| 273 if (!notification_data.IsTargeted(invalidator_client_id_)) { | 261 if (!notification_data.IsTargeted(invalidator_client_id_)) { |
| 274 DVLOG(1) << "Not a target of the notification -- " | 262 DVLOG(1) << "Not a target of the notification -- " |
| 275 << "not emitting notification"; | 263 << "not emitting notification"; |
| 276 return; | 264 return; |
| 277 } | 265 } |
| 278 registrar_.DispatchInvalidationsToHandlers( | 266 registrar_.DispatchInvalidationsToHandlers( |
| 279 notification_data.GetIdInvalidationMap()); | 267 notification_data.GetIdInvalidationMap()); |
| 280 } | 268 } |
| 281 | 269 |
| 282 void P2PInvalidator::SendNotificationDataForTest( | 270 void P2PInvalidator::SendNotificationDataForTest( |
| 283 const P2PNotificationData& notification_data) { | 271 const P2PNotificationData& notification_data) { |
| 284 DCHECK(thread_checker_.CalledOnValidThread()); | 272 DCHECK(thread_checker_.CalledOnValidThread()); |
| 285 SendNotificationData(notification_data); | 273 SendNotificationData(notification_data); |
| 286 } | 274 } |
| 287 | 275 |
| 288 void P2PInvalidator::SendNotificationData( | 276 void P2PInvalidator::SendNotificationData( |
| 289 const P2PNotificationData& notification_data) { | 277 const P2PNotificationData& notification_data) { |
| 290 DCHECK(thread_checker_.CalledOnValidThread()); | 278 DCHECK(thread_checker_.CalledOnValidThread()); |
| 291 if (notification_data.GetIdInvalidationMap().empty()) { | 279 if (notification_data.GetIdInvalidationMap().Empty()) { |
| 292 DVLOG(1) << "Not sending XMPP notification with empty state map: " | 280 DVLOG(1) << "Not sending XMPP notification with empty state map: " |
| 293 << notification_data.ToString(); | 281 << notification_data.ToString(); |
| 294 return; | 282 return; |
| 295 } | 283 } |
| 296 notifier::Notification notification; | 284 notifier::Notification notification; |
| 297 notification.channel = kSyncP2PNotificationChannel; | 285 notification.channel = kSyncP2PNotificationChannel; |
| 298 notification.data = notification_data.ToString(); | 286 notification.data = notification_data.ToString(); |
| 299 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 287 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
| 300 push_client_->SendNotification(notification); | 288 push_client_->SendNotification(notification); |
| 301 } | 289 } |
| 302 | 290 |
| 303 } // namespace syncer | 291 } // namespace syncer |
| OLD | NEW |