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_
; }
90 int64
GetOriginUsage(const GURL
& origin
);
91 bool GetAllOriginUsage(std::map
<GURL
, int64
>* usage_map
);
93 bool FindOriginsWithGroups(std::set
<GURL
>* origins
);
94 bool FindLastStorageIds(
95 int64
* last_group_id
, int64
* last_cache_id
, int64
* last_response_id
,
96 int64
* last_deletable_response_rowid
);
98 bool FindGroup(int64 group_id
, GroupRecord
* record
);
99 bool FindGroupForManifestUrl(const GURL
& manifest_url
, GroupRecord
* record
);
100 bool FindGroupsForOrigin(
101 const GURL
& origin
, std::vector
<GroupRecord
>* records
);
102 bool FindGroupForCache(int64 cache_id
, GroupRecord
* record
);
103 bool UpdateGroupLastAccessTime(
104 int64 group_id
, base::Time last_access_time
);
105 bool InsertGroup(const GroupRecord
* record
);
106 bool DeleteGroup(int64 group_id
);
108 bool FindCache(int64 cache_id
, CacheRecord
* record
);
109 bool FindCacheForGroup(int64 group_id
, CacheRecord
* record
);
110 bool FindCachesForOrigin(
111 const GURL
& origin
, std::vector
<CacheRecord
>* records
);
112 bool InsertCache(const CacheRecord
* record
);
113 bool DeleteCache(int64 cache_id
);
115 bool FindEntriesForCache(
116 int64 cache_id
, std::vector
<EntryRecord
>* records
);
117 bool FindEntriesForUrl(
118 const GURL
& url
, std::vector
<EntryRecord
>* records
);
119 bool FindEntry(int64 cache_id
, const GURL
& url
, EntryRecord
* record
);
120 bool InsertEntry(const EntryRecord
* record
);
121 bool InsertEntryRecords(
122 const std::vector
<EntryRecord
>& records
);
123 bool DeleteEntriesForCache(int64 cache_id
);
124 bool AddEntryFlags(const GURL
& entry_url
, int64 cache_id
,
125 int additional_flags
);
126 bool FindResponseIdsForCacheAsVector(
127 int64 cache_id
, std::vector
<int64
>* response_ids
) {
128 return FindResponseIdsForCacheHelper(cache_id
, response_ids
, NULL
);
130 bool FindResponseIdsForCacheAsSet(
131 int64 cache_id
, std::set
<int64
>* response_ids
) {
132 return FindResponseIdsForCacheHelper(cache_id
, NULL
, response_ids
);
135 bool FindNamespacesForOrigin(
137 NamespaceRecordVector
* intercepts
,
138 NamespaceRecordVector
* fallbacks
);
139 bool FindNamespacesForCache(
141 NamespaceRecordVector
* intercepts
,
142 std::vector
<NamespaceRecord
>* fallbacks
);
143 bool InsertNamespaceRecords(
144 const NamespaceRecordVector
& records
);
145 bool InsertNamespace(const NamespaceRecord
* record
);
146 bool DeleteNamespacesForCache(int64 cache_id
);
148 bool FindOnlineWhiteListForCache(
149 int64 cache_id
, std::vector
<OnlineWhiteListRecord
>* records
);
150 bool InsertOnlineWhiteList(const OnlineWhiteListRecord
* record
);
151 bool InsertOnlineWhiteListRecords(
152 const std::vector
<OnlineWhiteListRecord
>& records
);
153 bool DeleteOnlineWhiteListForCache(int64 cache_id
);
155 bool GetDeletableResponseIds(std::vector
<int64
>* response_ids
,
156 int64 max_rowid
, int limit
);
157 bool InsertDeletableResponseIds(const std::vector
<int64
>& response_ids
);
158 bool DeleteDeletableResponseIds(const std::vector
<int64
>& response_ids
);
160 // So our callers can wrap operations in transactions.
161 sql::Connection
* db_connection() {
167 bool RunCachedStatementWithIds(
168 const sql::StatementID
& statement_id
, const char* sql
,
169 const std::vector
<int64
>& ids
);
170 bool RunUniqueStatementWithInt64Result(const char* sql
, int64
* result
);
172 bool FindResponseIdsForCacheHelper(
173 int64 cache_id
, std::vector
<int64
>* ids_vector
,
174 std::set
<int64
>* ids_set
);
176 // Record retrieval helpers
177 void ReadGroupRecord(const sql::Statement
& statement
, GroupRecord
* record
);
178 void ReadCacheRecord(const sql::Statement
& statement
, CacheRecord
* record
);
179 void ReadEntryRecord(const sql::Statement
& statement
, EntryRecord
* record
);
180 void ReadNamespaceRecords(
181 sql::Statement
* statement
,
182 NamespaceRecordVector
* intercepts
,
183 NamespaceRecordVector
* fallbacks
);
184 void ReadNamespaceRecord(
185 const sql::Statement
* statement
, NamespaceRecord
* record
);
186 void ReadOnlineWhiteListRecord(
187 const sql::Statement
& statement
, OnlineWhiteListRecord
* record
);
190 bool LazyOpen(bool create_if_needed
);
191 bool EnsureDatabaseVersion();
193 bool UpgradeSchema();
195 void ResetConnectionAndTables();
197 // Deletes the existing database file and the entire directory containing
198 // the database file including the disk cache in which response headers
199 // and bodies are stored, and then creates a new database file.
200 bool DeleteExistingAndCreateNewDatabase();
202 base::FilePath db_file_path_
;
203 scoped_ptr
<sql::Connection
> db_
;
204 scoped_ptr
<sql::MetaTable
> meta_table_
;
208 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, CacheRecords
);
209 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, EntryRecords
);
210 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, NamespaceRecords
);
211 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, GroupRecords
);
212 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, LazyOpen
);
213 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, ExperimentalFlags
);
214 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, OnlineWhiteListRecords
);
215 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, ReCreate
);
216 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, DeletableResponseIds
);
217 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, OriginUsage
);
218 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, UpgradeSchema3to5
);
219 FRIEND_TEST_ALL_PREFIXES(AppCacheDatabaseTest
, UpgradeSchema4to5
);
221 DISALLOW_COPY_AND_ASSIGN(AppCacheDatabase
);
224 } // namespace appcache
226 #endif // WEBKIT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_