OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "sync/notifier/chrome_system_resources.h" | |
6 | |
7 #include <cstdlib> | |
8 #include <cstring> | |
9 #include <string> | |
10 | |
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/deps/callback.h" | |
18 #include "google/cacheinvalidation/include/types.h" | |
19 #include "jingle/notifier/listener/push_client.h" | |
20 #include "sync/notifier/invalidation_util.h" | |
21 | |
22 namespace syncer { | |
23 | |
24 ChromeLogger::ChromeLogger() {} | |
25 ChromeLogger::~ChromeLogger() {} | |
26 | |
27 void ChromeLogger::Log(LogLevel level, const char* file, int line, | |
28 const char* format, ...) { | |
29 logging::LogSeverity log_severity = -2; // VLOG(2) | |
30 bool emit_log = false; | |
31 switch (level) { | |
32 case FINE_LEVEL: | |
33 log_severity = -2; // VLOG(2) | |
34 emit_log = VLOG_IS_ON(2); | |
35 break; | |
36 case INFO_LEVEL: | |
37 log_severity = -1; // VLOG(1) | |
38 emit_log = VLOG_IS_ON(1); | |
39 break; | |
40 case WARNING_LEVEL: | |
41 log_severity = logging::LOG_WARNING; | |
42 emit_log = LOG_IS_ON(WARNING); | |
43 break; | |
44 case SEVERE_LEVEL: | |
45 log_severity = logging::LOG_ERROR; | |
46 emit_log = LOG_IS_ON(ERROR); | |
47 break; | |
48 } | |
49 if (emit_log) { | |
50 va_list ap; | |
51 va_start(ap, format); | |
52 std::string result; | |
53 base::StringAppendV(&result, format, ap); | |
54 logging::LogMessage(file, line, log_severity).stream() << result; | |
55 va_end(ap); | |
56 } | |
57 } | |
58 | |
59 void ChromeLogger::SetSystemResources( | |
60 invalidation::SystemResources* resources) { | |
61 // Do nothing. | |
62 } | |
63 | |
64 ChromeScheduler::ChromeScheduler() | |
65 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
66 created_on_loop_(MessageLoop::current()), | |
67 is_started_(false), | |
68 is_stopped_(false) { | |
69 CHECK(created_on_loop_); | |
70 } | |
71 | |
72 ChromeScheduler::~ChromeScheduler() { | |
73 CHECK_EQ(created_on_loop_, MessageLoop::current()); | |
74 CHECK(is_stopped_); | |
75 } | |
76 | |
77 void ChromeScheduler::Start() { | |
78 CHECK_EQ(created_on_loop_, MessageLoop::current()); | |
79 CHECK(!is_started_); | |
80 is_started_ = true; | |
81 is_stopped_ = false; | |
82 weak_factory_.InvalidateWeakPtrs(); | |
83 } | |
84 | |
85 void ChromeScheduler::Stop() { | |
86 CHECK_EQ(created_on_loop_, MessageLoop::current()); | |
87 is_stopped_ = true; | |
88 is_started_ = false; | |
89 weak_factory_.InvalidateWeakPtrs(); | |
90 STLDeleteElements(&posted_tasks_); | |
91 posted_tasks_.clear(); | |
92 } | |
93 | |
94 void ChromeScheduler::Schedule(invalidation::TimeDelta delay, | |
95 invalidation::Closure* task) { | |
96 DCHECK(invalidation::IsCallbackRepeatable(task)); | |
97 CHECK_EQ(created_on_loop_, MessageLoop::current()); | |
98 | |
99 if (!is_started_) { | |
100 delete task; | |
101 return; | |
102 } | |
103 | |
104 posted_tasks_.insert(task); | |
105 MessageLoop::current()->PostDelayedTask( | |
106 FROM_HERE, base::Bind(&ChromeScheduler::RunPostedTask, | |
107 weak_factory_.GetWeakPtr(), task), | |
108 delay); | |
109 } | |
110 | |
111 bool ChromeScheduler::IsRunningOnThread() const { | |
112 return created_on_loop_ == MessageLoop::current(); | |
113 } | |
114 | |
115 invalidation::Time ChromeScheduler::GetCurrentTime() const { | |
116 CHECK_EQ(created_on_loop_, MessageLoop::current()); | |
117 return base::Time::Now(); | |
118 } | |
119 | |
120 void ChromeScheduler::SetSystemResources( | |
121 invalidation::SystemResources* resources) { | |
122 // Do nothing. | |
123 } | |
124 | |
125 void ChromeScheduler::RunPostedTask(invalidation::Closure* task) { | |
126 CHECK_EQ(created_on_loop_, MessageLoop::current()); | |
127 task->Run(); | |
128 posted_tasks_.erase(task); | |
129 delete task; | |
130 } | |
131 | |
132 ChromeStorage::ChromeStorage(StateWriter* state_writer, | |
133 invalidation::Scheduler* scheduler) | |
134 : state_writer_(state_writer), | |
135 scheduler_(scheduler) { | |
136 DCHECK(state_writer_); | |
137 DCHECK(scheduler_); | |
138 } | |
139 | |
140 ChromeStorage::~ChromeStorage() {} | |
141 | |
142 void ChromeStorage::WriteKey(const std::string& key, const std::string& value, | |
143 invalidation::WriteKeyCallback* done) { | |
144 CHECK(state_writer_); | |
145 // TODO(ghc): actually write key,value associations, and don't invoke the | |
146 // callback until the operation completes. | |
147 state_writer_->WriteState(value); | |
148 cached_state_ = value; | |
149 // According to the cache invalidation API folks, we can do this as | |
150 // long as we make sure to clear the persistent state that we start | |
151 // up the cache invalidation client with. However, we musn't do it | |
152 // right away, as we may be called under a lock that the callback | |
153 // uses. | |
154 scheduler_->Schedule( | |
155 invalidation::Scheduler::NoDelay(), | |
156 invalidation::NewPermanentCallback( | |
157 this, &ChromeStorage::RunAndDeleteWriteKeyCallback, | |
158 done)); | |
159 } | |
160 | |
161 void ChromeStorage::ReadKey(const std::string& key, | |
162 invalidation::ReadKeyCallback* done) { | |
163 DCHECK(scheduler_->IsRunningOnThread()) << "not running on scheduler thread"; | |
164 RunAndDeleteReadKeyCallback(done, cached_state_); | |
165 } | |
166 | |
167 void ChromeStorage::DeleteKey(const std::string& key, | |
168 invalidation::DeleteKeyCallback* done) { | |
169 // TODO(ghc): Implement. | |
170 LOG(WARNING) << "ignoring call to DeleteKey(" << key << ", callback)"; | |
171 } | |
172 | |
173 void ChromeStorage::ReadAllKeys(invalidation::ReadAllKeysCallback* done) { | |
174 // TODO(ghc): Implement. | |
175 LOG(WARNING) << "ignoring call to ReadAllKeys(callback)"; | |
176 } | |
177 | |
178 void ChromeStorage::SetSystemResources( | |
179 invalidation::SystemResources* resources) { | |
180 // Do nothing. | |
181 } | |
182 | |
183 void ChromeStorage::RunAndDeleteWriteKeyCallback( | |
184 invalidation::WriteKeyCallback* callback) { | |
185 callback->Run(invalidation::Status(invalidation::Status::SUCCESS, "")); | |
186 delete callback; | |
187 } | |
188 | |
189 void ChromeStorage::RunAndDeleteReadKeyCallback( | |
190 invalidation::ReadKeyCallback* callback, const std::string& value) { | |
191 callback->Run(std::make_pair( | |
192 invalidation::Status(invalidation::Status::SUCCESS, ""), | |
193 value)); | |
194 delete callback; | |
195 } | |
196 | |
197 ChromeSystemResources::ChromeSystemResources( | |
198 scoped_ptr<notifier::PushClient> push_client, | |
199 StateWriter* state_writer) | |
200 : is_started_(false), | |
201 logger_(new ChromeLogger()), | |
202 internal_scheduler_(new ChromeScheduler()), | |
203 listener_scheduler_(new ChromeScheduler()), | |
204 storage_(new ChromeStorage(state_writer, internal_scheduler_.get())), | |
205 push_client_channel_(push_client.Pass()) { | |
206 } | |
207 | |
208 ChromeSystemResources::~ChromeSystemResources() { | |
209 Stop(); | |
210 } | |
211 | |
212 void ChromeSystemResources::Start() { | |
213 internal_scheduler_->Start(); | |
214 listener_scheduler_->Start(); | |
215 is_started_ = true; | |
216 } | |
217 | |
218 void ChromeSystemResources::Stop() { | |
219 internal_scheduler_->Stop(); | |
220 listener_scheduler_->Stop(); | |
221 } | |
222 | |
223 bool ChromeSystemResources::IsStarted() const { | |
224 return is_started_; | |
225 } | |
226 | |
227 void ChromeSystemResources::set_platform(const std::string& platform) { | |
228 platform_ = platform; | |
229 } | |
230 | |
231 std::string ChromeSystemResources::platform() const { | |
232 return platform_; | |
233 } | |
234 | |
235 ChromeLogger* ChromeSystemResources::logger() { | |
236 return logger_.get(); | |
237 } | |
238 | |
239 ChromeStorage* ChromeSystemResources::storage() { | |
240 return storage_.get(); | |
241 } | |
242 | |
243 PushClientChannel* ChromeSystemResources::network() { | |
244 return &push_client_channel_; | |
245 } | |
246 | |
247 ChromeScheduler* ChromeSystemResources::internal_scheduler() { | |
248 return internal_scheduler_.get(); | |
249 } | |
250 | |
251 ChromeScheduler* ChromeSystemResources::listener_scheduler() { | |
252 return listener_scheduler_.get(); | |
253 } | |
254 | |
255 } // namespace syncer | |
OLD | NEW |