summaryrefslogtreecommitdiffstats
path: root/webkit/appcache/appcache_database.h
blob: 1c02463699f67aac90a2b524b75a237714fad530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef WEBKIT_APPCACHE_APPCACHE_DATABASE_H_
#define WEBKIT_APPCACHE_APPCACHE_DATABASE_H_

#include <set>
#include <vector>

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/scoped_ptr.h"
#include "base/time.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest_prod.h"

namespace sql {
class Connection;
class MetaTable;
class Statement;
class StatementID;
}

namespace webkit_database {
class QuotaTable;
}

namespace appcache {

class AppCacheDatabase {
 public:
  struct GroupRecord {
    int64 group_id;
    GURL origin;
    GURL manifest_url;
    base::Time creation_time;
    base::Time last_access_time;
    GroupRecord() : group_id(0) {}
  };

  struct CacheRecord {
    int64 cache_id;
    int64 group_id;
    bool online_wildcard;
    base::Time update_time;
    int64 cache_size;  // the sum of all response sizes in this cache
    CacheRecord()
        : cache_id(0), group_id(0), online_wildcard(false), cache_size(0) {}
  };

  struct EntryRecord {
    int64 cache_id;
    GURL url;
    int flags;
    int64 response_id;
    int64 response_size;
    EntryRecord() : cache_id(0), flags(0), response_id(0), response_size(0) {}
  };

  struct FallbackNameSpaceRecord {
    int64 cache_id;
    GURL origin;  // intentionally not normalized
    GURL namespace_url;
    GURL fallback_entry_url;
    FallbackNameSpaceRecord() : cache_id(0) {}
  };

  struct OnlineWhiteListRecord {
    int64 cache_id;
    GURL namespace_url;
    OnlineWhiteListRecord() : cache_id(0) {}
  };

  explicit AppCacheDatabase(const FilePath& path);
  ~AppCacheDatabase();

  void CloseConnection();
  void Disable();
  bool is_disabled() const { return is_disabled_; }

  int64 GetDefaultOriginQuota() { return 5 * 1024 * 1024; }
  int64 GetOriginUsage(const GURL& origin);
  int64 GetOriginQuota(const GURL& origin);

  bool FindOriginsWithGroups(std::set<GURL>* origins);
  bool FindLastStorageIds(
      int64* last_group_id, int64* last_cache_id, int64* last_response_id,
      int64* last_deletable_response_rowid);

  bool FindGroup(int64 group_id, GroupRecord* record);
  bool FindGroupForManifestUrl(const GURL& manifest_url, GroupRecord* record);
  bool FindGroupsForOrigin(
      const GURL& origin, std::vector<GroupRecord>* records);
  bool FindGroupForCache(int64 cache_id, GroupRecord* record);
  bool UpdateGroupLastAccessTime(
      int64 group_id, base::Time last_access_time);
  bool InsertGroup(const GroupRecord* record);
  bool DeleteGroup(int64 group_id);

  bool FindCache(int64 cache_id, CacheRecord* record);
  bool FindCacheForGroup(int64 group_id, CacheRecord* record);
  bool FindCachesForOrigin(
      const GURL& origin, std::vector<CacheRecord>* records);
  bool InsertCache(const CacheRecord* record);
  bool DeleteCache(int64 cache_id);

  bool FindEntriesForCache(
      int64 cache_id, std::vector<EntryRecord>* records);
  bool FindEntriesForUrl(
      const GURL& url, std::vector<EntryRecord>* records);
  bool FindEntry(int64 cache_id, const GURL& url, EntryRecord* record);
  bool InsertEntry(const EntryRecord* record);
  bool InsertEntryRecords(
      const std::vector<EntryRecord>& records);
  bool DeleteEntriesForCache(int64 cache_id);
  bool AddEntryFlags(const GURL& entry_url, int64 cache_id,
                     int additional_flags);
  bool FindResponseIdsForCacheAsVector(
      int64 cache_id, std::vector<int64>* response_ids) {
    return FindResponseIdsForCacheHelper(cache_id, response_ids, NULL);
  }
  bool FindResponseIdsForCacheAsSet(
      int64 cache_id, std::set<int64>* response_ids) {
    return FindResponseIdsForCacheHelper(cache_id, NULL, response_ids);
  }

  bool FindFallbackNameSpacesForOrigin(
      const GURL& origin, std::vector<FallbackNameSpaceRecord>* records);
  bool FindFallbackNameSpacesForCache(
      int64 cache_id, std::vector<FallbackNameSpaceRecord>* records);
  bool InsertFallbackNameSpace(const FallbackNameSpaceRecord* record);
  bool InsertFallbackNameSpaceRecords(
      const std::vector<FallbackNameSpaceRecord>& records);
  bool DeleteFallbackNameSpacesForCache(int64 cache_id);

  bool FindOnlineWhiteListForCache(
      int64 cache_id, std::vector<OnlineWhiteListRecord>* records);
  bool InsertOnlineWhiteList(const OnlineWhiteListRecord* record);
  bool InsertOnlineWhiteListRecords(
      const std::vector<OnlineWhiteListRecord>& records);
  bool DeleteOnlineWhiteListForCache(int64 cache_id);

  bool GetDeletableResponseIds(std::vector<int64>* response_ids,
                               int64 max_rowid, int limit);
  bool InsertDeletableResponseIds(const std::vector<int64>& response_ids);
  bool DeleteDeletableResponseIds(const std::vector<int64>& response_ids);

  // So our callers can wrap operations in transactions.
  sql::Connection* db_connection() {
    LazyOpen(true);
    return db_.get();
  }

 private:
  bool RunCachedStatementWithIds(
      const sql::StatementID& statement_id, const char* sql,
      const std::vector<int64>& ids);
  bool RunUniqueStatementWithInt64Result(const char* sql, int64* result);

  bool PrepareUniqueStatement(const char* sql, sql::Statement* statement);
  bool PrepareCachedStatement(
      const sql::StatementID& id, const char* sql, sql::Statement* statement);

  bool FindResponseIdsForCacheHelper(
      int64 cache_id, std::vector<int64>* ids_vector,
      std::set<int64>* ids_set);

  // Record retrieval helpers
  void ReadGroupRecord(const sql::Statement& statement, GroupRecord* record);
  void ReadCacheRecord(const sql::Statement& statement, CacheRecord* record);
  void ReadEntryRecord(const sql::Statement& statement, EntryRecord* record);
  void ReadFallbackNameSpaceRecord(
      const sql::Statement& statement, FallbackNameSpaceRecord* record);
  void ReadOnlineWhiteListRecord(
      const sql::Statement& statement, OnlineWhiteListRecord* record);

  // Database creation
  bool LazyOpen(bool create_if_needed);
  bool EnsureDatabaseVersion();
  bool CreateSchema();
  bool UpgradeSchema();

  void ResetConnectionAndTables();

  // Deletes the existing database file and the entire directory containing
  // the database file including the disk cache in which response headers
  // and bodies are stored, and then creates a new database file.
  bool DeleteExistingAndCreateNewDatabase();

  FilePath db_file_path_;
  scoped_ptr<sql::Connection> db_;
  scoped_ptr<sql::MetaTable> meta_table_;
  scoped_ptr<webkit_database::QuotaTable> quota_table_;
  bool is_disabled_;
  bool is_recreating_;

  FRIEND_TEST(AppCacheDatabaseTest, CacheRecords);
  FRIEND_TEST(AppCacheDatabaseTest, EntryRecords);
  FRIEND_TEST(AppCacheDatabaseTest, FallbackNameSpaceRecords);
  FRIEND_TEST(AppCacheDatabaseTest, GroupRecords);
  FRIEND_TEST(AppCacheDatabaseTest, LazyOpen);
  FRIEND_TEST(AppCacheDatabaseTest, OnlineWhiteListRecords);
  FRIEND_TEST(AppCacheDatabaseTest, ReCreate);
  FRIEND_TEST(AppCacheDatabaseTest, DeletableResponseIds);
  FRIEND_TEST(AppCacheDatabaseTest, Quotas);
  DISALLOW_COPY_AND_ASSIGN(AppCacheDatabase);
};

}  // namespace appcache

#endif  // WEBKIT_APPCACHE_APPCACHE_DATABASE_H_