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