| 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/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 const UpdateCallback& callback); | 155 const UpdateCallback& callback); |
| 156 void RemoveUserSerialNumber(const std::string& username, | 156 void RemoveUserSerialNumber(const std::string& username, |
| 157 const UpdateCallback& callback); | 157 const UpdateCallback& callback); |
| 158 void SetLastCheckinInfo(const base::Time& time, | 158 void SetLastCheckinInfo(const base::Time& time, |
| 159 const std::set<std::string>& accounts, | 159 const std::set<std::string>& accounts, |
| 160 const UpdateCallback& callback); | 160 const UpdateCallback& callback); |
| 161 void SetGServicesSettings( | 161 void SetGServicesSettings( |
| 162 const std::map<std::string, std::string>& settings, | 162 const std::map<std::string, std::string>& settings, |
| 163 const std::string& digest, | 163 const std::string& digest, |
| 164 const UpdateCallback& callback); | 164 const UpdateCallback& callback); |
| 165 void AddAccountMapping(const AccountInfo& account_info, | 165 void AddAccountMapping(const AccountMapping& account_mapping, |
| 166 const UpdateCallback& callback); | 166 const UpdateCallback& callback); |
| 167 void RemoveAccountMapping(const std::string& account_id, | 167 void RemoveAccountMapping(const std::string& account_id, |
| 168 const UpdateCallback& callback); | 168 const UpdateCallback& callback); |
| 169 | 169 |
| 170 private: | 170 private: |
| 171 friend class base::RefCountedThreadSafe<Backend>; | 171 friend class base::RefCountedThreadSafe<Backend>; |
| 172 ~Backend(); | 172 ~Backend(); |
| 173 | 173 |
| 174 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); | 174 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); |
| 175 bool LoadRegistrations(RegistrationInfoMap* registrations); | 175 bool LoadRegistrations(RegistrationInfoMap* registrations); |
| 176 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); | 176 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); |
| 177 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); | 177 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); |
| 178 bool LoadLastCheckinInfo(base::Time* last_checkin_time, | 178 bool LoadLastCheckinInfo(base::Time* last_checkin_time, |
| 179 std::set<std::string>* accounts); | 179 std::set<std::string>* accounts); |
| 180 bool LoadGServicesSettings(std::map<std::string, std::string>* settings, | 180 bool LoadGServicesSettings(std::map<std::string, std::string>* settings, |
| 181 std::string* digest); | 181 std::string* digest); |
| 182 bool LoadAccountMappingInfo(AccountInfoMap* account_infos); | 182 bool LoadAccountMappingInfo(AccountMappingMap* account_mappings); |
| 183 | 183 |
| 184 const base::FilePath path_; | 184 const base::FilePath path_; |
| 185 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; | 185 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; |
| 186 scoped_ptr<Encryptor> encryptor_; | 186 scoped_ptr<Encryptor> encryptor_; |
| 187 | 187 |
| 188 scoped_ptr<leveldb::DB> db_; | 188 scoped_ptr<leveldb::DB> db_; |
| 189 }; | 189 }; |
| 190 | 190 |
| 191 GCMStoreImpl::Backend::Backend( | 191 GCMStoreImpl::Backend::Backend( |
| 192 const base::FilePath& path, | 192 const base::FilePath& path, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 | 227 |
| 228 if (!LoadDeviceCredentials(&result->device_android_id, | 228 if (!LoadDeviceCredentials(&result->device_android_id, |
| 229 &result->device_security_token) || | 229 &result->device_security_token) || |
| 230 !LoadRegistrations(&result->registrations) || | 230 !LoadRegistrations(&result->registrations) || |
| 231 !LoadIncomingMessages(&result->incoming_messages) || | 231 !LoadIncomingMessages(&result->incoming_messages) || |
| 232 !LoadOutgoingMessages(&result->outgoing_messages) || | 232 !LoadOutgoingMessages(&result->outgoing_messages) || |
| 233 !LoadLastCheckinInfo(&result->last_checkin_time, | 233 !LoadLastCheckinInfo(&result->last_checkin_time, |
| 234 &result->last_checkin_accounts) || | 234 &result->last_checkin_accounts) || |
| 235 !LoadGServicesSettings(&result->gservices_settings, | 235 !LoadGServicesSettings(&result->gservices_settings, |
| 236 &result->gservices_digest) || | 236 &result->gservices_digest) || |
| 237 !LoadAccountMappingInfo(&result->account_infos)) { | 237 !LoadAccountMappingInfo(&result->account_mappings)) { |
| 238 result->Reset(); | 238 result->Reset(); |
| 239 foreground_task_runner_->PostTask(FROM_HERE, | 239 foreground_task_runner_->PostTask(FROM_HERE, |
| 240 base::Bind(callback, | 240 base::Bind(callback, |
| 241 base::Passed(&result))); | 241 base::Passed(&result))); |
| 242 return; | 242 return; |
| 243 } | 243 } |
| 244 | 244 |
| 245 // Only record histograms if GCM had already been set up for this device. | 245 // Only record histograms if GCM had already been set up for this device. |
| 246 if (result->device_android_id != 0 && result->device_security_token != 0) { | 246 if (result->device_android_id != 0 && result->device_security_token != 0) { |
| 247 int64 file_size = 0; | 247 int64 file_size = 0; |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 // Write it all in a batch. | 567 // Write it all in a batch. |
| 568 leveldb::WriteOptions write_options; | 568 leveldb::WriteOptions write_options; |
| 569 write_options.sync = true; | 569 write_options.sync = true; |
| 570 | 570 |
| 571 leveldb::Status s = db_->Write(write_options, &write_batch); | 571 leveldb::Status s = db_->Write(write_options, &write_batch); |
| 572 if (!s.ok()) | 572 if (!s.ok()) |
| 573 LOG(ERROR) << "LevelDB GService Settings update failed: " << s.ToString(); | 573 LOG(ERROR) << "LevelDB GService Settings update failed: " << s.ToString(); |
| 574 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok())); | 574 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok())); |
| 575 } | 575 } |
| 576 | 576 |
| 577 void GCMStoreImpl::Backend::AddAccountMapping(const AccountInfo& account_info, | 577 void GCMStoreImpl::Backend::AddAccountMapping( |
| 578 const UpdateCallback& callback) { | 578 const AccountMapping& account_mapping, |
| 579 const UpdateCallback& callback) { |
| 579 DVLOG(1) << "Saving account info for account with email: " | 580 DVLOG(1) << "Saving account info for account with email: " |
| 580 << account_info.email; | 581 << account_mapping.email; |
| 581 if (!db_.get()) { | 582 if (!db_.get()) { |
| 582 LOG(ERROR) << "GCMStore db doesn't exist."; | 583 LOG(ERROR) << "GCMStore db doesn't exist."; |
| 583 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 584 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
| 584 return; | 585 return; |
| 585 } | 586 } |
| 586 | 587 |
| 587 leveldb::WriteOptions write_options; | 588 leveldb::WriteOptions write_options; |
| 588 write_options.sync = true; | 589 write_options.sync = true; |
| 589 | 590 |
| 590 std::string data = account_info.SerializeAsString(); | 591 std::string data = account_mapping.SerializeAsString(); |
| 591 std::string key = MakeAccountKey(account_info.account_id); | 592 std::string key = MakeAccountKey(account_mapping.account_id); |
| 592 const leveldb::Status s = | 593 const leveldb::Status s = |
| 593 db_->Put(write_options, MakeSlice(key), MakeSlice(data)); | 594 db_->Put(write_options, MakeSlice(key), MakeSlice(data)); |
| 594 if (!s.ok()) | 595 if (!s.ok()) |
| 595 LOG(ERROR) << "LevelDB adding account mapping failed: " << s.ToString(); | 596 LOG(ERROR) << "LevelDB adding account mapping failed: " << s.ToString(); |
| 596 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok())); | 597 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok())); |
| 597 } | 598 } |
| 598 | 599 |
| 599 void GCMStoreImpl::Backend::RemoveAccountMapping( | 600 void GCMStoreImpl::Backend::RemoveAccountMapping( |
| 600 const std::string& account_id, | 601 const std::string& account_id, |
| 601 const UpdateCallback& callback) { | 602 const UpdateCallback& callback) { |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 << ", and value: " << value; | 784 << ", and value: " << value; |
| 784 } | 785 } |
| 785 | 786 |
| 786 // Load the settings digest. It's ok if it is empty. | 787 // Load the settings digest. It's ok if it is empty. |
| 787 db_->Get(read_options, MakeSlice(kGServiceSettingsDigestKey), digest); | 788 db_->Get(read_options, MakeSlice(kGServiceSettingsDigestKey), digest); |
| 788 | 789 |
| 789 return true; | 790 return true; |
| 790 } | 791 } |
| 791 | 792 |
| 792 bool GCMStoreImpl::Backend::LoadAccountMappingInfo( | 793 bool GCMStoreImpl::Backend::LoadAccountMappingInfo( |
| 793 AccountInfoMap* account_infos) { | 794 AccountMappingMap* account_mappings) { |
| 794 leveldb::ReadOptions read_options; | 795 leveldb::ReadOptions read_options; |
| 795 read_options.verify_checksums = true; | 796 read_options.verify_checksums = true; |
| 796 | 797 |
| 797 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); | 798 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); |
| 798 for (iter->Seek(MakeSlice(kAccountKeyStart)); | 799 for (iter->Seek(MakeSlice(kAccountKeyStart)); |
| 799 iter->Valid() && iter->key().ToString() < kAccountKeyEnd; | 800 iter->Valid() && iter->key().ToString() < kAccountKeyEnd; |
| 800 iter->Next()) { | 801 iter->Next()) { |
| 801 AccountInfo account_info; | 802 AccountMapping account_mapping; |
| 802 account_info.account_id = ParseAccountKey(iter->key().ToString()); | 803 account_mapping.account_id = ParseAccountKey(iter->key().ToString()); |
| 803 if (!account_info.ParseFromString(iter->value().ToString())) { | 804 if (!account_mapping.ParseFromString(iter->value().ToString())) { |
| 804 DVLOG(1) << "Failed to parse account info with ID: " | 805 DVLOG(1) << "Failed to parse account info with ID: " |
| 805 << account_info.account_id; | 806 << account_mapping.account_id; |
| 806 return false; | 807 return false; |
| 807 } | 808 } |
| 808 DVLOG(1) << "Found account mapping with ID: " << account_info.account_id; | 809 DVLOG(1) << "Found account mapping with ID: " << account_mapping.account_id; |
| 809 (*account_infos)[account_info.account_id] = account_info; | 810 (*account_mappings)[account_mapping.account_id] = account_mapping; |
| 810 } | 811 } |
| 811 | 812 |
| 812 return true; | 813 return true; |
| 813 } | 814 } |
| 814 | 815 |
| 815 GCMStoreImpl::GCMStoreImpl( | 816 GCMStoreImpl::GCMStoreImpl( |
| 816 const base::FilePath& path, | 817 const base::FilePath& path, |
| 817 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, | 818 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, |
| 818 scoped_ptr<Encryptor> encryptor) | 819 scoped_ptr<Encryptor> encryptor) |
| 819 : backend_(new Backend(path, | 820 : backend_(new Backend(path, |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1004 const UpdateCallback& callback) { | 1005 const UpdateCallback& callback) { |
| 1005 blocking_task_runner_->PostTask( | 1006 blocking_task_runner_->PostTask( |
| 1006 FROM_HERE, | 1007 FROM_HERE, |
| 1007 base::Bind(&GCMStoreImpl::Backend::SetGServicesSettings, | 1008 base::Bind(&GCMStoreImpl::Backend::SetGServicesSettings, |
| 1008 backend_, | 1009 backend_, |
| 1009 settings, | 1010 settings, |
| 1010 digest, | 1011 digest, |
| 1011 callback)); | 1012 callback)); |
| 1012 } | 1013 } |
| 1013 | 1014 |
| 1014 void GCMStoreImpl::AddAccountMapping(const AccountInfo& account_info, | 1015 void GCMStoreImpl::AddAccountMapping(const AccountMapping& account_mapping, |
| 1015 const UpdateCallback& callback) { | 1016 const UpdateCallback& callback) { |
| 1016 blocking_task_runner_->PostTask( | 1017 blocking_task_runner_->PostTask( |
| 1017 FROM_HERE, | 1018 FROM_HERE, |
| 1018 base::Bind(&GCMStoreImpl::Backend::AddAccountMapping, | 1019 base::Bind(&GCMStoreImpl::Backend::AddAccountMapping, |
| 1019 backend_, | 1020 backend_, |
| 1020 account_info, | 1021 account_mapping, |
| 1021 callback)); | 1022 callback)); |
| 1022 } | 1023 } |
| 1023 | 1024 |
| 1024 void GCMStoreImpl::RemoveAccountMapping(const std::string& account_id, | 1025 void GCMStoreImpl::RemoveAccountMapping(const std::string& account_id, |
| 1025 const UpdateCallback& callback) { | 1026 const UpdateCallback& callback) { |
| 1026 blocking_task_runner_->PostTask( | 1027 blocking_task_runner_->PostTask( |
| 1027 FROM_HERE, | 1028 FROM_HERE, |
| 1028 base::Bind(&GCMStoreImpl::Backend::RemoveAccountMapping, | 1029 base::Bind(&GCMStoreImpl::Backend::RemoveAccountMapping, |
| 1029 backend_, | 1030 backend_, |
| 1030 account_id, | 1031 account_id, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1078 removed_message_counts.begin(); | 1079 removed_message_counts.begin(); |
| 1079 iter != removed_message_counts.end(); ++iter) { | 1080 iter != removed_message_counts.end(); ++iter) { |
| 1080 DCHECK_NE(app_message_counts_.count(iter->first), 0U); | 1081 DCHECK_NE(app_message_counts_.count(iter->first), 0U); |
| 1081 app_message_counts_[iter->first] -= iter->second; | 1082 app_message_counts_[iter->first] -= iter->second; |
| 1082 DCHECK_GE(app_message_counts_[iter->first], 0); | 1083 DCHECK_GE(app_message_counts_[iter->first], 0); |
| 1083 } | 1084 } |
| 1084 callback.Run(true); | 1085 callback.Run(true); |
| 1085 } | 1086 } |
| 1086 | 1087 |
| 1087 } // namespace gcm | 1088 } // namespace gcm |
| OLD | NEW |