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( | |
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 helper_.OnNotificationsEnabled(); |
220 SyncNotifierObserver, observer_list_, | |
221 OnNotificationsEnabled()); | |
222 if (just_turned_on) { | 212 if (just_turned_on) { |
223 const P2PNotificationData notification_data( | 213 const P2PNotificationData notification_data( |
224 unique_id_, NOTIFY_SELF, enabled_types_); | 214 unique_id_, NOTIFY_SELF, enabled_types_); |
225 SendNotificationData(notification_data); | 215 SendNotificationData(notification_data); |
226 } | 216 } |
227 } | 217 } |
228 | 218 |
229 void P2PNotifier::OnNotificationsDisabled( | 219 void P2PNotifier::OnNotificationsDisabled( |
230 notifier::NotificationsDisabledReason reason) { | 220 notifier::NotificationsDisabledReason reason) { |
231 DCHECK(thread_checker_.CalledOnValidThread()); | 221 DCHECK(thread_checker_.CalledOnValidThread()); |
232 FOR_EACH_OBSERVER( | 222 helper_.OnNotificationsDisabled(FromNotifierReason(reason)); |
233 SyncNotifierObserver, observer_list_, | |
234 OnNotificationsDisabled(FromNotifierReason(reason))); | |
235 } | 223 } |
236 | 224 |
237 void P2PNotifier::OnIncomingNotification( | 225 void P2PNotifier::OnIncomingNotification( |
238 const notifier::Notification& notification) { | 226 const notifier::Notification& notification) { |
239 DCHECK(thread_checker_.CalledOnValidThread()); | 227 DCHECK(thread_checker_.CalledOnValidThread()); |
240 DVLOG(1) << "Received notification " << notification.ToString(); | 228 DVLOG(1) << "Received notification " << notification.ToString(); |
241 if (!logged_in_) { | 229 if (!logged_in_) { |
242 DVLOG(1) << "Not logged in yet -- not emitting notification"; | 230 DVLOG(1) << "Not logged in yet -- not emitting notification"; |
243 return; | 231 return; |
244 } | 232 } |
(...skipping 14 matching lines...) Expand all Loading... |
259 } | 247 } |
260 if (!notification_data.IsTargeted(unique_id_)) { | 248 if (!notification_data.IsTargeted(unique_id_)) { |
261 DVLOG(1) << "Not a target of the notification -- " | 249 DVLOG(1) << "Not a target of the notification -- " |
262 << "not emitting notification"; | 250 << "not emitting notification"; |
263 return; | 251 return; |
264 } | 252 } |
265 if (notification_data.GetChangedTypes().Empty()) { | 253 if (notification_data.GetChangedTypes().Empty()) { |
266 DVLOG(1) << "No changed types -- not emitting notification"; | 254 DVLOG(1) << "No changed types -- not emitting notification"; |
267 return; | 255 return; |
268 } | 256 } |
269 const syncer::ModelTypePayloadMap& type_payloads = | 257 const ModelTypePayloadMap& type_payloads = ModelTypePayloadMapFromEnumSet( |
270 syncer::ModelTypePayloadMapFromEnumSet( | 258 notification_data.GetChangedTypes(), std::string()); |
271 notification_data.GetChangedTypes(), std::string()); | 259 helper_.DispatchInvalidationsToHandlers( |
272 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | 260 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), |
273 OnIncomingNotification(type_payloads, REMOTE_NOTIFICATION)); | 261 REMOTE_NOTIFICATION); |
274 } | 262 } |
275 | 263 |
276 void P2PNotifier::SendNotificationDataForTest( | 264 void P2PNotifier::SendNotificationDataForTest( |
277 const P2PNotificationData& notification_data) { | 265 const P2PNotificationData& notification_data) { |
278 DCHECK(thread_checker_.CalledOnValidThread()); | 266 DCHECK(thread_checker_.CalledOnValidThread()); |
279 SendNotificationData(notification_data); | 267 SendNotificationData(notification_data); |
280 } | 268 } |
281 | 269 |
282 void P2PNotifier::SendNotificationData( | 270 void P2PNotifier::SendNotificationData( |
283 const P2PNotificationData& notification_data) { | 271 const P2PNotificationData& notification_data) { |
284 DCHECK(thread_checker_.CalledOnValidThread()); | 272 DCHECK(thread_checker_.CalledOnValidThread()); |
285 notifier::Notification notification; | 273 notifier::Notification notification; |
286 notification.channel = kSyncP2PNotificationChannel; | 274 notification.channel = kSyncP2PNotificationChannel; |
287 notification.data = notification_data.ToString(); | 275 notification.data = notification_data.ToString(); |
288 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 276 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
289 push_client_->SendNotification(notification); | 277 push_client_->SendNotification(notification); |
290 } | 278 } |
291 | 279 |
292 } // namespace syncer | 280 } // namespace syncer |
OLD | NEW |