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