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