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
3 // found in the LICENSE file.
5 #include "sync/notifier/chrome_system_resources.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/message_loop.h"
14 #include "base/stl_util.h"
15 #include "base/string_util.h"
16 #include "base/stringprintf.h"
17 #include "google/cacheinvalidation/include/types.h"
18 #include "sync/notifier/cache_invalidation_packet_handler.h"
19 #include "sync/notifier/invalidation_util.h"
21 namespace sync_notifier
{
23 ChromeLogger::ChromeLogger() {}
24 ChromeLogger::~ChromeLogger() {}
26 void ChromeLogger::Log(LogLevel level
, const char* file
, int line
,
27 const char* format
, ...) {
28 logging::LogSeverity log_severity
= -2; // VLOG(2)
29 bool emit_log
= false;
32 log_severity
= -2; // VLOG(2)
33 emit_log
= VLOG_IS_ON(2);
36 log_severity
= -1; // VLOG(1)
37 emit_log
= VLOG_IS_ON(1);
40 log_severity
= logging::LOG_WARNING
;
41 emit_log
= LOG_IS_ON(WARNING
);
44 log_severity
= logging::LOG_ERROR
;
45 emit_log
= LOG_IS_ON(ERROR
);
52 base::StringAppendV(&result
, format
, ap
);
53 logging::LogMessage(file
, line
, log_severity
).stream() << result
;
58 void ChromeLogger::SetSystemResources(
59 invalidation::SystemResources
* resources
) {
63 ChromeScheduler::ChromeScheduler()
64 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
65 created_on_loop_(MessageLoop::current()),
68 CHECK(created_on_loop_
);
71 ChromeScheduler::~ChromeScheduler() {
72 CHECK_EQ(created_on_loop_
, MessageLoop::current());
76 void ChromeScheduler::Start() {
77 CHECK_EQ(created_on_loop_
, MessageLoop::current());
81 weak_factory_
.InvalidateWeakPtrs();
84 void ChromeScheduler::Stop() {
85 CHECK_EQ(created_on_loop_
, MessageLoop::current());
88 weak_factory_
.InvalidateWeakPtrs();
89 STLDeleteElements(&posted_tasks_
);
90 posted_tasks_
.clear();
93 void ChromeScheduler::Schedule(invalidation::TimeDelta delay
,
94 invalidation::Closure
* task
) {
95 DCHECK(invalidation::IsCallbackRepeatable(task
));
96 CHECK_EQ(created_on_loop_
, MessageLoop::current());
103 posted_tasks_
.insert(task
);
104 MessageLoop::current()->PostDelayedTask(
105 FROM_HERE
, base::Bind(&ChromeScheduler::RunPostedTask
,
106 weak_factory_
.GetWeakPtr(), task
),
110 bool ChromeScheduler::IsRunningOnThread() const {
111 return created_on_loop_
== MessageLoop::current();
114 invalidation::Time
ChromeScheduler::GetCurrentTime() const {
115 CHECK_EQ(created_on_loop_
, MessageLoop::current());
116 return base::Time::Now();
119 void ChromeScheduler::SetSystemResources(
120 invalidation::SystemResources
* resources
) {
124 void ChromeScheduler::RunPostedTask(invalidation::Closure
* task
) {
125 CHECK_EQ(created_on_loop_
, MessageLoop::current());
126 RunAndDeleteClosure(task
);
127 posted_tasks_
.erase(task
);
130 ChromeStorage::ChromeStorage(StateWriter
* state_writer
,
131 invalidation::Scheduler
* scheduler
)
132 : state_writer_(state_writer
),
133 scheduler_(scheduler
) {
134 DCHECK(state_writer_
);
138 ChromeStorage::~ChromeStorage() {}
140 void ChromeStorage::WriteKey(const std::string
& key
, const std::string
& value
,
141 invalidation::WriteKeyCallback
* done
) {
142 CHECK(state_writer_
);
143 // TODO(ghc): actually write key,value associations, and don't invoke the
144 // callback until the operation completes.
145 state_writer_
->WriteState(value
);
146 cached_state_
= value
;
147 // According to the cache invalidation API folks, we can do this as
148 // long as we make sure to clear the persistent state that we start
149 // up the cache invalidation client with. However, we musn't do it
150 // right away, as we may be called under a lock that the callback
152 scheduler_
->Schedule(
153 invalidation::Scheduler::NoDelay(),
154 invalidation::NewPermanentCallback(
155 this, &ChromeStorage::RunAndDeleteWriteKeyCallback
,
159 void ChromeStorage::ReadKey(const std::string
& key
,
160 invalidation::ReadKeyCallback
* done
) {
161 DCHECK(scheduler_
->IsRunningOnThread()) << "not running on scheduler thread";
162 RunAndDeleteReadKeyCallback(done
, cached_state_
);
165 void ChromeStorage::DeleteKey(const std::string
& key
,
166 invalidation::DeleteKeyCallback
* done
) {
167 // TODO(ghc): Implement.
168 LOG(WARNING
) << "ignoring call to DeleteKey(" << key
<< ", callback)";
171 void ChromeStorage::ReadAllKeys(invalidation::ReadAllKeysCallback
* done
) {
172 // TODO(ghc): Implement.
173 LOG(WARNING
) << "ignoring call to ReadAllKeys(callback)";
176 void ChromeStorage::SetSystemResources(
177 invalidation::SystemResources
* resources
) {
181 void ChromeStorage::RunAndDeleteWriteKeyCallback(
182 invalidation::WriteKeyCallback
* callback
) {
183 callback
->Run(invalidation::Status(invalidation::Status::SUCCESS
, ""));
187 void ChromeStorage::RunAndDeleteReadKeyCallback(
188 invalidation::ReadKeyCallback
* callback
, const std::string
& value
) {
189 callback
->Run(std::make_pair(
190 invalidation::Status(invalidation::Status::SUCCESS
, ""),
195 ChromeNetwork::ChromeNetwork()
196 : packet_handler_(NULL
),
197 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
199 ChromeNetwork::~ChromeNetwork() {
200 STLDeleteElements(&network_status_receivers_
);
203 void ChromeNetwork::SendMessage(const std::string
& outgoing_message
) {
204 if (packet_handler_
) {
205 packet_handler_
->SendMessage(outgoing_message
);
209 void ChromeNetwork::SetMessageReceiver(
210 invalidation::MessageCallback
* incoming_receiver
) {
211 incoming_receiver_
.reset(incoming_receiver
);
214 void ChromeNetwork::AddNetworkStatusReceiver(
215 invalidation::NetworkStatusCallback
* network_status_receiver
) {
216 network_status_receivers_
.push_back(network_status_receiver
);
219 void ChromeNetwork::SetSystemResources(
220 invalidation::SystemResources
* resources
) {
224 void ChromeNetwork::UpdatePacketHandler(
225 CacheInvalidationPacketHandler
* packet_handler
) {
226 packet_handler_
= packet_handler
;
227 if (packet_handler_
!= NULL
) {
228 packet_handler_
->SetMessageReceiver(
229 new invalidation::MessageCallback(
230 base::Bind(&ChromeNetwork::HandleInboundMessage
,
231 weak_factory_
.GetWeakPtr())));
233 packet_handler_
->SendSubscriptionRequest();
236 void ChromeNetwork::HandleInboundMessage(const std::string
& incoming_message
) {
237 if (incoming_receiver_
.get()) {
238 incoming_receiver_
->Run(incoming_message
);
242 ChromeSystemResources::ChromeSystemResources(StateWriter
* state_writer
)
243 : is_started_(false),
244 logger_(new ChromeLogger()),
245 internal_scheduler_(new ChromeScheduler()),
246 listener_scheduler_(new ChromeScheduler()),
247 storage_(new ChromeStorage(state_writer
, internal_scheduler_
.get())),
248 network_(new ChromeNetwork()) {
251 ChromeSystemResources::~ChromeSystemResources() {
255 void ChromeSystemResources::Start() {
256 internal_scheduler_
->Start();
257 listener_scheduler_
->Start();
261 void ChromeSystemResources::Stop() {
262 internal_scheduler_
->Stop();
263 listener_scheduler_
->Stop();
266 bool ChromeSystemResources::IsStarted() const {
270 void ChromeSystemResources::set_platform(const std::string
& platform
) {
271 platform_
= platform
;
274 std::string
ChromeSystemResources::platform() const {
278 ChromeLogger
* ChromeSystemResources::logger() {
279 return logger_
.get();
282 ChromeStorage
* ChromeSystemResources::storage() {
283 return storage_
.get();
286 ChromeNetwork
* ChromeSystemResources::network() {
287 return network_
.get();
290 ChromeScheduler
* ChromeSystemResources::internal_scheduler() {
291 return internal_scheduler_
.get();
294 ChromeScheduler
* ChromeSystemResources::listener_scheduler() {
295 return listener_scheduler_
.get();
298 } // namespace sync_notifier