Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: sync/notifier/p2p_notifier.cc

Issue 10696087: [Sync] Move ModelType and related classes to 'syncer' namespace (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sort headers, update copyrights Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sync/notifier/p2p_notifier.h ('k') | sync/notifier/p2p_notifier_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 syncable::ModelTypeSet changed_types) 67 syncer::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 syncable::ModelTypeSet P2PNotificationData::GetChangedTypes() const { 88 syncer::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, syncable::ModelTypeSetToValue(changed_types_)); 104 dict->Set(kChangedTypesKey, syncer::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
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_ = syncable::ModelTypeSetFromValue(*changed_types_list); 139 changed_types_ = syncer::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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(
197 syncable::ModelTypeSet enabled_types) { 197 syncer::ModelTypeSet enabled_types) {
198 DCHECK(thread_checker_.CalledOnValidThread()); 198 DCHECK(thread_checker_.CalledOnValidThread());
199 const syncable::ModelTypeSet new_enabled_types = 199 const syncer::ModelTypeSet new_enabled_types =
200 Difference(enabled_types, enabled_types_); 200 Difference(enabled_types, enabled_types_);
201 enabled_types_ = enabled_types; 201 enabled_types_ = enabled_types;
202 const P2PNotificationData notification_data( 202 const P2PNotificationData notification_data(
203 unique_id_, NOTIFY_SELF, new_enabled_types); 203 unique_id_, NOTIFY_SELF, new_enabled_types);
204 SendNotificationData(notification_data); 204 SendNotificationData(notification_data);
205 } 205 }
206 206
207 void P2PNotifier::SendNotification( 207 void P2PNotifier::SendNotification(
208 syncable::ModelTypeSet changed_types) { 208 syncer::ModelTypeSet changed_types) {
209 DCHECK(thread_checker_.CalledOnValidThread()); 209 DCHECK(thread_checker_.CalledOnValidThread());
210 const P2PNotificationData notification_data( 210 const P2PNotificationData notification_data(
211 unique_id_, send_notification_target_, changed_types); 211 unique_id_, send_notification_target_, changed_types);
212 SendNotificationData(notification_data); 212 SendNotificationData(notification_data);
213 } 213 }
214 214
215 void P2PNotifier::OnNotificationsEnabled() { 215 void P2PNotifier::OnNotificationsEnabled() {
216 DCHECK(thread_checker_.CalledOnValidThread()); 216 DCHECK(thread_checker_.CalledOnValidThread());
217 bool just_turned_on = (notifications_enabled_ == false); 217 bool just_turned_on = (notifications_enabled_ == false);
218 notifications_enabled_ = true; 218 notifications_enabled_ = true;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 } 259 }
260 if (!notification_data.IsTargeted(unique_id_)) { 260 if (!notification_data.IsTargeted(unique_id_)) {
261 DVLOG(1) << "Not a target of the notification -- " 261 DVLOG(1) << "Not a target of the notification -- "
262 << "not emitting notification"; 262 << "not emitting notification";
263 return; 263 return;
264 } 264 }
265 if (notification_data.GetChangedTypes().Empty()) { 265 if (notification_data.GetChangedTypes().Empty()) {
266 DVLOG(1) << "No changed types -- not emitting notification"; 266 DVLOG(1) << "No changed types -- not emitting notification";
267 return; 267 return;
268 } 268 }
269 const syncable::ModelTypePayloadMap& type_payloads = 269 const syncer::ModelTypePayloadMap& type_payloads =
270 syncable::ModelTypePayloadMapFromEnumSet( 270 syncer::ModelTypePayloadMapFromEnumSet(
271 notification_data.GetChangedTypes(), std::string()); 271 notification_data.GetChangedTypes(), std::string());
272 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, 272 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_,
273 OnIncomingNotification(type_payloads, REMOTE_NOTIFICATION)); 273 OnIncomingNotification(type_payloads, REMOTE_NOTIFICATION));
274 } 274 }
275 275
276 void P2PNotifier::SendNotificationDataForTest( 276 void P2PNotifier::SendNotificationDataForTest(
277 const P2PNotificationData& notification_data) { 277 const P2PNotificationData& notification_data) {
278 DCHECK(thread_checker_.CalledOnValidThread()); 278 DCHECK(thread_checker_.CalledOnValidThread());
279 SendNotificationData(notification_data); 279 SendNotificationData(notification_data);
280 } 280 }
281 281
282 void P2PNotifier::SendNotificationData( 282 void P2PNotifier::SendNotificationData(
283 const P2PNotificationData& notification_data) { 283 const P2PNotificationData& notification_data) {
284 DCHECK(thread_checker_.CalledOnValidThread()); 284 DCHECK(thread_checker_.CalledOnValidThread());
285 notifier::Notification notification; 285 notifier::Notification notification;
286 notification.channel = kSyncP2PNotificationChannel; 286 notification.channel = kSyncP2PNotificationChannel;
287 notification.data = notification_data.ToString(); 287 notification.data = notification_data.ToString();
288 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); 288 DVLOG(1) << "Sending XMPP notification: " << notification.ToString();
289 push_client_->SendNotification(notification); 289 push_client_->SendNotification(notification);
290 } 290 }
291 291
292 } // namespace syncer 292 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/notifier/p2p_notifier.h ('k') | sync/notifier/p2p_notifier_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698