| Index: components/precache/core/precache_session_table.cc
|
| diff --git a/components/precache/core/precache_session_table.cc b/components/precache/core/precache_session_table.cc
|
| index 9993c069bbe72ef3de7d9384c6bd79b3d25042b4..ae938ba2d57ff01d026384c128e8338f7532499b 100644
|
| --- a/components/precache/core/precache_session_table.cc
|
| +++ b/components/precache/core/precache_session_table.cc
|
| @@ -9,6 +9,7 @@
|
|
|
| #include "base/logging.h"
|
| #include "base/time/time.h"
|
| +#include "components/precache/core/proto/quota.pb.h"
|
| #include "components/precache/core/proto/timestamp.pb.h"
|
| #include "components/precache/core/proto/unfinished_work.pb.h"
|
| #include "sql/connection.h"
|
| @@ -29,25 +30,35 @@ bool PrecacheSessionTable::Init(sql::Connection* db) {
|
| return CreateTableIfNonExistent();
|
| }
|
|
|
| -void PrecacheSessionTable::SetLastPrecacheTimestamp(const base::Time& time) {
|
| - DCHECK(!time.is_null());
|
| - Timestamp timestamp;
|
| - timestamp.set_seconds((time - base::Time::UnixEpoch()).InSeconds());
|
| +void PrecacheSessionTable::SetSessionDataType(SessionDataType id,
|
| + const std::string& data) {
|
| Statement statement(db_->GetCachedStatement(
|
| SQL_FROM_HERE,
|
| "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)"));
|
| - statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP));
|
| - statement.BindString(1, timestamp.SerializeAsString());
|
| + statement.BindInt(0, static_cast<int>(id));
|
| + statement.BindString(1, data);
|
| statement.Run();
|
| }
|
|
|
| -base::Time PrecacheSessionTable::GetLastPrecacheTimestamp() {
|
| +std::string PrecacheSessionTable::GetSessionDataType(SessionDataType id) {
|
| Statement statement(db_->GetCachedStatement(
|
| SQL_FROM_HERE, "SELECT value from precache_session where type=?"));
|
| - statement.BindInt(0, static_cast<int>(LAST_PRECACHE_TIMESTAMP));
|
| + statement.BindInt(0, static_cast<int>(id));
|
| + return statement.Step() ? statement.ColumnString(0) : std::string();
|
| +}
|
| +
|
| +void PrecacheSessionTable::SetLastPrecacheTimestamp(const base::Time& time) {
|
| + DCHECK(!time.is_null());
|
| + Timestamp timestamp;
|
| + timestamp.set_seconds((time - base::Time::UnixEpoch()).InSeconds());
|
| + SetSessionDataType(LAST_PRECACHE_TIMESTAMP, timestamp.SerializeAsString());
|
| +}
|
| +
|
| +base::Time PrecacheSessionTable::GetLastPrecacheTimestamp() {
|
| Timestamp timestamp;
|
| - if (statement.Step())
|
| - timestamp.ParseFromString(statement.ColumnString(0));
|
| + const std::string data = GetSessionDataType(LAST_PRECACHE_TIMESTAMP);
|
| + if (!data.empty())
|
| + timestamp.ParseFromString(data);
|
| return timestamp.has_seconds()
|
| ? base::Time::UnixEpoch() +
|
| base::TimeDelta::FromSeconds(timestamp.seconds())
|
| @@ -64,29 +75,20 @@ void PrecacheSessionTable::DeleteLastPrecacheTimestamp() {
|
| // Store unfinished work.
|
| void PrecacheSessionTable::SaveUnfinishedWork(
|
| std::unique_ptr<PrecacheUnfinishedWork> unfinished_work) {
|
| - Statement statement(db_->GetCachedStatement(
|
| - SQL_FROM_HERE,
|
| - "INSERT OR REPLACE INTO precache_session (type, value) VALUES(?,?)"));
|
| - statement.BindInt(0, static_cast<int>(UNFINISHED_WORK));
|
| - statement.BindString(1, unfinished_work->SerializeAsString());
|
| - statement.Run();
|
| + SetSessionDataType(UNFINISHED_WORK, unfinished_work->SerializeAsString());
|
| }
|
|
|
| // Retrieve unfinished work.
|
| std::unique_ptr<PrecacheUnfinishedWork>
|
| PrecacheSessionTable::GetUnfinishedWork() {
|
| - Statement statement(db_->GetCachedStatement(
|
| - SQL_FROM_HERE, "SELECT value from precache_session where type=?"));
|
| - statement.BindInt(0, static_cast<int>(UNFINISHED_WORK));
|
| + const std::string data = GetSessionDataType(UNFINISHED_WORK);
|
| std::unique_ptr<PrecacheUnfinishedWork> unfinished_work(
|
| new PrecacheUnfinishedWork());
|
| - if (statement.Step())
|
| - unfinished_work->ParseFromString(statement.ColumnString(0));
|
| + if (!data.empty())
|
| + unfinished_work->ParseFromString(data);
|
| return unfinished_work;
|
| }
|
|
|
| -
|
| -
|
| void PrecacheSessionTable::DeleteUnfinishedWork() {
|
| Statement statement(
|
| db_->GetCachedStatement(
|
| @@ -95,6 +97,18 @@ void PrecacheSessionTable::DeleteUnfinishedWork() {
|
| statement.Run();
|
| }
|
|
|
| +void PrecacheSessionTable::SaveQuota(const PrecacheQuota& quota) {
|
| + SetSessionDataType(QUOTA, quota.SerializeAsString());
|
| +}
|
| +
|
| +PrecacheQuota PrecacheSessionTable::GetQuota() {
|
| + PrecacheQuota quota;
|
| + const std::string data = GetSessionDataType(QUOTA);
|
| + if (!data.empty())
|
| + quota.ParseFromString(data);
|
| + return quota;
|
| +}
|
| +
|
| bool PrecacheSessionTable::CreateTableIfNonExistent() {
|
| return db_->Execute(
|
| "CREATE TABLE IF NOT EXISTS precache_session (type INTEGER PRIMARY KEY, "
|
|
|