Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / appcache / appcache_database.h
blobccfd891abbbd59983e6d5c2ce0d1464def5fce4a
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_
8 #include <map>
9 #include <set>
10 #include <vector>
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"
19 #include "url/gurl.h"
21 namespace sql {
22 class Connection;
23 class MetaTable;
24 class Statement;
25 class StatementID;
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;
46 class CONTENT_EXPORT AppCacheDatabase {
47 public:
48 struct CONTENT_EXPORT GroupRecord {
49 GroupRecord();
50 ~GroupRecord();
52 int64 group_id;
53 GURL origin;
54 GURL manifest_url;
55 base::Time creation_time;
56 base::Time last_access_time;
59 struct CONTENT_EXPORT CacheRecord {
60 CacheRecord()
61 : cache_id(0), group_id(0), online_wildcard(false), cache_size(0) {}
63 int64 cache_id;
64 int64 group_id;
65 bool online_wildcard;
66 base::Time update_time;
67 int64 cache_size; // the sum of all response sizes in this cache
70 struct EntryRecord {
71 EntryRecord() : cache_id(0), flags(0), response_id(0), response_size(0) {}
73 int64 cache_id;
74 GURL url;
75 int flags;
76 int64 response_id;
77 int64 response_size;
80 struct CONTENT_EXPORT NamespaceRecord {
81 NamespaceRecord();
82 ~NamespaceRecord();
84 int64 cache_id;
85 GURL origin;
86 AppCacheNamespace namespace_;
89 typedef std::vector<NamespaceRecord> NamespaceRecordVector;
91 struct OnlineWhiteListRecord {
92 OnlineWhiteListRecord() : cache_id(0), is_pattern(false) {}
94 int64 cache_id;
95 GURL namespace_url;
96 bool is_pattern;
99 explicit AppCacheDatabase(const base::FilePath& path);
100 ~AppCacheDatabase();
102 void Disable();
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(
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);
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() {
181 LazyOpen(true);
182 return db_.get();
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);
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);
208 // Database creation
209 bool LazyOpen(bool create_if_needed);
210 bool EnsureDatabaseVersion();
211 bool CreateSchema();
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_;
227 bool is_disabled_;
228 bool is_recreating_;
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_