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

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

Issue 10875064: Rename SyncNotifier->Invalidator and SyncNotifierObserver->InvalidationHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge to ToT for landing Created 8 years, 3 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "sync/notifier/p2p_notifier.h"
6
7 #include <algorithm>
8
9 #include "base/json/json_reader.h"
10 #include "base/json/json_writer.h"
11 #include "base/logging.h"
12 #include "base/values.h"
13 #include "jingle/notifier/listener/push_client.h"
14 #include "sync/internal_api/public/base/model_type_state_map.h"
15 #include "sync/notifier/invalidation_util.h"
16 #include "sync/notifier/sync_notifier_observer.h"
17
18 namespace syncer {
19
20 const char* kSyncP2PNotificationChannel = "http://www.google.com/chrome/sync";
21
22 namespace {
23
24 const char kNotifySelf[] = "notifySelf";
25 const char kNotifyOthers[] = "notifyOthers";
26 const char kNotifyAll[] = "notifyAll";
27
28 const char kSenderIdKey[] = "senderId";
29 const char kNotificationTypeKey[] = "notificationType";
30 const char kChangedTypesKey[] = "changedTypes";
31
32 } // namespace
33
34 std::string P2PNotificationTargetToString(P2PNotificationTarget target) {
35 switch (target) {
36 case NOTIFY_SELF:
37 return kNotifySelf;
38 case NOTIFY_OTHERS:
39 return kNotifyOthers;
40 case NOTIFY_ALL:
41 return kNotifyAll;
42 default:
43 NOTREACHED();
44 return "";
45 }
46 }
47
48 P2PNotificationTarget P2PNotificationTargetFromString(
49 const std::string& target_str) {
50 if (target_str == kNotifySelf) {
51 return NOTIFY_SELF;
52 }
53 if (target_str == kNotifyOthers) {
54 return NOTIFY_OTHERS;
55 }
56 if (target_str == kNotifyAll) {
57 return NOTIFY_ALL;
58 }
59 LOG(WARNING) << "Could not parse " << target_str;
60 return NOTIFY_SELF;
61 }
62
63 P2PNotificationData::P2PNotificationData() : target_(NOTIFY_SELF) {}
64
65 P2PNotificationData::P2PNotificationData(
66 const std::string& sender_id,
67 P2PNotificationTarget target,
68 ModelTypeSet changed_types)
69 : sender_id_(sender_id),
70 target_(target),
71 changed_types_(changed_types) {}
72
73 P2PNotificationData::~P2PNotificationData() {}
74
75 bool P2PNotificationData::IsTargeted(const std::string& id) const {
76 switch (target_) {
77 case NOTIFY_SELF:
78 return sender_id_ == id;
79 case NOTIFY_OTHERS:
80 return sender_id_ != id;
81 case NOTIFY_ALL:
82 return true;
83 default:
84 NOTREACHED();
85 return false;
86 }
87 }
88
89 ModelTypeSet P2PNotificationData::GetChangedTypes() const {
90 return changed_types_;
91 }
92
93 bool P2PNotificationData::Equals(const P2PNotificationData& other) const {
94 return
95 (sender_id_ == other.sender_id_) &&
96 (target_ == other.target_) &&
97 changed_types_.Equals(other.changed_types_);
98 }
99
100 std::string P2PNotificationData::ToString() const {
101 scoped_ptr<DictionaryValue> dict(new DictionaryValue());
102 dict->SetString(kSenderIdKey, sender_id_);
103 dict->SetString(kNotificationTypeKey,
104 P2PNotificationTargetToString(target_));
105 dict->Set(kChangedTypesKey, ModelTypeSetToValue(changed_types_));
106 std::string json;
107 base::JSONWriter::Write(dict.get(), &json);
108 return json;
109 }
110
111 bool P2PNotificationData::ResetFromString(const std::string& str) {
112 scoped_ptr<Value> data_value(base::JSONReader::Read(str));
113 if (!data_value.get()) {
114 LOG(WARNING) << "Could not parse " << str;
115 return false;
116 }
117 if (!data_value->IsType(Value::TYPE_DICTIONARY)) {
118 LOG(WARNING) << "Could not parse " << str << " as a dictionary";
119 return false;
120 }
121 // TODO(akalin): Use Values::AsDictionary() when it becomes
122 // available.
123 DictionaryValue* data_dict =
124 static_cast<DictionaryValue*>(data_value.get());
125 if (!data_dict->GetString(kSenderIdKey, &sender_id_)) {
126 LOG(WARNING) << "Could not find string value for " << kSenderIdKey;
127 }
128 std::string target_str;
129 if (!data_dict->GetString(kNotificationTypeKey, &target_str)) {
130 LOG(WARNING) << "Could not find string value for "
131 << kNotificationTypeKey;
132 }
133 target_ = P2PNotificationTargetFromString(target_str);
134 ListValue* changed_types_list = NULL;
135 if (!data_dict->GetList(kChangedTypesKey, &changed_types_list)) {
136 LOG(WARNING) << "Could not find list value for "
137 << kChangedTypesKey;
138 return false;
139 }
140 changed_types_ = ModelTypeSetFromValue(*changed_types_list);
141 return true;
142 }
143
144 P2PNotifier::P2PNotifier(scoped_ptr<notifier::PushClient> push_client,
145 P2PNotificationTarget send_notification_target)
146 : push_client_(push_client.Pass()),
147 logged_in_(false),
148 notifications_enabled_(false),
149 send_notification_target_(send_notification_target) {
150 DCHECK(send_notification_target_ == NOTIFY_OTHERS ||
151 send_notification_target_ == NOTIFY_ALL);
152 push_client_->AddObserver(this);
153 }
154
155 P2PNotifier::~P2PNotifier() {
156 DCHECK(thread_checker_.CalledOnValidThread());
157 push_client_->RemoveObserver(this);
158 }
159
160 void P2PNotifier::RegisterHandler(SyncNotifierObserver* handler) {
161 DCHECK(thread_checker_.CalledOnValidThread());
162 registrar_.RegisterHandler(handler);
163 }
164
165 void P2PNotifier::UpdateRegisteredIds(SyncNotifierObserver* handler,
166 const ObjectIdSet& ids) {
167 // TODO(akalin): Handle arbitrary object IDs (http://crbug.com/140411).
168 DCHECK(thread_checker_.CalledOnValidThread());
169 registrar_.UpdateRegisteredIds(handler, ids);
170 const ModelTypeSet enabled_types =
171 ObjectIdSetToModelTypeSet(registrar_.GetAllRegisteredIds());
172 const ModelTypeSet new_enabled_types =
173 Difference(enabled_types, enabled_types_);
174 const P2PNotificationData notification_data(
175 unique_id_, NOTIFY_SELF, new_enabled_types);
176 SendNotificationData(notification_data);
177 enabled_types_ = enabled_types;
178 }
179
180 void P2PNotifier::UnregisterHandler(SyncNotifierObserver* handler) {
181 DCHECK(thread_checker_.CalledOnValidThread());
182 registrar_.UnregisterHandler(handler);
183 }
184
185 void P2PNotifier::SetUniqueId(const std::string& unique_id) {
186 DCHECK(thread_checker_.CalledOnValidThread());
187 unique_id_ = unique_id;
188 }
189
190 void P2PNotifier::SetStateDeprecated(const std::string& state) {
191 DCHECK(thread_checker_.CalledOnValidThread());
192 // Do nothing.
193 }
194
195 void P2PNotifier::UpdateCredentials(
196 const std::string& email, const std::string& token) {
197 DCHECK(thread_checker_.CalledOnValidThread());
198 notifier::Subscription subscription;
199 subscription.channel = kSyncP2PNotificationChannel;
200 // There may be some subtle issues around case sensitivity of the
201 // from field, but it doesn't matter too much since this is only
202 // used in p2p mode (which is only used in testing).
203 subscription.from = email;
204 push_client_->UpdateSubscriptions(
205 notifier::SubscriptionList(1, subscription));
206 // If already logged in, the new credentials will take effect on the
207 // next reconnection.
208 push_client_->UpdateCredentials(email, token);
209 logged_in_ = true;
210 }
211
212 void P2PNotifier::SendNotification(ModelTypeSet changed_types) {
213 DCHECK(thread_checker_.CalledOnValidThread());
214 const P2PNotificationData notification_data(
215 unique_id_, send_notification_target_, changed_types);
216 SendNotificationData(notification_data);
217 }
218
219 void P2PNotifier::OnNotificationsEnabled() {
220 DCHECK(thread_checker_.CalledOnValidThread());
221 bool just_turned_on = (notifications_enabled_ == false);
222 notifications_enabled_ = true;
223 registrar_.EmitOnNotificationsEnabled();
224 if (just_turned_on) {
225 const P2PNotificationData notification_data(
226 unique_id_, NOTIFY_SELF, enabled_types_);
227 SendNotificationData(notification_data);
228 }
229 }
230
231 void P2PNotifier::OnNotificationsDisabled(
232 notifier::NotificationsDisabledReason reason) {
233 DCHECK(thread_checker_.CalledOnValidThread());
234 registrar_.EmitOnNotificationsDisabled(FromNotifierReason(reason));
235 }
236
237 void P2PNotifier::OnIncomingNotification(
238 const notifier::Notification& notification) {
239 DCHECK(thread_checker_.CalledOnValidThread());
240 DVLOG(1) << "Received notification " << notification.ToString();
241 if (!logged_in_) {
242 DVLOG(1) << "Not logged in yet -- not emitting notification";
243 return;
244 }
245 if (!notifications_enabled_) {
246 DVLOG(1) << "Notifications not on -- not emitting notification";
247 return;
248 }
249 if (notification.channel != kSyncP2PNotificationChannel) {
250 LOG(WARNING) << "Notification from unexpected source "
251 << notification.channel;
252 }
253 P2PNotificationData notification_data;
254 if (!notification_data.ResetFromString(notification.data)) {
255 LOG(WARNING) << "Could not parse notification data from "
256 << notification.data;
257 notification_data =
258 P2PNotificationData(unique_id_, NOTIFY_ALL, enabled_types_);
259 }
260 if (!notification_data.IsTargeted(unique_id_)) {
261 DVLOG(1) << "Not a target of the notification -- "
262 << "not emitting notification";
263 return;
264 }
265 const ModelTypeSet types_to_notify =
266 Intersection(enabled_types_, notification_data.GetChangedTypes());
267 if (types_to_notify.Empty()) {
268 DVLOG(1) << "No enabled and changed types -- not emitting notification";
269 return;
270 }
271 const ModelTypeStateMap& type_state_map = ModelTypeSetToStateMap(
272 notification_data.GetChangedTypes(), std::string());
273 registrar_.DispatchInvalidationsToHandlers(
274 ModelTypeStateMapToObjectIdStateMap(type_state_map),
275 REMOTE_NOTIFICATION);
276 }
277
278 void P2PNotifier::SendNotificationDataForTest(
279 const P2PNotificationData& notification_data) {
280 DCHECK(thread_checker_.CalledOnValidThread());
281 SendNotificationData(notification_data);
282 }
283
284 void P2PNotifier::SendNotificationData(
285 const P2PNotificationData& notification_data) {
286 DCHECK(thread_checker_.CalledOnValidThread());
287 if (notification_data.GetChangedTypes().Empty()) {
288 DVLOG(1) << "Not sending XMPP notification with no changed types: "
289 << notification_data.ToString();
290 return;
291 }
292 notifier::Notification notification;
293 notification.channel = kSyncP2PNotificationChannel;
294 notification.data = notification_data.ToString();
295 DVLOG(1) << "Sending XMPP notification: " << notification.ToString();
296 push_client_->SendNotification(notification);
297 }
298
299 } // 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