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
, 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
;
46 class CONTENT_EXPORT AppCacheDatabase
{
48 struct CONTENT_EXPORT GroupRecord
{
55 base::Time creation_time
;
56 base::Time last_access_time
;
59 struct CONTENT_EXPORT CacheRecord
{
61 : cache_id(0), group_id(0), online_wildcard(false), cache_size(0) {}
66 base::Time update_time
;
67 int64 cache_size
; // the sum of all response sizes in this cache
71 EntryRecord() : cache_id(0), flags(0), response_id(0), response_size(0) {}
80 struct CONTENT_EXPORT NamespaceRecord
{
86 AppCacheNamespace namespace_
;
89 typedef std::vector
<NamespaceRecord
> NamespaceRecordVector
;
91 struct OnlineWhiteListRecord
{
92 OnlineWhiteListRecord() : cache_id(0), is_pattern(false) {}
99 explicit AppCacheDatabase(const base::FilePath
& path
);
103 bool is_disabled() const { return is_disabled_
; }
104 bool was_corruption_detected() const { return was_corruption_detected_
; }
106 int64
GetOriginUsage(const GURL
& origin
);
107 bool GetAllOriginUsage(std::map
<GURL
, int64
>* usage_map
);
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
);
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 UpdateLastAccessTime(
120 int64 group_id
, base::Time last_access_time
);
121 bool LazyUpdateLastAccessTime(
122 int64 group_id
, base::Time last_access_time
);
123 bool CommitLazyLastAccessTimes(); // The destructor calls this too.
124 bool InsertGroup(const GroupRecord
* record
);
125 bool DeleteGroup(int64 group_id
);
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
);
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
);
149 bool FindResponseIdsForCacheAsSet(
150 int64 cache_id
, std::set
<int64
>* response_ids
) {
151 return FindResponseIdsForCacheHelper(cache_id
, NULL
, response_ids
);
154 bool FindNamespacesForOrigin(
156 NamespaceRecordVector
* intercepts
,
157 NamespaceRecordVector
* fallbacks
);
158 bool FindNamespacesForCache(
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
);
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
);
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
);
179 // So our callers can wrap operations in transactions.
180 sql::Connection
* db_connection() {
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
);
191 bool FindResponseIdsForCacheHelper(
192 int64 cache_id
, std::vector
<int64
>* ids_vector
,
193 std::set
<int64
>* ids_set
);
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
);
209 bool LazyOpen(bool create_if_needed
);
210 bool EnsureDatabaseVersion();
212 bool UpgradeSchema();
214 void ResetConnectionAndTables();
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();
221 void OnDatabaseError(int err
, sql::Statement
* stmt
);
223 base::FilePath db_file_path_
;
224 scoped_ptr
<sql::Connection
> db_
;
225 scoped_ptr
<sql::MetaTable
> meta_table_
;
226 std::map
<int64
, base::Time
> lazy_last_access_times_
;
229 bool was_corruption_detected_
;
231 friend class content::AppCacheDatabaseTest
;
232 friend class content::AppCacheStorageImplTest
;
234 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, CacheRecords
);
235 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, EntryRecords
);
236 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, QuickIntegrityCheck
);
237 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, NamespaceRecords
);
238 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, GroupRecords
);
239 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, LazyOpen
);
240 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, ExperimentalFlags
);
241 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
,
242 OnlineWhiteListRecords
);
243 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, ReCreate
);
244 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, DeletableResponseIds
);
245 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, OriginUsage
);
246 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, UpgradeSchema3to5
);
247 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, UpgradeSchema4to5
);
248 FRIEND_TEST_ALL_PREFIXES(content::AppCacheDatabaseTest
, WasCorrutionDetected
);
250 DISALLOW_COPY_AND_ASSIGN(AppCacheDatabase
);
253 } // namespace content
255 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_