| Index: components/precache/core/precache_url_table.cc
|
| diff --git a/components/precache/core/precache_url_table.cc b/components/precache/core/precache_url_table.cc
|
| index 3c39b9e5e0da05b824a96e4ab58f9cfaee846a88..9955377facd5ff17f39bd8aa5bf22e27edc93051 100644
|
| --- a/components/precache/core/precache_url_table.cc
|
| +++ b/components/precache/core/precache_url_table.cc
|
| @@ -35,24 +35,69 @@ bool PrecacheURLTable::Init(sql::Connection* db) {
|
| }
|
|
|
| void PrecacheURLTable::AddURL(const GURL& url,
|
| + int64_t referrer_host_id,
|
| + bool is_precached,
|
| const base::Time& precache_time) {
|
| - Statement statement(db_->GetCachedStatement(
|
| - SQL_FROM_HERE,
|
| - "INSERT OR REPLACE INTO precache_urls (url, time) VALUES(?,?)"));
|
| + Statement statement(
|
| + db_->GetCachedStatement(SQL_FROM_HERE,
|
| + "INSERT OR REPLACE INTO precache_urls (url, "
|
| + "referrer_host_id, was_used, is_precached, time) "
|
| + "VALUES(?,?,0,?,?)"));
|
|
|
| statement.BindString(0, GetKey(url));
|
| - statement.BindInt64(1, precache_time.ToInternalValue());
|
| + statement.BindInt64(1, referrer_host_id);
|
| + statement.BindInt64(2, is_precached ? 1 : 0);
|
| + statement.BindInt64(3, precache_time.ToInternalValue());
|
| statement.Run();
|
| }
|
|
|
| -bool PrecacheURLTable::HasURL(const GURL& url) {
|
| +bool PrecacheURLTable::IsURLPrecached(const GURL& url) {
|
| Statement statement(db_->GetCachedStatement(
|
| - SQL_FROM_HERE, "SELECT time FROM precache_urls WHERE url=?"));
|
| + SQL_FROM_HERE,
|
| + "SELECT time FROM precache_urls WHERE url=? and is_precached=1"));
|
| +
|
| + statement.BindString(0, GetKey(url));
|
| + return statement.Step();
|
| +}
|
| +
|
| +bool PrecacheURLTable::IsURLPrecachedAndUnused(const GURL& url) {
|
| + Statement statement(db_->GetCachedStatement(SQL_FROM_HERE,
|
| + "SELECT time FROM precache_urls "
|
| + "WHERE url=? and is_precached=1 "
|
| + "and was_used=0"));
|
|
|
| statement.BindString(0, GetKey(url));
|
| return statement.Step();
|
| }
|
|
|
| +void PrecacheURLTable::SetPrecachedURLAsUsed(const GURL& url) {
|
| + Statement statement(
|
| + db_->GetCachedStatement(SQL_FROM_HERE,
|
| + "UPDATE precache_urls SET was_used=1, "
|
| + "is_precached=0 "
|
| + "WHERE url=? and was_used=0 and is_precached=1"));
|
| +
|
| + statement.BindString(0, GetKey(url));
|
| + statement.Run();
|
| +}
|
| +
|
| +void PrecacheURLTable::GetURLListForReferrerHost(
|
| + int64_t referrer_host_id,
|
| + std::deque<GURL>& used_urls,
|
| + std::deque<GURL>& unused_urls) {
|
| + Statement statement(db_->GetCachedStatement(
|
| + SQL_FROM_HERE,
|
| + "SELECT url, was_used from precache_urls where referrer_host_id=?"));
|
| + statement.BindInt64(0, referrer_host_id);
|
| + while (statement.Step()) {
|
| + GURL url(statement.ColumnString(0));
|
| + if (statement.ColumnInt(1))
|
| + used_urls.push_back(url);
|
| + else
|
| + unused_urls.push_back(url);
|
| + }
|
| +}
|
| +
|
| void PrecacheURLTable::DeleteURL(const GURL& url) {
|
| Statement statement(db_->GetCachedStatement(
|
| SQL_FROM_HERE, "DELETE FROM precache_urls WHERE url=?"));
|
| @@ -80,7 +125,8 @@ void PrecacheURLTable::GetAllDataForTesting(std::map<GURL, base::Time>* map) {
|
| map->clear();
|
|
|
| Statement statement(db_->GetCachedStatement(
|
| - SQL_FROM_HERE, "SELECT url, time FROM precache_urls"));
|
| + SQL_FROM_HERE,
|
| + "SELECT url, time FROM precache_urls where is_precached=1"));
|
|
|
| while (statement.Step()) {
|
| GURL url = GURL(statement.ColumnString(0));
|
| @@ -89,9 +135,27 @@ void PrecacheURLTable::GetAllDataForTesting(std::map<GURL, base::Time>* map) {
|
| }
|
|
|
| bool PrecacheURLTable::CreateTableIfNonExistent() {
|
| - return db_->Execute(
|
| - "CREATE TABLE IF NOT EXISTS precache_urls (url TEXT PRIMARY KEY, time "
|
| - "INTEGER)");
|
| + if (!db_->DoesTableExist("precache_urls")) {
|
| + return db_->Execute(
|
| + "CREATE TABLE precache_urls "
|
| + "(url TEXT PRIMARY KEY, referrer_host_id INTEGER, was_used INTEGER, "
|
| + "is_precached INTEGER, "
|
| + "time INTEGER)");
|
| + } else {
|
| + // Migrate the table by creating the missing columns.
|
| + if (!db_->DoesColumnExist("precache_urls", "was_used") &&
|
| + !db_->Execute("ALTER TABLE precache_urls ADD COLUMN was_used INTEGER"))
|
| + return false;
|
| + if (!db_->DoesColumnExist("precache_urls", "is_precached") &&
|
| + !db_->Execute(
|
| + "ALTER TABLE precache_urls ADD COLUMN is_precached INTEGER"))
|
| + return false;
|
| + if (!db_->DoesColumnExist("precache_urls", "referrer_host_id") &&
|
| + !db_->Execute(
|
| + "ALTER TABLE precache_urls ADD COLUMN referrer_host_id INTEGER"))
|
| + return false;
|
| + }
|
| + return true;
|
| }
|
|
|
| } // namespace precache
|
|
|