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

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

Issue 23441042: Refactor common invalidation framework types (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 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
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_invalidator.h" 5 #include "sync/notifier/p2p_invalidator.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
11 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "jingle/notifier/listener/push_client.h" 14 #include "jingle/notifier/listener/push_client.h"
15 #include "sync/notifier/invalidation_handler.h" 15 #include "sync/notifier/invalidation_handler.h"
16 #include "sync/notifier/invalidation_util.h" 16 #include "sync/notifier/invalidation_util.h"
17 #include "sync/notifier/object_id_invalidation_map.h"
17 18
18 namespace syncer { 19 namespace syncer {
19 20
20 const char kSyncP2PNotificationChannel[] = "http://www.google.com/chrome/sync"; 21 const char kSyncP2PNotificationChannel[] = "http://www.google.com/chrome/sync";
21 22
22 namespace { 23 namespace {
23 24
24 const char kNotifySelf[] = "notifySelf"; 25 const char kNotifySelf[] = "notifySelf";
25 const char kNotifyOthers[] = "notifyOthers"; 26 const char kNotifyOthers[] = "notifyOthers";
26 const char kNotifyAll[] = "notifyAll"; 27 const char kNotifyAll[] = "notifyAll";
27 28
28 const char kSenderIdKey[] = "senderId"; 29 const char kSenderIdKey[] = "senderId";
29 const char kNotificationTypeKey[] = "notificationType"; 30 const char kNotificationTypeKey[] = "notificationType";
30 const char kIdInvalidationMapKey[] = "idInvalidationMap"; 31 const char kInvalidationsKey[] = "invalidations";
31 32
32 } // namespace 33 } // namespace
33 34
34 std::string P2PNotificationTargetToString(P2PNotificationTarget target) { 35 std::string P2PNotificationTargetToString(P2PNotificationTarget target) {
35 switch (target) { 36 switch (target) {
36 case NOTIFY_SELF: 37 case NOTIFY_SELF:
37 return kNotifySelf; 38 return kNotifySelf;
38 case NOTIFY_OTHERS: 39 case NOTIFY_OTHERS:
39 return kNotifyOthers; 40 return kNotifyOthers;
40 case NOTIFY_ALL: 41 case NOTIFY_ALL:
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 90
90 const ObjectIdInvalidationMap& 91 const ObjectIdInvalidationMap&
91 P2PNotificationData::GetIdInvalidationMap() const { 92 P2PNotificationData::GetIdInvalidationMap() const {
92 return invalidation_map_; 93 return invalidation_map_;
93 } 94 }
94 95
95 bool P2PNotificationData::Equals(const P2PNotificationData& other) const { 96 bool P2PNotificationData::Equals(const P2PNotificationData& other) const {
96 return 97 return
97 (sender_id_ == other.sender_id_) && 98 (sender_id_ == other.sender_id_) &&
98 (target_ == other.target_) && 99 (target_ == other.target_) &&
99 ObjectIdInvalidationMapEquals(invalidation_map_, 100 (invalidation_map_ == other.invalidation_map_);
100 other.invalidation_map_);
101 } 101 }
102 102
103 std::string P2PNotificationData::ToString() const { 103 std::string P2PNotificationData::ToString() const {
104 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); 104 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
105 dict->SetString(kSenderIdKey, sender_id_); 105 dict->SetString(kSenderIdKey, sender_id_);
106 dict->SetString(kNotificationTypeKey, 106 dict->SetString(kNotificationTypeKey,
107 P2PNotificationTargetToString(target_)); 107 P2PNotificationTargetToString(target_));
108 dict->Set(kIdInvalidationMapKey, 108 dict->Set(kInvalidationsKey, invalidation_map_.ToValue().release());
109 ObjectIdInvalidationMapToValue(invalidation_map_).release());
110 std::string json; 109 std::string json;
111 base::JSONWriter::Write(dict.get(), &json); 110 base::JSONWriter::Write(dict.get(), &json);
112 return json; 111 return json;
113 } 112 }
114 113
115 bool P2PNotificationData::ResetFromString(const std::string& str) { 114 bool P2PNotificationData::ResetFromString(const std::string& str) {
116 scoped_ptr<base::Value> data_value(base::JSONReader::Read(str)); 115 scoped_ptr<base::Value> data_value(base::JSONReader::Read(str));
117 const base::DictionaryValue* data_dict = NULL; 116 const base::DictionaryValue* data_dict = NULL;
118 if (!data_value.get() || !data_value->GetAsDictionary(&data_dict)) { 117 if (!data_value.get() || !data_value->GetAsDictionary(&data_dict)) {
119 LOG(WARNING) << "Could not parse " << str << " as a dictionary"; 118 LOG(WARNING) << "Could not parse " << str << " as a dictionary";
120 return false; 119 return false;
121 } 120 }
122 if (!data_dict->GetString(kSenderIdKey, &sender_id_)) { 121 if (!data_dict->GetString(kSenderIdKey, &sender_id_)) {
123 LOG(WARNING) << "Could not find string value for " << kSenderIdKey; 122 LOG(WARNING) << "Could not find string value for " << kSenderIdKey;
124 } 123 }
125 std::string target_str; 124 std::string target_str;
126 if (!data_dict->GetString(kNotificationTypeKey, &target_str)) { 125 if (!data_dict->GetString(kNotificationTypeKey, &target_str)) {
127 LOG(WARNING) << "Could not find string value for " 126 LOG(WARNING) << "Could not find string value for "
128 << kNotificationTypeKey; 127 << kNotificationTypeKey;
129 } 128 }
130 target_ = P2PNotificationTargetFromString(target_str); 129 target_ = P2PNotificationTargetFromString(target_str);
131 const base::ListValue* invalidation_map_list = NULL; 130 const base::ListValue* invalidation_map_list = NULL;
132 if (!data_dict->GetList(kIdInvalidationMapKey, &invalidation_map_list) || 131 if (!data_dict->GetList(kInvalidationsKey, &invalidation_map_list) ||
133 !ObjectIdInvalidationMapFromValue(*invalidation_map_list, 132 !invalidation_map_.ResetFromValue(*invalidation_map_list)) {
134 &invalidation_map_)) { 133 LOG(WARNING) << "Could not parse " << kInvalidationsKey;
135 LOG(WARNING) << "Could not parse " << kIdInvalidationMapKey;
136 } 134 }
137 return true; 135 return true;
138 } 136 }
139 137
140 P2PInvalidator::P2PInvalidator(scoped_ptr<notifier::PushClient> push_client, 138 P2PInvalidator::P2PInvalidator(scoped_ptr<notifier::PushClient> push_client,
141 const std::string& invalidator_client_id, 139 const std::string& invalidator_client_id,
142 P2PNotificationTarget send_notification_target) 140 P2PNotificationTarget send_notification_target)
143 : push_client_(push_client.Pass()), 141 : push_client_(push_client.Pass()),
144 invalidator_client_id_(invalidator_client_id), 142 invalidator_client_id_(invalidator_client_id),
145 logged_in_(false), 143 logged_in_(false),
146 notifications_enabled_(false), 144 notifications_enabled_(false),
147 send_notification_target_(send_notification_target) { 145 send_notification_target_(send_notification_target) {
148 DCHECK(send_notification_target_ == NOTIFY_OTHERS || 146 DCHECK(send_notification_target_ == NOTIFY_OTHERS ||
149 send_notification_target_ == NOTIFY_ALL); 147 send_notification_target_ == NOTIFY_ALL);
150 push_client_->AddObserver(this); 148 push_client_->AddObserver(this);
151 } 149 }
152 150
153 P2PInvalidator::~P2PInvalidator() { 151 P2PInvalidator::~P2PInvalidator() {
154 DCHECK(thread_checker_.CalledOnValidThread()); 152 DCHECK(thread_checker_.CalledOnValidThread());
155 push_client_->RemoveObserver(this); 153 push_client_->RemoveObserver(this);
156 } 154 }
157 155
158 void P2PInvalidator::RegisterHandler(InvalidationHandler* handler) { 156 void P2PInvalidator::RegisterHandler(InvalidationHandler* handler) {
159 DCHECK(thread_checker_.CalledOnValidThread()); 157 DCHECK(thread_checker_.CalledOnValidThread());
160 registrar_.RegisterHandler(handler); 158 registrar_.RegisterHandler(handler);
161 } 159 }
162 160
163 void P2PInvalidator::UpdateRegisteredIds(InvalidationHandler* handler, 161 void P2PInvalidator::UpdateRegisteredIds(InvalidationHandler* handler,
164 const ObjectIdSet& ids) { 162 const ObjectIdSet& ids) {
165 // TODO(akalin): Handle arbitrary object IDs (http://crbug.com/140411).
166 DCHECK(thread_checker_.CalledOnValidThread()); 163 DCHECK(thread_checker_.CalledOnValidThread());
167 ObjectIdSet new_ids; 164 ObjectIdSet new_ids;
168 const ObjectIdSet& old_ids = registrar_.GetRegisteredIds(handler); 165 const ObjectIdSet& old_ids = registrar_.GetRegisteredIds(handler);
169 std::set_difference(ids.begin(), ids.end(), 166 std::set_difference(ids.begin(), ids.end(),
170 old_ids.begin(), old_ids.end(), 167 old_ids.begin(), old_ids.end(),
171 std::inserter(new_ids, new_ids.end()), 168 std::inserter(new_ids, new_ids.end()),
172 ObjectIdLessThan()); 169 ObjectIdLessThan());
173 registrar_.UpdateRegisteredIds(handler, ids); 170 registrar_.UpdateRegisteredIds(handler, ids);
174 const P2PNotificationData notification_data( 171 const P2PNotificationData notification_data(
175 invalidator_client_id_, 172 invalidator_client_id_,
176 NOTIFY_SELF, 173 send_notification_target_,
177 ObjectIdSetToInvalidationMap(new_ids, 174 ObjectIdInvalidationMap::InvalidateAll(ids));
178 Invalidation::kUnknownVersion,
179 std::string()));
180 SendNotificationData(notification_data); 175 SendNotificationData(notification_data);
181 } 176 }
182 177
183 void P2PInvalidator::UnregisterHandler(InvalidationHandler* handler) { 178 void P2PInvalidator::UnregisterHandler(InvalidationHandler* handler) {
184 DCHECK(thread_checker_.CalledOnValidThread()); 179 DCHECK(thread_checker_.CalledOnValidThread());
185 registrar_.UnregisterHandler(handler); 180 registrar_.UnregisterHandler(handler);
186 } 181 }
187 182
188 void P2PInvalidator::Acknowledge(const invalidation::ObjectId& id, 183 void P2PInvalidator::Acknowledge(const invalidation::ObjectId& id,
189 const AckHandle& ack_handle) { 184 const AckHandle& ack_handle) {
(...skipping 16 matching lines...) Expand all
206 // used in p2p mode (which is only used in testing). 201 // used in p2p mode (which is only used in testing).
207 subscription.from = email; 202 subscription.from = email;
208 push_client_->UpdateSubscriptions( 203 push_client_->UpdateSubscriptions(
209 notifier::SubscriptionList(1, subscription)); 204 notifier::SubscriptionList(1, subscription));
210 // If already logged in, the new credentials will take effect on the 205 // If already logged in, the new credentials will take effect on the
211 // next reconnection. 206 // next reconnection.
212 push_client_->UpdateCredentials(email, token); 207 push_client_->UpdateCredentials(email, token);
213 logged_in_ = true; 208 logged_in_ = true;
214 } 209 }
215 210
216 void P2PInvalidator::SendInvalidation( 211 void P2PInvalidator::SendInvalidation(const ObjectIdSet& ids) {
217 const ObjectIdInvalidationMap& invalidation_map) {
218 DCHECK(thread_checker_.CalledOnValidThread()); 212 DCHECK(thread_checker_.CalledOnValidThread());
213 ObjectIdInvalidationMap invalidation_map =
214 ObjectIdInvalidationMap::InvalidateAll(ids);
219 const P2PNotificationData notification_data( 215 const P2PNotificationData notification_data(
220 invalidator_client_id_, send_notification_target_, invalidation_map); 216 invalidator_client_id_, send_notification_target_, invalidation_map);
221 SendNotificationData(notification_data); 217 SendNotificationData(notification_data);
222 } 218 }
223 219
224 void P2PInvalidator::OnNotificationsEnabled() { 220 void P2PInvalidator::OnNotificationsEnabled() {
225 DCHECK(thread_checker_.CalledOnValidThread()); 221 DCHECK(thread_checker_.CalledOnValidThread());
226 bool just_turned_on = (notifications_enabled_ == false); 222 bool just_turned_on = (notifications_enabled_ == false);
227 notifications_enabled_ = true; 223 notifications_enabled_ = true;
228 registrar_.UpdateInvalidatorState(INVALIDATIONS_ENABLED); 224 registrar_.UpdateInvalidatorState(INVALIDATIONS_ENABLED);
229 if (just_turned_on) { 225 if (just_turned_on) {
230 const P2PNotificationData notification_data( 226 const P2PNotificationData notification_data(
231 invalidator_client_id_, 227 invalidator_client_id_,
232 NOTIFY_SELF, 228 NOTIFY_SELF,
233 ObjectIdSetToInvalidationMap(registrar_.GetAllRegisteredIds(), 229 ObjectIdInvalidationMap::InvalidateAll(
234 Invalidation::kUnknownVersion, 230 registrar_.GetAllRegisteredIds()));
235 std::string()));
236 SendNotificationData(notification_data); 231 SendNotificationData(notification_data);
237 } 232 }
238 } 233 }
239 234
240 void P2PInvalidator::OnNotificationsDisabled( 235 void P2PInvalidator::OnNotificationsDisabled(
241 notifier::NotificationsDisabledReason reason) { 236 notifier::NotificationsDisabledReason reason) {
242 DCHECK(thread_checker_.CalledOnValidThread()); 237 DCHECK(thread_checker_.CalledOnValidThread());
243 registrar_.UpdateInvalidatorState(FromNotifierReason(reason)); 238 registrar_.UpdateInvalidatorState(FromNotifierReason(reason));
244 } 239 }
245 240
(...skipping 13 matching lines...) Expand all
259 LOG(WARNING) << "Notification from unexpected source " 254 LOG(WARNING) << "Notification from unexpected source "
260 << notification.channel; 255 << notification.channel;
261 } 256 }
262 P2PNotificationData notification_data; 257 P2PNotificationData notification_data;
263 if (!notification_data.ResetFromString(notification.data)) { 258 if (!notification_data.ResetFromString(notification.data)) {
264 LOG(WARNING) << "Could not parse notification data from " 259 LOG(WARNING) << "Could not parse notification data from "
265 << notification.data; 260 << notification.data;
266 notification_data = P2PNotificationData( 261 notification_data = P2PNotificationData(
267 invalidator_client_id_, 262 invalidator_client_id_,
268 NOTIFY_ALL, 263 NOTIFY_ALL,
269 ObjectIdSetToInvalidationMap(registrar_.GetAllRegisteredIds(), 264 ObjectIdInvalidationMap::InvalidateAll(
270 Invalidation::kUnknownVersion, 265 registrar_.GetAllRegisteredIds()));
271 std::string()));
272 } 266 }
273 if (!notification_data.IsTargeted(invalidator_client_id_)) { 267 if (!notification_data.IsTargeted(invalidator_client_id_)) {
274 DVLOG(1) << "Not a target of the notification -- " 268 DVLOG(1) << "Not a target of the notification -- "
275 << "not emitting notification"; 269 << "not emitting notification";
276 return; 270 return;
277 } 271 }
278 registrar_.DispatchInvalidationsToHandlers( 272 registrar_.DispatchInvalidationsToHandlers(
279 notification_data.GetIdInvalidationMap()); 273 notification_data.GetIdInvalidationMap());
280 } 274 }
281 275
282 void P2PInvalidator::SendNotificationDataForTest( 276 void P2PInvalidator::SendNotificationDataForTest(
283 const P2PNotificationData& notification_data) { 277 const P2PNotificationData& notification_data) {
284 DCHECK(thread_checker_.CalledOnValidThread()); 278 DCHECK(thread_checker_.CalledOnValidThread());
285 SendNotificationData(notification_data); 279 SendNotificationData(notification_data);
286 } 280 }
287 281
288 void P2PInvalidator::SendNotificationData( 282 void P2PInvalidator::SendNotificationData(
289 const P2PNotificationData& notification_data) { 283 const P2PNotificationData& notification_data) {
290 DCHECK(thread_checker_.CalledOnValidThread()); 284 DCHECK(thread_checker_.CalledOnValidThread());
291 if (notification_data.GetIdInvalidationMap().empty()) { 285 if (notification_data.GetIdInvalidationMap().Empty()) {
292 DVLOG(1) << "Not sending XMPP notification with empty state map: " 286 DVLOG(1) << "Not sending XMPP notification with empty state map: "
293 << notification_data.ToString(); 287 << notification_data.ToString();
294 return; 288 return;
295 } 289 }
296 notifier::Notification notification; 290 notifier::Notification notification;
297 notification.channel = kSyncP2PNotificationChannel; 291 notification.channel = kSyncP2PNotificationChannel;
298 notification.data = notification_data.ToString(); 292 notification.data = notification_data.ToString();
299 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); 293 DVLOG(1) << "Sending XMPP notification: " << notification.ToString();
300 push_client_->SendNotification(notification); 294 push_client_->SendNotification(notification);
301 } 295 }
302 296
303 } // namespace syncer 297 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698