OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sync/notifier/sync_notifier_impl.h" |
| 6 |
| 7 #include "chrome/browser/sync/notifier/server_notifier_thread.h" |
| 8 #include "chrome/browser/sync/notifier/sync_notifier_observer.h" |
| 9 #include "chrome/browser/sync/protocol/service_constants.h" |
| 10 #include "chrome/browser/sync/sessions/session_state.h" |
| 11 #include "chrome/browser/sync/syncable/model_type.h" |
| 12 #include "chrome/browser/sync/sync_constants.h" |
| 13 #include "jingle/notifier/listener/mediator_thread_impl.h" |
| 14 #include "jingle/notifier/listener/notification_constants.h" |
| 15 |
| 16 // TODO(akalin): Split this class into two implementations - one for p2p and |
| 17 // one for server issued notifications. |
| 18 using notifier::TalkMediator; |
| 19 using notifier::TalkMediatorImpl; |
| 20 namespace sync_notifier { |
| 21 |
| 22 SyncNotifierImpl::SyncNotifierImpl( |
| 23 const notifier::NotifierOptions& notifier_options) |
| 24 : notifier_options_(notifier_options), |
| 25 server_notifier_thread_(NULL) { } |
| 26 |
| 27 SyncNotifierImpl::~SyncNotifierImpl() { |
| 28 scoped_ptr<TalkMediator> talk_mediator(talk_mediator_.release()); |
| 29 |
| 30 // Shutdown the xmpp buzz connection. |
| 31 if (talk_mediator.get()) { |
| 32 VLOG(1) << "P2P: Mediator logout started."; |
| 33 talk_mediator->Logout(); |
| 34 VLOG(1) << "P2P: Mediator logout completed."; |
| 35 talk_mediator.reset(); |
| 36 |
| 37 // |server_notifier_thread_| is owned by |talk_mediator|. We NULL |
| 38 // it out here so as to not have a dangling pointer. |
| 39 server_notifier_thread_= NULL; |
| 40 VLOG(1) << "P2P: Mediator destroyed."; |
| 41 } |
| 42 } |
| 43 |
| 44 void SyncNotifierImpl::AddObserver(SyncNotifierObserver* observer) { |
| 45 observer_list_.AddObserver(observer); |
| 46 } |
| 47 |
| 48 void SyncNotifierImpl::RemoveObserver(SyncNotifierObserver* observer) { |
| 49 observer_list_.RemoveObserver(observer); |
| 50 } |
| 51 |
| 52 void SyncNotifierImpl::OnNotificationStateChange(bool notifications_enabled) { |
| 53 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, |
| 54 OnNotificationStateChange(notifications_enabled)); |
| 55 |
| 56 // If using p2p notifications, generate a notification for all enabled types. |
| 57 // Used only for tests. |
| 58 if ((notifier_options_.notification_method != |
| 59 notifier::NOTIFICATION_SERVER) && notifications_enabled) { |
| 60 browser_sync::sessions::TypePayloadMap model_types_with_payloads = |
| 61 browser_sync::sessions::MakeTypePayloadMapFromBitSet( |
| 62 syncable::ModelTypeBitSetFromSet(enabled_types_), std::string()); |
| 63 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, |
| 64 OnIncomingNotification(model_types_with_payloads)); |
| 65 } |
| 66 } |
| 67 |
| 68 void SyncNotifierImpl::OnIncomingNotification( |
| 69 const IncomingNotificationData& notification_data) { |
| 70 browser_sync::sessions::TypePayloadMap model_types_with_payloads; |
| 71 |
| 72 // Check if the service url is a sync URL. An empty service URL is |
| 73 // treated as a legacy sync notification. If we're listening to |
| 74 // server-issued notifications, no need to check the service_url. |
| 75 if (notifier_options_.notification_method == |
| 76 notifier::NOTIFICATION_SERVER) { |
| 77 VLOG(1) << "Sync received server notification from " << |
| 78 notification_data.service_url << ": " << |
| 79 notification_data.service_specific_data; |
| 80 syncable::ModelTypeBitSet model_types; |
| 81 const std::string& model_type_list = notification_data.service_url; |
| 82 const std::string& notification_payload = |
| 83 notification_data.service_specific_data; |
| 84 |
| 85 if (!syncable::ModelTypeBitSetFromString(model_type_list, &model_types)) { |
| 86 LOG(DFATAL) << "Could not extract model types from server data."; |
| 87 model_types.set(); |
| 88 } |
| 89 |
| 90 model_types_with_payloads = |
| 91 browser_sync::sessions::MakeTypePayloadMapFromBitSet(model_types, |
| 92 notification_payload); |
| 93 } else if (notification_data.service_url.empty() || |
| 94 (notification_data.service_url == |
| 95 browser_sync::kSyncLegacyServiceUrl) || |
| 96 (notification_data.service_url == |
| 97 browser_sync::kSyncServiceUrl)) { |
| 98 VLOG(1) << "Sync received P2P notification."; |
| 99 |
| 100 // Catch for sync integration tests (uses p2p). Just set all enabled |
| 101 // datatypes. |
| 102 model_types_with_payloads = |
| 103 browser_sync::sessions::MakeTypePayloadMapFromBitSet( |
| 104 syncable::ModelTypeBitSetFromSet(enabled_types_), std::string()); |
| 105 } else { |
| 106 LOG(WARNING) << "Notification fron unexpected source: " |
| 107 << notification_data.service_url; |
| 108 } |
| 109 |
| 110 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, |
| 111 OnIncomingNotification(model_types_with_payloads)); |
| 112 } |
| 113 |
| 114 void SyncNotifierImpl::WriteState(const std::string& state) { |
| 115 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, |
| 116 StoreState(state)); |
| 117 } |
| 118 |
| 119 void SyncNotifierImpl::UpdateCredentials( |
| 120 const std::string& email, const std::string& token) { |
| 121 // Reset talk_mediator_ before creating ServerNotifierThread/MediatorThread, |
| 122 // to avoid any problems with having two of those threads at the same time. |
| 123 talk_mediator_.reset(); |
| 124 |
| 125 if (notifier_options_.notification_method == |
| 126 notifier::NOTIFICATION_SERVER) { |
| 127 |
| 128 // |talk_mediator_| takes ownership of |sync_notifier_thread_| |
| 129 // but it is guaranteed that |sync_notifier_thread_| is destroyed only |
| 130 // when |talk_mediator_| is (see the comments in talk_mediator.h). |
| 131 server_notifier_thread_ = new sync_notifier::ServerNotifierThread( |
| 132 notifier_options_, state_, this); |
| 133 talk_mediator_.reset( |
| 134 new TalkMediatorImpl(server_notifier_thread_, |
| 135 notifier_options_.invalidate_xmpp_login, |
| 136 notifier_options_.allow_insecure_connection)); |
| 137 |
| 138 // Since we may be initialized more than once, make sure that any |
| 139 // newly created server notifier thread has the latest enabled types. |
| 140 server_notifier_thread_->UpdateEnabledTypes(enabled_types_); |
| 141 } else { |
| 142 notifier::MediatorThread* mediator_thread = |
| 143 new notifier::MediatorThreadImpl(notifier_options_); |
| 144 talk_mediator_.reset( |
| 145 new TalkMediatorImpl(mediator_thread, |
| 146 notifier_options_.invalidate_xmpp_login, |
| 147 notifier_options_.allow_insecure_connection)); |
| 148 talk_mediator_->AddSubscribedServiceUrl(browser_sync::kSyncServiceUrl); |
| 149 server_notifier_thread_ = NULL; |
| 150 } |
| 151 talk_mediator_->SetDelegate(this); |
| 152 talk_mediator_->SetAuthToken(email, token, SYNC_SERVICE_NAME); |
| 153 talk_mediator_->Login(); |
| 154 } |
| 155 |
| 156 void SyncNotifierImpl::SetState(const std::string& state) { |
| 157 state_ = state; |
| 158 } |
| 159 |
| 160 void SyncNotifierImpl::UpdateEnabledTypes(const syncable::ModelTypeSet& types) { |
| 161 enabled_types_ = types; |
| 162 if (server_notifier_thread_ != NULL) { |
| 163 server_notifier_thread_->UpdateEnabledTypes(types); |
| 164 } |
| 165 } |
| 166 |
| 167 void SyncNotifierImpl::SendNotification() { |
| 168 // Do nothing if we are using server based notifications. |
| 169 if (notifier_options_.notification_method == |
| 170 notifier::NOTIFICATION_SERVER) { |
| 171 return; |
| 172 } |
| 173 |
| 174 if (!talk_mediator_.get()) { |
| 175 NOTREACHED() << "Cannot send notification: talk_mediator_ is NULL"; |
| 176 return; |
| 177 } |
| 178 |
| 179 VLOG(1) << "Sending XMPP notification..."; |
| 180 OutgoingNotificationData notification_data; |
| 181 notification_data.service_id = browser_sync::kSyncServiceId; |
| 182 notification_data.service_url = browser_sync::kSyncServiceUrl; |
| 183 notification_data.send_content = true; |
| 184 notification_data.priority = browser_sync::kSyncPriority; |
| 185 notification_data.write_to_cache_only = true; |
| 186 notification_data.service_specific_data = |
| 187 browser_sync::kSyncServiceSpecificData; |
| 188 notification_data.require_subscription = true; |
| 189 bool success = talk_mediator_->SendNotification(notification_data); |
| 190 if (success) { |
| 191 VLOG(1) << "Sent XMPP notification"; |
| 192 } else { |
| 193 VLOG(1) << "Could not send XMPP notification"; |
| 194 } |
| 195 } |
| 196 } // namespace sync_notifier |
OLD | NEW |