Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: sync/notifier/p2p_notifier.cc

Issue 10702074: Refactor sync-specific parts out of SyncNotifier/SyncNotifierObserver (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Minor cleanup Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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::AddHandler(SyncNotifierObserver* observer) {
160 DCHECK(thread_checker_.CalledOnValidThread()); 161 DCHECK(thread_checker_.CalledOnValidThread());
161 observer_list_.AddObserver(observer); 162 InvalidationNotifierBase::AddHandler(observer);
162 } 163 }
163 164
164 void P2PNotifier::RemoveObserver(SyncNotifierObserver* observer) { 165 void P2PNotifier::RemoveHandler(SyncNotifierObserver* observer) {
165 DCHECK(thread_checker_.CalledOnValidThread()); 166 DCHECK(thread_checker_.CalledOnValidThread());
166 observer_list_.RemoveObserver(observer); 167 InvalidationNotifierBase::RemoveHandler(observer);
168 }
169
170 void P2PNotifier::ReloadHandlers() {
171 const ObjectIdSet& enabled_ids = UpdateObjectIdObserverMap();
172 const ModelTypeSet& enabled_types = ObjectIdSetToModelTypeSet(enabled_ids);
173 const ModelTypeSet new_enabled_types =
174 Difference(enabled_types, enabled_types_);
175 enabled_types_ = enabled_types;
176 const P2PNotificationData notification_data(
177 unique_id_, NOTIFY_SELF, new_enabled_types);
178 SendNotificationData(notification_data);
167 } 179 }
168 180
169 void P2PNotifier::SetUniqueId(const std::string& unique_id) { 181 void P2PNotifier::SetUniqueId(const std::string& unique_id) {
170 DCHECK(thread_checker_.CalledOnValidThread()); 182 DCHECK(thread_checker_.CalledOnValidThread());
171 unique_id_ = unique_id; 183 unique_id_ = unique_id;
172 } 184 }
173 185
174 void P2PNotifier::SetStateDeprecated(const std::string& state) { 186 void P2PNotifier::SetStateDeprecated(const std::string& state) {
175 DCHECK(thread_checker_.CalledOnValidThread()); 187 DCHECK(thread_checker_.CalledOnValidThread());
176 // Do nothing. 188 // Do nothing.
177 } 189 }
178 190
179 void P2PNotifier::UpdateCredentials( 191 void P2PNotifier::UpdateCredentials(
180 const std::string& email, const std::string& token) { 192 const std::string& email, const std::string& token) {
181 DCHECK(thread_checker_.CalledOnValidThread()); 193 DCHECK(thread_checker_.CalledOnValidThread());
182 notifier::Subscription subscription; 194 notifier::Subscription subscription;
183 subscription.channel = kSyncP2PNotificationChannel; 195 subscription.channel = kSyncP2PNotificationChannel;
184 // There may be some subtle issues around case sensitivity of the 196 // 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 197 // from field, but it doesn't matter too much since this is only
186 // used in p2p mode (which is only used in testing). 198 // used in p2p mode (which is only used in testing).
187 subscription.from = email; 199 subscription.from = email;
188 push_client_->UpdateSubscriptions( 200 push_client_->UpdateSubscriptions(
189 notifier::SubscriptionList(1, subscription)); 201 notifier::SubscriptionList(1, subscription));
190 // If already logged in, the new credentials will take effect on the 202 // If already logged in, the new credentials will take effect on the
191 // next reconnection. 203 // next reconnection.
192 push_client_->UpdateCredentials(email, token); 204 push_client_->UpdateCredentials(email, token);
193 logged_in_ = true; 205 logged_in_ = true;
194 } 206 }
195 207
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( 208 void P2PNotifier::SendNotification(
208 syncer::ModelTypeSet changed_types) { 209 syncer::ModelTypeSet changed_types) {
209 DCHECK(thread_checker_.CalledOnValidThread()); 210 DCHECK(thread_checker_.CalledOnValidThread());
210 const P2PNotificationData notification_data( 211 const P2PNotificationData notification_data(
211 unique_id_, send_notification_target_, changed_types); 212 unique_id_, send_notification_target_, changed_types);
212 SendNotificationData(notification_data); 213 SendNotificationData(notification_data);
213 } 214 }
214 215
215 void P2PNotifier::OnNotificationsEnabled() { 216 void P2PNotifier::OnNotificationsEnabled() {
216 DCHECK(thread_checker_.CalledOnValidThread()); 217 DCHECK(thread_checker_.CalledOnValidThread());
217 bool just_turned_on = (notifications_enabled_ == false); 218 bool just_turned_on = (notifications_enabled_ == false);
218 notifications_enabled_ = true; 219 notifications_enabled_ = true;
219 FOR_EACH_OBSERVER( 220 FOR_EACH_OBSERVER(SyncNotifierObserver, observers(),
220 SyncNotifierObserver, observer_list_, 221 OnNotificationsEnabled());
221 OnNotificationsEnabled());
222 if (just_turned_on) { 222 if (just_turned_on) {
223 const P2PNotificationData notification_data( 223 const P2PNotificationData notification_data(
224 unique_id_, NOTIFY_SELF, enabled_types_); 224 unique_id_, NOTIFY_SELF, enabled_types_);
225 SendNotificationData(notification_data); 225 SendNotificationData(notification_data);
226 } 226 }
227 } 227 }
228 228
229 void P2PNotifier::OnNotificationsDisabled( 229 void P2PNotifier::OnNotificationsDisabled(
230 notifier::NotificationsDisabledReason reason) { 230 notifier::NotificationsDisabledReason reason) {
231 DCHECK(thread_checker_.CalledOnValidThread()); 231 DCHECK(thread_checker_.CalledOnValidThread());
232 FOR_EACH_OBSERVER( 232 FOR_EACH_OBSERVER(SyncNotifierObserver, observers(),
233 SyncNotifierObserver, observer_list_, 233 OnNotificationsDisabled(FromNotifierReason(reason)));
234 OnNotificationsDisabled(FromNotifierReason(reason)));
235 } 234 }
236 235
237 void P2PNotifier::OnIncomingNotification( 236 void P2PNotifier::OnIncomingNotification(
238 const notifier::Notification& notification) { 237 const notifier::Notification& notification) {
239 DCHECK(thread_checker_.CalledOnValidThread()); 238 DCHECK(thread_checker_.CalledOnValidThread());
240 DVLOG(1) << "Received notification " << notification.ToString(); 239 DVLOG(1) << "Received notification " << notification.ToString();
241 if (!logged_in_) { 240 if (!logged_in_) {
242 DVLOG(1) << "Not logged in yet -- not emitting notification"; 241 DVLOG(1) << "Not logged in yet -- not emitting notification";
243 return; 242 return;
244 } 243 }
(...skipping 14 matching lines...) Expand all
259 } 258 }
260 if (!notification_data.IsTargeted(unique_id_)) { 259 if (!notification_data.IsTargeted(unique_id_)) {
261 DVLOG(1) << "Not a target of the notification -- " 260 DVLOG(1) << "Not a target of the notification -- "
262 << "not emitting notification"; 261 << "not emitting notification";
263 return; 262 return;
264 } 263 }
265 if (notification_data.GetChangedTypes().Empty()) { 264 if (notification_data.GetChangedTypes().Empty()) {
266 DVLOG(1) << "No changed types -- not emitting notification"; 265 DVLOG(1) << "No changed types -- not emitting notification";
267 return; 266 return;
268 } 267 }
269 const syncer::ModelTypePayloadMap& type_payloads = 268 const ModelTypePayloadMap& type_payloads = ModelTypePayloadMapFromEnumSet(
270 syncer::ModelTypePayloadMapFromEnumSet( 269 notification_data.GetChangedTypes(), std::string());
271 notification_data.GetChangedTypes(), std::string()); 270 DispatchInvalidationsToObservers(
272 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, 271 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads),
273 OnIncomingNotification(type_payloads, REMOTE_NOTIFICATION)); 272 REMOTE_NOTIFICATION);
274 } 273 }
275 274
276 void P2PNotifier::SendNotificationDataForTest( 275 void P2PNotifier::SendNotificationDataForTest(
277 const P2PNotificationData& notification_data) { 276 const P2PNotificationData& notification_data) {
278 DCHECK(thread_checker_.CalledOnValidThread()); 277 DCHECK(thread_checker_.CalledOnValidThread());
279 SendNotificationData(notification_data); 278 SendNotificationData(notification_data);
280 } 279 }
281 280
282 void P2PNotifier::SendNotificationData( 281 void P2PNotifier::SendNotificationData(
283 const P2PNotificationData& notification_data) { 282 const P2PNotificationData& notification_data) {
284 DCHECK(thread_checker_.CalledOnValidThread()); 283 DCHECK(thread_checker_.CalledOnValidThread());
285 notifier::Notification notification; 284 notifier::Notification notification;
286 notification.channel = kSyncP2PNotificationChannel; 285 notification.channel = kSyncP2PNotificationChannel;
287 notification.data = notification_data.ToString(); 286 notification.data = notification_data.ToString();
288 DVLOG(1) << "Sending XMPP notification: " << notification.ToString(); 287 DVLOG(1) << "Sending XMPP notification: " << notification.ToString();
289 push_client_->SendNotification(notification); 288 push_client_->SendNotification(notification);
290 } 289 }
291 290
292 } // namespace syncer 291 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698