blob: 05f1aee2226415162f294162c159f0f67d3f79c7 [file] [log] [blame]
[email protected]24c9ee52014-06-02 22:17:501// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]85164712010-06-23 01:13:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]44828772014-06-06 02:56:525#include "components/invalidation/sync_system_resources.h"
[email protected]85164712010-06-23 01:13:566
7#include <cstdlib>
[email protected]c16e90e2010-10-06 18:17:058#include <cstring>
[email protected]85164712010-06-23 01:13:569#include <string>
10
[email protected]d9d31532011-12-23 00:07:2011#include "base/bind.h"
[email protected]85164712010-06-23 01:13:5612#include "base/logging.h"
[email protected]a76295972013-07-18 00:42:3213#include "base/message_loop/message_loop.h"
[email protected]7286e3fc2011-07-19 22:13:2414#include "base/stl_util.h"
[email protected]a4bbc1f92013-06-11 07:28:1915#include "base/strings/string_util.h"
16#include "base/strings/stringprintf.h"
[email protected]44828772014-06-06 02:56:5217#include "components/invalidation/gcm_network_channel.h"
18#include "components/invalidation/gcm_network_channel_delegate.h"
[email protected]51766bf2014-07-24 01:13:4719#include "components/invalidation/invalidation_util.h"
[email protected]44828772014-06-06 02:56:5220#include "components/invalidation/push_client_channel.h"
[email protected]739cee32012-06-20 20:37:2821#include "google/cacheinvalidation/deps/callback.h"
[email protected]b0e1c132012-03-14 06:10:1322#include "google/cacheinvalidation/include/types.h"
[email protected]04a830a2014-01-04 02:48:5123#include "jingle/notifier/listener/push_client.h"
[email protected]85164712010-06-23 01:13:5624
[email protected]65f173552012-06-28 22:43:5825namespace syncer {
[email protected]85164712010-06-23 01:13:5626
[email protected]1f7893fa2012-09-06 03:06:1327SyncLogger::SyncLogger() {}
28SyncLogger::~SyncLogger() {}
[email protected]85164712010-06-23 01:13:5629
[email protected]1f7893fa2012-09-06 03:06:1330void SyncLogger::Log(LogLevel level, const char* file, int line,
31 const char* format, ...) {
[email protected]890de90272011-07-22 08:20:5232 logging::LogSeverity log_severity = -2; // VLOG(2)
33 bool emit_log = false;
[email protected]c16e90e2010-10-06 18:17:0534 switch (level) {
[email protected]1bfd03f2011-07-02 19:04:1835 case FINE_LEVEL:
[email protected]890de90272011-07-22 08:20:5236 log_severity = -2; // VLOG(2)
37 emit_log = VLOG_IS_ON(2);
[email protected]1bfd03f2011-07-02 19:04:1838 break;
[email protected]c16e90e2010-10-06 18:17:0539 case INFO_LEVEL:
[email protected]890de90272011-07-22 08:20:5240 log_severity = -1; // VLOG(1)
41 emit_log = VLOG_IS_ON(1);
[email protected]c16e90e2010-10-06 18:17:0542 break;
43 case WARNING_LEVEL:
44 log_severity = logging::LOG_WARNING;
[email protected]890de90272011-07-22 08:20:5245 emit_log = LOG_IS_ON(WARNING);
[email protected]c16e90e2010-10-06 18:17:0546 break;
[email protected]5bfa2562010-10-13 02:04:2247 case SEVERE_LEVEL:
[email protected]c16e90e2010-10-06 18:17:0548 log_severity = logging::LOG_ERROR;
[email protected]890de90272011-07-22 08:20:5249 emit_log = LOG_IS_ON(ERROR);
[email protected]c16e90e2010-10-06 18:17:0550 break;
51 }
[email protected]890de90272011-07-22 08:20:5252 if (emit_log) {
53 va_list ap;
54 va_start(ap, format);
55 std::string result;
56 base::StringAppendV(&result, format, ap);
57 logging::LogMessage(file, line, log_severity).stream() << result;
58 va_end(ap);
59 }
[email protected]85164712010-06-23 01:13:5660}
61
[email protected]1f7893fa2012-09-06 03:06:1362void SyncLogger::SetSystemResources(invalidation::SystemResources* resources) {
[email protected]b5d1f3f322012-01-20 09:51:4263 // Do nothing.
64}
65
[email protected]1f7893fa2012-09-06 03:06:1366SyncInvalidationScheduler::SyncInvalidationScheduler()
[email protected]37d5b3472013-10-10 16:20:3667 : created_on_loop_(base::MessageLoop::current()),
[email protected]1bfd03f2011-07-02 19:04:1868 is_started_(false),
[email protected]37d5b3472013-10-10 16:20:3669 is_stopped_(false),
70 weak_factory_(this) {
[email protected]1bfd03f2011-07-02 19:04:1871 CHECK(created_on_loop_);
[email protected]538f58d2010-10-12 18:04:1572}
73
[email protected]1f7893fa2012-09-06 03:06:1374SyncInvalidationScheduler::~SyncInvalidationScheduler() {
[email protected]21cb28a2013-05-07 03:52:4575 CHECK_EQ(created_on_loop_, base::MessageLoop::current());
[email protected]1bfd03f2011-07-02 19:04:1876 CHECK(is_stopped_);
[email protected]6c852922010-10-07 21:45:0277}
78
[email protected]1f7893fa2012-09-06 03:06:1379void SyncInvalidationScheduler::Start() {
[email protected]21cb28a2013-05-07 03:52:4580 CHECK_EQ(created_on_loop_, base::MessageLoop::current());
[email protected]1bfd03f2011-07-02 19:04:1881 CHECK(!is_started_);
82 is_started_ = true;
83 is_stopped_ = false;
[email protected]ab1e57e2011-12-21 23:21:5084 weak_factory_.InvalidateWeakPtrs();
[email protected]1bfd03f2011-07-02 19:04:1885}
86
[email protected]1f7893fa2012-09-06 03:06:1387void SyncInvalidationScheduler::Stop() {
[email protected]21cb28a2013-05-07 03:52:4588 CHECK_EQ(created_on_loop_, base::MessageLoop::current());
[email protected]1bfd03f2011-07-02 19:04:1889 is_stopped_ = true;
90 is_started_ = false;
[email protected]ab1e57e2011-12-21 23:21:5091 weak_factory_.InvalidateWeakPtrs();
[email protected]1bfd03f2011-07-02 19:04:1892 STLDeleteElements(&posted_tasks_);
[email protected]1bfd03f2011-07-02 19:04:1893}
94
[email protected]1f7893fa2012-09-06 03:06:1395void SyncInvalidationScheduler::Schedule(invalidation::TimeDelta delay,
96 invalidation::Closure* task) {
[email protected]ab1e57e2011-12-21 23:21:5097 DCHECK(invalidation::IsCallbackRepeatable(task));
[email protected]21cb28a2013-05-07 03:52:4598 CHECK_EQ(created_on_loop_, base::MessageLoop::current());
[email protected]ab1e57e2011-12-21 23:21:5099
100 if (!is_started_) {
101 delete task;
[email protected]1bfd03f2011-07-02 19:04:18102 return;
103 }
[email protected]ab1e57e2011-12-21 23:21:50104
105 posted_tasks_.insert(task);
[email protected]21cb28a2013-05-07 03:52:45106 base::MessageLoop::current()->PostDelayedTask(
[email protected]1f7893fa2012-09-06 03:06:13107 FROM_HERE, base::Bind(&SyncInvalidationScheduler::RunPostedTask,
[email protected]ab1e57e2011-12-21 23:21:50108 weak_factory_.GetWeakPtr(), task),
[email protected]d88fdb32012-01-12 21:32:44109 delay);
[email protected]1bfd03f2011-07-02 19:04:18110}
111
[email protected]1f7893fa2012-09-06 03:06:13112bool SyncInvalidationScheduler::IsRunningOnThread() const {
[email protected]21cb28a2013-05-07 03:52:45113 return created_on_loop_ == base::MessageLoop::current();
[email protected]1bfd03f2011-07-02 19:04:18114}
115
[email protected]1f7893fa2012-09-06 03:06:13116invalidation::Time SyncInvalidationScheduler::GetCurrentTime() const {
[email protected]21cb28a2013-05-07 03:52:45117 CHECK_EQ(created_on_loop_, base::MessageLoop::current());
[email protected]1bfd03f2011-07-02 19:04:18118 return base::Time::Now();
119}
120
[email protected]1f7893fa2012-09-06 03:06:13121void SyncInvalidationScheduler::SetSystemResources(
[email protected]b5d1f3f322012-01-20 09:51:42122 invalidation::SystemResources* resources) {
123 // Do nothing.
124}
125
[email protected]1f7893fa2012-09-06 03:06:13126void SyncInvalidationScheduler::RunPostedTask(invalidation::Closure* task) {
[email protected]21cb28a2013-05-07 03:52:45127 CHECK_EQ(created_on_loop_, base::MessageLoop::current());
[email protected]739cee32012-06-20 20:37:28128 task->Run();
[email protected]cbea2bd2010-07-13 19:26:24129 posted_tasks_.erase(task);
[email protected]739cee32012-06-20 20:37:28130 delete task;
[email protected]cbea2bd2010-07-13 19:26:24131}
132
[email protected]28935deb2013-12-06 12:45:18133SyncNetworkChannel::SyncNetworkChannel()
[email protected]afff1752014-06-20 04:42:10134 : last_network_status_(false),
[email protected]edfe19f2014-03-21 01:38:12135 received_messages_count_(0) {}
[email protected]28935deb2013-12-06 12:45:18136
137SyncNetworkChannel::~SyncNetworkChannel() {
138 STLDeleteElements(&network_status_receivers_);
139}
140
[email protected]28935deb2013-12-06 12:45:18141void SyncNetworkChannel::SetMessageReceiver(
142 invalidation::MessageCallback* incoming_receiver) {
143 incoming_receiver_.reset(incoming_receiver);
144}
145
146void SyncNetworkChannel::AddNetworkStatusReceiver(
147 invalidation::NetworkStatusCallback* network_status_receiver) {
[email protected]afff1752014-06-20 04:42:10148 network_status_receiver->Run(last_network_status_);
[email protected]28935deb2013-12-06 12:45:18149 network_status_receivers_.push_back(network_status_receiver);
150}
151
152void SyncNetworkChannel::SetSystemResources(
153 invalidation::SystemResources* resources) {
154 // Do nothing.
155}
156
157void SyncNetworkChannel::AddObserver(Observer* observer) {
158 observers_.AddObserver(observer);
159}
160
161void SyncNetworkChannel::RemoveObserver(Observer* observer) {
162 observers_.RemoveObserver(observer);
163}
164
[email protected]04a830a2014-01-04 02:48:51165scoped_ptr<SyncNetworkChannel> SyncNetworkChannel::CreatePushClientChannel(
166 const notifier::NotifierOptions& notifier_options) {
167 scoped_ptr<notifier::PushClient> push_client(
168 notifier::PushClient::CreateDefaultOnIOThread(notifier_options));
169 return scoped_ptr<SyncNetworkChannel>(
170 new PushClientChannel(push_client.Pass()));
171}
172
[email protected]df006cbc2014-01-22 18:36:20173scoped_ptr<SyncNetworkChannel> SyncNetworkChannel::CreateGCMNetworkChannel(
174 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
175 scoped_ptr<GCMNetworkChannelDelegate> delegate) {
176 return scoped_ptr<SyncNetworkChannel>(new GCMNetworkChannel(
177 request_context_getter, delegate.Pass()));
[email protected]04a830a2014-01-04 02:48:51178}
179
[email protected]afff1752014-06-20 04:42:10180void SyncNetworkChannel::NotifyNetworkStatusChange(bool online) {
181 // Remember network state for future NetworkStatusReceivers.
182 last_network_status_ = online;
[email protected]28935deb2013-12-06 12:45:18183 // Notify NetworkStatusReceivers in cacheinvalidation.
184 for (NetworkStatusReceiverList::const_iterator it =
185 network_status_receivers_.begin();
186 it != network_status_receivers_.end(); ++it) {
[email protected]afff1752014-06-20 04:42:10187 (*it)->Run(online);
[email protected]28935deb2013-12-06 12:45:18188 }
[email protected]afff1752014-06-20 04:42:10189}
190
191void SyncNetworkChannel::NotifyChannelStateChange(
192 InvalidatorState invalidator_state) {
[email protected]28935deb2013-12-06 12:45:18193 FOR_EACH_OBSERVER(Observer, observers_,
[email protected]afff1752014-06-20 04:42:10194 OnNetworkChannelStateChanged(invalidator_state));
[email protected]28935deb2013-12-06 12:45:18195}
196
[email protected]3b73b252014-03-01 00:18:54197bool SyncNetworkChannel::DeliverIncomingMessage(const std::string& message) {
[email protected]28935deb2013-12-06 12:45:18198 if (!incoming_receiver_) {
199 DLOG(ERROR) << "No receiver for incoming notification";
[email protected]28935deb2013-12-06 12:45:18200 return false;
201 }
[email protected]edfe19f2014-03-21 01:38:12202 received_messages_count_++;
[email protected]3b73b252014-03-01 00:18:54203 incoming_receiver_->Run(message);
[email protected]28935deb2013-12-06 12:45:18204 return true;
205}
206
[email protected]edfe19f2014-03-21 01:38:12207int SyncNetworkChannel::GetReceivedMessagesCount() const {
208 return received_messages_count_;
209}
[email protected]28935deb2013-12-06 12:45:18210
[email protected]1f7893fa2012-09-06 03:06:13211SyncStorage::SyncStorage(StateWriter* state_writer,
212 invalidation::Scheduler* scheduler)
[email protected]1bfd03f2011-07-02 19:04:18213 : state_writer_(state_writer),
214 scheduler_(scheduler) {
215 DCHECK(state_writer_);
216 DCHECK(scheduler_);
217}
218
[email protected]1f7893fa2012-09-06 03:06:13219SyncStorage::~SyncStorage() {}
[email protected]1bfd03f2011-07-02 19:04:18220
[email protected]1f7893fa2012-09-06 03:06:13221void SyncStorage::WriteKey(const std::string& key, const std::string& value,
222 invalidation::WriteKeyCallback* done) {
[email protected]1bfd03f2011-07-02 19:04:18223 CHECK(state_writer_);
224 // TODO(ghc): actually write key,value associations, and don't invoke the
225 // callback until the operation completes.
226 state_writer_->WriteState(value);
227 cached_state_ = value;
228 // According to the cache invalidation API folks, we can do this as
229 // long as we make sure to clear the persistent state that we start
230 // up the cache invalidation client with. However, we musn't do it
231 // right away, as we may be called under a lock that the callback
232 // uses.
233 scheduler_->Schedule(
234 invalidation::Scheduler::NoDelay(),
235 invalidation::NewPermanentCallback(
[email protected]1f7893fa2012-09-06 03:06:13236 this, &SyncStorage::RunAndDeleteWriteKeyCallback,
[email protected]1bfd03f2011-07-02 19:04:18237 done));
238}
239
[email protected]1f7893fa2012-09-06 03:06:13240void SyncStorage::ReadKey(const std::string& key,
241 invalidation::ReadKeyCallback* done) {
[email protected]8b2a7f602011-07-26 21:01:10242 DCHECK(scheduler_->IsRunningOnThread()) << "not running on scheduler thread";
243 RunAndDeleteReadKeyCallback(done, cached_state_);
[email protected]1bfd03f2011-07-02 19:04:18244}
245
[email protected]1f7893fa2012-09-06 03:06:13246void SyncStorage::DeleteKey(const std::string& key,
247 invalidation::DeleteKeyCallback* done) {
[email protected]1bfd03f2011-07-02 19:04:18248 // TODO(ghc): Implement.
249 LOG(WARNING) << "ignoring call to DeleteKey(" << key << ", callback)";
250}
251
[email protected]1f7893fa2012-09-06 03:06:13252void SyncStorage::ReadAllKeys(invalidation::ReadAllKeysCallback* done) {
[email protected]1bfd03f2011-07-02 19:04:18253 // TODO(ghc): Implement.
254 LOG(WARNING) << "ignoring call to ReadAllKeys(callback)";
255}
256
[email protected]1f7893fa2012-09-06 03:06:13257void SyncStorage::SetSystemResources(
[email protected]b5d1f3f322012-01-20 09:51:42258 invalidation::SystemResources* resources) {
259 // Do nothing.
260}
261
[email protected]1f7893fa2012-09-06 03:06:13262void SyncStorage::RunAndDeleteWriteKeyCallback(
[email protected]1bfd03f2011-07-02 19:04:18263 invalidation::WriteKeyCallback* callback) {
[email protected]007b3f82013-04-09 08:46:45264 callback->Run(
265 invalidation::Status(invalidation::Status::SUCCESS, std::string()));
[email protected]1bfd03f2011-07-02 19:04:18266 delete callback;
267}
268
[email protected]1f7893fa2012-09-06 03:06:13269void SyncStorage::RunAndDeleteReadKeyCallback(
[email protected]1bfd03f2011-07-02 19:04:18270 invalidation::ReadKeyCallback* callback, const std::string& value) {
271 callback->Run(std::make_pair(
[email protected]007b3f82013-04-09 08:46:45272 invalidation::Status(invalidation::Status::SUCCESS, std::string()),
[email protected]1bfd03f2011-07-02 19:04:18273 value));
274 delete callback;
275}
276
[email protected]1f7893fa2012-09-06 03:06:13277SyncSystemResources::SyncSystemResources(
[email protected]28935deb2013-12-06 12:45:18278 SyncNetworkChannel* sync_network_channel,
[email protected]1f7eba72012-05-30 09:10:17279 StateWriter* state_writer)
[email protected]1bfd03f2011-07-02 19:04:18280 : is_started_(false),
[email protected]1f7893fa2012-09-06 03:06:13281 logger_(new SyncLogger()),
282 internal_scheduler_(new SyncInvalidationScheduler()),
283 listener_scheduler_(new SyncInvalidationScheduler()),
284 storage_(new SyncStorage(state_writer, internal_scheduler_.get())),
[email protected]28935deb2013-12-06 12:45:18285 sync_network_channel_(sync_network_channel) {
[email protected]1bfd03f2011-07-02 19:04:18286}
287
[email protected]1f7893fa2012-09-06 03:06:13288SyncSystemResources::~SyncSystemResources() {
[email protected]1bfd03f2011-07-02 19:04:18289 Stop();
290}
291
[email protected]1f7893fa2012-09-06 03:06:13292void SyncSystemResources::Start() {
[email protected]1bfd03f2011-07-02 19:04:18293 internal_scheduler_->Start();
294 listener_scheduler_->Start();
[email protected]b5d1f3f322012-01-20 09:51:42295 is_started_ = true;
[email protected]1bfd03f2011-07-02 19:04:18296}
297
[email protected]1f7893fa2012-09-06 03:06:13298void SyncSystemResources::Stop() {
[email protected]1bfd03f2011-07-02 19:04:18299 internal_scheduler_->Stop();
300 listener_scheduler_->Stop();
301}
302
[email protected]1f7893fa2012-09-06 03:06:13303bool SyncSystemResources::IsStarted() const {
[email protected]1bfd03f2011-07-02 19:04:18304 return is_started_;
305}
306
[email protected]1f7893fa2012-09-06 03:06:13307void SyncSystemResources::set_platform(const std::string& platform) {
[email protected]1bfd03f2011-07-02 19:04:18308 platform_ = platform;
309}
310
[email protected]1f7893fa2012-09-06 03:06:13311std::string SyncSystemResources::platform() const {
[email protected]1bfd03f2011-07-02 19:04:18312 return platform_;
313}
314
[email protected]1f7893fa2012-09-06 03:06:13315SyncLogger* SyncSystemResources::logger() {
[email protected]1bfd03f2011-07-02 19:04:18316 return logger_.get();
317}
318
[email protected]1f7893fa2012-09-06 03:06:13319SyncStorage* SyncSystemResources::storage() {
[email protected]1bfd03f2011-07-02 19:04:18320 return storage_.get();
321}
322
[email protected]28935deb2013-12-06 12:45:18323SyncNetworkChannel* SyncSystemResources::network() {
324 return sync_network_channel_;
[email protected]1bfd03f2011-07-02 19:04:18325}
326
[email protected]1f7893fa2012-09-06 03:06:13327SyncInvalidationScheduler* SyncSystemResources::internal_scheduler() {
[email protected]1bfd03f2011-07-02 19:04:18328 return internal_scheduler_.get();
329}
330
[email protected]1f7893fa2012-09-06 03:06:13331SyncInvalidationScheduler* SyncSystemResources::listener_scheduler() {
[email protected]1bfd03f2011-07-02 19:04:18332 return listener_scheduler_.get();
333}
334
[email protected]65f173552012-06-28 22:43:58335} // namespace syncer