| 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_notifier.h" | 5 #include "sync/notifier/p2p_notifier.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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } | 57 } |
| 58 LOG(WARNING) << "Could not parse " << target_str; | 58 LOG(WARNING) << "Could not parse " << target_str; |
| 59 return NOTIFY_SELF; | 59 return NOTIFY_SELF; |
| 60 } | 60 } |
| 61 | 61 |
| 62 P2PNotificationData::P2PNotificationData() : target_(NOTIFY_SELF) {} | 62 P2PNotificationData::P2PNotificationData() : target_(NOTIFY_SELF) {} |
| 63 | 63 |
| 64 P2PNotificationData::P2PNotificationData( | 64 P2PNotificationData::P2PNotificationData( |
| 65 const std::string& sender_id, | 65 const std::string& sender_id, |
| 66 P2PNotificationTarget target, | 66 P2PNotificationTarget target, |
| 67 syncer::ModelTypeSet changed_types) | 67 ModelTypeSet changed_types) |
| 68 : sender_id_(sender_id), | 68 : sender_id_(sender_id), |
| 69 target_(target), | 69 target_(target), |
| 70 changed_types_(changed_types) {} | 70 changed_types_(changed_types) {} |
| 71 | 71 |
| 72 P2PNotificationData::~P2PNotificationData() {} | 72 P2PNotificationData::~P2PNotificationData() {} |
| 73 | 73 |
| 74 bool P2PNotificationData::IsTargeted(const std::string& id) const { | 74 bool P2PNotificationData::IsTargeted(const std::string& id) const { |
| 75 switch (target_) { | 75 switch (target_) { |
| 76 case NOTIFY_SELF: | 76 case NOTIFY_SELF: |
| 77 return sender_id_ == id; | 77 return sender_id_ == id; |
| 78 case NOTIFY_OTHERS: | 78 case NOTIFY_OTHERS: |
| 79 return sender_id_ != id; | 79 return sender_id_ != id; |
| 80 case NOTIFY_ALL: | 80 case NOTIFY_ALL: |
| 81 return true; | 81 return true; |
| 82 default: | 82 default: |
| 83 NOTREACHED(); | 83 NOTREACHED(); |
| 84 return false; | 84 return false; |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 syncer::ModelTypeSet P2PNotificationData::GetChangedTypes() const { | 88 ModelTypeSet P2PNotificationData::GetChangedTypes() const { |
| 89 return changed_types_; | 89 return changed_types_; |
| 90 } | 90 } |
| 91 | 91 |
| 92 bool P2PNotificationData::Equals(const P2PNotificationData& other) const { | 92 bool P2PNotificationData::Equals(const P2PNotificationData& other) const { |
| 93 return | 93 return |
| 94 (sender_id_ == other.sender_id_) && | 94 (sender_id_ == other.sender_id_) && |
| 95 (target_ == other.target_) && | 95 (target_ == other.target_) && |
| 96 changed_types_.Equals(other.changed_types_); | 96 changed_types_.Equals(other.changed_types_); |
| 97 } | 97 } |
| 98 | 98 |
| 99 std::string P2PNotificationData::ToString() const { | 99 std::string P2PNotificationData::ToString() const { |
| 100 scoped_ptr<DictionaryValue> dict(new DictionaryValue()); | 100 scoped_ptr<DictionaryValue> dict(new DictionaryValue()); |
| 101 dict->SetString(kSenderIdKey, sender_id_); | 101 dict->SetString(kSenderIdKey, sender_id_); |
| 102 dict->SetString(kNotificationTypeKey, | 102 dict->SetString(kNotificationTypeKey, |
| 103 P2PNotificationTargetToString(target_)); | 103 P2PNotificationTargetToString(target_)); |
| 104 dict->Set(kChangedTypesKey, syncer::ModelTypeSetToValue(changed_types_)); | 104 dict->Set(kChangedTypesKey, ModelTypeSetToValue(changed_types_)); |
| 105 std::string json; | 105 std::string json; |
| 106 base::JSONWriter::Write(dict.get(), &json); | 106 base::JSONWriter::Write(dict.get(), &json); |
| 107 return json; | 107 return json; |
| 108 } | 108 } |
| 109 | 109 |
| 110 bool P2PNotificationData::ResetFromString(const std::string& str) { | 110 bool P2PNotificationData::ResetFromString(const std::string& str) { |
| 111 scoped_ptr<Value> data_value(base::JSONReader::Read(str)); | 111 scoped_ptr<Value> data_value(base::JSONReader::Read(str)); |
| 112 if (!data_value.get()) { | 112 if (!data_value.get()) { |
| 113 LOG(WARNING) << "Could not parse " << str; | 113 LOG(WARNING) << "Could not parse " << str; |
| 114 return false; | 114 return false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 129 LOG(WARNING) << "Could not find string value for " | 129 LOG(WARNING) << "Could not find string value for " |
| 130 << kNotificationTypeKey; | 130 << kNotificationTypeKey; |
| 131 } | 131 } |
| 132 target_ = P2PNotificationTargetFromString(target_str); | 132 target_ = P2PNotificationTargetFromString(target_str); |
| 133 ListValue* changed_types_list = NULL; | 133 ListValue* changed_types_list = NULL; |
| 134 if (!data_dict->GetList(kChangedTypesKey, &changed_types_list)) { | 134 if (!data_dict->GetList(kChangedTypesKey, &changed_types_list)) { |
| 135 LOG(WARNING) << "Could not find list value for " | 135 LOG(WARNING) << "Could not find list value for " |
| 136 << kChangedTypesKey; | 136 << kChangedTypesKey; |
| 137 return false; | 137 return false; |
| 138 } | 138 } |
| 139 changed_types_ = syncer::ModelTypeSetFromValue(*changed_types_list); | 139 changed_types_ = ModelTypeSetFromValue(*changed_types_list); |
| 140 return true; | 140 return true; |
| 141 } | 141 } |
| 142 | 142 |
| 143 P2PNotifier::P2PNotifier(scoped_ptr<notifier::PushClient> push_client, | 143 P2PNotifier::P2PNotifier(scoped_ptr<notifier::PushClient> push_client, |
| 144 P2PNotificationTarget send_notification_target) | 144 P2PNotificationTarget send_notification_target) |
| 145 : push_client_(push_client.Pass()), | 145 : push_client_(push_client.Pass()), |
| 146 logged_in_(false), | 146 logged_in_(false), |
| 147 notifications_enabled_(false), | 147 notifications_enabled_(false), |
| 148 send_notification_target_(send_notification_target) { | 148 send_notification_target_(send_notification_target) { |
| 149 DCHECK(send_notification_target_ == NOTIFY_OTHERS || | 149 DCHECK(send_notification_target_ == NOTIFY_OTHERS || |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 // used in p2p mode (which is only used in testing). | 186 // used in p2p mode (which is only used in testing). |
| 187 subscription.from = email; | 187 subscription.from = email; |
| 188 push_client_->UpdateSubscriptions( | 188 push_client_->UpdateSubscriptions( |
| 189 notifier::SubscriptionList(1, subscription)); | 189 notifier::SubscriptionList(1, subscription)); |
| 190 // If already logged in, the new credentials will take effect on the | 190 // If already logged in, the new credentials will take effect on the |
| 191 // next reconnection. | 191 // next reconnection. |
| 192 push_client_->UpdateCredentials(email, token); | 192 push_client_->UpdateCredentials(email, token); |
| 193 logged_in_ = true; | 193 logged_in_ = true; |
| 194 } | 194 } |
| 195 | 195 |
| 196 void P2PNotifier::UpdateEnabledTypes( | 196 void P2PNotifier::UpdateEnabledTypes(ModelTypeSet enabled_types) { |
| 197 syncer::ModelTypeSet enabled_types) { | |
| 198 DCHECK(thread_checker_.CalledOnValidThread()); | 197 DCHECK(thread_checker_.CalledOnValidThread()); |
| 199 const syncer::ModelTypeSet new_enabled_types = | 198 const ModelTypeSet new_enabled_types = |
| 200 Difference(enabled_types, enabled_types_); | 199 Difference(enabled_types, enabled_types_); |
| 201 enabled_types_ = enabled_types; | 200 enabled_types_ = enabled_types; |
| 202 const P2PNotificationData notification_data( | 201 const P2PNotificationData notification_data( |
| 203 unique_id_, NOTIFY_SELF, new_enabled_types); | 202 unique_id_, NOTIFY_SELF, new_enabled_types); |
| 204 SendNotificationData(notification_data); | 203 SendNotificationData(notification_data); |
| 205 } | 204 } |
| 206 | 205 |
| 207 void P2PNotifier::SendNotification( | 206 void P2PNotifier::SendNotification(ModelTypeSet changed_types) { |
| 208 syncer::ModelTypeSet changed_types) { | |
| 209 DCHECK(thread_checker_.CalledOnValidThread()); | 207 DCHECK(thread_checker_.CalledOnValidThread()); |
| 210 const P2PNotificationData notification_data( | 208 const P2PNotificationData notification_data( |
| 211 unique_id_, send_notification_target_, changed_types); | 209 unique_id_, send_notification_target_, changed_types); |
| 212 SendNotificationData(notification_data); | 210 SendNotificationData(notification_data); |
| 213 } | 211 } |
| 214 | 212 |
| 215 void P2PNotifier::OnNotificationsEnabled() { | 213 void P2PNotifier::OnNotificationsEnabled() { |
| 216 DCHECK(thread_checker_.CalledOnValidThread()); | 214 DCHECK(thread_checker_.CalledOnValidThread()); |
| 217 bool just_turned_on = (notifications_enabled_ == false); | 215 bool just_turned_on = (notifications_enabled_ == false); |
| 218 notifications_enabled_ = true; | 216 notifications_enabled_ = true; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 } | 257 } |
| 260 if (!notification_data.IsTargeted(unique_id_)) { | 258 if (!notification_data.IsTargeted(unique_id_)) { |
| 261 DVLOG(1) << "Not a target of the notification -- " | 259 DVLOG(1) << "Not a target of the notification -- " |
| 262 << "not emitting notification"; | 260 << "not emitting notification"; |
| 263 return; | 261 return; |
| 264 } | 262 } |
| 265 if (notification_data.GetChangedTypes().Empty()) { | 263 if (notification_data.GetChangedTypes().Empty()) { |
| 266 DVLOG(1) << "No changed types -- not emitting notification"; | 264 DVLOG(1) << "No changed types -- not emitting notification"; |
| 267 return; | 265 return; |
| 268 } | 266 } |
| 269 const syncer::ModelTypePayloadMap& type_payloads = | 267 const ModelTypePayloadMap& type_payloads = |
| 270 syncer::ModelTypePayloadMapFromEnumSet( | 268 ModelTypePayloadMapFromEnumSet( |
| 271 notification_data.GetChangedTypes(), std::string()); | 269 notification_data.GetChangedTypes(), std::string()); |
| 272 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | 270 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, |
| 273 OnIncomingNotification(type_payloads, REMOTE_NOTIFICATION)); | 271 OnIncomingNotification(type_payloads, REMOTE_NOTIFICATION)); |
| 274 } | 272 } |
| 275 | 273 |
| 276 void P2PNotifier::SendNotificationDataForTest( | 274 void P2PNotifier::SendNotificationDataForTest( |
| 277 const P2PNotificationData& notification_data) { | 275 const P2PNotificationData& notification_data) { |
| 278 DCHECK(thread_checker_.CalledOnValidThread()); | 276 DCHECK(thread_checker_.CalledOnValidThread()); |
| 279 SendNotificationData(notification_data); | 277 SendNotificationData(notification_data); |
| 280 } | 278 } |
| 281 | 279 |
| 282 void P2PNotifier::SendNotificationData( | 280 void P2PNotifier::SendNotificationData( |
| 283 const P2PNotificationData& notification_data) { | 281 const P2PNotificationData& notification_data) { |
| 284 DCHECK(thread_checker_.CalledOnValidThread()); | 282 DCHECK(thread_checker_.CalledOnValidThread()); |
| 285 notifier::Notification notification; | 283 notifier::Notification notification; |
| 286 notification.channel = kSyncP2PNotificationChannel; | 284 notification.channel = kSyncP2PNotificationChannel; |
| 287 notification.data = notification_data.ToString(); | 285 notification.data = notification_data.ToString(); |
| 288 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 286 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
| 289 push_client_->SendNotification(notification); | 287 push_client_->SendNotification(notification); |
| 290 } | 288 } |
| 291 | 289 |
| 292 } // namespace syncer | 290 } // namespace syncer |
| OLD | NEW |