OLD | NEW |
1 // Copyright 2014 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/gcm_store_impl.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" |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 scoped_refptr<base::SequencedTaskRunner> foreground_runner, | 183 scoped_refptr<base::SequencedTaskRunner> foreground_runner, |
184 scoped_ptr<Encryptor> encryptor); | 184 scoped_ptr<Encryptor> encryptor); |
185 | 185 |
186 // Blocking implementations of GCMStoreImpl methods. | 186 // Blocking implementations of GCMStoreImpl methods. |
187 void Load(const LoadCallback& callback); | 187 void Load(const LoadCallback& callback); |
188 void Close(); | 188 void Close(); |
189 void Destroy(const UpdateCallback& callback); | 189 void Destroy(const UpdateCallback& callback); |
190 void SetDeviceCredentials(uint64 device_android_id, | 190 void SetDeviceCredentials(uint64 device_android_id, |
191 uint64 device_security_token, | 191 uint64 device_security_token, |
192 const UpdateCallback& callback); | 192 const UpdateCallback& callback); |
193 void AddRegistration(const std::string& app_id, | 193 void AddRegistration(const std::string& serialized_key, |
194 const std::string& serialized_registration, | 194 const std::string& serialized_value, |
195 const UpdateCallback& callback); | 195 const UpdateCallback& callback); |
196 void RemoveRegistration(const std::string& app_id, | 196 void RemoveRegistration(const std::string& serialized_key, |
197 const UpdateCallback& callback); | 197 const UpdateCallback& callback); |
198 void AddIncomingMessage(const std::string& persistent_id, | 198 void AddIncomingMessage(const std::string& persistent_id, |
199 const UpdateCallback& callback); | 199 const UpdateCallback& callback); |
200 void RemoveIncomingMessages(const PersistentIdList& persistent_ids, | 200 void RemoveIncomingMessages(const PersistentIdList& persistent_ids, |
201 const UpdateCallback& callback); | 201 const UpdateCallback& callback); |
202 void AddOutgoingMessage(const std::string& persistent_id, | 202 void AddOutgoingMessage(const std::string& persistent_id, |
203 const MCSMessage& message, | 203 const MCSMessage& message, |
204 const UpdateCallback& callback); | 204 const UpdateCallback& callback); |
205 void RemoveOutgoingMessages( | 205 void RemoveOutgoingMessages( |
206 const PersistentIdList& persistent_ids, | 206 const PersistentIdList& persistent_ids, |
(...skipping 30 matching lines...) Expand all Loading... |
237 void SetValue(const std::string& key, | 237 void SetValue(const std::string& key, |
238 const std::string& value, | 238 const std::string& value, |
239 const UpdateCallback& callback); | 239 const UpdateCallback& callback); |
240 | 240 |
241 private: | 241 private: |
242 friend class base::RefCountedThreadSafe<Backend>; | 242 friend class base::RefCountedThreadSafe<Backend>; |
243 ~Backend(); | 243 ~Backend(); |
244 | 244 |
245 LoadStatus OpenStoreAndLoadData(LoadResult* result); | 245 LoadStatus OpenStoreAndLoadData(LoadResult* result); |
246 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); | 246 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); |
247 bool LoadRegistrations(RegistrationInfoMap* registrations); | 247 bool LoadRegistrations(std::map<std::string, std::string>* registrations); |
248 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); | 248 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); |
249 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); | 249 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); |
250 bool LoadLastCheckinInfo(base::Time* last_checkin_time, | 250 bool LoadLastCheckinInfo(base::Time* last_checkin_time, |
251 std::set<std::string>* accounts); | 251 std::set<std::string>* accounts); |
252 bool LoadGServicesSettings(std::map<std::string, std::string>* settings, | 252 bool LoadGServicesSettings(std::map<std::string, std::string>* settings, |
253 std::string* digest); | 253 std::string* digest); |
254 bool LoadAccountMappingInfo(AccountMappings* account_mappings); | 254 bool LoadAccountMappingInfo(AccountMappings* account_mappings); |
255 bool LoadLastTokenFetchTime(base::Time* last_token_fetch_time); | 255 bool LoadLastTokenFetchTime(base::Time* last_token_fetch_time); |
256 bool LoadHeartbeatIntervals(std::map<std::string, int>* heartbeat_intervals); | 256 bool LoadHeartbeatIntervals(std::map<std::string, int>* heartbeat_intervals); |
257 bool LoadInstanceIDData(std::map<std::string, std::string>* instance_id_data); | 257 bool LoadInstanceIDData(std::map<std::string, std::string>* instance_id_data); |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 } | 410 } |
411 if (s.ok()) { | 411 if (s.ok()) { |
412 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 412 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
413 return; | 413 return; |
414 } | 414 } |
415 LOG(ERROR) << "LevelDB put failed: " << s.ToString(); | 415 LOG(ERROR) << "LevelDB put failed: " << s.ToString(); |
416 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 416 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
417 } | 417 } |
418 | 418 |
419 void GCMStoreImpl::Backend::AddRegistration( | 419 void GCMStoreImpl::Backend::AddRegistration( |
420 const std::string& app_id, | 420 const std::string& serialized_key, |
421 const std::string& serialized_registration, | 421 const std::string& serialized_value, |
422 const UpdateCallback& callback) { | 422 const UpdateCallback& callback) { |
423 DVLOG(1) << "Saving registration info for app: " << app_id; | 423 DVLOG(1) << "Saving registration info for app: " << serialized_key; |
424 if (!db_.get()) { | 424 if (!db_.get()) { |
425 LOG(ERROR) << "GCMStore db doesn't exist."; | 425 LOG(ERROR) << "GCMStore db doesn't exist."; |
426 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 426 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
427 return; | 427 return; |
428 } | 428 } |
429 leveldb::WriteOptions write_options; | 429 leveldb::WriteOptions write_options; |
430 write_options.sync = true; | 430 write_options.sync = true; |
431 | 431 |
432 std::string key = MakeRegistrationKey(app_id); | 432 const leveldb::Status status = db_->Put( |
433 const leveldb::Status status = db_->Put(write_options, | 433 write_options, |
434 MakeSlice(key), | 434 MakeSlice(MakeRegistrationKey(serialized_key)), |
435 MakeSlice(serialized_registration)); | 435 MakeSlice(serialized_value)); |
436 if (status.ok()) { | 436 if (!status.ok()) |
437 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 437 LOG(ERROR) << "LevelDB put failed: " << status.ToString(); |
438 return; | 438 foreground_task_runner_->PostTask( |
439 } | 439 FROM_HERE, base::Bind(callback, status.ok())); |
440 LOG(ERROR) << "LevelDB put failed: " << status.ToString(); | |
441 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | |
442 } | 440 } |
443 | 441 |
444 void GCMStoreImpl::Backend::RemoveRegistration(const std::string& app_id, | 442 void GCMStoreImpl::Backend::RemoveRegistration( |
445 const UpdateCallback& callback) { | 443 const std::string& serialized_key, |
| 444 const UpdateCallback& callback) { |
446 if (!db_.get()) { | 445 if (!db_.get()) { |
447 LOG(ERROR) << "GCMStore db doesn't exist."; | 446 LOG(ERROR) << "GCMStore db doesn't exist."; |
448 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 447 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
449 return; | 448 return; |
450 } | 449 } |
451 leveldb::WriteOptions write_options; | 450 leveldb::WriteOptions write_options; |
452 write_options.sync = true; | 451 write_options.sync = true; |
453 | 452 |
454 leveldb::Status status = | 453 leveldb::Status status = db_->Delete( |
455 db_->Delete(write_options, MakeSlice(MakeRegistrationKey(app_id))); | 454 write_options, MakeSlice(MakeRegistrationKey(serialized_key))); |
456 if (status.ok()) { | 455 if (!status.ok()) |
457 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 456 LOG(ERROR) << "LevelDB remove failed: " << status.ToString(); |
458 return; | 457 foreground_task_runner_->PostTask( |
459 } | 458 FROM_HERE, base::Bind(callback, status.ok())); |
460 LOG(ERROR) << "LevelDB remove failed: " << status.ToString(); | |
461 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | |
462 } | 459 } |
463 | 460 |
464 void GCMStoreImpl::Backend::AddIncomingMessage(const std::string& persistent_id, | 461 void GCMStoreImpl::Backend::AddIncomingMessage(const std::string& persistent_id, |
465 const UpdateCallback& callback) { | 462 const UpdateCallback& callback) { |
466 DVLOG(1) << "Saving incoming message with id " << persistent_id; | 463 DVLOG(1) << "Saving incoming message with id " << persistent_id; |
467 if (!db_.get()) { | 464 if (!db_.get()) { |
468 LOG(ERROR) << "GCMStore db doesn't exist."; | 465 LOG(ERROR) << "GCMStore db doesn't exist."; |
469 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 466 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
470 return; | 467 return; |
471 } | 468 } |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
895 if (s.IsNotFound()) { | 892 if (s.IsNotFound()) { |
896 DVLOG(1) << "No credentials found."; | 893 DVLOG(1) << "No credentials found."; |
897 return true; | 894 return true; |
898 } | 895 } |
899 | 896 |
900 LOG(ERROR) << "Error reading credentials from store."; | 897 LOG(ERROR) << "Error reading credentials from store."; |
901 return false; | 898 return false; |
902 } | 899 } |
903 | 900 |
904 bool GCMStoreImpl::Backend::LoadRegistrations( | 901 bool GCMStoreImpl::Backend::LoadRegistrations( |
905 RegistrationInfoMap* registrations) { | 902 std::map<std::string, std::string>* registrations) { |
906 leveldb::ReadOptions read_options; | 903 leveldb::ReadOptions read_options; |
907 read_options.verify_checksums = true; | 904 read_options.verify_checksums = true; |
908 | 905 |
909 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); | 906 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); |
910 for (iter->Seek(MakeSlice(kRegistrationKeyStart)); | 907 for (iter->Seek(MakeSlice(kRegistrationKeyStart)); |
911 iter->Valid() && iter->key().ToString() < kRegistrationKeyEnd; | 908 iter->Valid() && iter->key().ToString() < kRegistrationKeyEnd; |
912 iter->Next()) { | 909 iter->Next()) { |
913 leveldb::Slice s = iter->value(); | 910 leveldb::Slice s = iter->value(); |
914 if (s.size() <= 1) { | 911 if (s.size() <= 1) { |
915 LOG(ERROR) << "Error reading registration with key " << s.ToString(); | 912 LOG(ERROR) << "Error reading registration with key " << s.ToString(); |
916 return false; | 913 return false; |
917 } | 914 } |
918 std::string app_id = ParseRegistrationKey(iter->key().ToString()); | 915 std::string app_id = ParseRegistrationKey(iter->key().ToString()); |
919 linked_ptr<RegistrationInfo> registration(new RegistrationInfo); | |
920 if (!registration->ParseFromString(iter->value().ToString())) { | |
921 LOG(ERROR) << "Failed to parse registration with app id " << app_id; | |
922 return false; | |
923 } | |
924 DVLOG(1) << "Found registration with app id " << app_id; | 916 DVLOG(1) << "Found registration with app id " << app_id; |
925 (*registrations)[app_id] = registration; | 917 (*registrations)[app_id] = iter->value().ToString(); |
926 } | 918 } |
927 | 919 |
928 return true; | 920 return true; |
929 } | 921 } |
930 | 922 |
931 bool GCMStoreImpl::Backend::LoadIncomingMessages( | 923 bool GCMStoreImpl::Backend::LoadIncomingMessages( |
932 std::vector<std::string>* incoming_messages) { | 924 std::vector<std::string>* incoming_messages) { |
933 leveldb::ReadOptions read_options; | 925 leveldb::ReadOptions read_options; |
934 read_options.verify_checksums = true; | 926 read_options.verify_checksums = true; |
935 | 927 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1175 blocking_task_runner_->PostTask( | 1167 blocking_task_runner_->PostTask( |
1176 FROM_HERE, | 1168 FROM_HERE, |
1177 base::Bind(&GCMStoreImpl::Backend::SetDeviceCredentials, | 1169 base::Bind(&GCMStoreImpl::Backend::SetDeviceCredentials, |
1178 backend_, | 1170 backend_, |
1179 device_android_id, | 1171 device_android_id, |
1180 device_security_token, | 1172 device_security_token, |
1181 callback)); | 1173 callback)); |
1182 } | 1174 } |
1183 | 1175 |
1184 void GCMStoreImpl::AddRegistration( | 1176 void GCMStoreImpl::AddRegistration( |
1185 const std::string& app_id, | 1177 const std::string& serialized_key, |
1186 const linked_ptr<RegistrationInfo>& registration, | 1178 const std::string& serialized_value, |
1187 const UpdateCallback& callback) { | 1179 const UpdateCallback& callback) { |
1188 std::string serialized_registration = registration->SerializeAsString(); | |
1189 blocking_task_runner_->PostTask( | 1180 blocking_task_runner_->PostTask( |
1190 FROM_HERE, | 1181 FROM_HERE, |
1191 base::Bind(&GCMStoreImpl::Backend::AddRegistration, | 1182 base::Bind(&GCMStoreImpl::Backend::AddRegistration, |
1192 backend_, | 1183 backend_, |
1193 app_id, | 1184 serialized_key, |
1194 serialized_registration, | 1185 serialized_value, |
1195 callback)); | 1186 callback)); |
1196 } | 1187 } |
1197 | 1188 |
1198 void GCMStoreImpl::RemoveRegistration(const std::string& app_id, | 1189 void GCMStoreImpl::RemoveRegistration(const std::string& app_id, |
1199 const UpdateCallback& callback) { | 1190 const UpdateCallback& callback) { |
1200 blocking_task_runner_->PostTask( | 1191 blocking_task_runner_->PostTask( |
1201 FROM_HERE, | 1192 FROM_HERE, |
1202 base::Bind(&GCMStoreImpl::Backend::RemoveRegistration, | 1193 base::Bind(&GCMStoreImpl::Backend::RemoveRegistration, |
1203 backend_, | 1194 backend_, |
1204 app_id, | 1195 app_id, |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1447 removed_message_counts.begin(); | 1438 removed_message_counts.begin(); |
1448 iter != removed_message_counts.end(); ++iter) { | 1439 iter != removed_message_counts.end(); ++iter) { |
1449 DCHECK_NE(app_message_counts_.count(iter->first), 0U); | 1440 DCHECK_NE(app_message_counts_.count(iter->first), 0U); |
1450 app_message_counts_[iter->first] -= iter->second; | 1441 app_message_counts_[iter->first] -= iter->second; |
1451 DCHECK_GE(app_message_counts_[iter->first], 0); | 1442 DCHECK_GE(app_message_counts_[iter->first], 0); |
1452 } | 1443 } |
1453 callback.Run(true); | 1444 callback.Run(true); |
1454 } | 1445 } |
1455 | 1446 |
1456 } // namespace gcm | 1447 } // namespace gcm |
OLD | NEW |