1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_ 6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_ 7 8 #include <map> 9 #include <set> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/files/file_path.h" 14 #include "base/gtest_prod_util.h" 15 #include "base/memory/scoped_ptr.h" 16 #include "base/time/time.h" 17 #include "content/common/appcache_interfaces.h" 18 #include "content/common/content_export.h" 19 #include "url/gurl.h" 20 21 namespace sql { 22 class Connection; 23 class MetaTable; 24 class Statement; 25 class StatementID; 26 } 27 28 namespace content { 29 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, CacheRecords); 30 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, EntryRecords); 31 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, QuickIntegrityCheck); 32 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, NamespaceRecords); 33 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, GroupRecords); 34 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, LazyOpen); 35 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, ExperimentalFlags); 36 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, OnlineWhiteListRecords); 37 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, ReCreate); 38 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, DeletableResponseIds); 39 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, OriginUsage); 40 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, UpgradeSchema3to5); 41 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, UpgradeSchema4to5); 42 FORWARD_DECLARE_TEST(AppCacheDatabaseTest, WasCorrutionDetected); 43 class AppCacheDatabaseTest; 44 class AppCacheStorageImplTest; 45 46 class CONTENT_EXPORT AppCacheDatabase { 47 public: 48 struct CONTENT_EXPORT GroupRecord { 49 GroupRecord(); 50 ~GroupRecord(); 51 52 int64 group_id; 53 GURL origin; 54 GURL manifest_url; 55 base::Time creation_time; 56 base::Time last_access_time; 57 }; 58 59 struct CONTENT_EXPORT CacheRecord { 60 CacheRecord() 61 : cache_id(0), group_id(0), online_wildcard(false), cache_size(0) {} 62 63 int64 cache_id; 64 int64 group_id; 65 bool online_wildcard; 66 base::Time update_time; 67 int64 cache_size; // the sum of all response sizes in this cache 68 }; 69 70 struct EntryRecord { 71 EntryRecord() : cache_id(0), flags(0), response_id(0), response_size(0) {} 72 73 int64 cache_id; 74 GURL url; 75 int flags; 76 int64 response_id; 77 int64 response_size; 78 }; 79 80 struct CONTENT_EXPORT NamespaceRecord { 81 NamespaceRecord(); 82 ~NamespaceRecord(); 83 84 int64 cache_id; 85 GURL origin; 86 AppCacheNamespace namespace_; 87 }; 88 89 typedef std::vector<NamespaceRecord> NamespaceRecordVector; 90 91 struct OnlineWhiteListRecord { 92 OnlineWhiteListRecord() : cache_id(0), is_pattern(false) {} 93 94 int64 cache_id; 95 GURL namespace_url; 96 bool is_pattern; 97 }; 98 99 explicit AppCacheDatabase(const base::FilePath& path); 100 ~AppCacheDatabase(); 101 102 void Disable(); 103 bool is_disabled() const { return is_disabled_; } 104 bool was_corruption_detected() const { return was_corruption_detected_; } 105 106 int64 GetOriginUsage(const GURL& origin); 107 bool GetAllOriginUsage(std::map<GURL, int64>* usage_map); 108 109 bool FindOriginsWithGroups(std::set<GURL>* origins); 110 bool FindLastStorageIds( 111 int64* last_group_id, int64* last_cache_id, int64* last_response_id, 112 int64* last_deletable_response_rowid); 113 114 bool FindGroup(int64 group_id, GroupRecord* record); 115 bool FindGroupForManifestUrl(const GURL& manifest_url, GroupRecord* record); 116 bool FindGroupsForOrigin( 117 const GURL& origin, std::vector<GroupRecord>* records); 118 bool FindGroupForCache(int64 cache_id, GroupRecord* record); 119 bool UpdateGroupLastAccessTime( 120 int64 group_id, base::Time last_access_time); 121 bool InsertGroup(const GroupRecord* record); 122 bool DeleteGroup(int64 group_id); 123 124 bool FindCache(int64 cache_id, CacheRecord* record); 125 bool FindCacheForGroup(int64 group_id, CacheRecord* record); 126 bool FindCachesForOrigin( 127 const GURL& origin, std::vector<CacheRecord>* records); 128 bool InsertCache(const CacheRecord* record); 129 bool DeleteCache(int64 cache_id); 130 131 bool FindEntriesForCache( 132 int64 cache_id, std::vector<EntryRecord>* records); 133 bool FindEntriesForUrl( 134 const GURL& url, std::vector<EntryRecord>* records); 135 bool FindEntry(int64 cache_id, const GURL& url, EntryRecord* record); 136 bool InsertEntry(const EntryRecord* record); 137 bool InsertEntryRecords( 138 const std::vector<EntryRecord>& records); 139 bool DeleteEntriesForCache(int64 cache_id); 140 bool AddEntryFlags(const GURL& entry_url, int64 cache_id, 141 int additional_flags); 142 bool FindResponseIdsForCacheAsVector( 143 int64 cache_id, std::vector<int64>* response_ids) { 144 return FindResponseIdsForCacheHelper(cache_id, response_ids, NULL); 145 } 146 bool FindResponseIdsForCacheAsSet( 147 int64 cache_id, std::set<int64>* response_ids) { 148 return FindResponseIdsForCacheHelper(cache_id, NULL, response_ids); 149 } 150 151 bool FindNamespacesForOrigin( 152 const GURL& origin, 153 NamespaceRecordVector* intercepts, 154 NamespaceRecordVector* fallbacks); 155 bool FindNamespacesForCache( 156 int64 cache_id, 157 NamespaceRecordVector* intercepts, 158 std::vector<NamespaceRecord>* fallbacks); 159 bool InsertNamespaceRecords( 160 const NamespaceRecordVector& records); 161 bool InsertNamespace(const NamespaceRecord* record); 162 bool DeleteNamespacesForCache(int64 cache_id); 163 164 bool FindOnlineWhiteListForCache( 165 int64 cache_id, std::vector<OnlineWhiteListRecord>* records); 166 bool InsertOnlineWhiteList(const OnlineWhiteListRecord* record); 167 bool InsertOnlineWhiteListRecords( 168 const std::vector<OnlineWhiteListRecord>& records); 169 bool DeleteOnlineWhiteListForCache(int64 cache_id); 170 171 bool GetDeletableResponseIds(std::vector<int64>* response_ids, 172 int64 max_rowid, int limit); 173 bool InsertDeletableResponseIds(const std::vector<int64>& response_ids); 174 bool DeleteDeletableResponseIds(const std::vector<int64>& response_ids); 175 176 // So our callers can wrap operations in transactions. 177 sql::Connection* db_connection() { 178 LazyOpen(true); 179 return db_.get(); 180 } 181 182 private: 183 bool RunCachedStatementWithIds( 184 const sql::StatementID& statement_id, const char* sql, 185 const std::vector<int64>& ids); 186 bool RunUniqueStatementWithInt64Result(const char* sql, int64* result); 187 188 bool FindResponseIdsForCacheHelper( 189 int64 cache_id, std::vector<int64>* ids_vector, 190 std::set<int64>* ids_set); 191 192 // Record retrieval helpers 193 void ReadGroupRecord(const sql::Statement& statement, GroupRecord* record); 194 void ReadCacheRecord(const sql::Statement& statement, CacheRecord* record); 195 void ReadEntryRecord(const sql::Statement& statement, EntryRecord* record); 196 void ReadNamespaceRecords( 197 sql::Statement* statement, 198 NamespaceRecordVector* intercepts, 199 NamespaceRecordVector* fallbacks); 200 void ReadNamespaceRecord( 201 const sql::Statement* statement, NamespaceRecord* record); 202 void ReadOnlineWhiteListRecord( 203 const sql::Statement& statement, OnlineWhiteListRecord* record); 204 205 // Database creation 206 bool LazyOpen(bool create_if_needed); 207 bool EnsureDatabaseVersion(); 208 bool CreateSchema(); 209 bool UpgradeSchema(); 210 211 void ResetConnectionAndTables(); 212 213 // Deletes the existing database file and the entire directory containing 214 // the database file including the disk cache in which response headers 215 // and bodies are stored, and then creates a new database file. 216 bool DeleteExistingAndCreateNewDatabase(); 217 218 void OnDatabaseError(int err, sql::Statement* stmt); 219 220 base::FilePath db_file_path_; 221 scoped_ptr<sql::Connection> db_; 222 scoped_ptr<sql::MetaTable> meta_table_; 223 bool is_disabled_; 224 bool is_recreating_; 225 bool was_corruption_detected_; 226 227 friend class content::AppCacheDatabaseTest; 228 friend class content::AppCacheStorageImplTest; 229 230 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, CacheRecords); 231 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, EntryRecords); 232 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, QuickIntegrityCheck); 233 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, NamespaceRecords); 234 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, GroupRecords); 235 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, LazyOpen); 236 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, ExperimentalFlags); 237 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, 238 OnlineWhiteListRecords); 239 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, ReCreate); 240 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, DeletableResponseIds); 241 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, OriginUsage); 242 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, UpgradeSchema3to5); 243 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, UpgradeSchema4to5); 244 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest, WasCorrutionDetected); 245 246 DISALLOW_COPY_AND_ASSIGN(AppCacheDatabase); 247 }; 248 249 } // namespace content 250 251 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_ 252