1 // Copyright 2013 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_QUOTA_QUOTA_DATABASE_H_
6 #define WEBKIT_BROWSER_QUOTA_QUOTA_DATABASE_H_
11 #include "base/basictypes.h"
12 #include "base/callback.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 "base/timer/timer.h"
19 #include "webkit/browser/webkit_storage_browser_export.h"
20 #include "webkit/common/quota/quota_types.h"
31 class SpecialStoragePolicy;
33 // All the methods of this class must run on the DB thread.
34 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE QuotaDatabase {
36 // Constants for {Get,Set}QuotaConfigValue keys.
37 static const char kDesiredAvailableSpaceKey[];
38 static const char kTemporaryQuotaOverrideKey[];
40 // If 'path' is empty, an in memory database will be used.
41 explicit QuotaDatabase(const base::FilePath& path);
44 void CloseConnection();
46 bool GetHostQuota(const std::string& host, StorageType type, int64* quota);
47 bool SetHostQuota(const std::string& host, StorageType type, int64 quota);
48 bool DeleteHostQuota(const std::string& host, StorageType type);
50 bool SetOriginLastAccessTime(const GURL& origin,
52 base::Time last_access_time);
54 bool SetOriginLastModifiedTime(const GURL& origin,
56 base::Time last_modified_time);
58 // Register initial |origins| info |type| to the database.
59 // This method is assumed to be called only after the installation or
60 // the database schema reset.
61 bool RegisterInitialOriginInfo(
62 const std::set<GURL>& origins, StorageType type);
64 bool DeleteOriginInfo(const GURL& origin, StorageType type);
66 bool GetQuotaConfigValue(const char* key, int64* value);
67 bool SetQuotaConfigValue(const char* key, int64 value);
69 // Sets |origin| to the least recently used origin of origins not included
70 // in |exceptions| and not granted the special unlimited storage right.
71 // It returns false when it failed in accessing the database.
72 // |origin| is set to empty when there is no matching origin.
73 bool GetLRUOrigin(StorageType type,
74 const std::set<GURL>& exceptions,
75 SpecialStoragePolicy* special_storage_policy,
78 // Populates |origins| with the ones that have been modified since
79 // the |modified_since|.
80 bool GetOriginsModifiedSince(StorageType type,
81 std::set<GURL>* origins,
82 base::Time modified_since);
84 // Returns false if SetOriginDatabaseBootstrapped has never
85 // been called before, which means existing origins may not have been
87 bool IsOriginDatabaseBootstrapped();
88 bool SetOriginDatabaseBootstrapped(bool bootstrap_flag);
91 struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE QuotaTableEntry {
94 const std::string& host,
101 friend WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE bool operator <(
102 const QuotaTableEntry& lhs, const QuotaTableEntry& rhs);
104 struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE OriginInfoTableEntry {
105 OriginInfoTableEntry();
106 OriginInfoTableEntry(
110 const base::Time& last_access_time,
111 const base::Time& last_modified_time);
115 base::Time last_access_time;
116 base::Time last_modified_time;
118 friend WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE bool operator <(
119 const OriginInfoTableEntry& lhs, const OriginInfoTableEntry& rhs);
121 // Structures used for CreateSchema.
123 const char* table_name;
127 const char* index_name;
128 const char* table_name;
133 typedef base::Callback<bool (const QuotaTableEntry&)> QuotaTableCallback;
134 typedef base::Callback<bool (const OriginInfoTableEntry&)>
135 OriginInfoTableCallback;
137 struct QuotaTableImporter;
139 // For long-running transactions support. We always keep a transaction open
140 // so that multiple transactions can be batched. They are flushed
141 // with a delay after a modification has been made. We support neither
142 // nested transactions nor rollback (as we don't need them for now).
144 void ScheduleCommit();
146 bool FindOriginUsedCount(const GURL& origin,
150 bool LazyOpen(bool create_if_needed);
151 bool EnsureDatabaseVersion();
153 bool UpgradeSchema(int current_version);
155 static bool CreateSchema(
156 sql::Connection* database,
157 sql::MetaTable* meta_table,
158 int schema_version, int compatible_version,
159 const TableSchema* tables, size_t tables_size,
160 const IndexSchema* indexes, size_t indexes_size);
162 // |callback| may return false to stop reading data.
163 bool DumpQuotaTable(QuotaTableCallback* callback);
164 bool DumpOriginInfoTable(OriginInfoTableCallback* callback);
166 base::FilePath db_file_path_;
168 scoped_ptr<sql::Connection> db_;
169 scoped_ptr<sql::MetaTable> meta_table_;
173 base::OneShotTimer<QuotaDatabase> timer_;
175 friend class QuotaDatabaseTest;
176 friend class QuotaManager;
178 static const TableSchema kTables[];
179 static const IndexSchema kIndexes[];
181 DISALLOW_COPY_AND_ASSIGN(QuotaDatabase);
186 #endif // WEBKIT_BROWSER_QUOTA_QUOTA_DATABASE_H_