| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/invalidation/impl/sync_system_resources.h" | 5 #include "components/invalidation/impl/sync_system_resources.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 7 #include <cstdlib> | 8 #include <cstdlib> |
| 8 #include <cstring> | 9 #include <cstring> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <utility> | 11 #include <utility> |
| 11 | 12 |
| 12 #include "base/bind.h" | 13 #include "base/bind.h" |
| 13 #include "base/location.h" | 14 #include "base/location.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 17 #include "base/stl_util.h" | |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "components/invalidation/impl/gcm_network_channel.h" | 21 #include "components/invalidation/impl/gcm_network_channel.h" |
| 22 #include "components/invalidation/impl/gcm_network_channel_delegate.h" | 22 #include "components/invalidation/impl/gcm_network_channel_delegate.h" |
| 23 #include "components/invalidation/impl/push_client_channel.h" | 23 #include "components/invalidation/impl/push_client_channel.h" |
| 24 #include "components/invalidation/public/invalidation_util.h" | 24 #include "components/invalidation/public/invalidation_util.h" |
| 25 #include "google/cacheinvalidation/deps/callback.h" | 25 #include "google/cacheinvalidation/deps/callback.h" |
| 26 #include "google/cacheinvalidation/include/types.h" | 26 #include "google/cacheinvalidation/include/types.h" |
| 27 #include "jingle/notifier/listener/push_client.h" | 27 #include "jingle/notifier/listener/push_client.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 is_started_ = true; | 86 is_started_ = true; |
| 87 is_stopped_ = false; | 87 is_stopped_ = false; |
| 88 weak_factory_.InvalidateWeakPtrs(); | 88 weak_factory_.InvalidateWeakPtrs(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void SyncInvalidationScheduler::Stop() { | 91 void SyncInvalidationScheduler::Stop() { |
| 92 CHECK(IsRunningOnThread()); | 92 CHECK(IsRunningOnThread()); |
| 93 is_stopped_ = true; | 93 is_stopped_ = true; |
| 94 is_started_ = false; | 94 is_started_ = false; |
| 95 weak_factory_.InvalidateWeakPtrs(); | 95 weak_factory_.InvalidateWeakPtrs(); |
| 96 base::STLDeleteElements(&posted_tasks_); | 96 posted_tasks_.clear(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void SyncInvalidationScheduler::Schedule(invalidation::TimeDelta delay, | 99 void SyncInvalidationScheduler::Schedule(invalidation::TimeDelta delay, |
| 100 invalidation::Closure* task) { | 100 invalidation::Closure* task) { |
| 101 DCHECK(invalidation::IsCallbackRepeatable(task)); | 101 DCHECK(invalidation::IsCallbackRepeatable(task)); |
| 102 CHECK(IsRunningOnThread()); | 102 CHECK(IsRunningOnThread()); |
| 103 | 103 |
| 104 if (!is_started_) { | 104 if (!is_started_) { |
| 105 delete task; | 105 delete task; |
| 106 return; | 106 return; |
| 107 } | 107 } |
| 108 | 108 |
| 109 posted_tasks_.insert(task); | 109 posted_tasks_.insert(base::WrapUnique(task)); |
| 110 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 110 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 111 FROM_HERE, base::Bind(&SyncInvalidationScheduler::RunPostedTask, | 111 FROM_HERE, base::Bind(&SyncInvalidationScheduler::RunPostedTask, |
| 112 weak_factory_.GetWeakPtr(), task), | 112 weak_factory_.GetWeakPtr(), task), |
| 113 delay); | 113 delay); |
| 114 } | 114 } |
| 115 | 115 |
| 116 bool SyncInvalidationScheduler::IsRunningOnThread() const { | 116 bool SyncInvalidationScheduler::IsRunningOnThread() const { |
| 117 return created_on_task_runner_->BelongsToCurrentThread(); | 117 return created_on_task_runner_->BelongsToCurrentThread(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 invalidation::Time SyncInvalidationScheduler::GetCurrentTime() const { | 120 invalidation::Time SyncInvalidationScheduler::GetCurrentTime() const { |
| 121 CHECK(IsRunningOnThread()); | 121 CHECK(IsRunningOnThread()); |
| 122 return base::Time::Now(); | 122 return base::Time::Now(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void SyncInvalidationScheduler::SetSystemResources( | 125 void SyncInvalidationScheduler::SetSystemResources( |
| 126 invalidation::SystemResources* resources) { | 126 invalidation::SystemResources* resources) { |
| 127 // Do nothing. | 127 // Do nothing. |
| 128 } | 128 } |
| 129 | 129 |
| 130 void SyncInvalidationScheduler::RunPostedTask(invalidation::Closure* task) { | 130 void SyncInvalidationScheduler::RunPostedTask(invalidation::Closure* task) { |
| 131 CHECK(IsRunningOnThread()); | 131 CHECK(IsRunningOnThread()); |
| 132 task->Run(); | 132 task->Run(); |
| 133 posted_tasks_.erase(task); | 133 auto it = |
| 134 delete task; | 134 std::find_if(posted_tasks_.begin(), posted_tasks_.end(), |
| 135 [task](const std::unique_ptr<invalidation::Closure>& ptr) { |
| 136 return ptr.get() == task; |
| 137 }); |
| 138 posted_tasks_.erase(it); |
| 135 } | 139 } |
| 136 | 140 |
| 137 SyncNetworkChannel::SyncNetworkChannel() | 141 SyncNetworkChannel::SyncNetworkChannel() |
| 138 : last_network_status_(false), | 142 : last_network_status_(false), |
| 139 received_messages_count_(0) {} | 143 received_messages_count_(0) {} |
| 140 | 144 |
| 141 SyncNetworkChannel::~SyncNetworkChannel() { | 145 SyncNetworkChannel::~SyncNetworkChannel() { |
| 142 base::STLDeleteElements(&network_status_receivers_); | |
| 143 } | 146 } |
| 144 | 147 |
| 145 void SyncNetworkChannel::SetMessageReceiver( | 148 void SyncNetworkChannel::SetMessageReceiver( |
| 146 invalidation::MessageCallback* incoming_receiver) { | 149 invalidation::MessageCallback* incoming_receiver) { |
| 147 incoming_receiver_.reset(incoming_receiver); | 150 incoming_receiver_.reset(incoming_receiver); |
| 148 } | 151 } |
| 149 | 152 |
| 150 void SyncNetworkChannel::AddNetworkStatusReceiver( | 153 void SyncNetworkChannel::AddNetworkStatusReceiver( |
| 151 invalidation::NetworkStatusCallback* network_status_receiver) { | 154 invalidation::NetworkStatusCallback* network_status_receiver) { |
| 152 network_status_receiver->Run(last_network_status_); | 155 network_status_receiver->Run(last_network_status_); |
| 153 network_status_receivers_.push_back(network_status_receiver); | 156 network_status_receivers_.push_back( |
| 157 base::WrapUnique(network_status_receiver)); |
| 154 } | 158 } |
| 155 | 159 |
| 156 void SyncNetworkChannel::SetSystemResources( | 160 void SyncNetworkChannel::SetSystemResources( |
| 157 invalidation::SystemResources* resources) { | 161 invalidation::SystemResources* resources) { |
| 158 // Do nothing. | 162 // Do nothing. |
| 159 } | 163 } |
| 160 | 164 |
| 161 void SyncNetworkChannel::AddObserver(Observer* observer) { | 165 void SyncNetworkChannel::AddObserver(Observer* observer) { |
| 162 observers_.AddObserver(observer); | 166 observers_.AddObserver(observer); |
| 163 } | 167 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 177 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 181 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 178 std::unique_ptr<GCMNetworkChannelDelegate> delegate) { | 182 std::unique_ptr<GCMNetworkChannelDelegate> delegate) { |
| 179 return base::MakeUnique<GCMNetworkChannel>(request_context_getter, | 183 return base::MakeUnique<GCMNetworkChannel>(request_context_getter, |
| 180 std::move(delegate)); | 184 std::move(delegate)); |
| 181 } | 185 } |
| 182 | 186 |
| 183 void SyncNetworkChannel::NotifyNetworkStatusChange(bool online) { | 187 void SyncNetworkChannel::NotifyNetworkStatusChange(bool online) { |
| 184 // Remember network state for future NetworkStatusReceivers. | 188 // Remember network state for future NetworkStatusReceivers. |
| 185 last_network_status_ = online; | 189 last_network_status_ = online; |
| 186 // Notify NetworkStatusReceivers in cacheinvalidation. | 190 // Notify NetworkStatusReceivers in cacheinvalidation. |
| 187 for (NetworkStatusReceiverList::const_iterator it = | 191 for (const auto& receiver : network_status_receivers_) { |
| 188 network_status_receivers_.begin(); | 192 receiver->Run(online); |
| 189 it != network_status_receivers_.end(); ++it) { | |
| 190 (*it)->Run(online); | |
| 191 } | 193 } |
| 192 } | 194 } |
| 193 | 195 |
| 194 void SyncNetworkChannel::NotifyChannelStateChange( | 196 void SyncNetworkChannel::NotifyChannelStateChange( |
| 195 InvalidatorState invalidator_state) { | 197 InvalidatorState invalidator_state) { |
| 196 for (auto& observer : observers_) | 198 for (auto& observer : observers_) |
| 197 observer.OnNetworkChannelStateChanged(invalidator_state); | 199 observer.OnNetworkChannelStateChanged(invalidator_state); |
| 198 } | 200 } |
| 199 | 201 |
| 200 bool SyncNetworkChannel::DeliverIncomingMessage(const std::string& message) { | 202 bool SyncNetworkChannel::DeliverIncomingMessage(const std::string& message) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 223 | 225 |
| 224 void SyncStorage::WriteKey(const std::string& key, const std::string& value, | 226 void SyncStorage::WriteKey(const std::string& key, const std::string& value, |
| 225 invalidation::WriteKeyCallback* done) { | 227 invalidation::WriteKeyCallback* done) { |
| 226 CHECK(state_writer_); | 228 CHECK(state_writer_); |
| 227 // TODO(ghc): actually write key,value associations, and don't invoke the | 229 // TODO(ghc): actually write key,value associations, and don't invoke the |
| 228 // callback until the operation completes. | 230 // callback until the operation completes. |
| 229 state_writer_->WriteState(value); | 231 state_writer_->WriteState(value); |
| 230 cached_state_ = value; | 232 cached_state_ = value; |
| 231 // According to the cache invalidation API folks, we can do this as | 233 // According to the cache invalidation API folks, we can do this as |
| 232 // long as we make sure to clear the persistent state that we start | 234 // long as we make sure to clear the persistent state that we start |
| 233 // up the cache invalidation client with. However, we musn't do it | 235 // up the cache invalidation client with. However, we mustn't do it |
| 234 // right away, as we may be called under a lock that the callback | 236 // right away, as we may be called under a lock that the callback |
| 235 // uses. | 237 // uses. |
| 236 scheduler_->Schedule( | 238 scheduler_->Schedule( |
| 237 invalidation::Scheduler::NoDelay(), | 239 invalidation::Scheduler::NoDelay(), |
| 238 invalidation::NewPermanentCallback( | 240 invalidation::NewPermanentCallback( |
| 239 this, &SyncStorage::RunAndDeleteWriteKeyCallback, | 241 this, &SyncStorage::RunAndDeleteWriteKeyCallback, |
| 240 done)); | 242 done)); |
| 241 } | 243 } |
| 242 | 244 |
| 243 void SyncStorage::ReadKey(const std::string& key, | 245 void SyncStorage::ReadKey(const std::string& key, |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 | 331 |
| 330 SyncInvalidationScheduler* SyncSystemResources::internal_scheduler() { | 332 SyncInvalidationScheduler* SyncSystemResources::internal_scheduler() { |
| 331 return internal_scheduler_.get(); | 333 return internal_scheduler_.get(); |
| 332 } | 334 } |
| 333 | 335 |
| 334 SyncInvalidationScheduler* SyncSystemResources::listener_scheduler() { | 336 SyncInvalidationScheduler* SyncSystemResources::listener_scheduler() { |
| 335 return listener_scheduler_.get(); | 337 return listener_scheduler_.get(); |
| 336 } | 338 } |
| 337 | 339 |
| 338 } // namespace syncer | 340 } // namespace syncer |
| OLD | NEW |