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 syncable::ModelEnumSet changed_types) | 68 syncable::ModelTypeSet 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 syncable::ModelEnumSet P2PNotificationData::GetChangedTypes() const { | 89 syncable::ModelTypeSet 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_.Equals(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::ModelEnumSetToValue(changed_types_)); | 105 dict->Set(kChangedTypesKey, syncable::ModelTypeSetToValue(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::ModelEnumSetFromValue(*changed_types_list); | 141 changed_types_ = syncable::ModelTypeSetFromValue(*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 syncable::ModelEnumSet enabled_types) { | 215 syncable::ModelTypeSet enabled_types) { |
216 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); | 216 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); |
217 const syncable::ModelEnumSet new_enabled_types = | 217 const syncable::ModelTypeSet new_enabled_types = |
218 Difference(enabled_types, enabled_types_); | 218 Difference(enabled_types, enabled_types_); |
219 enabled_types_ = enabled_types; | 219 enabled_types_ = enabled_types; |
220 const P2PNotificationData notification_data( | 220 const P2PNotificationData notification_data( |
221 unique_id_, NOTIFY_SELF, new_enabled_types); | 221 unique_id_, NOTIFY_SELF, new_enabled_types); |
222 SendNotificationData(notification_data); | 222 SendNotificationData(notification_data); |
223 } | 223 } |
224 | 224 |
225 void P2PNotifier::SendNotification( | 225 void P2PNotifier::SendNotification( |
226 syncable::ModelEnumSet changed_types) { | 226 syncable::ModelTypeSet changed_types) { |
227 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); | 227 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); |
228 const P2PNotificationData notification_data( | 228 const P2PNotificationData notification_data( |
229 unique_id_, send_notification_target_, changed_types); | 229 unique_id_, send_notification_target_, changed_types); |
230 SendNotificationData(notification_data); | 230 SendNotificationData(notification_data); |
231 } | 231 } |
232 | 232 |
233 void P2PNotifier::OnNotificationStateChange(bool notifications_enabled) { | 233 void P2PNotifier::OnNotificationStateChange(bool notifications_enabled) { |
234 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); | 234 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); |
235 bool disabled_to_enabled = notifications_enabled && !notifications_enabled_; | 235 bool disabled_to_enabled = notifications_enabled && !notifications_enabled_; |
236 notifications_enabled_ = notifications_enabled; | 236 notifications_enabled_ = notifications_enabled; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 const P2PNotificationData& notification_data) { | 293 const P2PNotificationData& notification_data) { |
294 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); | 294 DCHECK(parent_message_loop_proxy_->BelongsToCurrentThread()); |
295 notifier::Notification notification; | 295 notifier::Notification notification; |
296 notification.channel = kSyncP2PNotificationChannel; | 296 notification.channel = kSyncP2PNotificationChannel; |
297 notification.data = notification_data.ToString(); | 297 notification.data = notification_data.ToString(); |
298 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 298 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
299 talk_mediator_->SendNotification(notification); | 299 talk_mediator_->SendNotification(notification); |
300 } | 300 } |
301 | 301 |
302 } // namespace sync_notifier | 302 } // namespace sync_notifier |
OLD | NEW |