| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/sync/notifier/p2p_notifier.h" | 5 #include "chrome/browser/sync/notifier/p2p_notifier.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 } | 58 } |
| 59 LOG(WARNING) << "Could not parse " << target_str; | 59 LOG(WARNING) << "Could not parse " << target_str; |
| 60 return NOTIFY_SELF; | 60 return NOTIFY_SELF; |
| 61 } | 61 } |
| 62 | 62 |
| 63 P2PNotificationData::P2PNotificationData() : target_(NOTIFY_SELF) {} | 63 P2PNotificationData::P2PNotificationData() : target_(NOTIFY_SELF) {} |
| 64 | 64 |
| 65 P2PNotificationData::P2PNotificationData( | 65 P2PNotificationData::P2PNotificationData( |
| 66 const std::string& sender_id, | 66 const std::string& sender_id, |
| 67 P2PNotificationTarget target, | 67 P2PNotificationTarget target, |
| 68 const syncable::ModelTypeSet& changed_types) | 68 syncable::ModelEnumSet changed_types) |
| 69 : sender_id_(sender_id), | 69 : sender_id_(sender_id), |
| 70 target_(target), | 70 target_(target), |
| 71 changed_types_(changed_types) {} | 71 changed_types_(changed_types) {} |
| 72 | 72 |
| 73 P2PNotificationData::~P2PNotificationData() {} | 73 P2PNotificationData::~P2PNotificationData() {} |
| 74 | 74 |
| 75 bool P2PNotificationData::IsTargeted(const std::string& id) const { | 75 bool P2PNotificationData::IsTargeted(const std::string& id) const { |
| 76 switch (target_) { | 76 switch (target_) { |
| 77 case NOTIFY_SELF: | 77 case NOTIFY_SELF: |
| 78 return sender_id_ == id; | 78 return sender_id_ == id; |
| 79 case NOTIFY_OTHERS: | 79 case NOTIFY_OTHERS: |
| 80 return sender_id_ != id; | 80 return sender_id_ != id; |
| 81 case NOTIFY_ALL: | 81 case NOTIFY_ALL: |
| 82 return true; | 82 return true; |
| 83 default: | 83 default: |
| 84 NOTREACHED(); | 84 NOTREACHED(); |
| 85 return false; | 85 return false; |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 | 88 |
| 89 const syncable::ModelTypeSet& P2PNotificationData::GetChangedTypes() const { | 89 syncable::ModelEnumSet P2PNotificationData::GetChangedTypes() const { |
| 90 return changed_types_; | 90 return changed_types_; |
| 91 } | 91 } |
| 92 | 92 |
| 93 bool P2PNotificationData::Equals(const P2PNotificationData& other) const { | 93 bool P2PNotificationData::Equals(const P2PNotificationData& other) const { |
| 94 return | 94 return |
| 95 (sender_id_ == other.sender_id_) && | 95 (sender_id_ == other.sender_id_) && |
| 96 (target_ == other.target_) && | 96 (target_ == other.target_) && |
| 97 (changed_types_ == other.changed_types_); | 97 changed_types_.Equals(other.changed_types_); |
| 98 } | 98 } |
| 99 | 99 |
| 100 std::string P2PNotificationData::ToString() const { | 100 std::string P2PNotificationData::ToString() const { |
| 101 scoped_ptr<DictionaryValue> dict(new DictionaryValue()); | 101 scoped_ptr<DictionaryValue> dict(new DictionaryValue()); |
| 102 dict->SetString(kSenderIdKey, sender_id_); | 102 dict->SetString(kSenderIdKey, sender_id_); |
| 103 dict->SetString(kNotificationTypeKey, | 103 dict->SetString(kNotificationTypeKey, |
| 104 P2PNotificationTargetToString(target_)); | 104 P2PNotificationTargetToString(target_)); |
| 105 dict->Set(kChangedTypesKey, syncable::ModelTypeSetToValue(changed_types_)); | 105 dict->Set(kChangedTypesKey, syncable::ModelEnumSetToValue(changed_types_)); |
| 106 std::string json; | 106 std::string json; |
| 107 base::JSONWriter::Write(dict.get(), false /* pretty_print */, &json); | 107 base::JSONWriter::Write(dict.get(), false /* pretty_print */, &json); |
| 108 return json; | 108 return json; |
| 109 } | 109 } |
| 110 | 110 |
| 111 bool P2PNotificationData::ResetFromString(const std::string& str) { | 111 bool P2PNotificationData::ResetFromString(const std::string& str) { |
| 112 scoped_ptr<Value> data_value( | 112 scoped_ptr<Value> data_value( |
| 113 base::JSONReader::Read(str, false /* allow_trailing_comma */)); | 113 base::JSONReader::Read(str, false /* allow_trailing_comma */)); |
| 114 if (!data_value.get()) { | 114 if (!data_value.get()) { |
| 115 LOG(WARNING) << "Could not parse " << str; | 115 LOG(WARNING) << "Could not parse " << str; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 131 LOG(WARNING) << "Could not find string value for " | 131 LOG(WARNING) << "Could not find string value for " |
| 132 << kNotificationTypeKey; | 132 << kNotificationTypeKey; |
| 133 } | 133 } |
| 134 target_ = P2PNotificationTargetFromString(target_str); | 134 target_ = P2PNotificationTargetFromString(target_str); |
| 135 ListValue* changed_types_list = NULL; | 135 ListValue* changed_types_list = NULL; |
| 136 if (!data_dict->GetList(kChangedTypesKey, &changed_types_list)) { | 136 if (!data_dict->GetList(kChangedTypesKey, &changed_types_list)) { |
| 137 LOG(WARNING) << "Could not find list value for " | 137 LOG(WARNING) << "Could not find list value for " |
| 138 << kChangedTypesKey; | 138 << kChangedTypesKey; |
| 139 return false; | 139 return false; |
| 140 } | 140 } |
| 141 changed_types_ = syncable::ModelTypeSetFromValue(*changed_types_list); | 141 changed_types_ = syncable::ModelEnumSetFromValue(*changed_types_list); |
| 142 return true; | 142 return true; |
| 143 } | 143 } |
| 144 | 144 |
| 145 P2PNotifier::P2PNotifier(notifier::TalkMediator* talk_mediator, | 145 P2PNotifier::P2PNotifier(notifier::TalkMediator* talk_mediator, |
| 146 P2PNotificationTarget send_notification_target) | 146 P2PNotificationTarget send_notification_target) |
| 147 : talk_mediator_(talk_mediator), | 147 : talk_mediator_(talk_mediator), |
| 148 logged_in_(false), | 148 logged_in_(false), |
| 149 notifications_enabled_(false), | 149 notifications_enabled_(false), |
| 150 send_notification_target_(send_notification_target), | 150 send_notification_target_(send_notification_target), |
| 151 parent_message_loop_proxy_( | 151 parent_message_loop_proxy_( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 // from field, but it doesn't matter too much since this is only | 205 // from field, but it doesn't matter too much since this is only |
| 206 // used in p2p mode (which is only used in testing). | 206 // used in p2p mode (which is only used in testing). |
| 207 subscription.from = email; | 207 subscription.from = email; |
| 208 talk_mediator_->AddSubscription(subscription); | 208 talk_mediator_->AddSubscription(subscription); |
| 209 | 209 |
| 210 logged_in_ = true; | 210 logged_in_ = true; |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 | 213 |
| 214 void P2PNotifier::UpdateEnabledTypes( | 214 void P2PNotifier::UpdateEnabledTypes( |
| 215 const syncable::ModelTypeSet& enabled_types) { | 215 syncable::ModelEnumSet enabled_types) { |
| 216 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); | 216 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); |
| 217 syncable::ModelTypeSet new_enabled_types; | 217 const syncable::ModelEnumSet new_enabled_types = |
| 218 std::set_difference(enabled_types.begin(), enabled_types.end(), | 218 Difference(enabled_types, enabled_types_); |
| 219 enabled_types_.begin(), enabled_types_.end(), | |
| 220 std::inserter(new_enabled_types, | |
| 221 new_enabled_types.end())); | |
| 222 enabled_types_ = enabled_types; | 219 enabled_types_ = enabled_types; |
| 223 const P2PNotificationData notification_data( | 220 const P2PNotificationData notification_data( |
| 224 unique_id_, NOTIFY_SELF, new_enabled_types); | 221 unique_id_, NOTIFY_SELF, new_enabled_types); |
| 225 SendNotificationData(notification_data); | 222 SendNotificationData(notification_data); |
| 226 } | 223 } |
| 227 | 224 |
| 228 void P2PNotifier::SendNotification( | 225 void P2PNotifier::SendNotification( |
| 229 const syncable::ModelTypeSet& changed_types) { | 226 syncable::ModelEnumSet changed_types) { |
| 230 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); | 227 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); |
| 231 const P2PNotificationData notification_data( | 228 const P2PNotificationData notification_data( |
| 232 unique_id_, send_notification_target_, changed_types); | 229 unique_id_, send_notification_target_, changed_types); |
| 233 SendNotificationData(notification_data); | 230 SendNotificationData(notification_data); |
| 234 } | 231 } |
| 235 | 232 |
| 236 void P2PNotifier::OnNotificationStateChange(bool notifications_enabled) { | 233 void P2PNotifier::OnNotificationStateChange(bool notifications_enabled) { |
| 237 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); | 234 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); |
| 238 bool disabled_to_enabled = notifications_enabled && !notifications_enabled_; | 235 bool disabled_to_enabled = notifications_enabled && !notifications_enabled_; |
| 239 notifications_enabled_ = notifications_enabled; | 236 notifications_enabled_ = notifications_enabled; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 267 LOG(WARNING) << "Could not parse notification data from " | 264 LOG(WARNING) << "Could not parse notification data from " |
| 268 << notification.data; | 265 << notification.data; |
| 269 notification_data = | 266 notification_data = |
| 270 P2PNotificationData(unique_id_, NOTIFY_ALL, enabled_types_); | 267 P2PNotificationData(unique_id_, NOTIFY_ALL, enabled_types_); |
| 271 } | 268 } |
| 272 if (!notification_data.IsTargeted(unique_id_)) { | 269 if (!notification_data.IsTargeted(unique_id_)) { |
| 273 DVLOG(1) << "Not a target of the notification -- " | 270 DVLOG(1) << "Not a target of the notification -- " |
| 274 << "not emitting notification"; | 271 << "not emitting notification"; |
| 275 return; | 272 return; |
| 276 } | 273 } |
| 277 if (notification_data.GetChangedTypes().empty()) { | 274 if (notification_data.GetChangedTypes().Empty()) { |
| 278 DVLOG(1) << "No changed types -- not emitting notification"; | 275 DVLOG(1) << "No changed types -- not emitting notification"; |
| 279 return; | 276 return; |
| 280 } | 277 } |
| 281 const syncable::ModelTypePayloadMap& type_payloads = | 278 const syncable::ModelTypePayloadMap& type_payloads = |
| 282 syncable::ModelTypePayloadMapFromBitSet( | 279 syncable::ModelTypePayloadMapFromEnumSet( |
| 283 syncable::ModelTypeBitSetFromSet( | 280 notification_data.GetChangedTypes(), std::string()); |
| 284 notification_data.GetChangedTypes()), std::string()); | |
| 285 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | 281 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, |
| 286 OnIncomingNotification(type_payloads)); | 282 OnIncomingNotification(type_payloads)); |
| 287 } | 283 } |
| 288 | 284 |
| 289 void P2PNotifier::OnOutgoingNotification() {} | 285 void P2PNotifier::OnOutgoingNotification() {} |
| 290 | 286 |
| 291 void P2PNotifier::SendNotificationDataForTest( | 287 void P2PNotifier::SendNotificationDataForTest( |
| 292 const P2PNotificationData& notification_data) { | 288 const P2PNotificationData& notification_data) { |
| 293 SendNotificationData(notification_data); | 289 SendNotificationData(notification_data); |
| 294 } | 290 } |
| 295 | 291 |
| 296 void P2PNotifier::SendNotificationData( | 292 void P2PNotifier::SendNotificationData( |
| 297 const P2PNotificationData& notification_data) { | 293 const P2PNotificationData& notification_data) { |
| 298 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); | 294 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); |
| 299 notifier::Notification notification; | 295 notifier::Notification notification; |
| 300 notification.channel = kSyncP2PNotificationChannel; | 296 notification.channel = kSyncP2PNotificationChannel; |
| 301 notification.data = notification_data.ToString(); | 297 notification.data = notification_data.ToString(); |
| 302 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 298 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
| 303 talk_mediator_->SendNotification(notification); | 299 talk_mediator_->SendNotification(notification); |
| 304 } | 300 } |
| 305 | 301 |
| 306 } // namespace sync_notifier | 302 } // namespace sync_notifier |
| OLD | NEW |