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/protocol/service_constants.h" |
| 9 #include "chrome/browser/sync/sync_constants.h" |
| 10 #include "jingle/notifier/listener/mediator_thread_impl.h" |
| 11 #include "jingle/notifier/listener/notification_constants.h" |
| 12 |
| 13 using notifier::TalkMediator; |
| 14 using notifier::TalkMediatorImpl; |
| 15 namespace sync_notifier { |
| 16 |
| 17 void SyncNotifierImpl::InitializeTalkMediator(const std::string& state, |
| 18 const notifier::NotifierOptions& notifier_options_) { |
| 19 } |
| 20 |
| 21 SyncNotifierImpl::SyncNotifierImpl( |
| 22 notifier::NotifierOptions* notifier_options) |
| 23 : notifier_options_(notifier_options) { } |
| 24 |
| 25 SyncNotifierImpl::~SyncNotifierImpl() { |
| 26 delete notifier_options_; |
| 27 } |
| 28 |
| 29 void SyncNotifierImpl::Login( |
| 30 const std::string& email, const std::string& token, |
| 31 const std::string& state, |
| 32 SyncNotifierCallback* sync_notifier_callback) { |
| 33 sync_notifier_callback_ = sync_notifier_callback; |
| 34 if (notifier_options_->notification_method == |
| 35 notifier::NOTIFICATION_SERVER) { |
| 36 |
| 37 // |talk_mediator_| takes ownership of |sync_notifier_thread_| |
| 38 // but it is guaranteed that |sync_notifier_thread_| is destroyed only |
| 39 // when |talk_mediator_| is (see the comments in talk_mediator.h). |
| 40 server_notifier_thread_ = new sync_notifier::ServerNotifierThread( |
| 41 *notifier_options_, state, this); |
| 42 talk_mediator_.reset( |
| 43 new TalkMediatorImpl(server_notifier_thread_, |
| 44 notifier_options_->invalidate_xmpp_login, |
| 45 notifier_options_->allow_insecure_connection)); |
| 46 |
| 47 // Since we may be initialized more than once, make sure that any |
| 48 // newly created server notifier thread has the latest enabled types. |
| 49 server_notifier_thread_->UpdateEnabledTypes(enabled_types_); |
| 50 } else { |
| 51 notifier::MediatorThread* mediator_thread = |
| 52 new notifier::MediatorThreadImpl(*notifier_options_); |
| 53 talk_mediator_.reset( |
| 54 new TalkMediatorImpl(mediator_thread, |
| 55 notifier_options_->invalidate_xmpp_login, |
| 56 notifier_options_->allow_insecure_connection)); |
| 57 talk_mediator_->AddSubscribedServiceUrl(browser_sync::kSyncServiceUrl); |
| 58 server_notifier_thread_ = NULL; |
| 59 } |
| 60 talk_mediator()->SetDelegate(this); |
| 61 talk_mediator()->SetAuthToken(email, token, SYNC_SERVICE_NAME); |
| 62 talk_mediator()->Login(); |
| 63 } |
| 64 |
| 65 void SyncNotifierImpl::UpdateEnabledTypes(const syncable::ModelTypeSet& types) { |
| 66 enabled_types_ = types; |
| 67 if (server_notifier_thread_ != NULL) { |
| 68 server_notifier_thread_->UpdateEnabledTypes(types); |
| 69 } |
| 70 } |
| 71 |
| 72 void SyncNotifierImpl::Logout() { |
| 73 // We NULL out talk_mediator_ so that any pending tasks do not |
| 74 // trigger further XMPP actions. |
| 75 // TODO(akalin): NULL the other member variables defensively, too. |
| 76 scoped_ptr<TalkMediator> talk_mediator(talk_mediator_.release()); |
| 77 |
| 78 // Shutdown the xmpp buzz connection. |
| 79 if (talk_mediator.get()) { |
| 80 VLOG(1) << "P2P: Mediator logout started."; |
| 81 talk_mediator->Logout(); |
| 82 VLOG(1) << "P2P: Mediator logout completed."; |
| 83 talk_mediator.reset(); |
| 84 |
| 85 // |server_notifier_thread_| is owned by |talk_mediator|. We NULL |
| 86 // it out here so as to not have a dangling pointer. |
| 87 server_notifier_thread_= NULL; |
| 88 VLOG(1) << "P2P: Mediator destroyed."; |
| 89 } |
| 90 } |
| 91 |
| 92 #if defined(UNIT_TEST) |
| 93 void SyncNotifierImpl::SendNotification() { |
| 94 if (!talk_mediator()) { |
| 95 VLOG(1) << "Not sending notification: shutting down (talk_mediator_ is " |
| 96 "NULL)"; |
| 97 return; |
| 98 } |
| 99 |
| 100 VLOG(1) << "Sending XMPP notification..."; |
| 101 OutgoingNotificationData notification_data; |
| 102 notification_data.service_id = browser_sync::kSyncServiceId; |
| 103 notification_data.service_url = browser_sync::kSyncServiceUrl; |
| 104 notification_data.send_content = true; |
| 105 notification_data.priority = browser_sync::kSyncPriority; |
| 106 notification_data.write_to_cache_only = true; |
| 107 notification_data.service_specific_data = |
| 108 browser_sync::kSyncServiceSpecificData; |
| 109 notification_data.require_subscription = true; |
| 110 bool success = talk_mediator()->SendNotification(notification_data); |
| 111 if (success) { |
| 112 notification_pending_ = false; |
| 113 VLOG(1) << "Sent XMPP notification"; |
| 114 } else { |
| 115 VLOG(1) << "Could not send XMPP notification"; |
| 116 } |
| 117 } |
| 118 #endif |
| 119 } // namespace sync_notifier |
OLD | NEW |