Define DevTools content API
[chromium-blink-merge.git] / webkit / quota / quota_database.h
blob1fed8fa085041a3f5ed2e610cd590903f8a0890d
1 // Copyright (c) 2011 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_QUOTA_QUOTA_DATABASE_H_
6 #define WEBKIT_QUOTA_QUOTA_DATABASE_H_
8 #include <set>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/file_path.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/time.h"
17 #include "base/timer.h"
18 #include "googleurl/src/gurl.h"
19 #include "webkit/quota/quota_types.h"
21 namespace sql {
22 class Connection;
23 class MetaTable;
26 class GURL;
28 namespace quota {
30 class SpecialStoragePolicy;
32 // All the methods of this class must run on the DB thread.
33 class QuotaDatabase {
34 public:
35 // Constants for {Get,Set}QuotaConfigValue keys.
36 static const char kDesiredAvailableSpaceKey[];
37 static const char kTemporaryQuotaOverrideKey[];
39 // If 'path' is empty, an in memory database will be used.
40 explicit QuotaDatabase(const FilePath& path);
41 ~QuotaDatabase();
43 void CloseConnection();
45 bool GetHostQuota(const std::string& host, StorageType type, int64* quota);
46 bool SetHostQuota(const std::string& host, StorageType type, int64 quota);
47 bool DeleteHostQuota(const std::string& host, StorageType type);
49 bool SetOriginLastAccessTime(const GURL& origin,
50 StorageType type,
51 base::Time last_access_time);
53 bool SetOriginLastModifiedTime(const GURL& origin,
54 StorageType type,
55 base::Time last_modified_time);
57 // Register initial |origins| info |type| to the database.
58 // This method is assumed to be called only after the installation or
59 // the database schema reset.
60 bool RegisterInitialOriginInfo(
61 const std::set<GURL>& origins, StorageType type);
63 bool DeleteOriginInfo(const GURL& origin, StorageType type);
65 bool GetQuotaConfigValue(const char* key, int64* value);
66 bool SetQuotaConfigValue(const char* key, int64 value);
68 // Sets |origin| to the least recently used origin of origins not included
69 // in |exceptions| and not granted the special unlimited storage right.
70 // It returns false when it failed in accessing the database.
71 // |origin| is set to empty when there is no matching origin.
72 bool GetLRUOrigin(StorageType type,
73 const std::set<GURL>& exceptions,
74 SpecialStoragePolicy* special_storage_policy,
75 GURL* origin);
77 // Populates |origins| with the ones that have been modified since
78 // the |modified_since|.
79 bool GetOriginsModifiedSince(StorageType type,
80 std::set<GURL>* origins,
81 base::Time modified_since);
83 // Returns false if SetOriginDatabaseBootstrapped has never
84 // been called before, which means existing origins may not have been
85 // registered.
86 bool IsOriginDatabaseBootstrapped();
87 bool SetOriginDatabaseBootstrapped(bool bootstrap_flag);
89 private:
90 struct QuotaTableEntry {
91 QuotaTableEntry();
92 QuotaTableEntry(
93 const std::string& host,
94 StorageType type,
95 int64 quota);
96 std::string host;
97 StorageType type;
98 int64 quota;
100 friend bool operator <(const QuotaTableEntry& lhs,
101 const QuotaTableEntry& rhs);
103 struct OriginInfoTableEntry {
104 OriginInfoTableEntry();
105 OriginInfoTableEntry(
106 const GURL& origin,
107 StorageType type,
108 int used_count,
109 const base::Time& last_access_time,
110 const base::Time& last_modified_time);
111 GURL origin;
112 StorageType type;
113 int used_count;
114 base::Time last_access_time;
115 base::Time last_modified_time;
117 friend bool operator <(const OriginInfoTableEntry& lhs,
118 const OriginInfoTableEntry& rhs);
120 // Structures used for CreateSchema.
121 struct TableSchema {
122 const char* table_name;
123 const char* columns;
125 struct IndexSchema {
126 const char* index_name;
127 const char* table_name;
128 const char* columns;
129 bool unique;
132 typedef base::Callback<bool (const QuotaTableEntry&)> QuotaTableCallback;
133 typedef base::Callback<bool (const OriginInfoTableEntry&)>
134 OriginInfoTableCallback;
136 struct QuotaTableImporter;
138 // For long-running transactions support. We always keep a transaction open
139 // so that multiple transactions can be batched. They are flushed
140 // with a delay after a modification has been made. We support neither
141 // nested transactions nor rollback (as we don't need them for now).
142 void Commit();
143 void ScheduleCommit();
145 bool FindOriginUsedCount(const GURL& origin,
146 StorageType type,
147 int* used_count);
149 bool LazyOpen(bool create_if_needed);
150 bool EnsureDatabaseVersion();
151 bool ResetSchema();
152 bool UpgradeSchema(int current_version);
154 static bool CreateSchema(
155 sql::Connection* database,
156 sql::MetaTable* meta_table,
157 int schema_version, int compatible_version,
158 const TableSchema* tables, size_t tables_size,
159 const IndexSchema* indexes, size_t indexes_size);
161 // |callback| may return false to stop reading data.
162 bool DumpQuotaTable(QuotaTableCallback* callback);
163 bool DumpOriginInfoTable(OriginInfoTableCallback* callback);
165 FilePath db_file_path_;
167 scoped_ptr<sql::Connection> db_;
168 scoped_ptr<sql::MetaTable> meta_table_;
169 bool is_recreating_;
170 bool is_disabled_;
172 base::OneShotTimer<QuotaDatabase> timer_;
174 friend class QuotaDatabaseTest;
175 friend class QuotaManager;
177 static const TableSchema kTables[];
178 static const IndexSchema kIndexes[];
180 DISALLOW_COPY_AND_ASSIGN(QuotaDatabase);
183 } // namespace quota
185 #endif // WEBKIT_QUOTA_QUOTA_DATABASE_H_