Chromium Code Reviews| Index: content/browser/appcache/appcache_database.cc |
| diff --git a/content/browser/appcache/appcache_database.cc b/content/browser/appcache/appcache_database.cc |
| index 985173be166b49be5017b5ad8e6478488b5307bf..f926888ca3f1ff4a8f68daec6ec013e69e5b80fe 100644 |
| --- a/content/browser/appcache/appcache_database.cc |
| +++ b/content/browser/appcache/appcache_database.cc |
| @@ -23,13 +23,8 @@ namespace content { |
| // Schema ------------------------------------------------------------------- |
| namespace { |
| -#if defined(APPCACHE_USE_SIMPLE_CACHE) |
| -const int kCurrentVersion = 6; |
| -const int kCompatibleVersion = 6; |
| -#else |
| -const int kCurrentVersion = 5; |
| -const int kCompatibleVersion = 5; |
| -#endif |
| +const int kCurrentVersion = 7; |
| +const int kCompatibleVersion = 7; |
| // A mechanism to run experiments that may affect in data being persisted |
| // in different ways such that when the experiment is toggled on/off via |
| @@ -63,7 +58,9 @@ const TableInfo kTables[] = { |
| " origin TEXT," |
| " manifest_url TEXT," |
| " creation_time INTEGER," |
| - " last_access_time INTEGER)" }, |
| + " last_access_time INTEGER," |
| + " last_full_update_check_time INTEGER," |
| + " first_evictable_error_time INTEGER)" }, |
| { kCachesTable, |
| "(cache_id INTEGER PRIMARY KEY," |
| @@ -249,7 +246,7 @@ bool AppCacheDatabase::FindOriginsWithGroups(std::set<GURL>* origins) { |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT DISTINCT(origin) FROM Groups"; |
| sql::Statement statement(db_->GetUniqueStatement(kSql)); |
| @@ -311,9 +308,11 @@ bool AppCacheDatabase::FindGroup(int64 group_id, GroupRecord* record) { |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT group_id, origin, manifest_url," |
| - " creation_time, last_access_time" |
| + " creation_time, last_access_time," |
| + " last_full_update_check_time," |
| + " first_evictable_error_time" |
| " FROM Groups WHERE group_id = ?"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| @@ -333,9 +332,11 @@ bool AppCacheDatabase::FindGroupForManifestUrl( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT group_id, origin, manifest_url," |
| - " creation_time, last_access_time" |
| + " creation_time, last_access_time," |
| + " last_full_update_check_time," |
| + " first_evictable_error_time" |
| " FROM Groups WHERE manifest_url = ?"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| @@ -355,9 +356,11 @@ bool AppCacheDatabase::FindGroupsForOrigin( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT group_id, origin, manifest_url," |
| - " creation_time, last_access_time" |
| + " creation_time, last_access_time," |
| + " last_full_update_check_time," |
| + " first_evictable_error_time" |
| " FROM Groups WHERE origin = ?"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| @@ -377,9 +380,11 @@ bool AppCacheDatabase::FindGroupForCache(int64 cache_id, GroupRecord* record) { |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT g.group_id, g.origin, g.manifest_url," |
| - " g.creation_time, g.last_access_time" |
| + " g.creation_time, g.last_access_time," |
| + " g.last_full_update_check_time," |
| + " g.first_evictable_error_time" |
| " FROM Groups g, Caches c" |
| " WHERE c.cache_id = ? AND c.group_id = g.group_id"; |
| @@ -393,21 +398,44 @@ bool AppCacheDatabase::FindGroupForCache(int64 cache_id, GroupRecord* record) { |
| return true; |
| } |
| -bool AppCacheDatabase::UpdateLastAccessTime( |
| - int64 group_id, base::Time time) { |
| +bool AppCacheDatabase::InsertGroup(const GroupRecord* record) { |
| if (!LazyOpen(true)) |
|
jsbell
2015/07/07 00:18:59
Would be nice to have /* create_if_needed */ comme
|
| return false; |
| - const char* kSql = |
| - "UPDATE Groups SET last_access_time = ? WHERE group_id = ?"; |
| - |
| + const char kSql[] = |
| + "INSERT INTO Groups" |
| + " (group_id, origin, manifest_url, creation_time, last_access_time," |
| + " last_full_update_check_time, first_evictable_error_time)" |
| + " VALUES(?, ?, ?, ?, ?, ?, ?)"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| - statement.BindInt64(0, time.ToInternalValue()); |
| - statement.BindInt64(1, group_id); |
| + statement.BindInt64(0, record->group_id); |
| + statement.BindString(1, record->origin.spec()); |
| + statement.BindString(2, record->manifest_url.spec()); |
| + statement.BindInt64(3, record->creation_time.ToInternalValue()); |
| + statement.BindInt64(4, record->last_access_time.ToInternalValue()); |
| + statement.BindInt64(5, record->last_full_update_check_time.ToInternalValue()); |
| + statement.BindInt64(6, record->first_evictable_error_time.ToInternalValue()); |
| + return statement.Run(); |
| +} |
| + |
| +bool AppCacheDatabase::DeleteGroup(int64 group_id) { |
| + if (!LazyOpen(false)) |
| + return false; |
| + const char kSql[] = |
| + "DELETE FROM Groups WHERE group_id = ?"; |
| + sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| + statement.BindInt64(0, group_id); |
| return statement.Run(); |
| } |
| +bool AppCacheDatabase::UpdateLastAccessTime( |
| + int64 group_id, base::Time time) { |
| + if (!LazyUpdateLastAccessTime(group_id, time)) |
| + return false; |
| + return CommitLazyLastAccessTimes(); |
| +} |
| + |
| bool AppCacheDatabase::LazyUpdateLastAccessTime( |
| int64 group_id, base::Time time) { |
| if (!LazyOpen(true)) |
| @@ -425,42 +453,34 @@ bool AppCacheDatabase::CommitLazyLastAccessTimes() { |
| sql::Transaction transaction(db_.get()); |
| if (!transaction.Begin()) |
| return false; |
| - for (const auto& pair : lazy_last_access_times_) |
| - UpdateLastAccessTime(pair.first, pair.second); |
| + for (const auto& pair : lazy_last_access_times_) { |
| + const char kSql[] = |
| + "UPDATE Groups SET last_access_time = ? WHERE group_id = ?"; |
| + sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| + statement.BindInt64(0, pair.second.ToInternalValue()); // time |
| + statement.BindInt64(1, pair.first); // group_id |
| + statement.Run(); |
| + } |
| lazy_last_access_times_.clear(); |
| return transaction.Commit(); |
| } |
| -bool AppCacheDatabase::InsertGroup(const GroupRecord* record) { |
| +bool AppCacheDatabase::UpdateEvictionTimes( |
| + int64 group_id, |
| + base::Time last_full_update_check_time, |
| + base::Time first_evictable_error_time) { |
| if (!LazyOpen(true)) |
| return false; |
| - const char* kSql = |
| - "INSERT INTO Groups" |
| - " (group_id, origin, manifest_url, creation_time, last_access_time)" |
| - " VALUES(?, ?, ?, ?, ?)"; |
| - |
| + const char kSql[] = |
| + "UPDATE Groups" |
| + " SET last_full_update_check_time = ?, first_evictable_error_time = ?" |
| + " WHERE group_id = ?"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| - statement.BindInt64(0, record->group_id); |
| - statement.BindString(1, record->origin.spec()); |
| - statement.BindString(2, record->manifest_url.spec()); |
| - statement.BindInt64(3, record->creation_time.ToInternalValue()); |
| - statement.BindInt64(4, record->last_access_time.ToInternalValue()); |
| - |
| - return statement.Run(); |
| -} |
| - |
| -bool AppCacheDatabase::DeleteGroup(int64 group_id) { |
| - if (!LazyOpen(false)) |
| - return false; |
| - |
| - const char* kSql = |
| - "DELETE FROM Groups WHERE group_id = ?"; |
| - |
| - sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| - statement.BindInt64(0, group_id); |
| - |
| - return statement.Run(); |
| + statement.BindInt64(0, last_full_update_check_time.ToInternalValue()); |
| + statement.BindInt64(1, first_evictable_error_time.ToInternalValue()); |
| + statement.BindInt64(2, group_id); |
| + return statement.Run(); // Will succeed even if group_id is invalid. |
| } |
| bool AppCacheDatabase::FindCache(int64 cache_id, CacheRecord* record) { |
| @@ -468,7 +488,7 @@ bool AppCacheDatabase::FindCache(int64 cache_id, CacheRecord* record) { |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT cache_id, group_id, online_wildcard, update_time, cache_size" |
| " FROM Caches WHERE cache_id = ?"; |
| @@ -487,7 +507,7 @@ bool AppCacheDatabase::FindCacheForGroup(int64 group_id, CacheRecord* record) { |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT cache_id, group_id, online_wildcard, update_time, cache_size" |
| " FROM Caches WHERE group_id = ?"; |
| @@ -522,7 +542,7 @@ bool AppCacheDatabase::InsertCache(const CacheRecord* record) { |
| if (!LazyOpen(true)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "INSERT INTO Caches (cache_id, group_id, online_wildcard," |
| " update_time, cache_size)" |
| " VALUES(?, ?, ?, ?, ?)"; |
| @@ -541,7 +561,7 @@ bool AppCacheDatabase::DeleteCache(int64 cache_id) { |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "DELETE FROM Caches WHERE cache_id = ?"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| @@ -556,7 +576,7 @@ bool AppCacheDatabase::FindEntriesForCache( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT cache_id, url, flags, response_id, response_size FROM Entries" |
| " WHERE cache_id = ?"; |
| @@ -578,7 +598,7 @@ bool AppCacheDatabase::FindEntriesForUrl( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT cache_id, url, flags, response_id, response_size FROM Entries" |
| " WHERE url = ?"; |
| @@ -600,7 +620,7 @@ bool AppCacheDatabase::FindEntry( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT cache_id, url, flags, response_id, response_size FROM Entries" |
| " WHERE cache_id = ? AND url = ?"; |
| @@ -621,7 +641,7 @@ bool AppCacheDatabase::InsertEntry(const EntryRecord* record) { |
| if (!LazyOpen(true)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "INSERT INTO Entries (cache_id, url, flags, response_id, response_size)" |
| " VALUES(?, ?, ?, ?, ?)"; |
| @@ -655,7 +675,7 @@ bool AppCacheDatabase::DeleteEntriesForCache(int64 cache_id) { |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "DELETE FROM Entries WHERE cache_id = ?"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| @@ -669,7 +689,7 @@ bool AppCacheDatabase::AddEntryFlags( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "UPDATE Entries SET flags = flags | ? WHERE cache_id = ? AND url = ?"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| @@ -689,7 +709,7 @@ bool AppCacheDatabase::FindNamespacesForOrigin( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT cache_id, origin, type, namespace_url, target_url, is_pattern" |
| " FROM Namespaces WHERE origin = ?"; |
| @@ -710,7 +730,7 @@ bool AppCacheDatabase::FindNamespacesForCache( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT cache_id, origin, type, namespace_url, target_url, is_pattern" |
| " FROM Namespaces WHERE cache_id = ?"; |
| @@ -727,7 +747,7 @@ bool AppCacheDatabase::InsertNamespace( |
| if (!LazyOpen(true)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "INSERT INTO Namespaces" |
| " (cache_id, origin, type, namespace_url, target_url, is_pattern)" |
| " VALUES (?, ?, ?, ?, ?, ?)"; |
| @@ -771,7 +791,7 @@ bool AppCacheDatabase::DeleteNamespacesForCache(int64 cache_id) { |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "DELETE FROM Namespaces WHERE cache_id = ?"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| @@ -786,7 +806,7 @@ bool AppCacheDatabase::FindOnlineWhiteListForCache( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT cache_id, namespace_url, is_pattern FROM OnlineWhiteLists" |
| " WHERE cache_id = ?"; |
| @@ -806,7 +826,7 @@ bool AppCacheDatabase::InsertOnlineWhiteList( |
| if (!LazyOpen(true)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "INSERT INTO OnlineWhiteLists (cache_id, namespace_url, is_pattern)" |
| " VALUES (?, ?, ?)"; |
| @@ -838,7 +858,7 @@ bool AppCacheDatabase::DeleteOnlineWhiteListForCache(int64 cache_id) { |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "DELETE FROM OnlineWhiteLists WHERE cache_id = ?"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| @@ -852,7 +872,7 @@ bool AppCacheDatabase::GetDeletableResponseIds( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT response_id FROM DeletableResponseIds " |
| " WHERE rowid <= ?" |
| " LIMIT ?"; |
| @@ -868,14 +888,14 @@ bool AppCacheDatabase::GetDeletableResponseIds( |
| bool AppCacheDatabase::InsertDeletableResponseIds( |
| const std::vector<int64>& response_ids) { |
| - const char* kSql = |
| + const char kSql[] = |
| "INSERT INTO DeletableResponseIds (response_id) VALUES (?)"; |
| return RunCachedStatementWithIds(SQL_FROM_HERE, kSql, response_ids); |
| } |
| bool AppCacheDatabase::DeleteDeletableResponseIds( |
| const std::vector<int64>& response_ids) { |
| - const char* kSql = |
| + const char kSql[] = |
| "DELETE FROM DeletableResponseIds WHERE response_id = ?"; |
| return RunCachedStatementWithIds(SQL_FROM_HERE, kSql, response_ids); |
| } |
| @@ -924,7 +944,7 @@ bool AppCacheDatabase::FindResponseIdsForCacheHelper( |
| if (!LazyOpen(false)) |
| return false; |
| - const char* kSql = |
| + const char kSql[] = |
| "SELECT response_id FROM Entries WHERE cache_id = ?"; |
| sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| @@ -956,6 +976,11 @@ void AppCacheDatabase::ReadGroupRecord( |
| record->last_access_time = |
| base::Time::FromInternalValue(statement.ColumnInt64(4)); |
| } |
| + |
| + record->last_full_update_check_time = |
| + base::Time::FromInternalValue(statement.ColumnInt64(5)); |
| + record->first_evictable_error_time = |
| + base::Time::FromInternalValue(statement.ColumnInt64(6)); |
| } |
| void AppCacheDatabase::ReadCacheRecord( |
| @@ -1134,8 +1159,9 @@ bool AppCacheDatabase::CreateSchema() { |
| bool AppCacheDatabase::UpgradeSchema() { |
| #if defined(APPCACHE_USE_SIMPLE_CACHE) |
| - return DeleteExistingAndCreateNewDatabase(); |
| -#else |
| + if (meta_table_->GetVersionNumber() < 6) |
| + return DeleteExistingAndCreateNewDatabase(); |
| +#endif |
| if (meta_table_->GetVersionNumber() == 3) { |
| // version 3 was pre 12/17/2011 |
| DCHECK_EQ(strcmp(kNamespacesTable, kTables[3].table_name), 0); |
| @@ -1207,13 +1233,43 @@ bool AppCacheDatabase::UpgradeSchema() { |
| } |
| meta_table_->SetVersionNumber(5); |
| meta_table_->SetCompatibleVersionNumber(5); |
| + if (!transaction.Commit()) |
| + return false; |
| + } |
| + |
| +#if defined(APPCACHE_USE_SIMPLE_CACHE) |
| + // The schema version number was increased to 6 when we switched to the |
| + // SimpleCache for Android, but the SQL part of the schema is identical |
| + // to v5 on desktop chrome. |
| + if (meta_table_->GetVersionNumber() == 6) { |
| +#else |
| + if (meta_table_->GetVersionNumber() == 5) { |
| +#endif |
| + // Versions 5 and 6 were pre-May 2015. |
|
jsbell
2015/07/07 00:18:59
Bump to July ?
|
| + // Version 7 adds support for expiring caches that are failing to update. |
| + sql::Transaction transaction(db_.get()); |
| + if (!transaction.Begin() || |
| + !db_->Execute( |
| + "ALTER TABLE Groups ADD COLUMN" |
| + " last_full_update_check_time INTEGER") || |
| + !db_->Execute( |
| + "ALTER TABLE Groups ADD COLUMN" |
| + " first_evictable_error_time INTEGER") || |
| + !db_->Execute( |
| + "UPDATE Groups" |
| + " SET last_full_update_check_time =" |
| + " (SELECT update_time FROM Caches" |
| + " WHERE Caches.group_id = Groups.group_id)")) { |
| + return false; |
| + } |
| + meta_table_->SetVersionNumber(7); |
| + meta_table_->SetCompatibleVersionNumber(7); |
| return transaction.Commit(); |
| } |
| // If there is no upgrade path for the version on disk to the current |
| // version, nuke everything and start over. |
| return DeleteExistingAndCreateNewDatabase(); |
| -#endif |
| } |
| void AppCacheDatabase::ResetConnectionAndTables() { |