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 CONTENT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_
6 #define CONTENT_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"
17 #include "content/common/appcache_interfaces.h"
18 #include "content/common/content_export.h"
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
, GroupAccessAndEvictionTimes
);
35 FORWARD_DECLARE_TEST(AppCacheDatabaseTest
, LazyOpen
);
36 FORWARD_DECLARE_TEST(AppCacheDatabaseTest
, ExperimentalFlags
);
37 FORWARD_DECLARE_TEST(AppCacheDatabaseTest
, OnlineWhiteListRecords
);
38 FORWARD_DECLARE_TEST(AppCacheDatabaseTest
, ReCreate
);
39 FORWARD_DECLARE_TEST(AppCacheDatabaseTest
, DeletableResponseIds
);
40 FORWARD_DECLARE_TEST(AppCacheDatabaseTest
, OriginUsage
);
41 FORWARD_DECLARE_TEST(AppCacheDatabaseTest
, UpgradeSchema3to7
);
42 FORWARD_DECLARE_TEST(AppCacheDatabaseTest
, UpgradeSchema4to7
);
43 FORWARD_DECLARE_TEST(AppCacheDatabaseTest
, UpgradeSchema5or6to7
);
44 FORWARD_DECLARE_TEST(AppCacheDatabaseTest
, WasCorrutionDetected
);
45 class AppCacheDatabaseTest
;
46 class AppCacheStorageImplTest
;
48 class CONTENT_EXPORT AppCacheDatabase
{
50 struct CONTENT_EXPORT GroupRecord
{
57 base::Time creation_time
;
58 base::Time last_access_time
;
59 base::Time last_full_update_check_time
;
60 base::Time first_evictable_error_time
;
63 struct CONTENT_EXPORT CacheRecord
{
65 : cache_id(0), group_id(0), online_wildcard(false), cache_size(0) {}
70 base::Time update_time
;
71 int64 cache_size
; // the sum of all response sizes in this cache
75 EntryRecord() : cache_id(0), flags(0), response_id(0), response_size(0) {}
84 struct CONTENT_EXPORT NamespaceRecord
{
90 AppCacheNamespace namespace_
;
93 typedef std::vector
<NamespaceRecord
> NamespaceRecordVector
;
95 struct OnlineWhiteListRecord
{
96 OnlineWhiteListRecord() : cache_id(0), is_pattern(false) {}
103 explicit AppCacheDatabase(const base::FilePath
& path
);
107 bool is_disabled() const { return is_disabled_
; }
108 bool was_corruption_detected() const { return was_corruption_detected_
; }
110 int64
GetOriginUsage(const GURL
& origin
);
111 bool GetAllOriginUsage(std::map
<GURL
, int64
>* usage_map
);
113 bool FindOriginsWithGroups(std::set
<GURL
>* origins
);
114 bool FindLastStorageIds(
115 int64
* last_group_id
, int64
* last_cache_id
, int64
* last_response_id
,
116 int64
* last_deletable_response_rowid
);
118 bool FindGroup(int64 group_id
, GroupRecord
* record
);
119 bool FindGroupForManifestUrl(const GURL
& manifest_url
, GroupRecord
* record
);
120 bool FindGroupsForOrigin(
121 const GURL
& origin
, std::vector
<GroupRecord
>* records
);
122 bool FindGroupForCache(int64 cache_id
, GroupRecord
* record
);
123 bool InsertGroup(const GroupRecord
* record
);
124 bool DeleteGroup(int64 group_id
);
126 // The access and eviction time update methods do not fail when
127 // given invalid group_ids. The return value only indicates whether
128 // the database is functioning.
129 bool UpdateLastAccessTime(int64 group_id
,
130 base::Time last_access_time
);
131 bool LazyUpdateLastAccessTime(int64 group_id
,
132 base::Time last_access_time
);
133 bool UpdateEvictionTimes(int64 group_id
,
134 base::Time last_full_update_check_time
,
135 base::Time first_evictable_error_time
);
136 bool CommitLazyLastAccessTimes(); // The destructor calls this too.
138 bool FindCache(int64 cache_id
, CacheRecord
* record
);
139 bool FindCacheForGroup(int64 group_id
, CacheRecord
* record
);
140 bool FindCachesForOrigin(
141 const GURL
& origin
, std::vector
<CacheRecord
>* records
);
142 bool InsertCache(const CacheRecord
* record
);
143 bool DeleteCache(int64 cache_id
);
145 bool FindEntriesForCache(
146 int64 cache_id
, std::vector
<EntryRecord
>* records
);
147 bool FindEntriesForUrl(
148 const GURL
& url
, std::vector
<EntryRecord
>* records
);
149 bool FindEntry(int64 cache_id
, const GURL
& url
, EntryRecord
* record
);
150 bool InsertEntry(const EntryRecord
* record
);
151 bool InsertEntryRecords(
152 const std::vector
<EntryRecord
>& records
);
153 bool DeleteEntriesForCache(int64 cache_id
);
154 bool AddEntryFlags(const GURL
& entry_url
, int64 cache_id
,
155 int additional_flags
);
156 bool FindResponseIdsForCacheAsVector(
157 int64 cache_id
, std::vector
<int64
>* response_ids
) {
158 return FindResponseIdsForCacheHelper(cache_id
, response_ids
, NULL
);
160 bool FindResponseIdsForCacheAsSet(
161 int64 cache_id
, std::set
<int64
>* response_ids
) {
162 return FindResponseIdsForCacheHelper(cache_id
, NULL
, response_ids
);
165 bool FindNamespacesForOrigin(
167 NamespaceRecordVector
* intercepts
,
168 NamespaceRecordVector
* fallbacks
);
169 bool FindNamespacesForCache(
171 NamespaceRecordVector
* intercepts
,
172 std::vector
<NamespaceRecord
>* fallbacks
);
173 bool InsertNamespaceRecords(
174 const NamespaceRecordVector
& records
);
175 bool InsertNamespace(const NamespaceRecord
* record
);
176 bool DeleteNamespacesForCache(int64 cache_id
);
178 bool FindOnlineWhiteListForCache(
179 int64 cache_id
, std::vector
<OnlineWhiteListRecord
>* records
);
180 bool InsertOnlineWhiteList(const OnlineWhiteListRecord
* record
);
181 bool InsertOnlineWhiteListRecords(
182 const std::vector
<OnlineWhiteListRecord
>& records
);
183 bool DeleteOnlineWhiteListForCache(int64 cache_id
);
185 bool GetDeletableResponseIds(std::vector
<int64
>* response_ids
,
186 int64 max_rowid
, int limit
);
187 bool InsertDeletableResponseIds(const std::vector
<int64
>& response_ids
);
188 bool DeleteDeletableResponseIds(const std::vector
<int64
>& response_ids
);
190 // So our callers can wrap operations in transactions.
191 sql::Connection
* db_connection() {
197 bool RunCachedStatementWithIds(
198 const sql::StatementID
& statement_id
, const char* sql
,
199 const std::vector
<int64
>& ids
);
200 bool RunUniqueStatementWithInt64Result(const char* sql
, int64
* result
);
202 bool FindResponseIdsForCacheHelper(
203 int64 cache_id
, std::vector
<int64
>* ids_vector
,
204 std::set
<int64
>* ids_set
);
206 // Record retrieval helpers
207 void ReadGroupRecord(const sql::Statement
& statement
, GroupRecord
* record
);
208 void ReadCacheRecord(const sql::Statement
& statement
, CacheRecord
* record
);
209 void ReadEntryRecord(const sql::Statement
& statement
, EntryRecord
* record
);
210 void ReadNamespaceRecords(
211 sql::Statement
* statement
,
212 NamespaceRecordVector
* intercepts
,
213 NamespaceRecordVector
* fallbacks
);
214 void ReadNamespaceRecord(
215 const sql::Statement
* statement
, NamespaceRecord
* record
);
216 void ReadOnlineWhiteListRecord(
217 const sql::Statement
& statement
, OnlineWhiteListRecord
* record
);
220 bool LazyOpen(bool create_if_needed
);
221 bool EnsureDatabaseVersion();
223 bool UpgradeSchema();
225 void ResetConnectionAndTables();
227 // Deletes the existing database file and the entire directory containing
228 // the database file including the disk cache in which response headers
229 // and bodies are stored, and then creates a new database file.
230 bool DeleteExistingAndCreateNewDatabase();
232 void OnDatabaseError(int err
, sql::Statement
* stmt
);
234 base::FilePath db_file_path_
;
235 scoped_ptr
<sql::Connection
> db_
;
236 scoped_ptr
<sql::MetaTable
> meta_table_
;
237 std::map
<int64
, base::Time
> lazy_last_access_times_
;
240 bool was_corruption_detected_
;
242 friend class content::AppCacheDatabaseTest
;
243 friend class content::AppCacheStorageImplTest
;
245 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, CacheRecords
);
246 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, EntryRecords
);
247 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, QuickIntegrityCheck
);
248 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, NamespaceRecords
);
249 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, GroupRecords
);
250 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
,
251 GroupAccessAndEvictionTimes
);
252 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, LazyOpen
);
253 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, ExperimentalFlags
);
254 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
,
255 OnlineWhiteListRecords
);
256 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, ReCreate
);
257 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, DeletableResponseIds
);
258 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, OriginUsage
);
259 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, UpgradeSchema3to7
);
260 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, UpgradeSchema4to7
);
261 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, UpgradeSchema5or6to7
);
262 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, WasCorrutionDetected
);
264 DISALLOW_COPY_AND_ASSIGN(AppCacheDatabase
);
267 } // namespace content
269 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_