| 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 std::string key = MakeRegistrationKey(serialized_key); |
| 433 const leveldb::Status status = db_->Put(write_options, | 433 const leveldb::Status status = db_->Put( |
| 434 MakeSlice(key), | 434 write_options, |
| 435 MakeSlice(serialized_registration)); | 435 MakeSlice(MakeRegistrationKey(serialized_key)), |
| 436 if (status.ok()) { | 436 MakeSlice(serialized_value)); |
| 437 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 437 if (!status.ok()) |
| 438 return; | 438 LOG(ERROR) << "LevelDB put failed: " << status.ToString(); |
| 439 } | 439 foreground_task_runner_->PostTask( |
| 440 LOG(ERROR) << "LevelDB put failed: " << status.ToString(); | 440 FROM_HERE, base::Bind(callback, status.ok())); |
| 441 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | |
| 442 } | 441 } |
| 443 | 442 |
| 444 void GCMStoreImpl::Backend::RemoveRegistration(const std::string& app_id, | 443 void GCMStoreImpl::Backend::RemoveRegistration( |
| 445 const UpdateCallback& callback) { | 444 const std::string& serialized_key, |
| 445 const UpdateCallback& callback) { |
| 446 if (!db_.get()) { | 446 if (!db_.get()) { |
| 447 LOG(ERROR) << "GCMStore db doesn't exist."; | 447 LOG(ERROR) << "GCMStore db doesn't exist."; |
| 448 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 448 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
| 449 return; | 449 return; |
| 450 } | 450 } |
| 451 leveldb::WriteOptions write_options; | 451 leveldb::WriteOptions write_options; |
| 452 write_options.sync = true; | 452 write_options.sync = true; |
| 453 | 453 |
| 454 leveldb::Status status = | 454 leveldb::Status status = db_->Delete( |
| 455 db_->Delete(write_options, MakeSlice(MakeRegistrationKey(app_id))); | 455 write_options, MakeSlice(MakeRegistrationKey(serialized_key))); |
| 456 if (status.ok()) { | 456 if (!status.ok()) |
| 457 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 457 LOG(ERROR) << "LevelDB remove failed: " << status.ToString(); |
| 458 return; | 458 foreground_task_runner_->PostTask( |
| 459 } | 459 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 } | 460 } |
| 463 | 461 |
| 464 void GCMStoreImpl::Backend::AddIncomingMessage(const std::string& persistent_id, | 462 void GCMStoreImpl::Backend::AddIncomingMessage(const std::string& persistent_id, |
| 465 const UpdateCallback& callback) { | 463 const UpdateCallback& callback) { |
| 466 DVLOG(1) << "Saving incoming message with id " << persistent_id; | 464 DVLOG(1) << "Saving incoming message with id " << persistent_id; |
| 467 if (!db_.get()) { | 465 if (!db_.get()) { |
| 468 LOG(ERROR) << "GCMStore db doesn't exist."; | 466 LOG(ERROR) << "GCMStore db doesn't exist."; |
| 469 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 467 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
| 470 return; | 468 return; |
| 471 } | 469 } |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 if (s.IsNotFound()) { | 893 if (s.IsNotFound()) { |
| 896 DVLOG(1) << "No credentials found."; | 894 DVLOG(1) << "No credentials found."; |
| 897 return true; | 895 return true; |
| 898 } | 896 } |
| 899 | 897 |
| 900 LOG(ERROR) << "Error reading credentials from store."; | 898 LOG(ERROR) << "Error reading credentials from store."; |
| 901 return false; | 899 return false; |
| 902 } | 900 } |
| 903 | 901 |
| 904 bool GCMStoreImpl::Backend::LoadRegistrations( | 902 bool GCMStoreImpl::Backend::LoadRegistrations( |
| 905 RegistrationInfoMap* registrations) { | 903 std::map<std::string, std::string>* registrations) { |
| 906 leveldb::ReadOptions read_options; | 904 leveldb::ReadOptions read_options; |
| 907 read_options.verify_checksums = true; | 905 read_options.verify_checksums = true; |
| 908 | 906 |
| 909 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); | 907 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); |
| 910 for (iter->Seek(MakeSlice(kRegistrationKeyStart)); | 908 for (iter->Seek(MakeSlice(kRegistrationKeyStart)); |
| 911 iter->Valid() && iter->key().ToString() < kRegistrationKeyEnd; | 909 iter->Valid() && iter->key().ToString() < kRegistrationKeyEnd; |
| 912 iter->Next()) { | 910 iter->Next()) { |
| 913 leveldb::Slice s = iter->value(); | 911 leveldb::Slice s = iter->value(); |
| 914 if (s.size() <= 1) { | 912 if (s.size() <= 1) { |
| 915 LOG(ERROR) << "Error reading registration with key " << s.ToString(); | 913 LOG(ERROR) << "Error reading registration with key " << s.ToString(); |
| 916 return false; | 914 return false; |
| 917 } | 915 } |
| 918 std::string app_id = ParseRegistrationKey(iter->key().ToString()); | 916 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; | 917 DVLOG(1) << "Found registration with app id " << app_id; |
| 925 (*registrations)[app_id] = registration; | 918 (*registrations)[app_id] = iter->value().ToString(); |
| 926 } | 919 } |
| 927 | 920 |
| 928 return true; | 921 return true; |
| 929 } | 922 } |
| 930 | 923 |
| 931 bool GCMStoreImpl::Backend::LoadIncomingMessages( | 924 bool GCMStoreImpl::Backend::LoadIncomingMessages( |
| 932 std::vector<std::string>* incoming_messages) { | 925 std::vector<std::string>* incoming_messages) { |
| 933 leveldb::ReadOptions read_options; | 926 leveldb::ReadOptions read_options; |
| 934 read_options.verify_checksums = true; | 927 read_options.verify_checksums = true; |
| 935 | 928 |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1175 blocking_task_runner_->PostTask( | 1168 blocking_task_runner_->PostTask( |
| 1176 FROM_HERE, | 1169 FROM_HERE, |
| 1177 base::Bind(&GCMStoreImpl::Backend::SetDeviceCredentials, | 1170 base::Bind(&GCMStoreImpl::Backend::SetDeviceCredentials, |
| 1178 backend_, | 1171 backend_, |
| 1179 device_android_id, | 1172 device_android_id, |
| 1180 device_security_token, | 1173 device_security_token, |
| 1181 callback)); | 1174 callback)); |
| 1182 } | 1175 } |
| 1183 | 1176 |
| 1184 void GCMStoreImpl::AddRegistration( | 1177 void GCMStoreImpl::AddRegistration( |
| 1185 const std::string& app_id, | 1178 const std::string& serialized_key, |
| 1186 const linked_ptr<RegistrationInfo>& registration, | 1179 const std::string& serialized_value, |
| 1187 const UpdateCallback& callback) { | 1180 const UpdateCallback& callback) { |
| 1188 std::string serialized_registration = registration->SerializeAsString(); | |
| 1189 blocking_task_runner_->PostTask( | 1181 blocking_task_runner_->PostTask( |
| 1190 FROM_HERE, | 1182 FROM_HERE, |
| 1191 base::Bind(&GCMStoreImpl::Backend::AddRegistration, | 1183 base::Bind(&GCMStoreImpl::Backend::AddRegistration, |
| 1192 backend_, | 1184 backend_, |
| 1193 app_id, | 1185 serialized_key, |
| 1194 serialized_registration, | 1186 serialized_value, |
| 1195 callback)); | 1187 callback)); |
| 1196 } | 1188 } |
| 1197 | 1189 |
| 1198 void GCMStoreImpl::RemoveRegistration(const std::string& app_id, | 1190 void GCMStoreImpl::RemoveRegistration(const std::string& app_id, |
| 1199 const UpdateCallback& callback) { | 1191 const UpdateCallback& callback) { |
| 1200 blocking_task_runner_->PostTask( | 1192 blocking_task_runner_->PostTask( |
| 1201 FROM_HERE, | 1193 FROM_HERE, |
| 1202 base::Bind(&GCMStoreImpl::Backend::RemoveRegistration, | 1194 base::Bind(&GCMStoreImpl::Backend::RemoveRegistration, |
| 1203 backend_, | 1195 backend_, |
| 1204 app_id, | 1196 app_id, |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1447 removed_message_counts.begin(); | 1439 removed_message_counts.begin(); |
| 1448 iter != removed_message_counts.end(); ++iter) { | 1440 iter != removed_message_counts.end(); ++iter) { |
| 1449 DCHECK_NE(app_message_counts_.count(iter->first), 0U); | 1441 DCHECK_NE(app_message_counts_.count(iter->first), 0U); |
| 1450 app_message_counts_[iter->first] -= iter->second; | 1442 app_message_counts_[iter->first] -= iter->second; |
| 1451 DCHECK_GE(app_message_counts_[iter->first], 0); | 1443 DCHECK_GE(app_message_counts_[iter->first], 0); |
| 1452 } | 1444 } |
| 1453 callback.Run(true); | 1445 callback.Run(true); |
| 1454 } | 1446 } |
| 1455 | 1447 |
| 1456 } // namespace gcm | 1448 } // namespace gcm |
| OLD | NEW |