Chromium Code Reviews| 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 using notifier::TalkMediator; | |
| 17 using notifier::TalkMediatorImpl; | |
| 18 namespace sync_notifier { | |
| 19 | |
| 20 SyncNotifierImpl::SyncNotifierImpl( | |
| 21 notifier::NotifierOptions* notifier_options) | |
| 22 : notifier_options_(notifier_options) { } | |
|
akalin
2011/03/11 04:17:14
null out server_notifier_thread_, too
Agrawal
2011/03/11 21:42:20
Done.
| |
| 23 | |
| 24 SyncNotifierImpl::~SyncNotifierImpl() { | |
| 25 // We NULL out talk_mediator_ so that any pending tasks do not | |
|
akalin
2011/03/11 04:17:14
this comment should be moved to SyncManager::Shutd
Agrawal
2011/03/11 21:42:20
Done.
| |
| 26 // trigger further XMPP actions. | |
| 27 // TODO(akalin): NULL the other member variables defensively, too. | |
| 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 delete notifier_options_; | |
| 44 } | |
| 45 | |
| 46 void SyncNotifierImpl::AddObserver(SyncNotifierObserver* observer) { | |
| 47 observer_list_.AddObserver(observer); | |
| 48 } | |
| 49 | |
| 50 void SyncNotifierImpl::RemoveObserver(SyncNotifierObserver* observer) { | |
| 51 observer_list_.RemoveObserver(observer); | |
| 52 } | |
| 53 | |
| 54 void SyncNotifierImpl::OnNotificationStateChange(bool notifications_enabled) { | |
| 55 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | |
| 56 OnNotificationStateChange(notifications_enabled)); | |
| 57 | |
| 58 // If using p2p notifications, generate a notification for all enabled types. | |
| 59 // Used only for tests. | |
| 60 if ((notifier_options_->notification_method != | |
| 61 notifier::NOTIFICATION_SERVER) && notifications_enabled) { | |
| 62 browser_sync::sessions::TypePayloadMap model_types_with_payloads; | |
| 63 model_types_with_payloads = | |
|
akalin
2011/03/11 04:17:14
combine w/ previous line
Agrawal
2011/03/11 21:42:20
Done.
| |
| 64 browser_sync::sessions::MakeTypePayloadMapFromBitSet( | |
| 65 syncable::ModelTypeBitSetFromSet(enabled_types_), std::string()); | |
| 66 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | |
| 67 OnIncomingNotification(model_types_with_payloads)); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 void SyncNotifierImpl::OnIncomingNotification( | |
| 72 const IncomingNotificationData& notification_data) { | |
| 73 browser_sync::sessions::TypePayloadMap model_types_with_payloads; | |
| 74 | |
| 75 // Check if the service url is a sync URL. An empty service URL is | |
| 76 // treated as a legacy sync notification. If we're listening to | |
| 77 // server-issued notifications, no need to check the service_url. | |
| 78 if (notifier_options_->notification_method == | |
| 79 notifier::NOTIFICATION_SERVER) { | |
| 80 VLOG(1) << "Sync received server notification from " << | |
| 81 notification_data.service_url << ": " << | |
| 82 notification_data.service_specific_data; | |
| 83 syncable::ModelTypeBitSet model_types; | |
| 84 const std::string& model_type_list = notification_data.service_url; | |
| 85 const std::string& notification_payload = | |
| 86 notification_data.service_specific_data; | |
| 87 | |
| 88 if (!syncable::ModelTypeBitSetFromString(model_type_list, &model_types)) { | |
| 89 LOG(DFATAL) << "Could not extract model types from server data."; | |
| 90 model_types.set(); | |
| 91 } | |
| 92 | |
| 93 model_types_with_payloads = | |
| 94 browser_sync::sessions::MakeTypePayloadMapFromBitSet(model_types, | |
| 95 notification_payload); | |
| 96 } else if (notification_data.service_url.empty() || | |
| 97 (notification_data.service_url == | |
| 98 browser_sync::kSyncLegacyServiceUrl) || | |
| 99 (notification_data.service_url == | |
| 100 browser_sync::kSyncServiceUrl)) { | |
| 101 VLOG(1) << "Sync received P2P notification."; | |
| 102 | |
| 103 // Catch for sync integration tests (uses p2p). Just set all enabled | |
| 104 // datatypes. | |
| 105 model_types_with_payloads = | |
| 106 browser_sync::sessions::MakeTypePayloadMapFromBitSet( | |
| 107 syncable::ModelTypeBitSetFromSet(enabled_types_), std::string()); | |
| 108 model_types_with_payloads = | |
|
akalin
2011/03/11 04:17:14
remove repeated line
Agrawal
2011/03/11 21:42:20
Done.
| |
| 109 browser_sync::sessions::MakeTypePayloadMapFromBitSet( | |
| 110 syncable::ModelTypeBitSetFromSet(enabled_types_), std::string()); | |
| 111 } else { | |
| 112 LOG(WARNING) << "Notification fron unexpected source: " | |
| 113 << notification_data.service_url; | |
| 114 } | |
| 115 | |
| 116 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | |
| 117 OnIncomingNotification(model_types_with_payloads)); | |
| 118 } | |
| 119 | |
| 120 void SyncNotifierImpl::WriteState(const std::string& state) { | |
| 121 FOR_EACH_OBSERVER(SyncNotifierObserver, observer_list_, | |
| 122 StoreState(state)); | |
| 123 } | |
| 124 | |
| 125 void SyncNotifierImpl::UpdateCredentials( | |
| 126 const std::string& email, const std::string& token) { | |
| 127 if (notifier_options_->notification_method == | |
|
akalin
2011/03/11 04:17:14
reset talk_mediator_ before creating the ServerNot
Agrawal
2011/03/11 21:42:20
Done. Do you think that we should call talk_mediat
akalin
2011/03/11 22:27:43
Nah, that's okay -- the destructor calls it if nec
| |
| 128 notifier::NOTIFICATION_SERVER) { | |
| 129 | |
| 130 // |talk_mediator_| takes ownership of |sync_notifier_thread_| | |
| 131 // but it is guaranteed that |sync_notifier_thread_| is destroyed only | |
| 132 // when |talk_mediator_| is (see the comments in talk_mediator.h). | |
| 133 server_notifier_thread_ = new sync_notifier::ServerNotifierThread( | |
| 134 *notifier_options_, state_, this); | |
| 135 talk_mediator_.reset( | |
| 136 new TalkMediatorImpl(server_notifier_thread_, | |
| 137 notifier_options_->invalidate_xmpp_login, | |
| 138 notifier_options_->allow_insecure_connection)); | |
| 139 | |
| 140 // Since we may be initialized more than once, make sure that any | |
| 141 // newly created server notifier thread has the latest enabled types. | |
| 142 server_notifier_thread_->UpdateEnabledTypes(enabled_types_); | |
| 143 } else { | |
| 144 notifier::MediatorThread* mediator_thread = | |
| 145 new notifier::MediatorThreadImpl(*notifier_options_); | |
| 146 talk_mediator_.reset( | |
| 147 new TalkMediatorImpl(mediator_thread, | |
| 148 notifier_options_->invalidate_xmpp_login, | |
| 149 notifier_options_->allow_insecure_connection)); | |
| 150 talk_mediator_->AddSubscribedServiceUrl(browser_sync::kSyncServiceUrl); | |
| 151 server_notifier_thread_ = NULL; | |
| 152 } | |
| 153 talk_mediator()->SetDelegate(this); | |
| 154 talk_mediator()->SetAuthToken(email, token, SYNC_SERVICE_NAME); | |
| 155 talk_mediator()->Login(); | |
| 156 } | |
| 157 | |
| 158 void SyncNotifierImpl::SetState(const std::string& state) { | |
| 159 state_ = state; | |
| 160 } | |
| 161 | |
| 162 void SyncNotifierImpl::UpdateEnabledTypes(const syncable::ModelTypeSet& types) { | |
| 163 enabled_types_ = types; | |
| 164 if (server_notifier_thread_ != NULL) { | |
| 165 server_notifier_thread_->UpdateEnabledTypes(types); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 void SyncNotifierImpl::SendNotification() { | |
| 170 // Do nothing if we are using server based notifications. | |
| 171 if (notifier_options_->notification_method == | |
| 172 notifier::NOTIFICATION_SERVER) { | |
| 173 return; | |
| 174 } | |
| 175 | |
| 176 if (!talk_mediator()) { | |
| 177 VLOG(1) << "Not sending notification: shutting down (talk_mediator_ is " | |
|
akalin
2011/03/11 04:17:14
replace this VLOG with NOTREACHED(), since it shou
Agrawal
2011/03/11 21:42:20
Done.
| |
| 178 "NULL)"; | |
| 179 return; | |
| 180 } | |
| 181 | |
| 182 VLOG(1) << "Sending XMPP notification..."; | |
| 183 OutgoingNotificationData notification_data; | |
| 184 notification_data.service_id = browser_sync::kSyncServiceId; | |
| 185 notification_data.service_url = browser_sync::kSyncServiceUrl; | |
| 186 notification_data.send_content = true; | |
| 187 notification_data.priority = browser_sync::kSyncPriority; | |
| 188 notification_data.write_to_cache_only = true; | |
| 189 notification_data.service_specific_data = | |
| 190 browser_sync::kSyncServiceSpecificData; | |
| 191 notification_data.require_subscription = true; | |
| 192 bool success = talk_mediator()->SendNotification(notification_data); | |
| 193 if (success) { | |
| 194 VLOG(1) << "Sent XMPP notification"; | |
| 195 } else { | |
| 196 VLOG(1) << "Could not send XMPP notification"; | |
| 197 } | |
| 198 } | |
| 199 } // namespace sync_notifier | |
| OLD | NEW |