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.
5 #ifndef WEBKIT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_
6 #define WEBKIT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_
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"
18 #include "webkit/browser/webkit_storage_browser_export.h"
19 #include "webkit/common/appcache/appcache_interfaces.h"
30 class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheDatabase {
32 struct WEBKIT_STORAGE_BROWSER_EXPORT GroupRecord {
39 base::Time creation_time;
40 base::Time last_access_time;
43 struct WEBKIT_STORAGE_BROWSER_EXPORT CacheRecord {
45 : cache_id(0), group_id(0), online_wildcard(false), cache_size(0) {}
50 base::Time update_time;
51 int64 cache_size; // the sum of all response sizes in this cache
55 EntryRecord() : cache_id(0), flags(0), response_id(0), response_size(0) {}
64 struct WEBKIT_STORAGE_BROWSER_EXPORT NamespaceRecord {
73 typedef std::vector<NamespaceRecord> NamespaceRecordVector;
75 struct OnlineWhiteListRecord {
76 OnlineWhiteListRecord() : cache_id(0), is_pattern(false) {}
83 explicit AppCacheDatabase(const base::FilePath& path);
86 void CloseConnection();
88 bool is_disabled() const { return is_disabled_; }
89 bool was_corruption_detected() const { return was_corruption_detected_; }
91 int64 GetOriginUsage(const GURL& origin);
92 bool GetAllOriginUsage(std::map<GURL, int64>* usage_map);
94 bool FindOriginsWithGroups(std::set<GURL>* origins);
95 bool FindLastStorageIds(
96 int64* last_group_id, int64* last_cache_id, int64* last_response_id,
97 int64* last_deletable_response_rowid);
99 bool FindGroup(int64 group_id, GroupRecord* record);
100 bool FindGroupForManifestUrl(const GURL& manifest_url, GroupRecord* record);
101 bool FindGroupsForOrigin(
102 const GURL& origin, std::vector<GroupRecord>* records);
103 bool FindGroupForCache(int64 cache_id, GroupRecord* record);
104 bool UpdateGroupLastAccessTime(
105 int64 group_id, base::Time last_access_time);
106 bool InsertGroup(const GroupRecord* record);
107 bool DeleteGroup(int64 group_id);
109 bool FindCache(int64 cache_id, CacheRecord* record);
110 bool FindCacheForGroup(int64 group_id, CacheRecord* record);
111 bool FindCachesForOrigin(
112 const GURL& origin, std::vector<CacheRecord>* records);
113 bool InsertCache(const CacheRecord* record);
114 bool DeleteCache(int64 cache_id);
116 bool FindEntriesForCache(
117 int64 cache_id, std::vector<EntryRecord>* records);
118 bool FindEntriesForUrl(
119 const GURL& url, std::vector<EntryRecord>* records);
120 bool FindEntry(int64 cache_id, const GURL& url, EntryRecord* record);
121 bool InsertEntry(const EntryRecord* record);
122 bool InsertEntryRecords(
123 const std::vector<EntryRecord>& records);
124 bool DeleteEntriesForCache(int64 cache_id);
125 bool AddEntryFlags(const GURL& entry_url, int64 cache_id,
126 int additional_flags);
127 bool FindResponseIdsForCacheAsVector(
128 int64 cache_id, std::vector<int64>* response_ids) {
129 return FindResponseIdsForCacheHelper(cache_id, response_ids, NULL);
131 bool FindResponseIdsForCacheAsSet(
132 int64 cache_id, std::set<int64>* response_ids) {
133 return FindResponseIdsForCacheHelper(cache_id, NULL, response_ids);
136 bool FindNamespacesForOrigin(
138 NamespaceRecordVector* intercepts,
139 NamespaceRecordVector* fallbacks);
140 bool FindNamespacesForCache(
142 NamespaceRecordVector* intercepts,
143 std::vector<NamespaceRecord>* fallbacks);
144 bool InsertNamespaceRecords(
145 const NamespaceRecordVector& records);
146 bool InsertNamespace(const NamespaceRecord* record);
147 bool DeleteNamespacesForCache(int64 cache_id);
149 bool FindOnlineWhiteListForCache(
150 int64 cache_id, std::vector<OnlineWhiteListRecord>* records);
151 bool InsertOnlineWhiteList(const OnlineWhiteListRecord* record);
152 bool InsertOnlineWhiteListRecords(
153 const std::vector<OnlineWhiteListRecord>& records);
154 bool DeleteOnlineWhiteListForCache(int64 cache_id);
156 bool GetDeletableResponseIds(std::vector<int64>* response_ids,
157 int64 max_rowid, int limit);
158 bool InsertDeletableResponseIds(const std::vector<int64>& response_ids);
159 bool DeleteDeletableResponseIds(const std::vector<int64>& response_ids);
161 // So our callers can wrap operations in transactions.
162 sql::Connection* db_connection() {
168 bool RunCachedStatementWithIds(
169 const sql::StatementID& statement_id, const char* sql,
170 const std::vector<int64>& ids);
171 bool RunUniqueStatementWithInt64Result(const char* sql, int64* result);
173 bool FindResponseIdsForCacheHelper(
174 int64 cache_id, std::vector<int64>* ids_vector,
175 std::set<int64>* ids_set);
177 // Record retrieval helpers
178 void ReadGroupRecord(const sql::Statement& statement, GroupRecord* record);
179 void ReadCacheRecord(const sql::Statement& statement, CacheRecord* record);
180 void ReadEntryRecord(const sql::Statement& statement, EntryRecord* record);
181 void ReadNamespaceRecords(
182 sql::Statement* statement,
183 NamespaceRecordVector* intercepts,
184 NamespaceRecordVector* fallbacks);
185 void ReadNamespaceRecord(
186 const sql::Statement* statement, NamespaceRecord* record);
187 void ReadOnlineWhiteListRecord(
188 const sql::Statement& statement, OnlineWhiteListRecord* record);
191 bool LazyOpen(bool create_if_needed);
192 bool EnsureDatabaseVersion();
194 bool UpgradeSchema();
196 void ResetConnectionAndTables();
198 // Deletes the existing database file and the entire directory containing
199 // the database file including the disk cache in which response headers
200 // and bodies are stored, and then creates a new database file.
201 bool DeleteExistingAndCreateNewDatabase();
203 void OnDatabaseError(int err, sql::Statement* stmt);
205 base::FilePath db_file_path_;
206 scoped_ptr<sql::Connection> db_;
207 scoped_ptr<sql::MetaTable> meta_table_;
210 bool was_corruption_detected_;
212 friend class AppCacheStorageImplTest;
213 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, CacheRecords);
214 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, EntryRecords);
215 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, QuickIntegrityCheck);
216 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, NamespaceRecords);
217 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, GroupRecords);
218 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, LazyOpen);
219 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, ExperimentalFlags);
220 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, OnlineWhiteListRecords);
221 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, ReCreate);
222 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, DeletableResponseIds);
223 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, OriginUsage);
224 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, UpgradeSchema3to5);
225 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, UpgradeSchema4to5);
226 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest, WasCorrutionDetected);
228 DISALLOW_COPY_AND_ASSIGN(AppCacheDatabase);
231 } // namespace appcache
233 #endif // WEBKIT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_