| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "jingle/notifier/listener/push_client.h" | 13 #include "jingle/notifier/listener/push_client.h" |
| 14 #include "sync/internal_api/public/base/model_type_payload_map.h" | 14 #include "sync/internal_api/public/base/model_type_payload_map.h" |
| 15 #include "sync/notifier/invalidation_util.h" |
| 15 #include "sync/notifier/sync_notifier_observer.h" | 16 #include "sync/notifier/sync_notifier_observer.h" |
| 16 | 17 |
| 17 namespace syncer { | 18 namespace syncer { |
| 18 | 19 |
| 19 const char* kSyncP2PNotificationChannel = "http://www.google.com/chrome/sync"; | 20 const char* kSyncP2PNotificationChannel = "http://www.google.com/chrome/sync"; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 const char kNotifySelf[] = "notifySelf"; | 24 const char kNotifySelf[] = "notifySelf"; |
| 24 const char kNotifyOthers[] = "notifyOthers"; | 25 const char kNotifyOthers[] = "notifyOthers"; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 DCHECK(send_notification_target_ == NOTIFY_OTHERS || | 150 DCHECK(send_notification_target_ == NOTIFY_OTHERS || |
| 150 send_notification_target_ == NOTIFY_ALL); | 151 send_notification_target_ == NOTIFY_ALL); |
| 151 push_client_->AddObserver(this); | 152 push_client_->AddObserver(this); |
| 152 } | 153 } |
| 153 | 154 |
| 154 P2PNotifier::~P2PNotifier() { | 155 P2PNotifier::~P2PNotifier() { |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); | 156 DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 push_client_->RemoveObserver(this); | 157 push_client_->RemoveObserver(this); |
| 157 } | 158 } |
| 158 | 159 |
| 159 void P2PNotifier::AddObserver(SyncNotifierObserver* observer) { | 160 void P2PNotifier::UpdateRegisteredIds(SyncNotifierObserver* handler, |
| 160 DCHECK(thread_checker_.CalledOnValidThread()); | 161 const ObjectIdSet& ids) { |
| 161 observer_list_.AddObserver(observer); | 162 const ModelTypeSet enabled_types = ObjectIdSetToModelTypeSet( |
| 162 } | 163 helper_.UpdateRegisteredIds(handler, ids)); |
| 163 | 164 const ModelTypeSet new_enabled_types = |
| 164 void P2PNotifier::RemoveObserver(SyncNotifierObserver* observer) { | 165 Difference(enabled_types, enabled_types_); |
| 165 DCHECK(thread_checker_.CalledOnValidThread()); | 166 const P2PNotificationData notification_data( |
| 166 observer_list_.RemoveObserver(observer); | 167 unique_id_, NOTIFY_SELF, new_enabled_types); |
| 168 SendNotificationData(notification_data); |
| 169 enabled_types_ = enabled_types; |
| 167 } | 170 } |
| 168 | 171 |
| 169 void P2PNotifier::SetUniqueId(const std::string& unique_id) { | 172 void P2PNotifier::SetUniqueId(const std::string& unique_id) { |
| 170 DCHECK(thread_checker_.CalledOnValidThread()); | 173 DCHECK(thread_checker_.CalledOnValidThread()); |
| 171 unique_id_ = unique_id; | 174 unique_id_ = unique_id; |
| 172 } | 175 } |
| 173 | 176 |
| 174 void P2PNotifier::SetStateDeprecated(const std::string& state) { | 177 void P2PNotifier::SetStateDeprecated(const std::string& state) { |
| 175 DCHECK(thread_checker_.CalledOnValidThread()); | 178 DCHECK(thread_checker_.CalledOnValidThread()); |
| 176 // Do nothing. | 179 // Do nothing. |
| 177 } | 180 } |
| 178 | 181 |
| 179 void P2PNotifier::UpdateCredentials( | 182 void P2PNotifier::UpdateCredentials( |
| 180 const std::string& email, const std::string& token) { | 183 const std::string& email, const std::string& token) { |
| 181 DCHECK(thread_checker_.CalledOnValidThread()); | 184 DCHECK(thread_checker_.CalledOnValidThread()); |
| 182 notifier::Subscription subscription; | 185 notifier::Subscription subscription; |
| 183 subscription.channel = kSyncP2PNotificationChannel; | 186 subscription.channel = kSyncP2PNotificationChannel; |
| 184 // There may be some subtle issues around case sensitivity of the | 187 // There may be some subtle issues around case sensitivity of the |
| 185 // from field, but it doesn't matter too much since this is only | 188 // from field, but it doesn't matter too much since this is only |
| 186 // used in p2p mode (which is only used in testing). | 189 // used in p2p mode (which is only used in testing). |
| 187 subscription.from = email; | 190 subscription.from = email; |
| 188 push_client_->UpdateSubscriptions( | 191 push_client_->UpdateSubscriptions( |
| 189 notifier::SubscriptionList(1, subscription)); | 192 notifier::SubscriptionList(1, subscription)); |
| 190 // If already logged in, the new credentials will take effect on the | 193 // If already logged in, the new credentials will take effect on the |
| 191 // next reconnection. | 194 // next reconnection. |
| 192 push_client_->UpdateCredentials(email, token); | 195 push_client_->UpdateCredentials(email, token); |
| 193 logged_in_ = true; | 196 logged_in_ = true; |
| 194 } | 197 } |
| 195 | 198 |
| 196 void P2PNotifier::UpdateEnabledTypes(ModelTypeSet enabled_types) { | |
| 197 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 198 const ModelTypeSet new_enabled_types = | |
| 199 Difference(enabled_types, enabled_types_); | |
| 200 enabled_types_ = enabled_types; | |
| 201 const P2PNotificationData notification_data( | |
| 202 unique_id_, NOTIFY_SELF, new_enabled_types); | |
| 203 SendNotificationData(notification_data); | |
| 204 } | |
| 205 | |
| 206 void P2PNotifier::SendNotification(ModelTypeSet changed_types) { | 199 void P2PNotifier::SendNotification(ModelTypeSet changed_types) { |
| 207 DCHECK(thread_checker_.CalledOnValidThread()); | 200 DCHECK(thread_checker_.CalledOnValidThread()); |
| 208 const P2PNotificationData notification_data( | 201 const P2PNotificationData notification_data( |
| 209 unique_id_, send_notification_target_, changed_types); | 202 unique_id_, send_notification_target_, changed_types); |
| 210 SendNotificationData(notification_data); | 203 SendNotificationData(notification_data); |
| 211 } | 204 } |
| 212 | 205 |
| 213 void P2PNotifier::OnNotificationsEnabled() { | 206 void P2PNotifier::OnNotificationsEnabled() { |
| 214 DCHECK(thread_checker_.CalledOnValidThread()); | 207 DCHECK(thread_checker_.CalledOnValidThread()); |
| 215 bool just_turned_on = (notifications_enabled_ == false); | 208 bool just_turned_on = (notifications_enabled_ == false); |
| 216 notifications_enabled_ = true; | 209 notifications_enabled_ = true; |
| 217 FOR_EACH_OBSERVER( | 210 helper_.EmitOnNotificationsEnabled(); |
| 218 SyncNotifierObserver, observer_list_, | |
| 219 OnNotificationsEnabled()); | |
| 220 if (just_turned_on) { | 211 if (just_turned_on) { |
| 221 const P2PNotificationData notification_data( | 212 const P2PNotificationData notification_data( |
| 222 unique_id_, NOTIFY_SELF, enabled_types_); | 213 unique_id_, NOTIFY_SELF, enabled_types_); |
| 223 SendNotificationData(notification_data); | 214 SendNotificationData(notification_data); |
| 224 } | 215 } |
| 225 } | 216 } |
| 226 | 217 |
| 227 void P2PNotifier::OnNotificationsDisabled( | 218 void P2PNotifier::OnNotificationsDisabled( |
| 228 notifier::NotificationsDisabledReason reason) { | 219 notifier::NotificationsDisabledReason reason) { |
| 229 DCHECK(thread_checker_.CalledOnValidThread()); | 220 DCHECK(thread_checker_.CalledOnValidThread()); |
| 230 FOR_EACH_OBSERVER( | 221 helper_.EmitOnNotificationsDisabled(FromNotifierReason(reason)); |
| 231 SyncNotifierObserver, observer_list_, | |
| 232 OnNotificationsDisabled(FromNotifierReason(reason))); | |
| 233 } | 222 } |
| 234 | 223 |
| 235 void P2PNotifier::OnIncomingNotification( | 224 void P2PNotifier::OnIncomingNotification( |
| 236 const notifier::Notification& notification) { | 225 const notifier::Notification& notification) { |
| 237 DCHECK(thread_checker_.CalledOnValidThread()); | 226 DCHECK(thread_checker_.CalledOnValidThread()); |
| 238 DVLOG(1) << "Received notification " << notification.ToString(); | 227 DVLOG(1) << "Received notification " << notification.ToString(); |
| 239 if (!logged_in_) { | 228 if (!logged_in_) { |
| 240 DVLOG(1) << "Not logged in yet -- not emitting notification"; | 229 DVLOG(1) << "Not logged in yet -- not emitting notification"; |
| 241 return; | 230 return; |
| 242 } | 231 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 259 DVLOG(1) << "Not a target of the notification -- " | 248 DVLOG(1) << "Not a target of the notification -- " |
| 260 << "not emitting notification"; | 249 << "not emitting notification"; |
| 261 return; | 250 return; |
| 262 } | 251 } |
| 263 const ModelTypeSet types_to_notify = | 252 const ModelTypeSet types_to_notify = |
| 264 Intersection(enabled_types_, notification_data.GetChangedTypes()); | 253 Intersection(enabled_types_, notification_data.GetChangedTypes()); |
| 265 if (types_to_notify.Empty()) { | 254 if (types_to_notify.Empty()) { |
| 266 DVLOG(1) << "No enabled and changed types -- not emitting notification"; | 255 DVLOG(1) << "No enabled and changed types -- not emitting notification"; |
| 267 return; | 256 return; |
| 268 } | 257 } |
| 269 const ModelTypePayloadMap& type_payloads = | 258 const ModelTypePayloadMap& type_payloads = ModelTypePayloadMapFromEnumSet( |
| 270 ModelTypePayloadMapFromEnumSet(types_to_notify, std::string()); | 259 notification_data.GetChangedTypes(), std::string()); |
| 271 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | 260 helper_.DispatchInvalidationsToHandlers( |
| 272 OnIncomingNotification(type_payloads, REMOTE_NOTIFICATION)); | 261 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), |
| 262 REMOTE_NOTIFICATION); |
| 273 } | 263 } |
| 274 | 264 |
| 275 void P2PNotifier::SendNotificationDataForTest( | 265 void P2PNotifier::SendNotificationDataForTest( |
| 276 const P2PNotificationData& notification_data) { | 266 const P2PNotificationData& notification_data) { |
| 277 DCHECK(thread_checker_.CalledOnValidThread()); | 267 DCHECK(thread_checker_.CalledOnValidThread()); |
| 278 SendNotificationData(notification_data); | 268 SendNotificationData(notification_data); |
| 279 } | 269 } |
| 280 | 270 |
| 281 void P2PNotifier::SendNotificationData( | 271 void P2PNotifier::SendNotificationData( |
| 282 const P2PNotificationData& notification_data) { | 272 const P2PNotificationData& notification_data) { |
| 283 DCHECK(thread_checker_.CalledOnValidThread()); | 273 DCHECK(thread_checker_.CalledOnValidThread()); |
| 284 notifier::Notification notification; | 274 notifier::Notification notification; |
| 285 notification.channel = kSyncP2PNotificationChannel; | 275 notification.channel = kSyncP2PNotificationChannel; |
| 286 notification.data = notification_data.ToString(); | 276 notification.data = notification_data.ToString(); |
| 287 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 277 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
| 288 push_client_->SendNotification(notification); | 278 push_client_->SendNotification(notification); |
| 289 } | 279 } |
| 290 | 280 |
| 291 } // namespace syncer | 281 } // namespace syncer |
| OLD | NEW |