Chromium Code Reviews| 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( |
|
akalin
2012/07/19 00:42:06
const ref
dcheng
2012/07/19 18:31:05
Done.
| |
| 162 } | 163 helper_.UpdateRegisteredIds(handler, ids)); |
| 163 | 164 const ModelTypeSet new_enabled_types = |
|
akalin
2012/07/19 00:42:07
const ref
dcheng
2012/07/19 18:31:05
Done.
| |
| 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( | |
| 197 syncer::ModelTypeSet enabled_types) { | |
| 198 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 199 const syncer::ModelTypeSet new_enabled_types = | |
| 200 Difference(enabled_types, enabled_types_); | |
| 201 enabled_types_ = enabled_types; | |
| 202 const P2PNotificationData notification_data( | |
| 203 unique_id_, NOTIFY_SELF, new_enabled_types); | |
| 204 SendNotificationData(notification_data); | |
| 205 } | |
| 206 | |
| 207 void P2PNotifier::SendNotification( | 199 void P2PNotifier::SendNotification( |
| 208 syncer::ModelTypeSet changed_types) { | 200 syncer::ModelTypeSet changed_types) { |
| 209 DCHECK(thread_checker_.CalledOnValidThread()); | 201 DCHECK(thread_checker_.CalledOnValidThread()); |
| 210 const P2PNotificationData notification_data( | 202 const P2PNotificationData notification_data( |
| 211 unique_id_, send_notification_target_, changed_types); | 203 unique_id_, send_notification_target_, changed_types); |
| 212 SendNotificationData(notification_data); | 204 SendNotificationData(notification_data); |
| 213 } | 205 } |
| 214 | 206 |
| 215 void P2PNotifier::OnNotificationsEnabled() { | 207 void P2PNotifier::OnNotificationsEnabled() { |
| 216 DCHECK(thread_checker_.CalledOnValidThread()); | 208 DCHECK(thread_checker_.CalledOnValidThread()); |
| 217 bool just_turned_on = (notifications_enabled_ == false); | 209 bool just_turned_on = (notifications_enabled_ == false); |
| 218 notifications_enabled_ = true; | 210 notifications_enabled_ = true; |
| 219 FOR_EACH_OBSERVER( | 211 FOR_EACH_OBSERVER(SyncNotifierObserver, *helper_.handlers(), |
| 220 SyncNotifierObserver, observer_list_, | 212 OnNotificationsEnabled()); |
| 221 OnNotificationsEnabled()); | |
| 222 if (just_turned_on) { | 213 if (just_turned_on) { |
| 223 const P2PNotificationData notification_data( | 214 const P2PNotificationData notification_data( |
| 224 unique_id_, NOTIFY_SELF, enabled_types_); | 215 unique_id_, NOTIFY_SELF, enabled_types_); |
| 225 SendNotificationData(notification_data); | 216 SendNotificationData(notification_data); |
| 226 } | 217 } |
| 227 } | 218 } |
| 228 | 219 |
| 229 void P2PNotifier::OnNotificationsDisabled( | 220 void P2PNotifier::OnNotificationsDisabled( |
| 230 notifier::NotificationsDisabledReason reason) { | 221 notifier::NotificationsDisabledReason reason) { |
| 231 DCHECK(thread_checker_.CalledOnValidThread()); | 222 DCHECK(thread_checker_.CalledOnValidThread()); |
| 232 FOR_EACH_OBSERVER( | 223 FOR_EACH_OBSERVER(SyncNotifierObserver, *helper_.handlers(), |
| 233 SyncNotifierObserver, observer_list_, | 224 OnNotificationsDisabled(FromNotifierReason(reason))); |
| 234 OnNotificationsDisabled(FromNotifierReason(reason))); | |
| 235 } | 225 } |
| 236 | 226 |
| 237 void P2PNotifier::OnIncomingNotification( | 227 void P2PNotifier::OnIncomingNotification( |
| 238 const notifier::Notification& notification) { | 228 const notifier::Notification& notification) { |
| 239 DCHECK(thread_checker_.CalledOnValidThread()); | 229 DCHECK(thread_checker_.CalledOnValidThread()); |
| 240 DVLOG(1) << "Received notification " << notification.ToString(); | 230 DVLOG(1) << "Received notification " << notification.ToString(); |
| 241 if (!logged_in_) { | 231 if (!logged_in_) { |
| 242 DVLOG(1) << "Not logged in yet -- not emitting notification"; | 232 DVLOG(1) << "Not logged in yet -- not emitting notification"; |
| 243 return; | 233 return; |
| 244 } | 234 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 259 } | 249 } |
| 260 if (!notification_data.IsTargeted(unique_id_)) { | 250 if (!notification_data.IsTargeted(unique_id_)) { |
| 261 DVLOG(1) << "Not a target of the notification -- " | 251 DVLOG(1) << "Not a target of the notification -- " |
| 262 << "not emitting notification"; | 252 << "not emitting notification"; |
| 263 return; | 253 return; |
| 264 } | 254 } |
| 265 if (notification_data.GetChangedTypes().Empty()) { | 255 if (notification_data.GetChangedTypes().Empty()) { |
| 266 DVLOG(1) << "No changed types -- not emitting notification"; | 256 DVLOG(1) << "No changed types -- not emitting notification"; |
| 267 return; | 257 return; |
| 268 } | 258 } |
| 269 const syncer::ModelTypePayloadMap& type_payloads = | 259 const ModelTypePayloadMap& type_payloads = ModelTypePayloadMapFromEnumSet( |
| 270 syncer::ModelTypePayloadMapFromEnumSet( | 260 notification_data.GetChangedTypes(), std::string()); |
| 271 notification_data.GetChangedTypes(), std::string()); | 261 helper_.DispatchInvalidationsToHandlers( |
| 272 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | 262 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), |
| 273 OnIncomingNotification(type_payloads, REMOTE_NOTIFICATION)); | 263 REMOTE_NOTIFICATION); |
| 274 } | 264 } |
| 275 | 265 |
| 276 void P2PNotifier::SendNotificationDataForTest( | 266 void P2PNotifier::SendNotificationDataForTest( |
| 277 const P2PNotificationData& notification_data) { | 267 const P2PNotificationData& notification_data) { |
| 278 DCHECK(thread_checker_.CalledOnValidThread()); | 268 DCHECK(thread_checker_.CalledOnValidThread()); |
| 279 SendNotificationData(notification_data); | 269 SendNotificationData(notification_data); |
| 280 } | 270 } |
| 281 | 271 |
| 282 void P2PNotifier::SendNotificationData( | 272 void P2PNotifier::SendNotificationData( |
| 283 const P2PNotificationData& notification_data) { | 273 const P2PNotificationData& notification_data) { |
| 284 DCHECK(thread_checker_.CalledOnValidThread()); | 274 DCHECK(thread_checker_.CalledOnValidThread()); |
| 285 notifier::Notification notification; | 275 notifier::Notification notification; |
| 286 notification.channel = kSyncP2PNotificationChannel; | 276 notification.channel = kSyncP2PNotificationChannel; |
| 287 notification.data = notification_data.ToString(); | 277 notification.data = notification_data.ToString(); |
| 288 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 278 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
| 289 push_client_->SendNotification(notification); | 279 push_client_->SendNotification(notification); |
| 290 } | 280 } |
| 291 | 281 |
| 292 } // namespace syncer | 282 } // namespace syncer |
| OLD | NEW |