Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: google_apis/gcm/engine/gcm_store_impl.cc

Issue 884783003: [GCM] Fixing the initialization of last checkin/fetch time if they are (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding unit tests Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 void SetGServicesSettings( 163 void SetGServicesSettings(
164 const std::map<std::string, std::string>& settings, 164 const std::map<std::string, std::string>& settings,
165 const std::string& digest, 165 const std::string& digest,
166 const UpdateCallback& callback); 166 const UpdateCallback& callback);
167 void AddAccountMapping(const AccountMapping& account_mapping, 167 void AddAccountMapping(const AccountMapping& account_mapping,
168 const UpdateCallback& callback); 168 const UpdateCallback& callback);
169 void RemoveAccountMapping(const std::string& account_id, 169 void RemoveAccountMapping(const std::string& account_id,
170 const UpdateCallback& callback); 170 const UpdateCallback& callback);
171 void SetLastTokenFetchTime(const base::Time& time, 171 void SetLastTokenFetchTime(const base::Time& time,
172 const UpdateCallback& callback); 172 const UpdateCallback& callback);
173 void SetValue(const std::string& key,
174 const std::string& value,
175 const UpdateCallback& callback);
173 176
174 private: 177 private:
175 friend class base::RefCountedThreadSafe<Backend>; 178 friend class base::RefCountedThreadSafe<Backend>;
176 ~Backend(); 179 ~Backend();
177 180
178 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); 181 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token);
179 bool LoadRegistrations(RegistrationInfoMap* registrations); 182 bool LoadRegistrations(RegistrationInfoMap* registrations);
180 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); 183 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages);
181 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); 184 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages);
182 bool LoadLastCheckinInfo(base::Time* last_checkin_time, 185 bool LoadLastCheckinInfo(base::Time* last_checkin_time,
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 const leveldb::Status s = 643 const leveldb::Status s =
641 db_->Put(write_options, 644 db_->Put(write_options,
642 MakeSlice(kLastTokenFetchTimeKey), 645 MakeSlice(kLastTokenFetchTimeKey),
643 MakeSlice(base::Int64ToString(time.ToInternalValue()))); 646 MakeSlice(base::Int64ToString(time.ToInternalValue())));
644 647
645 if (!s.ok()) 648 if (!s.ok())
646 LOG(ERROR) << "LevelDB setting last token fetching time: " << s.ToString(); 649 LOG(ERROR) << "LevelDB setting last token fetching time: " << s.ToString();
647 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok())); 650 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok()));
648 } 651 }
649 652
653 void GCMStoreImpl::Backend::SetValue(const std::string& key,
654 const std::string& value,
655 const UpdateCallback& callback) {
656 DVLOG(1) << "Injecting a value to GCM Store for testing. Key: "
657 << key << ", Value: " << value;
658 if (!db_.get()) {
659 LOG(ERROR) << "GCMStore db doesn't exist.";
660 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false));
661 return;
662 }
663
664 leveldb::WriteOptions write_options;
665 write_options.sync = true;
666
667 const leveldb::Status s =
668 db_->Put(write_options, MakeSlice(key), MakeSlice(value));
669
670 if (!s.ok())
671 LOG(ERROR) << "LevelDB had problems injecting a value: " << s.ToString();
672 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok()));
673 }
674
650 bool GCMStoreImpl::Backend::LoadDeviceCredentials(uint64* android_id, 675 bool GCMStoreImpl::Backend::LoadDeviceCredentials(uint64* android_id,
651 uint64* security_token) { 676 uint64* security_token) {
652 leveldb::ReadOptions read_options; 677 leveldb::ReadOptions read_options;
653 read_options.verify_checksums = true; 678 read_options.verify_checksums = true;
654 679
655 std::string result; 680 std::string result;
656 leveldb::Status s = db_->Get(read_options, MakeSlice(kDeviceAIDKey), &result); 681 leveldb::Status s = db_->Get(read_options, MakeSlice(kDeviceAIDKey), &result);
657 if (s.ok()) { 682 if (s.ok()) {
658 if (!base::StringToUint64(result, android_id)) { 683 if (!base::StringToUint64(result, android_id)) {
659 LOG(ERROR) << "Failed to restore device id."; 684 LOG(ERROR) << "Failed to restore device id.";
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
766 base::Time* last_checkin_time, 791 base::Time* last_checkin_time,
767 std::set<std::string>* accounts) { 792 std::set<std::string>* accounts) {
768 leveldb::ReadOptions read_options; 793 leveldb::ReadOptions read_options;
769 read_options.verify_checksums = true; 794 read_options.verify_checksums = true;
770 795
771 std::string result; 796 std::string result;
772 leveldb::Status s = db_->Get(read_options, 797 leveldb::Status s = db_->Get(read_options,
773 MakeSlice(kLastCheckinTimeKey), 798 MakeSlice(kLastCheckinTimeKey),
774 &result); 799 &result);
775 int64 time_internal = 0LL; 800 int64 time_internal = 0LL;
776 if (s.ok() && !base::StringToInt64(result, &time_internal)) 801 if (s.ok() && !base::StringToInt64(result, &time_internal)) {
777 LOG(ERROR) << "Failed to restore last checkin time. Using default = 0."; 802 LOG(ERROR) << "Failed to restore last checkin time. Using default = 0.";
803 time_internal = 0LL;
804 }
778 805
779 // In case we cannot read last checkin time, we default it to 0, as we don't 806 // In case we cannot read last checkin time, we default it to 0, as we don't
780 // want that situation to cause the whole load to fail. 807 // want that situation to cause the whole load to fail.
781 *last_checkin_time = base::Time::FromInternalValue(time_internal); 808 *last_checkin_time = base::Time::FromInternalValue(time_internal);
782 809
783 accounts->clear(); 810 accounts->clear();
784 s = db_->Get(read_options, MakeSlice(kLastCheckinAccountsKey), &result); 811 s = db_->Get(read_options, MakeSlice(kLastCheckinAccountsKey), &result);
785 if (!s.ok()) 812 if (!s.ok())
786 DVLOG(1) << "No accounts where stored during last run."; 813 DVLOG(1) << "No accounts where stored during last run.";
787 814
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 872
846 bool GCMStoreImpl::Backend::LoadLastTokenFetchTime( 873 bool GCMStoreImpl::Backend::LoadLastTokenFetchTime(
847 base::Time* last_token_fetch_time) { 874 base::Time* last_token_fetch_time) {
848 leveldb::ReadOptions read_options; 875 leveldb::ReadOptions read_options;
849 read_options.verify_checksums = true; 876 read_options.verify_checksums = true;
850 877
851 std::string result; 878 std::string result;
852 leveldb::Status s = 879 leveldb::Status s =
853 db_->Get(read_options, MakeSlice(kLastTokenFetchTimeKey), &result); 880 db_->Get(read_options, MakeSlice(kLastTokenFetchTimeKey), &result);
854 int64 time_internal = 0LL; 881 int64 time_internal = 0LL;
855 if (s.ok() && !base::StringToInt64(result, &time_internal)) 882 if (s.ok() && !base::StringToInt64(result, &time_internal)) {
856 LOG(ERROR) << "Failed to restore last checkin time. Using default = 0."; 883 LOG(ERROR) <<
884 "Failed to restore last token fetching time. Using default = 0.";
885 time_internal = 0LL;
886 }
857 887
858 // In case we cannot read last token fetching time, we default it to 0. 888 // In case we cannot read last token fetching time, we default it to 0.
859 *last_token_fetch_time = base::Time::FromInternalValue(time_internal); 889 *last_token_fetch_time = base::Time::FromInternalValue(time_internal);
860 890
861 return true; 891 return true;
862 } 892 }
863 893
864 GCMStoreImpl::GCMStoreImpl( 894 GCMStoreImpl::GCMStoreImpl(
865 const base::FilePath& path, 895 const base::FilePath& path,
866 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, 896 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner,
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 void GCMStoreImpl::SetLastTokenFetchTime(const base::Time& time, 1113 void GCMStoreImpl::SetLastTokenFetchTime(const base::Time& time,
1084 const UpdateCallback& callback) { 1114 const UpdateCallback& callback) {
1085 blocking_task_runner_->PostTask( 1115 blocking_task_runner_->PostTask(
1086 FROM_HERE, 1116 FROM_HERE,
1087 base::Bind(&GCMStoreImpl::Backend::SetLastTokenFetchTime, 1117 base::Bind(&GCMStoreImpl::Backend::SetLastTokenFetchTime,
1088 backend_, 1118 backend_,
1089 time, 1119 time,
1090 callback)); 1120 callback));
1091 } 1121 }
1092 1122
1123 void GCMStoreImpl::SetValueForTesting(const std::string& key,
1124 const std::string& value,
1125 const UpdateCallback& callback) {
1126 blocking_task_runner_->PostTask(
1127 FROM_HERE,
1128 base::Bind(&GCMStoreImpl::Backend::SetValue,
1129 backend_,
1130 key,
1131 value,
1132 callback));
1133 }
1134
1093 void GCMStoreImpl::LoadContinuation(const LoadCallback& callback, 1135 void GCMStoreImpl::LoadContinuation(const LoadCallback& callback,
1094 scoped_ptr<LoadResult> result) { 1136 scoped_ptr<LoadResult> result) {
1095 if (!result->success) { 1137 if (!result->success) {
1096 callback.Run(result.Pass()); 1138 callback.Run(result.Pass());
1097 return; 1139 return;
1098 } 1140 }
1099 int num_throttled_apps = 0; 1141 int num_throttled_apps = 0;
1100 for (OutgoingMessageMap::const_iterator 1142 for (OutgoingMessageMap::const_iterator
1101 iter = result->outgoing_messages.begin(); 1143 iter = result->outgoing_messages.begin();
1102 iter != result->outgoing_messages.end(); ++iter) { 1144 iter != result->outgoing_messages.end(); ++iter) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1137 removed_message_counts.begin(); 1179 removed_message_counts.begin();
1138 iter != removed_message_counts.end(); ++iter) { 1180 iter != removed_message_counts.end(); ++iter) {
1139 DCHECK_NE(app_message_counts_.count(iter->first), 0U); 1181 DCHECK_NE(app_message_counts_.count(iter->first), 0U);
1140 app_message_counts_[iter->first] -= iter->second; 1182 app_message_counts_[iter->first] -= iter->second;
1141 DCHECK_GE(app_message_counts_[iter->first], 0); 1183 DCHECK_GE(app_message_counts_[iter->first], 0);
1142 } 1184 }
1143 callback.Run(true); 1185 callback.Run(true);
1144 } 1186 }
1145 1187
1146 } // namespace gcm 1188 } // namespace gcm
OLDNEW
« no previous file with comments | « google_apis/gcm/engine/gcm_store_impl.h ('k') | google_apis/gcm/engine/gcm_store_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698