Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / webkit / browser / appcache / appcache_database.h
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.
4
5 #ifndef WEBKIT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_
6 #define WEBKIT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_
7
8 #include <map>
9 #include <set>
10 #include <vector>
11
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 "url/gurl.h"
18 #include "webkit/browser/webkit_storage_browser_export.h"
19 #include "webkit/common/appcache/appcache_interfaces.h"
20
21 namespace sql {
22 class Connection;
23 class MetaTable;
24 class Statement;
25 class StatementID;
26 }
27
28 namespace appcache {
29
30 class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheDatabase {
31  public:
32   struct WEBKIT_STORAGE_BROWSER_EXPORT GroupRecord {
33     GroupRecord();
34     ~GroupRecord();
35
36     int64 group_id;
37     GURL origin;
38     GURL manifest_url;
39     base::Time creation_time;
40     base::Time last_access_time;
41   };
42
43   struct WEBKIT_STORAGE_BROWSER_EXPORT CacheRecord {
44     CacheRecord()
45         : cache_id(0), group_id(0), online_wildcard(false), cache_size(0) {}
46
47     int64 cache_id;
48     int64 group_id;
49     bool online_wildcard;
50     base::Time update_time;
51     int64 cache_size;  // the sum of all response sizes in this cache
52   };
53
54   struct EntryRecord {
55     EntryRecord() : cache_id(0), flags(0), response_id(0), response_size(0) {}
56
57     int64 cache_id;
58     GURL url;
59     int flags;
60     int64 response_id;
61     int64 response_size;
62   };
63
64   struct WEBKIT_STORAGE_BROWSER_EXPORT NamespaceRecord {
65     NamespaceRecord();
66     ~NamespaceRecord();
67
68     int64 cache_id;
69     GURL origin;
70     Namespace namespace_;
71   };
72
73   typedef std::vector<NamespaceRecord> NamespaceRecordVector;
74
75   struct OnlineWhiteListRecord {
76     OnlineWhiteListRecord() : cache_id(0), is_pattern(false) {}
77
78     int64 cache_id;
79     GURL namespace_url;
80     bool is_pattern;
81   };
82
83   explicit AppCacheDatabase(const base::FilePath& path);
84   ~AppCacheDatabase();
85
86   void CloseConnection();
87   void Disable();
88   bool is_disabled() const { return is_disabled_; }
89   bool was_corruption_detected() const { return was_corruption_detected_; }
90
91   int64 GetOriginUsage(const GURL& origin);
92   bool GetAllOriginUsage(std::map<GURL, int64>* usage_map);
93
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);
98
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);
108
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);
115
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);
130   }
131   bool FindResponseIdsForCacheAsSet(
132       int64 cache_id, std::set<int64>* response_ids) {
133     return FindResponseIdsForCacheHelper(cache_id, NULL, response_ids);
134   }
135
136   bool FindNamespacesForOrigin(
137       const GURL& origin,
138       NamespaceRecordVector* intercepts,
139       NamespaceRecordVector* fallbacks);
140   bool FindNamespacesForCache(
141       int64 cache_id,
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);
148
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);
155
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);
160
161   // So our callers can wrap operations in transactions.
162   sql::Connection* db_connection() {
163     LazyOpen(true);
164     return db_.get();
165   }
166
167  private:
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);
172
173   bool FindResponseIdsForCacheHelper(
174       int64 cache_id, std::vector<int64>* ids_vector,
175       std::set<int64>* ids_set);
176
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);
189
190   // Database creation
191   bool LazyOpen(bool create_if_needed);
192   bool EnsureDatabaseVersion();
193   bool CreateSchema();
194   bool UpgradeSchema();
195
196   void ResetConnectionAndTables();
197
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();
202
203   void OnDatabaseError(int err, sql::Statement* stmt);
204
205   base::FilePath db_file_path_;
206   scoped_ptr<sql::Connection> db_;
207   scoped_ptr<sql::MetaTable> meta_table_;
208   bool is_disabled_;
209   bool is_recreating_;
210   bool was_corruption_detected_;
211
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);
227
228   DISALLOW_COPY_AND_ASSIGN(AppCacheDatabase);
229 };
230
231 }  // namespace appcache
232
233 #endif  // WEBKIT_BROWSER_APPCACHE_APPCACHE_DATABASE_H_