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_invalidator.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_state_map.h" | 14 #include "sync/internal_api/public/base/model_type_state_map.h" |
| 15 #include "sync/notifier/invalidation_handler.h" |
15 #include "sync/notifier/invalidation_util.h" | 16 #include "sync/notifier/invalidation_util.h" |
16 #include "sync/notifier/sync_notifier_observer.h" | |
17 | 17 |
18 namespace syncer { | 18 namespace syncer { |
19 | 19 |
20 const char* kSyncP2PNotificationChannel = "http://www.google.com/chrome/sync"; | 20 const char* kSyncP2PNotificationChannel = "http://www.google.com/chrome/sync"; |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 const char kNotifySelf[] = "notifySelf"; | 24 const char kNotifySelf[] = "notifySelf"; |
25 const char kNotifyOthers[] = "notifyOthers"; | 25 const char kNotifyOthers[] = "notifyOthers"; |
26 const char kNotifyAll[] = "notifyAll"; | 26 const char kNotifyAll[] = "notifyAll"; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 ListValue* changed_types_list = NULL; | 134 ListValue* changed_types_list = NULL; |
135 if (!data_dict->GetList(kChangedTypesKey, &changed_types_list)) { | 135 if (!data_dict->GetList(kChangedTypesKey, &changed_types_list)) { |
136 LOG(WARNING) << "Could not find list value for " | 136 LOG(WARNING) << "Could not find list value for " |
137 << kChangedTypesKey; | 137 << kChangedTypesKey; |
138 return false; | 138 return false; |
139 } | 139 } |
140 changed_types_ = ModelTypeSetFromValue(*changed_types_list); | 140 changed_types_ = ModelTypeSetFromValue(*changed_types_list); |
141 return true; | 141 return true; |
142 } | 142 } |
143 | 143 |
144 P2PNotifier::P2PNotifier(scoped_ptr<notifier::PushClient> push_client, | 144 P2PInvalidator::P2PInvalidator(scoped_ptr<notifier::PushClient> push_client, |
145 P2PNotificationTarget send_notification_target) | 145 P2PNotificationTarget send_notification_target) |
146 : push_client_(push_client.Pass()), | 146 : push_client_(push_client.Pass()), |
147 logged_in_(false), | 147 logged_in_(false), |
148 notifications_enabled_(false), | 148 notifications_enabled_(false), |
149 send_notification_target_(send_notification_target) { | 149 send_notification_target_(send_notification_target) { |
150 DCHECK(send_notification_target_ == NOTIFY_OTHERS || | 150 DCHECK(send_notification_target_ == NOTIFY_OTHERS || |
151 send_notification_target_ == NOTIFY_ALL); | 151 send_notification_target_ == NOTIFY_ALL); |
152 push_client_->AddObserver(this); | 152 push_client_->AddObserver(this); |
153 } | 153 } |
154 | 154 |
155 P2PNotifier::~P2PNotifier() { | 155 P2PInvalidator::~P2PInvalidator() { |
156 DCHECK(thread_checker_.CalledOnValidThread()); | 156 DCHECK(thread_checker_.CalledOnValidThread()); |
157 push_client_->RemoveObserver(this); | 157 push_client_->RemoveObserver(this); |
158 } | 158 } |
159 | 159 |
160 void P2PNotifier::RegisterHandler(SyncNotifierObserver* handler) { | 160 void P2PInvalidator::RegisterHandler(InvalidationHandler* handler) { |
161 DCHECK(thread_checker_.CalledOnValidThread()); | 161 DCHECK(thread_checker_.CalledOnValidThread()); |
162 registrar_.RegisterHandler(handler); | 162 registrar_.RegisterHandler(handler); |
163 } | 163 } |
164 | 164 |
165 void P2PNotifier::UpdateRegisteredIds(SyncNotifierObserver* handler, | 165 void P2PInvalidator::UpdateRegisteredIds(InvalidationHandler* handler, |
166 const ObjectIdSet& ids) { | 166 const ObjectIdSet& ids) { |
167 // TODO(akalin): Handle arbitrary object IDs (http://crbug.com/140411). | 167 // TODO(akalin): Handle arbitrary object IDs (http://crbug.com/140411). |
168 DCHECK(thread_checker_.CalledOnValidThread()); | 168 DCHECK(thread_checker_.CalledOnValidThread()); |
169 registrar_.UpdateRegisteredIds(handler, ids); | 169 registrar_.UpdateRegisteredIds(handler, ids); |
170 const ModelTypeSet enabled_types = | 170 const ModelTypeSet enabled_types = |
171 ObjectIdSetToModelTypeSet(registrar_.GetAllRegisteredIds()); | 171 ObjectIdSetToModelTypeSet(registrar_.GetAllRegisteredIds()); |
172 const ModelTypeSet new_enabled_types = | 172 const ModelTypeSet new_enabled_types = |
173 Difference(enabled_types, enabled_types_); | 173 Difference(enabled_types, enabled_types_); |
174 const P2PNotificationData notification_data( | 174 const P2PNotificationData notification_data( |
175 unique_id_, NOTIFY_SELF, new_enabled_types); | 175 unique_id_, NOTIFY_SELF, new_enabled_types); |
176 SendNotificationData(notification_data); | 176 SendNotificationData(notification_data); |
177 enabled_types_ = enabled_types; | 177 enabled_types_ = enabled_types; |
178 } | 178 } |
179 | 179 |
180 void P2PNotifier::UnregisterHandler(SyncNotifierObserver* handler) { | 180 void P2PInvalidator::UnregisterHandler(InvalidationHandler* handler) { |
181 DCHECK(thread_checker_.CalledOnValidThread()); | 181 DCHECK(thread_checker_.CalledOnValidThread()); |
182 registrar_.UnregisterHandler(handler); | 182 registrar_.UnregisterHandler(handler); |
183 } | 183 } |
184 | 184 |
185 void P2PNotifier::SetUniqueId(const std::string& unique_id) { | 185 void P2PInvalidator::SetUniqueId(const std::string& unique_id) { |
186 DCHECK(thread_checker_.CalledOnValidThread()); | 186 DCHECK(thread_checker_.CalledOnValidThread()); |
187 unique_id_ = unique_id; | 187 unique_id_ = unique_id; |
188 } | 188 } |
189 | 189 |
190 void P2PNotifier::SetStateDeprecated(const std::string& state) { | 190 void P2PInvalidator::SetStateDeprecated(const std::string& state) { |
191 DCHECK(thread_checker_.CalledOnValidThread()); | 191 DCHECK(thread_checker_.CalledOnValidThread()); |
192 // Do nothing. | 192 // Do nothing. |
193 } | 193 } |
194 | 194 |
195 void P2PNotifier::UpdateCredentials( | 195 void P2PInvalidator::UpdateCredentials( |
196 const std::string& email, const std::string& token) { | 196 const std::string& email, const std::string& token) { |
197 DCHECK(thread_checker_.CalledOnValidThread()); | 197 DCHECK(thread_checker_.CalledOnValidThread()); |
198 notifier::Subscription subscription; | 198 notifier::Subscription subscription; |
199 subscription.channel = kSyncP2PNotificationChannel; | 199 subscription.channel = kSyncP2PNotificationChannel; |
200 // There may be some subtle issues around case sensitivity of the | 200 // There may be some subtle issues around case sensitivity of the |
201 // from field, but it doesn't matter too much since this is only | 201 // from field, but it doesn't matter too much since this is only |
202 // used in p2p mode (which is only used in testing). | 202 // used in p2p mode (which is only used in testing). |
203 subscription.from = email; | 203 subscription.from = email; |
204 push_client_->UpdateSubscriptions( | 204 push_client_->UpdateSubscriptions( |
205 notifier::SubscriptionList(1, subscription)); | 205 notifier::SubscriptionList(1, subscription)); |
206 // If already logged in, the new credentials will take effect on the | 206 // If already logged in, the new credentials will take effect on the |
207 // next reconnection. | 207 // next reconnection. |
208 push_client_->UpdateCredentials(email, token); | 208 push_client_->UpdateCredentials(email, token); |
209 logged_in_ = true; | 209 logged_in_ = true; |
210 } | 210 } |
211 | 211 |
212 void P2PNotifier::SendNotification(ModelTypeSet changed_types) { | 212 void P2PInvalidator::SendNotification(ModelTypeSet changed_types) { |
213 DCHECK(thread_checker_.CalledOnValidThread()); | 213 DCHECK(thread_checker_.CalledOnValidThread()); |
214 const P2PNotificationData notification_data( | 214 const P2PNotificationData notification_data( |
215 unique_id_, send_notification_target_, changed_types); | 215 unique_id_, send_notification_target_, changed_types); |
216 SendNotificationData(notification_data); | 216 SendNotificationData(notification_data); |
217 } | 217 } |
218 | 218 |
219 void P2PNotifier::OnNotificationsEnabled() { | 219 void P2PInvalidator::OnNotificationsEnabled() { |
220 DCHECK(thread_checker_.CalledOnValidThread()); | 220 DCHECK(thread_checker_.CalledOnValidThread()); |
221 bool just_turned_on = (notifications_enabled_ == false); | 221 bool just_turned_on = (notifications_enabled_ == false); |
222 notifications_enabled_ = true; | 222 notifications_enabled_ = true; |
223 registrar_.EmitOnNotificationsEnabled(); | 223 registrar_.EmitOnNotificationsEnabled(); |
224 if (just_turned_on) { | 224 if (just_turned_on) { |
225 const P2PNotificationData notification_data( | 225 const P2PNotificationData notification_data( |
226 unique_id_, NOTIFY_SELF, enabled_types_); | 226 unique_id_, NOTIFY_SELF, enabled_types_); |
227 SendNotificationData(notification_data); | 227 SendNotificationData(notification_data); |
228 } | 228 } |
229 } | 229 } |
230 | 230 |
231 void P2PNotifier::OnNotificationsDisabled( | 231 void P2PInvalidator::OnNotificationsDisabled( |
232 notifier::NotificationsDisabledReason reason) { | 232 notifier::NotificationsDisabledReason reason) { |
233 DCHECK(thread_checker_.CalledOnValidThread()); | 233 DCHECK(thread_checker_.CalledOnValidThread()); |
234 registrar_.EmitOnNotificationsDisabled(FromNotifierReason(reason)); | 234 registrar_.EmitOnNotificationsDisabled(FromNotifierReason(reason)); |
235 } | 235 } |
236 | 236 |
237 void P2PNotifier::OnIncomingNotification( | 237 void P2PInvalidator::OnIncomingNotification( |
238 const notifier::Notification& notification) { | 238 const notifier::Notification& notification) { |
239 DCHECK(thread_checker_.CalledOnValidThread()); | 239 DCHECK(thread_checker_.CalledOnValidThread()); |
240 DVLOG(1) << "Received notification " << notification.ToString(); | 240 DVLOG(1) << "Received notification " << notification.ToString(); |
241 if (!logged_in_) { | 241 if (!logged_in_) { |
242 DVLOG(1) << "Not logged in yet -- not emitting notification"; | 242 DVLOG(1) << "Not logged in yet -- not emitting notification"; |
243 return; | 243 return; |
244 } | 244 } |
245 if (!notifications_enabled_) { | 245 if (!notifications_enabled_) { |
246 DVLOG(1) << "Notifications not on -- not emitting notification"; | 246 DVLOG(1) << "Notifications not on -- not emitting notification"; |
247 return; | 247 return; |
(...skipping 20 matching lines...) Expand all Loading... |
268 DVLOG(1) << "No enabled and changed types -- not emitting notification"; | 268 DVLOG(1) << "No enabled and changed types -- not emitting notification"; |
269 return; | 269 return; |
270 } | 270 } |
271 const ModelTypeStateMap& type_state_map = ModelTypeSetToStateMap( | 271 const ModelTypeStateMap& type_state_map = ModelTypeSetToStateMap( |
272 notification_data.GetChangedTypes(), std::string()); | 272 notification_data.GetChangedTypes(), std::string()); |
273 registrar_.DispatchInvalidationsToHandlers( | 273 registrar_.DispatchInvalidationsToHandlers( |
274 ModelTypeStateMapToObjectIdStateMap(type_state_map), | 274 ModelTypeStateMapToObjectIdStateMap(type_state_map), |
275 REMOTE_NOTIFICATION); | 275 REMOTE_NOTIFICATION); |
276 } | 276 } |
277 | 277 |
278 void P2PNotifier::SendNotificationDataForTest( | 278 void P2PInvalidator::SendNotificationDataForTest( |
279 const P2PNotificationData& notification_data) { | 279 const P2PNotificationData& notification_data) { |
280 DCHECK(thread_checker_.CalledOnValidThread()); | 280 DCHECK(thread_checker_.CalledOnValidThread()); |
281 SendNotificationData(notification_data); | 281 SendNotificationData(notification_data); |
282 } | 282 } |
283 | 283 |
284 void P2PNotifier::SendNotificationData( | 284 void P2PInvalidator::SendNotificationData( |
285 const P2PNotificationData& notification_data) { | 285 const P2PNotificationData& notification_data) { |
286 DCHECK(thread_checker_.CalledOnValidThread()); | 286 DCHECK(thread_checker_.CalledOnValidThread()); |
287 if (notification_data.GetChangedTypes().Empty()) { | 287 if (notification_data.GetChangedTypes().Empty()) { |
288 DVLOG(1) << "Not sending XMPP notification with no changed types: " | 288 DVLOG(1) << "Not sending XMPP notification with no changed types: " |
289 << notification_data.ToString(); | 289 << notification_data.ToString(); |
290 return; | 290 return; |
291 } | 291 } |
292 notifier::Notification notification; | 292 notifier::Notification notification; |
293 notification.channel = kSyncP2PNotificationChannel; | 293 notification.channel = kSyncP2PNotificationChannel; |
294 notification.data = notification_data.ToString(); | 294 notification.data = notification_data.ToString(); |
295 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); | 295 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); |
296 push_client_->SendNotification(notification); | 296 push_client_->SendNotification(notification); |
297 } | 297 } |
298 | 298 |
299 } // namespace syncer | 299 } // namespace syncer |
OLD | NEW |