Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // | |
| 5 // Note: Since we need to shutdown TalkMediator on the method_thread, we are | |
| 6 // calling Logout on TalkMediator when the last observer is removed. | |
| 7 // Users will need to call UpdateCredentials again to use the same object. | |
| 4 | 8 |
| 5 #include "chrome/browser/sync/notifier/p2p_notifier.h" | 9 #include "chrome/browser/sync/notifier/p2p_notifier.h" |
| 6 | 10 |
| 7 #include "chrome/browser/sync/notifier/sync_notifier_observer.h" | 11 #include "chrome/browser/sync/notifier/sync_notifier_observer.h" |
| 8 #include "chrome/browser/sync/protocol/service_constants.h" | 12 #include "chrome/browser/sync/protocol/service_constants.h" |
| 9 #include "chrome/browser/sync/syncable/model_type_payload_map.h" | 13 #include "chrome/browser/sync/syncable/model_type_payload_map.h" |
| 10 #include "jingle/notifier/listener/mediator_thread_impl.h" | 14 #include "jingle/notifier/listener/mediator_thread_impl.h" |
| 11 #include "jingle/notifier/listener/talk_mediator_impl.h" | 15 #include "jingle/notifier/listener/talk_mediator_impl.h" |
| 12 | 16 |
| 13 namespace sync_notifier { | 17 namespace sync_notifier { |
| 14 | 18 |
| 15 namespace { | 19 namespace { |
| 16 const char kSyncNotificationChannel[] = "http://www.google.com/chrome/sync"; | 20 const char kSyncNotificationChannel[] = "http://www.google.com/chrome/sync"; |
| 17 const char kSyncNotificationData[] = "sync-ping-p2p"; | 21 const char kSyncNotificationData[] = "sync-ping-p2p"; |
| 18 } // namespace | 22 } // namespace |
| 19 | 23 |
| 20 P2PNotifier::P2PNotifier( | 24 P2PNotifier::P2PNotifier( |
| 21 const notifier::NotifierOptions& notifier_options) | 25 const notifier::NotifierOptions& notifier_options) |
| 22 : talk_mediator_( | 26 : talk_mediator_( |
| 23 new notifier::TalkMediatorImpl( | 27 new notifier::TalkMediatorImpl( |
| 24 new notifier::MediatorThreadImpl(notifier_options), | 28 new notifier::MediatorThreadImpl(notifier_options), |
| 25 notifier_options)), | 29 notifier_options)), |
| 26 logged_in_(false), | 30 logged_in_(false), |
| 27 notifications_enabled_(false) { | 31 notifications_enabled_(false), |
| 32 construction_message_loop_(MessageLoop::current()), | |
| 33 method_message_loop_(NULL) { | |
| 28 talk_mediator_->SetDelegate(this); | 34 talk_mediator_->SetDelegate(this); |
| 29 } | 35 } |
| 30 | 36 |
| 31 P2PNotifier::~P2PNotifier() {} | 37 P2PNotifier::~P2PNotifier() { |
| 38 DCHECK_EQ(MessageLoop::current(), construction_message_loop_); | |
| 39 } | |
| 32 | 40 |
| 33 void P2PNotifier::AddObserver(SyncNotifierObserver* observer) { | 41 void P2PNotifier::AddObserver(SyncNotifierObserver* observer) { |
| 42 // Can be the first method to be called. Assign method_message_loop_ if | |
|
akalin
2011/04/05 22:02:46
See comment in TalkMediatorImpl
nilesh
2011/04/05 23:36:17
Done.
| |
| 43 // not already assigned. | |
| 44 if (!method_message_loop_) { | |
| 45 method_message_loop_ = MessageLoop::current(); | |
| 46 } else { | |
| 47 DCHECK_EQ(MessageLoop::current(), method_message_loop_); | |
| 48 } | |
| 34 observer_list_.AddObserver(observer); | 49 observer_list_.AddObserver(observer); |
| 35 } | 50 } |
| 36 | 51 |
| 37 void P2PNotifier::RemoveObserver(SyncNotifierObserver* observer) { | 52 void P2PNotifier::RemoveObserver(SyncNotifierObserver* observer) { |
| 53 DCHECK_EQ(MessageLoop::current(), method_message_loop_); | |
| 38 observer_list_.RemoveObserver(observer); | 54 observer_list_.RemoveObserver(observer); |
| 55 | |
| 56 // Logout after the last observer is removed. | |
| 57 if (observer_list_.size() == 0) { | |
| 58 talk_mediator_->Logout(); | |
| 59 } | |
| 39 } | 60 } |
| 40 | 61 |
| 41 void P2PNotifier::SetState(const std::string& state) {} | 62 void P2PNotifier::SetState(const std::string& state) { |
| 63 // Can be the first method to be called. Assign method_message_loop_ if | |
| 64 // not already assigned. | |
| 65 if (!method_message_loop_) { | |
| 66 method_message_loop_ = MessageLoop::current(); | |
| 67 } else { | |
| 68 DCHECK_EQ(MessageLoop::current(), method_message_loop_); | |
| 69 } | |
| 70 } | |
| 42 | 71 |
| 43 void P2PNotifier::UpdateCredentials( | 72 void P2PNotifier::UpdateCredentials( |
| 44 const std::string& email, const std::string& token) { | 73 const std::string& email, const std::string& token) { |
| 74 DCHECK_EQ(MessageLoop::current(), method_message_loop_); | |
| 45 // If already logged in, the new credentials will take effect on the | 75 // If already logged in, the new credentials will take effect on the |
| 46 // next reconnection. | 76 // next reconnection. |
| 47 talk_mediator_->SetAuthToken(email, token, SYNC_SERVICE_NAME); | 77 talk_mediator_->SetAuthToken(email, token, SYNC_SERVICE_NAME); |
| 48 if (!logged_in_) { | 78 if (!logged_in_) { |
| 49 if (!talk_mediator_->Login()) { | 79 if (!talk_mediator_->Login()) { |
| 50 LOG(DFATAL) << "Could not login for " << email; | 80 LOG(DFATAL) << "Could not login for " << email; |
| 51 return; | 81 return; |
| 52 } | 82 } |
| 53 | 83 |
| 54 notifier::Subscription subscription; | 84 notifier::Subscription subscription; |
| 55 subscription.channel = kSyncNotificationChannel; | 85 subscription.channel = kSyncNotificationChannel; |
| 56 // There may be some subtle issues around case sensitivity of the | 86 // There may be some subtle issues around case sensitivity of the |
| 57 // from field, but it doesn't matter too much since this is only | 87 // from field, but it doesn't matter too much since this is only |
| 58 // used in p2p mode (which is only used in testing). | 88 // used in p2p mode (which is only used in testing). |
| 59 subscription.from = email; | 89 subscription.from = email; |
| 60 talk_mediator_->AddSubscription(subscription); | 90 talk_mediator_->AddSubscription(subscription); |
| 61 | 91 |
| 62 logged_in_ = true; | 92 logged_in_ = true; |
| 63 } | 93 } |
| 64 } | 94 } |
| 65 | 95 |
| 66 void P2PNotifier::UpdateEnabledTypes(const syncable::ModelTypeSet& types) { | 96 void P2PNotifier::UpdateEnabledTypes(const syncable::ModelTypeSet& types) { |
| 97 DCHECK_EQ(MessageLoop::current(), method_message_loop_); | |
| 67 enabled_types_ = types; | 98 enabled_types_ = types; |
| 68 MaybeEmitNotification(); | 99 MaybeEmitNotification(); |
| 69 } | 100 } |
| 70 | 101 |
| 71 void P2PNotifier::SendNotification() { | 102 void P2PNotifier::SendNotification() { |
| 103 DCHECK_EQ(MessageLoop::current(), method_message_loop_); | |
| 72 VLOG(1) << "Sending XMPP notification..."; | 104 VLOG(1) << "Sending XMPP notification..."; |
| 73 notifier::Notification notification; | 105 notifier::Notification notification; |
| 74 notification.channel = kSyncNotificationChannel; | 106 notification.channel = kSyncNotificationChannel; |
| 75 notification.data = kSyncNotificationData; | 107 notification.data = kSyncNotificationData; |
| 76 bool success = talk_mediator_->SendNotification(notification); | 108 bool success = talk_mediator_->SendNotification(notification); |
| 77 if (success) { | 109 if (success) { |
| 78 VLOG(1) << "Sent XMPP notification"; | 110 VLOG(1) << "Sent XMPP notification"; |
| 79 } else { | 111 } else { |
| 80 VLOG(1) << "Could not send XMPP notification"; | 112 VLOG(1) << "Could not send XMPP notification"; |
| 81 } | 113 } |
| 82 } | 114 } |
| 83 | 115 |
| 84 void P2PNotifier::OnNotificationStateChange(bool notifications_enabled) { | 116 void P2PNotifier::OnNotificationStateChange(bool notifications_enabled) { |
| 117 DCHECK_EQ(MessageLoop::current(), method_message_loop_); | |
| 85 notifications_enabled_ = notifications_enabled; | 118 notifications_enabled_ = notifications_enabled; |
| 86 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | 119 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, |
| 87 OnNotificationStateChange(notifications_enabled_)); | 120 OnNotificationStateChange(notifications_enabled_)); |
| 88 MaybeEmitNotification(); | 121 MaybeEmitNotification(); |
| 89 } | 122 } |
| 90 | 123 |
| 91 void P2PNotifier::OnIncomingNotification( | 124 void P2PNotifier::OnIncomingNotification( |
| 92 const notifier::Notification& notification) { | 125 const notifier::Notification& notification) { |
| 126 DCHECK_EQ(MessageLoop::current(), method_message_loop_); | |
| 93 VLOG(1) << "Sync received P2P notification."; | 127 VLOG(1) << "Sync received P2P notification."; |
| 94 if (notification.channel != kSyncNotificationChannel) { | 128 if (notification.channel != kSyncNotificationChannel) { |
| 95 LOG(WARNING) << "Notification fron unexpected source: " | 129 LOG(WARNING) << "Notification fron unexpected source: " |
| 96 << notification.channel; | 130 << notification.channel; |
| 97 } | 131 } |
| 98 MaybeEmitNotification(); | 132 MaybeEmitNotification(); |
| 99 } | 133 } |
| 100 | 134 |
| 101 void P2PNotifier::OnOutgoingNotification() {} | 135 void P2PNotifier::OnOutgoingNotification() {} |
| 102 | 136 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 114 return; | 148 return; |
| 115 } | 149 } |
| 116 syncable::ModelTypePayloadMap type_payloads = | 150 syncable::ModelTypePayloadMap type_payloads = |
| 117 syncable::ModelTypePayloadMapFromBitSet( | 151 syncable::ModelTypePayloadMapFromBitSet( |
| 118 syncable::ModelTypeBitSetFromSet(enabled_types_), std::string()); | 152 syncable::ModelTypeBitSetFromSet(enabled_types_), std::string()); |
| 119 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | 153 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, |
| 120 OnIncomingNotification(type_payloads)); | 154 OnIncomingNotification(type_payloads)); |
| 121 } | 155 } |
| 122 | 156 |
| 123 } // namespace sync_notifier | 157 } // namespace sync_notifier |
| OLD | NEW |