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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 DCHECK(send_notification_target_ == NOTIFY_OTHERS || | 150 DCHECK(send_notification_target_ == NOTIFY_OTHERS || |
151 send_notification_target_ == NOTIFY_ALL); | 151 send_notification_target_ == NOTIFY_ALL); |
152 push_client_->AddObserver(this); | 152 push_client_->AddObserver(this); |
153 } | 153 } |
154 | 154 |
155 P2PNotifier::~P2PNotifier() { | 155 P2PNotifier::~P2PNotifier() { |
156 DCHECK(thread_checker_.CalledOnValidThread()); | 156 DCHECK(thread_checker_.CalledOnValidThread()); |
157 push_client_->RemoveObserver(this); | 157 push_client_->RemoveObserver(this); |
158 } | 158 } |
159 | 159 |
160 void P2PNotifier::RegisterHandler(SyncNotifierObserver* handler) { | |
161 DCHECK(thread_checker_.CalledOnValidThread()); | |
162 registrar_.RegisterHandler(handler); | |
163 } | |
164 | |
165 void P2PNotifier::UpdateRegisteredIds(SyncNotifierObserver* handler, | 160 void P2PNotifier::UpdateRegisteredIds(SyncNotifierObserver* handler, |
166 const ObjectIdSet& ids) { | 161 const ObjectIdSet& ids) { |
167 // TODO(akalin): Handle arbitrary object IDs (http://crbug.com/140411). | 162 const ModelTypeSet enabled_types = ObjectIdSetToModelTypeSet( |
168 DCHECK(thread_checker_.CalledOnValidThread()); | 163 helper_.UpdateRegisteredIds(handler, ids)); |
169 registrar_.UpdateRegisteredIds(handler, ids); | |
170 const ModelTypeSet enabled_types = | |
171 ObjectIdSetToModelTypeSet(registrar_.GetAllRegisteredIds()); | |
172 const ModelTypeSet new_enabled_types = | 164 const ModelTypeSet new_enabled_types = |
173 Difference(enabled_types, enabled_types_); | 165 Difference(enabled_types, enabled_types_); |
174 const P2PNotificationData notification_data( | 166 const P2PNotificationData notification_data( |
175 unique_id_, NOTIFY_SELF, new_enabled_types); | 167 unique_id_, NOTIFY_SELF, new_enabled_types); |
176 SendNotificationData(notification_data); | 168 SendNotificationData(notification_data); |
177 enabled_types_ = enabled_types; | 169 enabled_types_ = enabled_types; |
178 } | 170 } |
179 | 171 |
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) { | 172 void P2PNotifier::SetUniqueId(const std::string& unique_id) { |
186 DCHECK(thread_checker_.CalledOnValidThread()); | 173 DCHECK(thread_checker_.CalledOnValidThread()); |
187 unique_id_ = unique_id; | 174 unique_id_ = unique_id; |
188 } | 175 } |
189 | 176 |
190 void P2PNotifier::SetStateDeprecated(const std::string& state) { | 177 void P2PNotifier::SetStateDeprecated(const std::string& state) { |
191 DCHECK(thread_checker_.CalledOnValidThread()); | 178 DCHECK(thread_checker_.CalledOnValidThread()); |
192 // Do nothing. | 179 // Do nothing. |
193 } | 180 } |
194 | 181 |
(...skipping 18 matching lines...) Expand all Loading... |
213 DCHECK(thread_checker_.CalledOnValidThread()); | 200 DCHECK(thread_checker_.CalledOnValidThread()); |
214 const P2PNotificationData notification_data( | 201 const P2PNotificationData notification_data( |
215 unique_id_, send_notification_target_, changed_types); | 202 unique_id_, send_notification_target_, changed_types); |
216 SendNotificationData(notification_data); | 203 SendNotificationData(notification_data); |
217 } | 204 } |
218 | 205 |
219 void P2PNotifier::OnNotificationsEnabled() { | 206 void P2PNotifier::OnNotificationsEnabled() { |
220 DCHECK(thread_checker_.CalledOnValidThread()); | 207 DCHECK(thread_checker_.CalledOnValidThread()); |
221 bool just_turned_on = (notifications_enabled_ == false); | 208 bool just_turned_on = (notifications_enabled_ == false); |
222 notifications_enabled_ = true; | 209 notifications_enabled_ = true; |
223 registrar_.EmitOnNotificationsEnabled(); | 210 helper_.EmitOnNotificationsEnabled(); |
224 if (just_turned_on) { | 211 if (just_turned_on) { |
225 const P2PNotificationData notification_data( | 212 const P2PNotificationData notification_data( |
226 unique_id_, NOTIFY_SELF, enabled_types_); | 213 unique_id_, NOTIFY_SELF, enabled_types_); |
227 SendNotificationData(notification_data); | 214 SendNotificationData(notification_data); |
228 } | 215 } |
229 } | 216 } |
230 | 217 |
231 void P2PNotifier::OnNotificationsDisabled( | 218 void P2PNotifier::OnNotificationsDisabled( |
232 notifier::NotificationsDisabledReason reason) { | 219 notifier::NotificationsDisabledReason reason) { |
233 DCHECK(thread_checker_.CalledOnValidThread()); | 220 DCHECK(thread_checker_.CalledOnValidThread()); |
234 registrar_.EmitOnNotificationsDisabled(FromNotifierReason(reason)); | 221 helper_.EmitOnNotificationsDisabled(FromNotifierReason(reason)); |
235 } | 222 } |
236 | 223 |
237 void P2PNotifier::OnIncomingNotification( | 224 void P2PNotifier::OnIncomingNotification( |
238 const notifier::Notification& notification) { | 225 const notifier::Notification& notification) { |
239 DCHECK(thread_checker_.CalledOnValidThread()); | 226 DCHECK(thread_checker_.CalledOnValidThread()); |
240 DVLOG(1) << "Received notification " << notification.ToString(); | 227 DVLOG(1) << "Received notification " << notification.ToString(); |
241 if (!logged_in_) { | 228 if (!logged_in_) { |
242 DVLOG(1) << "Not logged in yet -- not emitting notification"; | 229 DVLOG(1) << "Not logged in yet -- not emitting notification"; |
243 return; | 230 return; |
244 } | 231 } |
(...skipping 18 matching lines...) Expand all Loading... |
263 return; | 250 return; |
264 } | 251 } |
265 const ModelTypeSet types_to_notify = | 252 const ModelTypeSet types_to_notify = |
266 Intersection(enabled_types_, notification_data.GetChangedTypes()); | 253 Intersection(enabled_types_, notification_data.GetChangedTypes()); |
267 if (types_to_notify.Empty()) { | 254 if (types_to_notify.Empty()) { |
268 DVLOG(1) << "No enabled and changed types -- not emitting notification"; | 255 DVLOG(1) << "No enabled and changed types -- not emitting notification"; |
269 return; | 256 return; |
270 } | 257 } |
271 const ModelTypePayloadMap& type_payloads = ModelTypePayloadMapFromEnumSet( | 258 const ModelTypePayloadMap& type_payloads = ModelTypePayloadMapFromEnumSet( |
272 notification_data.GetChangedTypes(), std::string()); | 259 notification_data.GetChangedTypes(), std::string()); |
273 registrar_.DispatchInvalidationsToHandlers( | 260 helper_.DispatchInvalidationsToHandlers( |
274 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), | 261 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), |
275 REMOTE_NOTIFICATION); | 262 REMOTE_NOTIFICATION); |
276 } | 263 } |
277 | 264 |
278 void P2PNotifier::SendNotificationDataForTest( | 265 void P2PNotifier::SendNotificationDataForTest( |
279 const P2PNotificationData& notification_data) { | 266 const P2PNotificationData& notification_data) { |
280 DCHECK(thread_checker_.CalledOnValidThread()); | 267 DCHECK(thread_checker_.CalledOnValidThread()); |
281 SendNotificationData(notification_data); | 268 SendNotificationData(notification_data); |
282 } | 269 } |
283 | 270 |
284 void P2PNotifier::SendNotificationData( | 271 void P2PNotifier::SendNotificationData( |
285 const P2PNotificationData& notification_data) { | 272 const P2PNotificationData& notification_data) { |
286 DCHECK(thread_checker_.CalledOnValidThread()); | 273 DCHECK(thread_checker_.CalledOnValidThread()); |
287 if (notification_data.GetChangedTypes().Empty()) { | 274 if (notification_data.GetChangedTypes().Empty()) { |
288 DVLOG(1) << "Not sending XMPP notification with no changed types: " | 275 DVLOG(1) << "Not sending XMPP notification with no changed types: " |
289 << notification_data.ToString(); | 276 << notification_data.ToString(); |
290 return; | 277 return; |
291 } | 278 } |
292 notifier::Notification notification; | 279 notifier::Notification notification; |
293 notification.channel = kSyncP2PNotificationChannel; | 280 notification.channel = kSyncP2PNotificationChannel; |
294 notification.data = notification_data.ToString(); | 281 notification.data = notification_data.ToString(); |
295 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 282 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
296 push_client_->SendNotification(notification); | 283 push_client_->SendNotification(notification); |
297 } | 284 } |
298 | 285 |
299 } // namespace syncer | 286 } // namespace syncer |
OLD | NEW |