Fix Win8 metro startup crash from window switcher button
[chromium-blink-merge.git] / webkit / quota / quota_database.h
blob6b01410cac8b88b378e4b08b878f9f380f4a6940
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/files/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"
20 #include "webkit/storage/webkit_storage_export.h"
22 namespace sql {
23 class Connection;
24 class MetaTable;
27 class GURL;
29 namespace quota {
31 class SpecialStoragePolicy;
33 // All the methods of this class must run on the DB thread.
34 class WEBKIT_STORAGE_EXPORT_PRIVATE QuotaDatabase {
35 public:
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);
42 ~QuotaDatabase();
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,
51 StorageType type,
52 base::Time last_access_time);
54 bool SetOriginLastModifiedTime(const GURL& origin,
55 StorageType type,
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,
76 GURL* origin);
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
86 // registered.
87 bool IsOriginDatabaseBootstrapped();
88 bool SetOriginDatabaseBootstrapped(bool bootstrap_flag);
90 private:
91 struct WEBKIT_STORAGE_EXPORT_PRIVATE QuotaTableEntry {
92 QuotaTableEntry();
93 QuotaTableEntry(
94 const std::string& host,
95 StorageType type,
96 int64 quota);
97 std::string host;
98 StorageType type;
99 int64 quota;
101 friend WEBKIT_STORAGE_EXPORT_PRIVATE bool operator <(
102 const QuotaTableEntry& lhs, const QuotaTableEntry& rhs);
104 struct WEBKIT_STORAGE_EXPORT_PRIVATE OriginInfoTableEntry {
105 OriginInfoTableEntry();
106 OriginInfoTableEntry(
107 const GURL& origin,
108 StorageType type,
109 int used_count,
110 const base::Time& last_access_time,
111 const base::Time& last_modified_time);
112 GURL origin;
113 StorageType type;
114 int used_count;
115 base::Time last_access_time;
116 base::Time last_modified_time;
118 friend WEBKIT_STORAGE_EXPORT_PRIVATE bool operator <(
119 const OriginInfoTableEntry& lhs, const OriginInfoTableEntry& rhs);
121 // Structures used for CreateSchema.
122 struct TableSchema {
123 const char* table_name;
124 const char* columns;
126 struct IndexSchema {
127 const char* index_name;
128 const char* table_name;
129 const char* columns;
130 bool unique;
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).
143 void Commit();
144 void ScheduleCommit();
146 bool FindOriginUsedCount(const GURL& origin,
147 StorageType type,
148 int* used_count);
150 bool LazyOpen(bool create_if_needed);
151 bool EnsureDatabaseVersion();
152 bool ResetSchema();
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_;
170 bool is_recreating_;
171 bool is_disabled_;
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);
184 } // namespace quota
186 #endif // WEBKIT_QUOTA_QUOTA_DATABASE_H_