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