| 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 "base/metrics/histogram.h" |
| 9 #include "jingle/notifier/base/const_communicator.h" | 10 #include "jingle/notifier/base/const_communicator.h" |
| 10 #include "jingle/notifier/base/notifier_options_util.h" | 11 #include "jingle/notifier/base/notifier_options_util.h" |
| 11 #include "net/url_request/url_request_context.h" | 12 #include "net/url_request/url_request_context.h" |
| 12 #include "sync/notifier/sync_notifier_observer.h" | 13 #include "sync/notifier/sync_notifier_observer.h" |
| 13 #include "sync/syncable/model_type_payload_map.h" | 14 #include "sync/syncable/model_type_payload_map.h" |
| 14 #include "talk/xmpp/jid.h" | 15 #include "talk/xmpp/jid.h" |
| 15 #include "talk/xmpp/xmppclientsettings.h" | 16 #include "talk/xmpp/xmppclientsettings.h" |
| 16 | 17 |
| 17 namespace sync_notifier { | 18 namespace sync_notifier { |
| 18 | 19 |
| 19 InvalidationNotifier::InvalidationNotifier( | 20 InvalidationNotifier::InvalidationNotifier( |
| 20 const notifier::NotifierOptions& notifier_options, | 21 const notifier::NotifierOptions& notifier_options, |
| 21 const InvalidationVersionMap& initial_max_invalidation_versions, | 22 const InvalidationVersionMap& initial_max_invalidation_versions, |
| 23 const std::string& initial_invalidation_state, |
| 22 const browser_sync::WeakHandle<InvalidationStateTracker>& | 24 const browser_sync::WeakHandle<InvalidationStateTracker>& |
| 23 invalidation_state_tracker, | 25 invalidation_state_tracker, |
| 24 const std::string& client_info) | 26 const std::string& client_info) |
| 25 : state_(STOPPED), | 27 : state_(STOPPED), |
| 26 notifier_options_(notifier_options), | 28 notifier_options_(notifier_options), |
| 27 initial_max_invalidation_versions_(initial_max_invalidation_versions), | 29 initial_max_invalidation_versions_(initial_max_invalidation_versions), |
| 28 invalidation_state_tracker_(invalidation_state_tracker), | 30 invalidation_state_tracker_(invalidation_state_tracker), |
| 29 client_info_(client_info) { | 31 client_info_(client_info), |
| 32 invalidation_state_(initial_invalidation_state) { |
| 30 DCHECK_EQ(notifier::NOTIFICATION_SERVER, | 33 DCHECK_EQ(notifier::NOTIFICATION_SERVER, |
| 31 notifier_options.notification_method); | 34 notifier_options.notification_method); |
| 32 DCHECK(notifier_options_.request_context_getter); | 35 DCHECK(notifier_options_.request_context_getter); |
| 33 // TODO(akalin): Replace NonThreadSafe checks with IO thread checks. | 36 // TODO(akalin): Replace NonThreadSafe checks with IO thread checks. |
| 34 DCHECK(notifier_options_.request_context_getter->GetIOMessageLoopProxy()-> | 37 DCHECK(notifier_options_.request_context_getter->GetIOMessageLoopProxy()-> |
| 35 BelongsToCurrentThread()); | 38 BelongsToCurrentThread()); |
| 36 } | 39 } |
| 37 | 40 |
| 38 InvalidationNotifier::~InvalidationNotifier() { | 41 InvalidationNotifier::~InvalidationNotifier() { |
| 39 DCHECK(non_thread_safe_.CalledOnValidThread()); | 42 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 40 } | 43 } |
| 41 | 44 |
| 42 void InvalidationNotifier::AddObserver(SyncNotifierObserver* observer) { | 45 void InvalidationNotifier::AddObserver(SyncNotifierObserver* observer) { |
| 43 DCHECK(non_thread_safe_.CalledOnValidThread()); | 46 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 44 observers_.AddObserver(observer); | 47 observers_.AddObserver(observer); |
| 45 } | 48 } |
| 46 | 49 |
| 47 void InvalidationNotifier::RemoveObserver(SyncNotifierObserver* observer) { | 50 void InvalidationNotifier::RemoveObserver(SyncNotifierObserver* observer) { |
| 48 DCHECK(non_thread_safe_.CalledOnValidThread()); | 51 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 49 observers_.RemoveObserver(observer); | 52 observers_.RemoveObserver(observer); |
| 50 } | 53 } |
| 51 | 54 |
| 52 void InvalidationNotifier::SetUniqueId(const std::string& unique_id) { | 55 void InvalidationNotifier::SetUniqueId(const std::string& unique_id) { |
| 53 DCHECK(non_thread_safe_.CalledOnValidThread()); | 56 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 54 invalidation_client_id_ = unique_id; | 57 invalidation_client_id_ = unique_id; |
| 55 DVLOG(1) << "Setting unique ID to " << unique_id; | 58 DVLOG(1) << "Setting unique ID to " << unique_id; |
| 56 CHECK(!invalidation_client_id_.empty()); | 59 CHECK(!invalidation_client_id_.empty()); |
| 57 } | 60 } |
| 58 | 61 |
| 59 void InvalidationNotifier::SetState(const std::string& state) { | 62 void InvalidationNotifier::SetStateDeprecated(const std::string& state) { |
| 60 DCHECK(non_thread_safe_.CalledOnValidThread()); | 63 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 61 invalidation_state_ = state; | 64 DCHECK_LT(state_, STARTED); |
| 62 DVLOG(1) << "Setting new state"; | 65 if (invalidation_state_.empty()) { |
| 66 // Migrate state from sync to invalidation state tracker (bug |
| 67 // 124140). We've just been handed state from the syncable::Directory, and |
| 68 // the initial invalidation state was empty, implying we've never written |
| 69 // to the new store. Do this here to ensure we always migrate (even if |
| 70 // we fail to establish an initial connection or receive an initial |
| 71 // invalidation) so that we can make the old code obsolete as soon as |
| 72 // possible. |
| 73 invalidation_state_ = state; |
| 74 invalidation_state_tracker_.Call( |
| 75 FROM_HERE, &InvalidationStateTracker::SetInvalidationState, state); |
| 76 UMA_HISTOGRAM_BOOLEAN("InvalidationNotifier.UsefulSetState", true); |
| 77 } else { |
| 78 UMA_HISTOGRAM_BOOLEAN("InvalidationNotifier.UsefulSetState", false); |
| 79 } |
| 63 } | 80 } |
| 64 | 81 |
| 65 void InvalidationNotifier::UpdateCredentials( | 82 void InvalidationNotifier::UpdateCredentials( |
| 66 const std::string& email, const std::string& token) { | 83 const std::string& email, const std::string& token) { |
| 67 DCHECK(non_thread_safe_.CalledOnValidThread()); | 84 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 68 CHECK(!invalidation_client_id_.empty()); | 85 CHECK(!invalidation_client_id_.empty()); |
| 69 DVLOG(1) << "Updating credentials for " << email; | 86 DVLOG(1) << "Updating credentials for " << email; |
| 70 buzz::XmppClientSettings xmpp_client_settings = | 87 buzz::XmppClientSettings xmpp_client_settings = |
| 71 notifier::MakeXmppClientSettings(notifier_options_, email, token); | 88 notifier::MakeXmppClientSettings(notifier_options_, email, token); |
| 72 if (state_ >= CONNECTING) { | 89 if (state_ >= CONNECTING) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 if (state_ >= STARTED) { | 122 if (state_ >= STARTED) { |
| 106 invalidation_client_.ChangeBaseTask(base_task); | 123 invalidation_client_.ChangeBaseTask(base_task); |
| 107 } else { | 124 } else { |
| 108 DVLOG(1) << "First time connecting: starting invalidation client with id " | 125 DVLOG(1) << "First time connecting: starting invalidation client with id " |
| 109 << invalidation_client_id_ << " and client info " | 126 << invalidation_client_id_ << " and client info " |
| 110 << client_info_; | 127 << client_info_; |
| 111 invalidation_client_.Start( | 128 invalidation_client_.Start( |
| 112 invalidation_client_id_, client_info_, invalidation_state_, | 129 invalidation_client_id_, client_info_, invalidation_state_, |
| 113 initial_max_invalidation_versions_, | 130 initial_max_invalidation_versions_, |
| 114 invalidation_state_tracker_, | 131 invalidation_state_tracker_, |
| 115 this, this, base_task); | 132 this, base_task); |
| 116 invalidation_state_.clear(); | 133 invalidation_state_.clear(); |
| 117 state_ = STARTED; | 134 state_ = STARTED; |
| 118 } | 135 } |
| 119 } | 136 } |
| 120 | 137 |
| 121 void InvalidationNotifier::OnDisconnect() { | 138 void InvalidationNotifier::OnDisconnect() { |
| 122 DCHECK(non_thread_safe_.CalledOnValidThread()); | 139 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 123 DVLOG(1) << "OnDisconnect"; | 140 DVLOG(1) << "OnDisconnect"; |
| 124 } | 141 } |
| 125 | 142 |
| 126 void InvalidationNotifier::OnInvalidate( | 143 void InvalidationNotifier::OnInvalidate( |
| 127 const syncable::ModelTypePayloadMap& type_payloads) { | 144 const syncable::ModelTypePayloadMap& type_payloads) { |
| 128 DCHECK(non_thread_safe_.CalledOnValidThread()); | 145 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 129 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 146 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, |
| 130 OnIncomingNotification(type_payloads, | 147 OnIncomingNotification(type_payloads, |
| 131 sync_notifier::REMOTE_NOTIFICATION)); | 148 sync_notifier::REMOTE_NOTIFICATION)); |
| 132 } | 149 } |
| 133 | 150 |
| 134 void InvalidationNotifier::OnSessionStatusChanged(bool has_session) { | 151 void InvalidationNotifier::OnSessionStatusChanged(bool has_session) { |
| 135 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, | 152 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, |
| 136 OnNotificationStateChange(has_session)); | 153 OnNotificationStateChange(has_session)); |
| 137 } | 154 } |
| 138 | 155 |
| 139 void InvalidationNotifier::WriteState(const std::string& state) { | |
| 140 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 141 DVLOG(1) << "WriteState"; | |
| 142 FOR_EACH_OBSERVER(SyncNotifierObserver, observers_, StoreState(state)); | |
| 143 } | |
| 144 | |
| 145 } // namespace sync_notifier | 156 } // namespace sync_notifier |
| OLD | NEW |