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

Unified Diff: google_apis/gcm/engine/gcm_store_impl.cc

Issue 681453004: [GCM] Adding last token fetching time handling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: google_apis/gcm/engine/gcm_store_impl.cc
diff --git a/google_apis/gcm/engine/gcm_store_impl.cc b/google_apis/gcm/engine/gcm_store_impl.cc
index 64683ed86c1bdf862483dae5a47380b72c02de07..ee1b5308d08280fa92617ece7863ba1fb2261bd3 100644
--- a/google_apis/gcm/engine/gcm_store_impl.cc
+++ b/google_apis/gcm/engine/gcm_store_impl.cc
@@ -74,6 +74,8 @@ const char kAccountKeyStart[] = "account1-";
// Key guaranteed to be higher than all account keys.
// Used for limiting iteration.
const char kAccountKeyEnd[] = "account2-";
+// Key user to timestamp last token fetching time.
+const char kLastTokenFetchingTimeKey[] = "last_token_fetching_time";
std::string MakeRegistrationKey(const std::string& app_id) {
return kRegistrationKeyStart + app_id;
@@ -166,6 +168,8 @@ class GCMStoreImpl::Backend
const UpdateCallback& callback);
void RemoveAccountMapping(const std::string& account_id,
const UpdateCallback& callback);
+ void SetLastTokenFetchingTime(const base::Time& time,
+ const UpdateCallback& callback);
private:
friend class base::RefCountedThreadSafe<Backend>;
@@ -180,6 +184,7 @@ class GCMStoreImpl::Backend
bool LoadGServicesSettings(std::map<std::string, std::string>* settings,
std::string* digest);
bool LoadAccountMappingInfo(AccountMappings* account_mappings);
+ bool LoadLastTokenFetchingTime(base::Time* last_token_fetching_time);
const base::FilePath path_;
scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_;
@@ -234,7 +239,8 @@ void GCMStoreImpl::Backend::Load(const LoadCallback& callback) {
&result->last_checkin_accounts) ||
!LoadGServicesSettings(&result->gservices_settings,
&result->gservices_digest) ||
- !LoadAccountMappingInfo(&result->account_mappings)) {
+ !LoadAccountMappingInfo(&result->account_mappings) ||
+ !LoadLastTokenFetchingTime(&result->last_token_fetching_time)) {
result->Reset();
foreground_task_runner_->PostTask(FROM_HERE,
base::Bind(callback,
@@ -618,6 +624,29 @@ void GCMStoreImpl::Backend::RemoveAccountMapping(
foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok()));
}
+void GCMStoreImpl::Backend::SetLastTokenFetchingTime(
+ const base::Time& time,
+ const UpdateCallback& callback) {
+ DVLOG(1) << "Setting last token fetching time.";
+ if (!db_.get()) {
+ LOG(ERROR) << "GCMStore db doesn't exist.";
+ foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false));
+ return;
+ }
+
+ leveldb::WriteOptions write_options;
+ write_options.sync = true;
+
+ const leveldb::Status s = db_->Put(
+ write_options,
+ MakeSlice(kLastTokenFetchingTimeKey),
+ MakeSlice(base::Int64ToString(time.ToInternalValue())));
+
+ if (!s.ok())
+ LOG(ERROR) << "LevelDB setting last token fetching time: " << s.ToString();
+ foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok()));
+}
+
bool GCMStoreImpl::Backend::LoadDeviceCredentials(uint64* android_id,
uint64* security_token) {
leveldb::ReadOptions read_options;
@@ -814,6 +843,25 @@ bool GCMStoreImpl::Backend::LoadAccountMappingInfo(
return true;
}
+bool GCMStoreImpl::Backend::LoadLastTokenFetchingTime(
+ base::Time* last_token_fetching_time) {
+ leveldb::ReadOptions read_options;
+ read_options.verify_checksums = true;
+
+ std::string result;
+ leveldb::Status s = db_->Get(read_options,
+ MakeSlice(kLastTokenFetchingTimeKey),
+ &result);
+ int64 time_internal = 0LL;
+ if (s.ok() && !base::StringToInt64(result, &time_internal))
+ LOG(ERROR) << "Failed to restore last checkin time. Using default = 0.";
+
+ // In case we cannot read last token fetching time, we default it to 0.
+ *last_token_fetching_time = base::Time::FromInternalValue(time_internal);
+
+ return true;
+}
+
GCMStoreImpl::GCMStoreImpl(
const base::FilePath& path,
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner,
@@ -1033,6 +1081,16 @@ void GCMStoreImpl::RemoveAccountMapping(const std::string& account_id,
callback));
}
+void GCMStoreImpl::SetLastTokenFetchingTime(const base::Time& time,
+ const UpdateCallback& callback) {
+ blocking_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&GCMStoreImpl::Backend::SetLastTokenFetchingTime,
+ backend_,
+ time,
+ callback));
+}
+
void GCMStoreImpl::LoadContinuation(const LoadCallback& callback,
scoped_ptr<LoadResult> result) {
if (!result->success) {

Powered by Google App Engine
This is Rietveld 408576698