| OLD | NEW |
| 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/invalidation_notifier.h" | 5 #include "sync/notifier/invalidation_notifier.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "jingle/notifier/base/const_communicator.h" | 9 #include "jingle/notifier/listener/push_client.h" |
| 10 #include "jingle/notifier/base/notifier_options_util.h" | |
| 11 #include "net/url_request/url_request_context.h" | 10 #include "net/url_request/url_request_context.h" |
| 12 #include "sync/notifier/sync_notifier_observer.h" | 11 #include "sync/notifier/sync_notifier_observer.h" |
| 13 #include "sync/syncable/model_type_payload_map.h" | 12 #include "sync/syncable/model_type_payload_map.h" |
| 14 #include "talk/xmpp/jid.h" | 13 #include "talk/xmpp/jid.h" |
| 15 #include "talk/xmpp/xmppclientsettings.h" | 14 #include "talk/xmpp/xmppclientsettings.h" |
| 16 | 15 |
| 17 namespace sync_notifier { | 16 namespace sync_notifier { |
| 18 | 17 |
| 19 InvalidationNotifier::InvalidationNotifier( | 18 InvalidationNotifier::InvalidationNotifier( |
| 20 const notifier::NotifierOptions& notifier_options, | 19 scoped_ptr<notifier::PushClient> push_client, |
| 21 const InvalidationVersionMap& initial_max_invalidation_versions, | 20 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 22 const browser_sync::WeakHandle<InvalidationStateTracker>& | 21 const browser_sync::WeakHandle<InvalidationStateTracker>& |
| 23 invalidation_state_tracker, | 22 invalidation_state_tracker, |
| 24 const std::string& client_info) | 23 const std::string& client_info) |
| 25 : state_(STOPPED), | 24 : state_(STOPPED), |
| 26 notifier_options_(notifier_options), | |
| 27 initial_max_invalidation_versions_(initial_max_invalidation_versions), | 25 initial_max_invalidation_versions_(initial_max_invalidation_versions), |
| 28 invalidation_state_tracker_(invalidation_state_tracker), | 26 invalidation_state_tracker_(invalidation_state_tracker), |
| 29 client_info_(client_info) { | 27 client_info_(client_info), |
| 30 DCHECK_EQ(notifier::NOTIFICATION_SERVER, | 28 invalidation_client_(push_client.Pass()) { |
| 31 notifier_options.notification_method); | |
| 32 DCHECK(notifier_options_.request_context_getter); | |
| 33 // TODO(akalin): Replace NonThreadSafe checks with IO thread checks. | |
| 34 DCHECK(notifier_options_.request_context_getter->GetIOMessageLoopProxy()-> | |
| 35 BelongsToCurrentThread()); | |
| 36 } | 29 } |
| 37 | 30 |
| 38 InvalidationNotifier::~InvalidationNotifier() { | 31 InvalidationNotifier::~InvalidationNotifier() { |
| 39 DCHECK(non_thread_safe_.CalledOnValidThread()); | 32 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 40 } | 33 } |
| 41 | 34 |
| 42 void InvalidationNotifier::AddObserver(SyncNotifierObserver* observer) { | 35 void InvalidationNotifier::AddObserver(SyncNotifierObserver* observer) { |
| 43 DCHECK(non_thread_safe_.CalledOnValidThread()); | 36 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 44 observers_.AddObserver(observer); | 37 observers_.AddObserver(observer); |
| 45 } | 38 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 57 } | 50 } |
| 58 | 51 |
| 59 void InvalidationNotifier::SetState(const std::string& state) { | 52 void InvalidationNotifier::SetState(const std::string& state) { |
| 60 DCHECK(non_thread_safe_.CalledOnValidThread()); | 53 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 61 invalidation_state_ = state; | 54 invalidation_state_ = state; |
| 62 DVLOG(1) << "Setting new state"; | 55 DVLOG(1) << "Setting new state"; |
| 63 } | 56 } |
| 64 | 57 |
| 65 void InvalidationNotifier::UpdateCredentials( | 58 void InvalidationNotifier::UpdateCredentials( |
| 66 const std::string& email, const std::string& token) { | 59 const std::string& email, const std::string& token) { |
| 67 DCHECK(non_thread_safe_.CalledOnValidThread()); | 60 if (state_ == STOPPED) { |
| 68 CHECK(!invalidation_client_id_.empty()); | 61 invalidation_client_.Start( |
| 69 DVLOG(1) << "Updating credentials for " << email; | 62 invalidation_client_id_, client_info_, invalidation_state_, |
| 70 buzz::XmppClientSettings xmpp_client_settings = | 63 initial_max_invalidation_versions_, |
| 71 notifier::MakeXmppClientSettings(notifier_options_, email, token); | 64 invalidation_state_tracker_, |
| 72 if (state_ >= CONNECTING) { | 65 this, this); |
| 73 login_->UpdateXmppSettings(xmpp_client_settings); | 66 invalidation_state_.clear(); |
| 74 } else { | 67 state_ = STARTED; |
| 75 DVLOG(1) << "First time updating credentials: connecting"; | |
| 76 login_.reset( | |
| 77 new notifier::Login(this, | |
| 78 xmpp_client_settings, | |
| 79 notifier_options_.request_context_getter, | |
| 80 notifier::GetServerList(notifier_options_), | |
| 81 notifier_options_.try_ssltcp_first, | |
| 82 notifier_options_.auth_mechanism)); | |
| 83 login_->StartConnection(); | |
| 84 state_ = CONNECTING; | |
| 85 } | 68 } |
| 69 invalidation_client_.UpdateCredentials(email, token); |
| 86 } | 70 } |
| 87 | 71 |
| 88 void InvalidationNotifier::UpdateEnabledTypes( | 72 void InvalidationNotifier::UpdateEnabledTypes( |
| 89 syncable::ModelTypeSet enabled_types) { | 73 syncable::ModelTypeSet enabled_types) { |
| 90 DCHECK(non_thread_safe_.CalledOnValidThread()); | 74 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 91 CHECK(!invalidation_client_id_.empty()); | 75 CHECK(!invalidation_client_id_.empty()); |
| 92 invalidation_client_.RegisterTypes(enabled_types); | 76 invalidation_client_.RegisterTypes(enabled_types); |
| 93 } | 77 } |
| 94 | 78 |
| 95 void InvalidationNotifier::SendNotification( | 79 void InvalidationNotifier::SendNotification( |
| 96 syncable::ModelTypeSet changed_types) { | 80 syncable::ModelTypeSet changed_types) { |
| 97 DCHECK(non_thread_safe_.CalledOnValidThread()); | 81 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 98 // Do nothing. | 82 // Do nothing. |
| 99 } | 83 } |
| 100 | 84 |
| 101 void InvalidationNotifier::OnConnect( | |
| 102 base::WeakPtr<buzz::XmppTaskParentInterface> base_task) { | |
| 103 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 104 DVLOG(1) << "OnConnect"; | |
| 105 if (state_ >= STARTED) { | |
| 106 invalidation_client_.ChangeBaseTask(base_task); | |
| 107 } else { | |
| 108 DVLOG(1) << "First time connecting: starting invalidation client with id " | |
| 109 << invalidation_client_id_ << " and client info " | |
| 110 << client_info_; | |
| 111 invalidation_client_.Start( | |
| 112 invalidation_client_id_, client_info_, invalidation_state_, | |
| 113 initial_max_invalidation_versions_, | |
| 114 invalidation_state_tracker_, | |
| 115 this, this, base_task); | |
| 116 invalidation_state_.clear(); | |
| 117 state_ = STARTED; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void InvalidationNotifier::OnDisconnect() { | |
| 122 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 123 DVLOG(1) << "OnDisconnect"; | |
| 124 } | |
| 125 | |
| 126 void InvalidationNotifier::OnInvalidate( | 85 void InvalidationNotifier::OnInvalidate( |
| 127 const syncable::ModelTypePayloadMap& type_payloads) { | 86 const syncable::ModelTypePayloadMap& type_payloads) { |
| 128 DCHECK(non_thread_safe_.CalledOnValidThread()); | 87 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 129 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 88 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, |
| 130 OnIncomingNotification(type_payloads, | 89 OnIncomingNotification(type_payloads, |
| 131 sync_notifier::REMOTE_NOTIFICATION)); | 90 sync_notifier::REMOTE_NOTIFICATION)); |
| 132 } | 91 } |
| 133 | 92 |
| 134 void InvalidationNotifier::OnSessionStatusChanged(bool has_session) { | 93 void InvalidationNotifier::OnSessionStatusChanged(bool has_session) { |
| 135 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 94 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, |
| 136 OnNotificationStateChange(has_session)); | 95 OnNotificationStateChange(has_session)); |
| 137 } | 96 } |
| 138 | 97 |
| 139 void InvalidationNotifier::WriteState(const std::string& state) { | 98 void InvalidationNotifier::WriteState(const std::string& state) { |
| 140 DCHECK(non_thread_safe_.CalledOnValidThread()); | 99 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 141 DVLOG(1) << "WriteState"; | 100 DVLOG(1) << "WriteState"; |
| 142 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, StoreState(state)); | 101 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, StoreState(state)); |
| 143 } | 102 } |
| 144 | 103 |
| 145 } // namespace sync_notifier | 104 } // namespace sync_notifier |
| OLD | NEW |