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 14 matching lines...) Expand all Loading... |
257 } | 246 } |
258 if (!notification_data.IsTargeted(unique_id_)) { | 247 if (!notification_data.IsTargeted(unique_id_)) { |
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 if (notification_data.GetChangedTypes().Empty()) { | 252 if (notification_data.GetChangedTypes().Empty()) { |
264 DVLOG(1) << "No changed types -- not emitting notification"; | 253 DVLOG(1) << "No changed types -- not emitting notification"; |
265 return; | 254 return; |
266 } | 255 } |
267 const ModelTypePayloadMap& type_payloads = | 256 const ModelTypePayloadMap& type_payloads = ModelTypePayloadMapFromEnumSet( |
268 ModelTypePayloadMapFromEnumSet( | 257 notification_data.GetChangedTypes(), std::string()); |
269 notification_data.GetChangedTypes(), std::string()); | 258 helper_.DispatchInvalidationsToHandlers( |
270 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | 259 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), |
271 OnIncomingNotification(type_payloads, REMOTE_NOTIFICATION)); | 260 REMOTE_NOTIFICATION); |
272 } | 261 } |
273 | 262 |
274 void P2PNotifier::SendNotificationDataForTest( | 263 void P2PNotifier::SendNotificationDataForTest( |
275 const P2PNotificationData& notification_data) { | 264 const P2PNotificationData& notification_data) { |
276 DCHECK(thread_checker_.CalledOnValidThread()); | 265 DCHECK(thread_checker_.CalledOnValidThread()); |
277 SendNotificationData(notification_data); | 266 SendNotificationData(notification_data); |
278 } | 267 } |
279 | 268 |
280 void P2PNotifier::SendNotificationData( | 269 void P2PNotifier::SendNotificationData( |
281 const P2PNotificationData& notification_data) { | 270 const P2PNotificationData& notification_data) { |
282 DCHECK(thread_checker_.CalledOnValidThread()); | 271 DCHECK(thread_checker_.CalledOnValidThread()); |
283 notifier::Notification notification; | 272 notifier::Notification notification; |
284 notification.channel = kSyncP2PNotificationChannel; | 273 notification.channel = kSyncP2PNotificationChannel; |
285 notification.data = notification_data.ToString(); | 274 notification.data = notification_data.ToString(); |
286 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 275 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
287 push_client_->SendNotification(notification); | 276 push_client_->SendNotification(notification); |
288 } | 277 } |
289 | 278 |
290 } // namespace syncer | 279 } // namespace syncer |
OLD | NEW |