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_
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"
30 class SpecialStoragePolicy
;
32 // All the methods of this class must run on the DB thread.
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
);
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
,
51 base::Time last_access_time
);
53 bool SetOriginLastModifiedTime(const GURL
& origin
,
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
,
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
86 bool IsOriginDatabaseBootstrapped();
87 bool SetOriginDatabaseBootstrapped(bool bootstrap_flag
);
90 struct QuotaTableEntry
{
93 const std::string
& host
,
100 friend bool operator <(const QuotaTableEntry
& lhs
,
101 const QuotaTableEntry
& rhs
);
103 struct OriginInfoTableEntry
{
104 OriginInfoTableEntry();
105 OriginInfoTableEntry(
109 const base::Time
& last_access_time
,
110 const base::Time
& last_modified_time
);
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.
122 const char* table_name
;
126 const char* index_name
;
127 const char* table_name
;
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).
143 void ScheduleCommit();
145 bool FindOriginUsedCount(const GURL
& origin
,
149 bool LazyOpen(bool create_if_needed
);
150 bool EnsureDatabaseVersion();
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_
;
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
);
185 #endif // WEBKIT_QUOTA_QUOTA_DATABASE_H_