OLD | NEW |
1 // Copyright 2013 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 "google_apis/gcm/engine/rmq_store.h" | 5 #include "google_apis/gcm/engine/gcm_store_impl.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 std::string ParseUsername(const std::string& key) { | 73 std::string ParseUsername(const std::string& key) { |
74 return key.substr(arraysize(kUserSerialNumberKeyStart) - 1); | 74 return key.substr(arraysize(kUserSerialNumberKeyStart) - 1); |
75 } | 75 } |
76 | 76 |
77 leveldb::Slice MakeSlice(const base::StringPiece& s) { | 77 leveldb::Slice MakeSlice(const base::StringPiece& s) { |
78 return leveldb::Slice(s.begin(), s.size()); | 78 return leveldb::Slice(s.begin(), s.size()); |
79 } | 79 } |
80 | 80 |
81 } // namespace | 81 } // namespace |
82 | 82 |
83 class RMQStore::Backend : public base::RefCountedThreadSafe<RMQStore::Backend> { | 83 class GCMStoreImpl::Backend |
| 84 : public base::RefCountedThreadSafe<GCMStoreImpl::Backend> { |
84 public: | 85 public: |
85 Backend(const base::FilePath& path, | 86 Backend(const base::FilePath& path, |
86 scoped_refptr<base::SequencedTaskRunner> foreground_runner); | 87 scoped_refptr<base::SequencedTaskRunner> foreground_runner); |
87 | 88 |
88 // Blocking implementations of RMQStore methods. | 89 // Blocking implementations of GCMStoreImpl methods. |
89 void Load(const LoadCallback& callback); | 90 void Load(const LoadCallback& callback); |
90 void Destroy(const UpdateCallback& callback); | 91 void Destroy(const UpdateCallback& callback); |
91 void SetDeviceCredentials(uint64 device_android_id, | 92 void SetDeviceCredentials(uint64 device_android_id, |
92 uint64 device_security_token, | 93 uint64 device_security_token, |
93 const UpdateCallback& callback); | 94 const UpdateCallback& callback); |
94 void AddIncomingMessage(const std::string& persistent_id, | 95 void AddIncomingMessage(const std::string& persistent_id, |
95 const UpdateCallback& callback); | 96 const UpdateCallback& callback); |
96 void RemoveIncomingMessages(const PersistentIdList& persistent_ids, | 97 void RemoveIncomingMessages(const PersistentIdList& persistent_ids, |
97 const UpdateCallback& callback); | 98 const UpdateCallback& callback); |
98 void AddOutgoingMessage(const std::string& persistent_id, | 99 void AddOutgoingMessage(const std::string& persistent_id, |
(...skipping 19 matching lines...) Expand all Loading... |
118 bool LoadNextSerialNumber(int64* next_serial_number); | 119 bool LoadNextSerialNumber(int64* next_serial_number); |
119 bool LoadUserSerialNumberMap( | 120 bool LoadUserSerialNumberMap( |
120 std::map<std::string, int64>* user_serial_number_map); | 121 std::map<std::string, int64>* user_serial_number_map); |
121 | 122 |
122 const base::FilePath path_; | 123 const base::FilePath path_; |
123 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; | 124 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; |
124 | 125 |
125 scoped_ptr<leveldb::DB> db_; | 126 scoped_ptr<leveldb::DB> db_; |
126 }; | 127 }; |
127 | 128 |
128 RMQStore::Backend::Backend( | 129 GCMStoreImpl::Backend::Backend( |
129 const base::FilePath& path, | 130 const base::FilePath& path, |
130 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner) | 131 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner) |
131 : path_(path), | 132 : path_(path), foreground_task_runner_(foreground_task_runner) {} |
132 foreground_task_runner_(foreground_task_runner) { | |
133 } | |
134 | 133 |
135 RMQStore::Backend::~Backend() { | 134 GCMStoreImpl::Backend::~Backend() {} |
136 } | |
137 | 135 |
138 void RMQStore::Backend::Load(const LoadCallback& callback) { | 136 void GCMStoreImpl::Backend::Load(const LoadCallback& callback) { |
139 LoadResult result; | 137 LoadResult result; |
140 | 138 |
141 leveldb::Options options; | 139 leveldb::Options options; |
142 options.create_if_missing = true; | 140 options.create_if_missing = true; |
143 leveldb::DB* db; | 141 leveldb::DB* db; |
144 leveldb::Status status = leveldb::DB::Open(options, | 142 leveldb::Status status = |
145 path_.AsUTF8Unsafe(), | 143 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db); |
146 &db); | |
147 if (!status.ok()) { | 144 if (!status.ok()) { |
148 LOG(ERROR) << "Failed to open database " << path_.value() | 145 LOG(ERROR) << "Failed to open database " << path_.value() << ": " |
149 << ": " << status.ToString(); | 146 << status.ToString(); |
150 foreground_task_runner_->PostTask(FROM_HERE, | 147 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); |
151 base::Bind(callback, result)); | |
152 return; | 148 return; |
153 } | 149 } |
154 db_.reset(db); | 150 db_.reset(db); |
155 | 151 |
156 if (!LoadDeviceCredentials(&result.device_android_id, | 152 if (!LoadDeviceCredentials(&result.device_android_id, |
157 &result.device_security_token) || | 153 &result.device_security_token) || |
158 !LoadIncomingMessages(&result.incoming_messages) || | 154 !LoadIncomingMessages(&result.incoming_messages) || |
159 !LoadOutgoingMessages(&result.outgoing_messages) || | 155 !LoadOutgoingMessages(&result.outgoing_messages) || |
160 !LoadNextSerialNumber(&result.next_serial_number) || | 156 !LoadNextSerialNumber(&result.next_serial_number) || |
161 !LoadUserSerialNumberMap(&result.user_serial_numbers)) { | 157 !LoadUserSerialNumberMap(&result.user_serial_numbers)) { |
162 result.device_android_id = 0; | 158 result.device_android_id = 0; |
163 result.device_security_token = 0; | 159 result.device_security_token = 0; |
164 result.incoming_messages.clear(); | 160 result.incoming_messages.clear(); |
165 STLDeleteContainerPairSecondPointers(result.outgoing_messages.begin(), | 161 STLDeleteContainerPairSecondPointers(result.outgoing_messages.begin(), |
166 result.outgoing_messages.end()); | 162 result.outgoing_messages.end()); |
167 result.outgoing_messages.clear(); | 163 result.outgoing_messages.clear(); |
168 foreground_task_runner_->PostTask(FROM_HERE, | 164 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); |
169 base::Bind(callback, result)); | |
170 return; | 165 return; |
171 } | 166 } |
172 | 167 |
173 DVLOG(1) << "Succeeded in loading " << result.incoming_messages.size() | 168 DVLOG(1) << "Succeeded in loading " << result.incoming_messages.size() |
174 << " unacknowledged incoming messages and " | 169 << " unacknowledged incoming messages and " |
175 << result.outgoing_messages.size() | 170 << result.outgoing_messages.size() |
176 << " unacknowledged outgoing messages."; | 171 << " unacknowledged outgoing messages."; |
177 result.success = true; | 172 result.success = true; |
178 foreground_task_runner_->PostTask(FROM_HERE, | 173 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); |
179 base::Bind(callback, result)); | |
180 return; | 174 return; |
181 } | 175 } |
182 | 176 |
183 void RMQStore::Backend::Destroy(const UpdateCallback& callback) { | 177 void GCMStoreImpl::Backend::Destroy(const UpdateCallback& callback) { |
184 DVLOG(1) << "Destroying RMQ store."; | 178 DVLOG(1) << "Destroying RMQ store."; |
185 const leveldb::Status s = | 179 const leveldb::Status s = |
186 leveldb::DestroyDB(path_.AsUTF8Unsafe(), | 180 leveldb::DestroyDB(path_.AsUTF8Unsafe(), leveldb::Options()); |
187 leveldb::Options()); | |
188 if (s.ok()) { | 181 if (s.ok()) { |
189 foreground_task_runner_->PostTask(FROM_HERE, | 182 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
190 base::Bind(callback, true)); | |
191 return; | 183 return; |
192 } | 184 } |
193 LOG(ERROR) << "Destroy failed."; | 185 LOG(ERROR) << "Destroy failed."; |
194 foreground_task_runner_->PostTask(FROM_HERE, | 186 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
195 base::Bind(callback, false)); | |
196 } | 187 } |
197 | 188 |
198 void RMQStore::Backend::SetDeviceCredentials(uint64 device_android_id, | 189 void GCMStoreImpl::Backend::SetDeviceCredentials( |
199 uint64 device_security_token, | 190 uint64 device_android_id, |
200 const UpdateCallback& callback) { | 191 uint64 device_security_token, |
| 192 const UpdateCallback& callback) { |
201 DVLOG(1) << "Saving device credentials with AID " << device_android_id; | 193 DVLOG(1) << "Saving device credentials with AID " << device_android_id; |
202 leveldb::WriteOptions write_options; | 194 leveldb::WriteOptions write_options; |
203 write_options.sync = true; | 195 write_options.sync = true; |
204 | 196 |
205 std::string encrypted_token; | 197 std::string encrypted_token; |
206 Encryptor::EncryptString(base::Uint64ToString(device_security_token), | 198 Encryptor::EncryptString(base::Uint64ToString(device_security_token), |
207 &encrypted_token); | 199 &encrypted_token); |
208 leveldb::Status s = | 200 leveldb::Status s = |
209 db_->Put(write_options, | 201 db_->Put(write_options, |
210 MakeSlice(kDeviceAIDKey), | 202 MakeSlice(kDeviceAIDKey), |
211 MakeSlice(base::Uint64ToString(device_android_id))); | 203 MakeSlice(base::Uint64ToString(device_android_id))); |
212 if (s.ok()) { | 204 if (s.ok()) { |
213 s = db_->Put(write_options, | 205 s = db_->Put( |
214 MakeSlice(kDeviceTokenKey), | 206 write_options, MakeSlice(kDeviceTokenKey), MakeSlice(encrypted_token)); |
215 MakeSlice(encrypted_token)); | |
216 } | 207 } |
217 if (s.ok()) { | 208 if (s.ok()) { |
218 foreground_task_runner_->PostTask(FROM_HERE, | 209 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
219 base::Bind(callback, true)); | |
220 return; | 210 return; |
221 } | 211 } |
222 LOG(ERROR) << "LevelDB put failed: " << s.ToString(); | 212 LOG(ERROR) << "LevelDB put failed: " << s.ToString(); |
223 foreground_task_runner_->PostTask(FROM_HERE, | 213 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
224 base::Bind(callback, false)); | |
225 } | 214 } |
226 | 215 |
227 void RMQStore::Backend::AddIncomingMessage(const std::string& persistent_id, | 216 void GCMStoreImpl::Backend::AddIncomingMessage(const std::string& persistent_id, |
228 const UpdateCallback& callback) { | 217 const UpdateCallback& callback) { |
229 DVLOG(1) << "Saving incoming message with id " << persistent_id; | 218 DVLOG(1) << "Saving incoming message with id " << persistent_id; |
230 leveldb::WriteOptions write_options; | 219 leveldb::WriteOptions write_options; |
231 write_options.sync = true; | 220 write_options.sync = true; |
232 | 221 |
233 const leveldb::Status s = | 222 const leveldb::Status s = db_->Put(write_options, |
234 db_->Put(write_options, | 223 MakeSlice(MakeIncomingKey(persistent_id)), |
235 MakeSlice(MakeIncomingKey(persistent_id)), | 224 MakeSlice(persistent_id)); |
236 MakeSlice(persistent_id)); | |
237 if (s.ok()) { | 225 if (s.ok()) { |
238 foreground_task_runner_->PostTask(FROM_HERE, | 226 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
239 base::Bind(callback, true)); | |
240 return; | 227 return; |
241 } | 228 } |
242 LOG(ERROR) << "LevelDB put failed: " << s.ToString(); | 229 LOG(ERROR) << "LevelDB put failed: " << s.ToString(); |
243 foreground_task_runner_->PostTask(FROM_HERE, | 230 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
244 base::Bind(callback, false)); | |
245 } | 231 } |
246 | 232 |
247 void RMQStore::Backend::RemoveIncomingMessages( | 233 void GCMStoreImpl::Backend::RemoveIncomingMessages( |
248 const PersistentIdList& persistent_ids, | 234 const PersistentIdList& persistent_ids, |
249 const UpdateCallback& callback) { | 235 const UpdateCallback& callback) { |
250 leveldb::WriteOptions write_options; | 236 leveldb::WriteOptions write_options; |
251 write_options.sync = true; | 237 write_options.sync = true; |
252 | 238 |
253 leveldb::Status s; | 239 leveldb::Status s; |
254 for (PersistentIdList::const_iterator iter = persistent_ids.begin(); | 240 for (PersistentIdList::const_iterator iter = persistent_ids.begin(); |
255 iter != persistent_ids.end(); ++iter){ | 241 iter != persistent_ids.end(); |
| 242 ++iter) { |
256 DVLOG(1) << "Removing incoming message with id " << *iter; | 243 DVLOG(1) << "Removing incoming message with id " << *iter; |
257 s = db_->Delete(write_options, | 244 s = db_->Delete(write_options, MakeSlice(MakeIncomingKey(*iter))); |
258 MakeSlice(MakeIncomingKey(*iter))); | |
259 if (!s.ok()) | 245 if (!s.ok()) |
260 break; | 246 break; |
261 } | 247 } |
262 if (s.ok()) { | 248 if (s.ok()) { |
263 foreground_task_runner_->PostTask(FROM_HERE, | 249 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
264 base::Bind(callback, true)); | |
265 return; | 250 return; |
266 } | 251 } |
267 LOG(ERROR) << "LevelDB remove failed: " << s.ToString(); | 252 LOG(ERROR) << "LevelDB remove failed: " << s.ToString(); |
268 foreground_task_runner_->PostTask(FROM_HERE, | 253 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
269 base::Bind(callback, false)); | |
270 } | 254 } |
271 | 255 |
272 void RMQStore::Backend::AddOutgoingMessage( | 256 void GCMStoreImpl::Backend::AddOutgoingMessage(const std::string& persistent_id, |
273 const std::string& persistent_id, | 257 const MCSMessage& message, |
274 const MCSMessage& message, | 258 const UpdateCallback& callback) { |
275 const UpdateCallback& callback) { | |
276 DVLOG(1) << "Saving outgoing message with id " << persistent_id; | 259 DVLOG(1) << "Saving outgoing message with id " << persistent_id; |
277 leveldb::WriteOptions write_options; | 260 leveldb::WriteOptions write_options; |
278 write_options.sync = true; | 261 write_options.sync = true; |
279 | 262 |
280 std::string data = static_cast<char>(message.tag()) + | 263 std::string data = |
281 message.SerializeAsString(); | 264 static_cast<char>(message.tag()) + message.SerializeAsString(); |
282 const leveldb::Status s = | 265 const leveldb::Status s = db_->Put(write_options, |
283 db_->Put(write_options, | 266 MakeSlice(MakeOutgoingKey(persistent_id)), |
284 MakeSlice(MakeOutgoingKey(persistent_id)), | 267 MakeSlice(data)); |
285 MakeSlice(data)); | |
286 if (s.ok()) { | 268 if (s.ok()) { |
287 foreground_task_runner_->PostTask(FROM_HERE, | 269 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
288 base::Bind(callback, true)); | |
289 return; | 270 return; |
290 } | 271 } |
291 LOG(ERROR) << "LevelDB put failed: " << s.ToString(); | 272 LOG(ERROR) << "LevelDB put failed: " << s.ToString(); |
292 foreground_task_runner_->PostTask(FROM_HERE, | 273 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
293 base::Bind(callback, false)); | |
294 | |
295 } | 274 } |
296 | 275 |
297 void RMQStore::Backend::RemoveOutgoingMessages( | 276 void GCMStoreImpl::Backend::RemoveOutgoingMessages( |
298 const PersistentIdList& persistent_ids, | 277 const PersistentIdList& persistent_ids, |
299 const UpdateCallback& callback) { | 278 const UpdateCallback& callback) { |
300 leveldb::WriteOptions write_options; | 279 leveldb::WriteOptions write_options; |
301 write_options.sync = true; | 280 write_options.sync = true; |
302 | 281 |
303 leveldb::Status s; | 282 leveldb::Status s; |
304 for (PersistentIdList::const_iterator iter = persistent_ids.begin(); | 283 for (PersistentIdList::const_iterator iter = persistent_ids.begin(); |
305 iter != persistent_ids.end(); ++iter){ | 284 iter != persistent_ids.end(); |
| 285 ++iter) { |
306 DVLOG(1) << "Removing outgoing message with id " << *iter; | 286 DVLOG(1) << "Removing outgoing message with id " << *iter; |
307 s = db_->Delete(write_options, | 287 s = db_->Delete(write_options, MakeSlice(MakeOutgoingKey(*iter))); |
308 MakeSlice(MakeOutgoingKey(*iter))); | |
309 if (!s.ok()) | 288 if (!s.ok()) |
310 break; | 289 break; |
311 } | 290 } |
312 if (s.ok()) { | 291 if (s.ok()) { |
313 foreground_task_runner_->PostTask(FROM_HERE, | 292 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
314 base::Bind(callback, true)); | |
315 return; | 293 return; |
316 } | 294 } |
317 LOG(ERROR) << "LevelDB remove failed: " << s.ToString(); | 295 LOG(ERROR) << "LevelDB remove failed: " << s.ToString(); |
318 foreground_task_runner_->PostTask(FROM_HERE, | 296 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
319 base::Bind(callback, false)); | |
320 } | 297 } |
321 | 298 |
322 void RMQStore::Backend::AddUserSerialNumber(const std::string& username, | 299 void GCMStoreImpl::Backend::AddUserSerialNumber( |
323 int64 serial_number, | 300 const std::string& username, |
324 const UpdateCallback& callback) { | 301 int64 serial_number, |
| 302 const UpdateCallback& callback) { |
325 DVLOG(1) << "Saving username to serial number mapping for user: " << username; | 303 DVLOG(1) << "Saving username to serial number mapping for user: " << username; |
326 leveldb::WriteOptions write_options; | 304 leveldb::WriteOptions write_options; |
327 write_options.sync = true; | 305 write_options.sync = true; |
328 | 306 |
329 const leveldb::Status status = | 307 const leveldb::Status status = |
330 db_->Put(write_options, | 308 db_->Put(write_options, |
331 MakeSlice(MakeUserSerialNumberKey(username)), | 309 MakeSlice(MakeUserSerialNumberKey(username)), |
332 MakeSlice(base::Int64ToString(serial_number))); | 310 MakeSlice(base::Int64ToString(serial_number))); |
333 if (status.ok()) { | 311 if (status.ok()) { |
334 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 312 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
335 return; | 313 return; |
336 } | 314 } |
337 LOG(ERROR) << "LevelDB put failed: " << status.ToString(); | 315 LOG(ERROR) << "LevelDB put failed: " << status.ToString(); |
338 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 316 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
339 } | 317 } |
340 | 318 |
341 void RMQStore::Backend::RemoveUserSerialNumber(const std::string& username, | 319 void GCMStoreImpl::Backend::RemoveUserSerialNumber( |
342 const UpdateCallback& callback) { | 320 const std::string& username, |
| 321 const UpdateCallback& callback) { |
343 leveldb::WriteOptions write_options; | 322 leveldb::WriteOptions write_options; |
344 write_options.sync = true; | 323 write_options.sync = true; |
345 | 324 |
346 leveldb::Status status = db_->Delete(write_options, MakeSlice(username)); | 325 leveldb::Status status = db_->Delete(write_options, MakeSlice(username)); |
347 if (status.ok()) { | 326 if (status.ok()) { |
348 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 327 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
349 return; | 328 return; |
350 } | 329 } |
351 LOG(ERROR) << "LevelDB remove failed: " << status.ToString(); | 330 LOG(ERROR) << "LevelDB remove failed: " << status.ToString(); |
352 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 331 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
353 } | 332 } |
354 | 333 |
355 void RMQStore::Backend::SetNextSerialNumber(int64 next_serial_number, | 334 void GCMStoreImpl::Backend::SetNextSerialNumber( |
356 const UpdateCallback& callback) { | 335 int64 next_serial_number, |
| 336 const UpdateCallback& callback) { |
357 DVLOG(1) << "Updating the value of next user serial number to: " | 337 DVLOG(1) << "Updating the value of next user serial number to: " |
358 << next_serial_number; | 338 << next_serial_number; |
359 leveldb::WriteOptions write_options; | 339 leveldb::WriteOptions write_options; |
360 write_options.sync = true; | 340 write_options.sync = true; |
361 | 341 |
362 const leveldb::Status status = | 342 const leveldb::Status status = |
363 db_->Put(write_options, | 343 db_->Put(write_options, |
364 MakeSlice(kNextSerialNumberKey), | 344 MakeSlice(kNextSerialNumberKey), |
365 MakeSlice(base::Int64ToString(next_serial_number))); | 345 MakeSlice(base::Int64ToString(next_serial_number))); |
366 if (status.ok()) { | 346 if (status.ok()) { |
367 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 347 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
368 return; | 348 return; |
369 } | 349 } |
370 LOG(ERROR) << "LevelDB put failed: " << status.ToString(); | 350 LOG(ERROR) << "LevelDB put failed: " << status.ToString(); |
371 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 351 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
372 } | 352 } |
373 | 353 |
374 bool RMQStore::Backend::LoadDeviceCredentials(uint64* android_id, | 354 bool GCMStoreImpl::Backend::LoadDeviceCredentials(uint64* android_id, |
375 uint64* security_token) { | 355 uint64* security_token) { |
376 leveldb::ReadOptions read_options; | 356 leveldb::ReadOptions read_options; |
377 read_options.verify_checksums = true; | 357 read_options.verify_checksums = true; |
378 | 358 |
379 std::string result; | 359 std::string result; |
380 leveldb::Status s = db_->Get(read_options, | 360 leveldb::Status s = db_->Get(read_options, MakeSlice(kDeviceAIDKey), &result); |
381 MakeSlice(kDeviceAIDKey), | |
382 &result); | |
383 if (s.ok()) { | 361 if (s.ok()) { |
384 if (!base::StringToUint64(result, android_id)) { | 362 if (!base::StringToUint64(result, android_id)) { |
385 LOG(ERROR) << "Failed to restore device id."; | 363 LOG(ERROR) << "Failed to restore device id."; |
386 return false; | 364 return false; |
387 } | 365 } |
388 result.clear(); | 366 result.clear(); |
389 s = db_->Get(read_options, | 367 s = db_->Get(read_options, MakeSlice(kDeviceTokenKey), &result); |
390 MakeSlice(kDeviceTokenKey), | |
391 &result); | |
392 } | 368 } |
393 if (s.ok()) { | 369 if (s.ok()) { |
394 std::string decrypted_token; | 370 std::string decrypted_token; |
395 Encryptor::DecryptString(result, &decrypted_token); | 371 Encryptor::DecryptString(result, &decrypted_token); |
396 if (!base::StringToUint64(decrypted_token, security_token)) { | 372 if (!base::StringToUint64(decrypted_token, security_token)) { |
397 LOG(ERROR) << "Failed to restore security token."; | 373 LOG(ERROR) << "Failed to restore security token."; |
398 return false; | 374 return false; |
399 } | 375 } |
400 return true; | 376 return true; |
401 } | 377 } |
402 | 378 |
403 if (s.IsNotFound()) { | 379 if (s.IsNotFound()) { |
404 DVLOG(1) << "No credentials found."; | 380 DVLOG(1) << "No credentials found."; |
405 return true; | 381 return true; |
406 } | 382 } |
407 | 383 |
408 LOG(ERROR) << "Error reading credentials from store."; | 384 LOG(ERROR) << "Error reading credentials from store."; |
409 return false; | 385 return false; |
410 } | 386 } |
411 | 387 |
412 bool RMQStore::Backend::LoadIncomingMessages( | 388 bool GCMStoreImpl::Backend::LoadIncomingMessages( |
413 std::vector<std::string>* incoming_messages) { | 389 std::vector<std::string>* incoming_messages) { |
414 leveldb::ReadOptions read_options; | 390 leveldb::ReadOptions read_options; |
415 read_options.verify_checksums = true; | 391 read_options.verify_checksums = true; |
416 | 392 |
417 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); | 393 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); |
418 for (iter->Seek(MakeSlice(kIncomingMsgKeyStart)); | 394 for (iter->Seek(MakeSlice(kIncomingMsgKeyStart)); |
419 iter->Valid() && iter->key().ToString() < kIncomingMsgKeyEnd; | 395 iter->Valid() && iter->key().ToString() < kIncomingMsgKeyEnd; |
420 iter->Next()) { | 396 iter->Next()) { |
421 leveldb::Slice s = iter->value(); | 397 leveldb::Slice s = iter->value(); |
422 if (s.empty()) { | 398 if (s.empty()) { |
423 LOG(ERROR) << "Error reading incoming message with key " | 399 LOG(ERROR) << "Error reading incoming message with key " |
424 << iter->key().ToString(); | 400 << iter->key().ToString(); |
425 return false; | 401 return false; |
426 } | 402 } |
427 DVLOG(1) << "Found incoming message with id " << s.ToString(); | 403 DVLOG(1) << "Found incoming message with id " << s.ToString(); |
428 incoming_messages->push_back(s.ToString()); | 404 incoming_messages->push_back(s.ToString()); |
429 } | 405 } |
430 | 406 |
431 return true; | 407 return true; |
432 } | 408 } |
433 | 409 |
434 bool RMQStore::Backend::LoadOutgoingMessages( | 410 bool GCMStoreImpl::Backend::LoadOutgoingMessages( |
435 std::map<std::string, google::protobuf::MessageLite*>* | 411 std::map<std::string, google::protobuf::MessageLite*>* outgoing_messages) { |
436 outgoing_messages) { | |
437 leveldb::ReadOptions read_options; | 412 leveldb::ReadOptions read_options; |
438 read_options.verify_checksums = true; | 413 read_options.verify_checksums = true; |
439 | 414 |
440 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); | 415 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); |
441 for (iter->Seek(MakeSlice(kOutgoingMsgKeyStart)); | 416 for (iter->Seek(MakeSlice(kOutgoingMsgKeyStart)); |
442 iter->Valid() && iter->key().ToString() < kOutgoingMsgKeyEnd; | 417 iter->Valid() && iter->key().ToString() < kOutgoingMsgKeyEnd; |
443 iter->Next()) { | 418 iter->Next()) { |
444 leveldb::Slice s = iter->value(); | 419 leveldb::Slice s = iter->value(); |
445 if (s.size() <= 1) { | 420 if (s.size() <= 1) { |
446 LOG(ERROR) << "Error reading incoming message with key " << s.ToString(); | 421 LOG(ERROR) << "Error reading incoming message with key " << s.ToString(); |
447 return false; | 422 return false; |
448 } | 423 } |
449 uint8 tag = iter->value().data()[0]; | 424 uint8 tag = iter->value().data()[0]; |
450 std::string id = ParseOutgoingKey(iter->key().ToString()); | 425 std::string id = ParseOutgoingKey(iter->key().ToString()); |
451 scoped_ptr<google::protobuf::MessageLite> message( | 426 scoped_ptr<google::protobuf::MessageLite> message( |
452 BuildProtobufFromTag(tag)); | 427 BuildProtobufFromTag(tag)); |
453 if (!message.get() || | 428 if (!message.get() || |
454 !message->ParseFromString(iter->value().ToString().substr(1))) { | 429 !message->ParseFromString(iter->value().ToString().substr(1))) { |
455 LOG(ERROR) << "Failed to parse outgoing message with id " | 430 LOG(ERROR) << "Failed to parse outgoing message with id " << id |
456 << id << " and tag " << tag; | 431 << " and tag " << tag; |
457 return false; | 432 return false; |
458 } | 433 } |
459 DVLOG(1) << "Found outgoing message with id " << id << " of type " | 434 DVLOG(1) << "Found outgoing message with id " << id << " of type " |
460 << base::IntToString(tag); | 435 << base::IntToString(tag); |
461 (*outgoing_messages)[id] = message.release(); | 436 (*outgoing_messages)[id] = message.release(); |
462 } | 437 } |
463 | 438 |
464 return true; | 439 return true; |
465 } | 440 } |
466 | 441 |
467 bool RMQStore::Backend::LoadNextSerialNumber(int64* next_serial_number) { | 442 bool GCMStoreImpl::Backend::LoadNextSerialNumber(int64* next_serial_number) { |
468 leveldb::ReadOptions read_options; | 443 leveldb::ReadOptions read_options; |
469 read_options.verify_checksums = true; | 444 read_options.verify_checksums = true; |
470 | 445 |
471 std::string result; | 446 std::string result; |
472 leveldb::Status status = db_->Get(read_options, | 447 leveldb::Status status = |
473 MakeSlice(kNextSerialNumberKey), | 448 db_->Get(read_options, MakeSlice(kNextSerialNumberKey), &result); |
474 &result); | |
475 if (status.ok()) { | 449 if (status.ok()) { |
476 if (!base::StringToInt64(result, next_serial_number)) { | 450 if (!base::StringToInt64(result, next_serial_number)) { |
477 LOG(ERROR) << "Failed to restore the next serial number."; | 451 LOG(ERROR) << "Failed to restore the next serial number."; |
478 return false; | 452 return false; |
479 } | 453 } |
480 return true; | 454 return true; |
481 } | 455 } |
482 | 456 |
483 if (status.IsNotFound()) { | 457 if (status.IsNotFound()) { |
484 DVLOG(1) << "No next serial number found."; | 458 DVLOG(1) << "No next serial number found."; |
485 return true; | 459 return true; |
486 } | 460 } |
487 | 461 |
488 LOG(ERROR) << "Error when reading the next serial number."; | 462 LOG(ERROR) << "Error when reading the next serial number."; |
489 return false; | 463 return false; |
490 } | 464 } |
491 | 465 |
492 bool RMQStore::Backend::LoadUserSerialNumberMap( | 466 bool GCMStoreImpl::Backend::LoadUserSerialNumberMap( |
493 std::map<std::string, int64>* user_serial_number_map) { | 467 std::map<std::string, int64>* user_serial_number_map) { |
494 leveldb::ReadOptions read_options; | 468 leveldb::ReadOptions read_options; |
495 read_options.verify_checksums = true; | 469 read_options.verify_checksums = true; |
496 | 470 |
497 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); | 471 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); |
498 for (iter->Seek(MakeSlice(kUserSerialNumberKeyStart)); | 472 for (iter->Seek(MakeSlice(kUserSerialNumberKeyStart)); |
499 iter->Valid() && iter->key().ToString() < kUserSerialNumberKeyEnd; | 473 iter->Valid() && iter->key().ToString() < kUserSerialNumberKeyEnd; |
500 iter->Next()) { | 474 iter->Next()) { |
501 std::string username = ParseUsername(iter->key().ToString()); | 475 std::string username = ParseUsername(iter->key().ToString()); |
502 if (username.empty()) { | 476 if (username.empty()) { |
503 LOG(ERROR) << "Error reading username. It should not be empty."; | 477 LOG(ERROR) << "Error reading username. It should not be empty."; |
504 return false; | 478 return false; |
505 } | 479 } |
506 std::string serial_number_string = iter->value().ToString(); | 480 std::string serial_number_string = iter->value().ToString(); |
507 int64 serial_number = kSerialNumberMissing; | 481 int64 serial_number = kSerialNumberMissing; |
508 if (!base::StringToInt64(serial_number_string, &serial_number)) { | 482 if (!base::StringToInt64(serial_number_string, &serial_number)) { |
509 LOG(ERROR) << "Error reading user serial number for user: " << username; | 483 LOG(ERROR) << "Error reading user serial number for user: " << username; |
510 return false; | 484 return false; |
511 } | 485 } |
512 | 486 |
513 (*user_serial_number_map)[username] = serial_number; | 487 (*user_serial_number_map)[username] = serial_number; |
514 } | 488 } |
515 | 489 |
516 return true; | 490 return true; |
517 } | 491 } |
518 | 492 |
519 RMQStore::LoadResult::LoadResult() | 493 GCMStoreImpl::GCMStoreImpl( |
520 : success(false), | |
521 device_android_id(0), | |
522 device_security_token(0), | |
523 next_serial_number(1LL) { | |
524 } | |
525 RMQStore::LoadResult::~LoadResult() {} | |
526 | |
527 RMQStore::RMQStore( | |
528 const base::FilePath& path, | 494 const base::FilePath& path, |
529 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | 495 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) |
530 : backend_(new Backend(path, base::MessageLoopProxy::current())), | 496 : backend_(new Backend(path, base::MessageLoopProxy::current())), |
531 blocking_task_runner_(blocking_task_runner) { | 497 blocking_task_runner_(blocking_task_runner) {} |
| 498 |
| 499 GCMStoreImpl::~GCMStoreImpl() {} |
| 500 |
| 501 void GCMStoreImpl::Load(const LoadCallback& callback) { |
| 502 blocking_task_runner_->PostTask( |
| 503 FROM_HERE, base::Bind(&GCMStoreImpl::Backend::Load, backend_, callback)); |
532 } | 504 } |
533 | 505 |
534 RMQStore::~RMQStore() { | 506 void GCMStoreImpl::Destroy(const UpdateCallback& callback) { |
| 507 blocking_task_runner_->PostTask( |
| 508 FROM_HERE, |
| 509 base::Bind(&GCMStoreImpl::Backend::Destroy, backend_, callback)); |
535 } | 510 } |
536 | 511 |
537 void RMQStore::Load(const LoadCallback& callback) { | 512 void GCMStoreImpl::SetDeviceCredentials(uint64 device_android_id, |
538 blocking_task_runner_->PostTask(FROM_HERE, | 513 uint64 device_security_token, |
539 base::Bind(&RMQStore::Backend::Load, | 514 const UpdateCallback& callback) { |
540 backend_, | |
541 callback)); | |
542 } | |
543 | |
544 void RMQStore::Destroy(const UpdateCallback& callback) { | |
545 blocking_task_runner_->PostTask( | 515 blocking_task_runner_->PostTask( |
546 FROM_HERE, | 516 FROM_HERE, |
547 base::Bind(&RMQStore::Backend::Destroy, | 517 base::Bind(&GCMStoreImpl::Backend::SetDeviceCredentials, |
548 backend_, | |
549 callback)); | |
550 } | |
551 | |
552 void RMQStore::SetDeviceCredentials(uint64 device_android_id, | |
553 uint64 device_security_token, | |
554 const UpdateCallback& callback) { | |
555 blocking_task_runner_->PostTask( | |
556 FROM_HERE, | |
557 base::Bind(&RMQStore::Backend::SetDeviceCredentials, | |
558 backend_, | 518 backend_, |
559 device_android_id, | 519 device_android_id, |
560 device_security_token, | 520 device_security_token, |
561 callback)); | 521 callback)); |
562 } | 522 } |
563 | 523 |
564 void RMQStore::AddIncomingMessage(const std::string& persistent_id, | 524 void GCMStoreImpl::AddIncomingMessage(const std::string& persistent_id, |
565 const UpdateCallback& callback) { | 525 const UpdateCallback& callback) { |
566 blocking_task_runner_->PostTask( | 526 blocking_task_runner_->PostTask( |
567 FROM_HERE, | 527 FROM_HERE, |
568 base::Bind(&RMQStore::Backend::AddIncomingMessage, | 528 base::Bind(&GCMStoreImpl::Backend::AddIncomingMessage, |
569 backend_, | 529 backend_, |
570 persistent_id, | 530 persistent_id, |
571 callback)); | 531 callback)); |
572 } | 532 } |
573 | 533 |
574 void RMQStore::RemoveIncomingMessage(const std::string& persistent_id, | 534 void GCMStoreImpl::RemoveIncomingMessage(const std::string& persistent_id, |
575 const UpdateCallback& callback) { | 535 const UpdateCallback& callback) { |
576 blocking_task_runner_->PostTask( | 536 blocking_task_runner_->PostTask( |
577 FROM_HERE, | 537 FROM_HERE, |
578 base::Bind(&RMQStore::Backend::RemoveIncomingMessages, | 538 base::Bind(&GCMStoreImpl::Backend::RemoveIncomingMessages, |
579 backend_, | 539 backend_, |
580 PersistentIdList(1, persistent_id), | 540 PersistentIdList(1, persistent_id), |
581 callback)); | 541 callback)); |
582 } | 542 } |
583 | 543 |
584 void RMQStore::RemoveIncomingMessages(const PersistentIdList& persistent_ids, | 544 void GCMStoreImpl::RemoveIncomingMessages( |
585 const UpdateCallback& callback) { | 545 const PersistentIdList& persistent_ids, |
| 546 const UpdateCallback& callback) { |
586 blocking_task_runner_->PostTask( | 547 blocking_task_runner_->PostTask( |
587 FROM_HERE, | 548 FROM_HERE, |
588 base::Bind(&RMQStore::Backend::RemoveIncomingMessages, | 549 base::Bind(&GCMStoreImpl::Backend::RemoveIncomingMessages, |
589 backend_, | 550 backend_, |
590 persistent_ids, | 551 persistent_ids, |
591 callback)); | 552 callback)); |
592 } | 553 } |
593 | 554 |
594 void RMQStore::AddOutgoingMessage(const std::string& persistent_id, | 555 void GCMStoreImpl::AddOutgoingMessage(const std::string& persistent_id, |
595 const MCSMessage& message, | 556 const MCSMessage& message, |
596 const UpdateCallback& callback) { | 557 const UpdateCallback& callback) { |
597 blocking_task_runner_->PostTask( | 558 blocking_task_runner_->PostTask( |
598 FROM_HERE, | 559 FROM_HERE, |
599 base::Bind(&RMQStore::Backend::AddOutgoingMessage, | 560 base::Bind(&GCMStoreImpl::Backend::AddOutgoingMessage, |
600 backend_, | 561 backend_, |
601 persistent_id, | 562 persistent_id, |
602 message, | 563 message, |
603 callback)); | 564 callback)); |
604 } | 565 } |
605 | 566 |
606 void RMQStore::RemoveOutgoingMessage(const std::string& persistent_id, | 567 void GCMStoreImpl::RemoveOutgoingMessage(const std::string& persistent_id, |
607 const UpdateCallback& callback) { | 568 const UpdateCallback& callback) { |
608 blocking_task_runner_->PostTask( | 569 blocking_task_runner_->PostTask( |
609 FROM_HERE, | 570 FROM_HERE, |
610 base::Bind(&RMQStore::Backend::RemoveOutgoingMessages, | 571 base::Bind(&GCMStoreImpl::Backend::RemoveOutgoingMessages, |
611 backend_, | 572 backend_, |
612 PersistentIdList(1, persistent_id), | 573 PersistentIdList(1, persistent_id), |
613 callback)); | 574 callback)); |
614 } | 575 } |
615 | 576 |
616 void RMQStore::RemoveOutgoingMessages(const PersistentIdList& persistent_ids, | 577 void GCMStoreImpl::RemoveOutgoingMessages( |
617 const UpdateCallback& callback) { | 578 const PersistentIdList& persistent_ids, |
| 579 const UpdateCallback& callback) { |
618 blocking_task_runner_->PostTask( | 580 blocking_task_runner_->PostTask( |
619 FROM_HERE, | 581 FROM_HERE, |
620 base::Bind(&RMQStore::Backend::RemoveOutgoingMessages, | 582 base::Bind(&GCMStoreImpl::Backend::RemoveOutgoingMessages, |
621 backend_, | 583 backend_, |
622 persistent_ids, | 584 persistent_ids, |
623 callback)); | 585 callback)); |
624 } | 586 } |
625 | 587 |
626 void RMQStore::SetNextSerialNumber(int64 next_serial_number, | 588 void GCMStoreImpl::SetNextSerialNumber(int64 next_serial_number, |
627 const UpdateCallback& callback) { | 589 const UpdateCallback& callback) { |
628 blocking_task_runner_->PostTask( | 590 blocking_task_runner_->PostTask( |
629 FROM_HERE, | 591 FROM_HERE, |
630 base::Bind(&RMQStore::Backend::SetNextSerialNumber, | 592 base::Bind(&GCMStoreImpl::Backend::SetNextSerialNumber, |
631 backend_, | 593 backend_, |
632 next_serial_number, | 594 next_serial_number, |
633 callback)); | 595 callback)); |
634 } | 596 } |
635 | 597 |
636 void RMQStore::AddUserSerialNumber(const std::string& username, | 598 void GCMStoreImpl::AddUserSerialNumber(const std::string& username, |
637 int64 serial_number, | 599 int64 serial_number, |
638 const UpdateCallback& callback) { | 600 const UpdateCallback& callback) { |
639 blocking_task_runner_->PostTask( | 601 blocking_task_runner_->PostTask( |
640 FROM_HERE, | 602 FROM_HERE, |
641 base::Bind(&RMQStore::Backend::AddUserSerialNumber, | 603 base::Bind(&GCMStoreImpl::Backend::AddUserSerialNumber, |
642 backend_, | 604 backend_, |
643 username, | 605 username, |
644 serial_number, | 606 serial_number, |
645 callback)); | 607 callback)); |
646 } | 608 } |
647 | 609 |
648 void RMQStore::RemoveUserSerialNumber(const std::string& username, | 610 void GCMStoreImpl::RemoveUserSerialNumber(const std::string& username, |
649 const UpdateCallback& callback) { | 611 const UpdateCallback& callback) { |
650 blocking_task_runner_->PostTask( | 612 blocking_task_runner_->PostTask( |
651 FROM_HERE, | 613 FROM_HERE, |
652 base::Bind(&RMQStore::Backend::RemoveUserSerialNumber, | 614 base::Bind(&GCMStoreImpl::Backend::RemoveUserSerialNumber, |
653 backend_, | 615 backend_, |
654 username, | 616 username, |
655 callback)); | 617 callback)); |
656 } | 618 } |
657 | 619 |
658 } // namespace gcm | 620 } // namespace gcm |
OLD | NEW |