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 #include "webkit/browser/quota/quota_database.h" | 5 #include "storage/browser/quota/quota_database.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "sql/connection.h" | 14 #include "sql/connection.h" |
15 #include "sql/meta_table.h" | 15 #include "sql/meta_table.h" |
16 #include "sql/statement.h" | 16 #include "sql/statement.h" |
17 #include "sql/transaction.h" | 17 #include "sql/transaction.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 #include "webkit/browser/quota/special_storage_policy.h" | 19 #include "storage/browser/quota/special_storage_policy.h" |
20 | 20 |
21 namespace quota { | 21 namespace quota { |
22 namespace { | 22 namespace { |
23 | 23 |
24 // Definitions for database schema. | 24 // Definitions for database schema. |
25 | 25 |
26 const int kCurrentVersion = 4; | 26 const int kCurrentVersion = 4; |
27 const int kCompatibleVersion = 2; | 27 const int kCompatibleVersion = 2; |
28 | 28 |
29 const char kHostQuotaTable[] = "HostQuotaTable"; | 29 const char kHostQuotaTable[] = "HostQuotaTable"; |
30 const char kOriginInfoTable[] = "OriginInfoTable"; | 30 const char kOriginInfoTable[] = "OriginInfoTable"; |
31 const char kIsOriginTableBootstrapped[] = "IsOriginTableBootstrapped"; | 31 const char kIsOriginTableBootstrapped[] = "IsOriginTableBootstrapped"; |
32 | 32 |
33 bool VerifyValidQuotaConfig(const char* key) { | 33 bool VerifyValidQuotaConfig(const char* key) { |
34 return (key != NULL && | 34 return (key != NULL && |
35 (!strcmp(key, QuotaDatabase::kDesiredAvailableSpaceKey) || | 35 (!strcmp(key, QuotaDatabase::kDesiredAvailableSpaceKey) || |
36 !strcmp(key, QuotaDatabase::kTemporaryQuotaOverrideKey))); | 36 !strcmp(key, QuotaDatabase::kTemporaryQuotaOverrideKey))); |
37 } | 37 } |
38 | 38 |
39 const int kCommitIntervalMs = 30000; | 39 const int kCommitIntervalMs = 30000; |
40 | 40 |
41 } // anonymous namespace | 41 } // anonymous namespace |
42 | 42 |
43 // static | 43 // static |
44 const char QuotaDatabase::kDesiredAvailableSpaceKey[] = "DesiredAvailableSpace"; | 44 const char QuotaDatabase::kDesiredAvailableSpaceKey[] = "DesiredAvailableSpace"; |
45 const char QuotaDatabase::kTemporaryQuotaOverrideKey[] = | 45 const char QuotaDatabase::kTemporaryQuotaOverrideKey[] = |
46 "TemporaryQuotaOverride"; | 46 "TemporaryQuotaOverride"; |
47 | 47 |
48 const QuotaDatabase::TableSchema QuotaDatabase::kTables[] = { | 48 const QuotaDatabase::TableSchema QuotaDatabase::kTables[] = { |
49 { kHostQuotaTable, | 49 {kHostQuotaTable, |
50 "(host TEXT NOT NULL," | 50 "(host TEXT NOT NULL," |
51 " type INTEGER NOT NULL," | 51 " type INTEGER NOT NULL," |
52 " quota INTEGER DEFAULT 0," | 52 " quota INTEGER DEFAULT 0," |
53 " UNIQUE(host, type))" }, | 53 " UNIQUE(host, type))"}, |
54 { kOriginInfoTable, | 54 {kOriginInfoTable, |
55 "(origin TEXT NOT NULL," | 55 "(origin TEXT NOT NULL," |
56 " type INTEGER NOT NULL," | 56 " type INTEGER NOT NULL," |
57 " used_count INTEGER DEFAULT 0," | 57 " used_count INTEGER DEFAULT 0," |
58 " last_access_time INTEGER DEFAULT 0," | 58 " last_access_time INTEGER DEFAULT 0," |
59 " last_modified_time INTEGER DEFAULT 0," | 59 " last_modified_time INTEGER DEFAULT 0," |
60 " UNIQUE(origin, type))" }, | 60 " UNIQUE(origin, type))"}, |
61 }; | 61 }; |
62 | 62 |
63 // static | 63 // static |
64 const QuotaDatabase::IndexSchema QuotaDatabase::kIndexes[] = { | 64 const QuotaDatabase::IndexSchema QuotaDatabase::kIndexes[] = { |
65 { "HostIndex", | 65 {"HostIndex", kHostQuotaTable, "(host)", false}, |
66 kHostQuotaTable, | 66 {"OriginInfoIndex", kOriginInfoTable, "(origin)", false}, |
67 "(host)", | 67 {"OriginLastAccessTimeIndex", kOriginInfoTable, "(last_access_time)", |
68 false }, | 68 false}, |
69 { "OriginInfoIndex", | 69 {"OriginLastModifiedTimeIndex", kOriginInfoTable, "(last_modified_time)", |
70 kOriginInfoTable, | 70 false}, |
71 "(origin)", | |
72 false }, | |
73 { "OriginLastAccessTimeIndex", | |
74 kOriginInfoTable, | |
75 "(last_access_time)", | |
76 false }, | |
77 { "OriginLastModifiedTimeIndex", | |
78 kOriginInfoTable, | |
79 "(last_modified_time)", | |
80 false }, | |
81 }; | 71 }; |
82 | 72 |
83 struct QuotaDatabase::QuotaTableImporter { | 73 struct QuotaDatabase::QuotaTableImporter { |
84 bool Append(const QuotaTableEntry& entry) { | 74 bool Append(const QuotaTableEntry& entry) { |
85 entries.push_back(entry); | 75 entries.push_back(entry); |
86 return true; | 76 return true; |
87 } | 77 } |
88 std::vector<QuotaTableEntry> entries; | 78 std::vector<QuotaTableEntry> entries; |
89 }; | 79 }; |
90 | 80 |
91 // Clang requires explicit out-of-line constructors for them. | 81 // Clang requires explicit out-of-line constructors for them. |
92 QuotaDatabase::QuotaTableEntry::QuotaTableEntry() | 82 QuotaDatabase::QuotaTableEntry::QuotaTableEntry() |
93 : type(kStorageTypeUnknown), | 83 : type(kStorageTypeUnknown), quota(0) { |
94 quota(0) { | |
95 } | 84 } |
96 | 85 |
97 QuotaDatabase::QuotaTableEntry::QuotaTableEntry( | 86 QuotaDatabase::QuotaTableEntry::QuotaTableEntry(const std::string& host, |
98 const std::string& host, | 87 StorageType type, |
99 StorageType type, | 88 int64 quota) |
100 int64 quota) | 89 : host(host), type(type), quota(quota) { |
101 : host(host), | |
102 type(type), | |
103 quota(quota) { | |
104 } | 90 } |
105 | 91 |
106 QuotaDatabase::OriginInfoTableEntry::OriginInfoTableEntry() | 92 QuotaDatabase::OriginInfoTableEntry::OriginInfoTableEntry() |
107 : type(kStorageTypeUnknown), | 93 : type(kStorageTypeUnknown), used_count(0) { |
108 used_count(0) { | |
109 } | 94 } |
110 | 95 |
111 QuotaDatabase::OriginInfoTableEntry::OriginInfoTableEntry( | 96 QuotaDatabase::OriginInfoTableEntry::OriginInfoTableEntry( |
112 const GURL& origin, | 97 const GURL& origin, |
113 StorageType type, | 98 StorageType type, |
114 int used_count, | 99 int used_count, |
115 const base::Time& last_access_time, | 100 const base::Time& last_access_time, |
116 const base::Time& last_modified_time) | 101 const base::Time& last_modified_time) |
117 : origin(origin), | 102 : origin(origin), |
118 type(type), | 103 type(type), |
119 used_count(used_count), | 104 used_count(used_count), |
120 last_access_time(last_access_time), | 105 last_access_time(last_access_time), |
121 last_modified_time(last_modified_time) { | 106 last_modified_time(last_modified_time) { |
122 } | 107 } |
123 | 108 |
124 // QuotaDatabase ------------------------------------------------------------ | 109 // QuotaDatabase ------------------------------------------------------------ |
125 QuotaDatabase::QuotaDatabase(const base::FilePath& path) | 110 QuotaDatabase::QuotaDatabase(const base::FilePath& path) |
126 : db_file_path_(path), | 111 : db_file_path_(path), is_recreating_(false), is_disabled_(false) { |
127 is_recreating_(false), | |
128 is_disabled_(false) { | |
129 } | 112 } |
130 | 113 |
131 QuotaDatabase::~QuotaDatabase() { | 114 QuotaDatabase::~QuotaDatabase() { |
132 if (db_) { | 115 if (db_) { |
133 db_->CommitTransaction(); | 116 db_->CommitTransaction(); |
134 } | 117 } |
135 } | 118 } |
136 | 119 |
137 void QuotaDatabase::CloseConnection() { | 120 void QuotaDatabase::CloseConnection() { |
138 meta_table_.reset(); | 121 meta_table_.reset(); |
139 db_.reset(); | 122 db_.reset(); |
140 } | 123 } |
141 | 124 |
142 bool QuotaDatabase::GetHostQuota( | 125 bool QuotaDatabase::GetHostQuota(const std::string& host, |
143 const std::string& host, StorageType type, int64* quota) { | 126 StorageType type, |
| 127 int64* quota) { |
144 DCHECK(quota); | 128 DCHECK(quota); |
145 if (!LazyOpen(false)) | 129 if (!LazyOpen(false)) |
146 return false; | 130 return false; |
147 | 131 |
148 const char* kSql = | 132 const char* kSql = |
149 "SELECT quota" | 133 "SELECT quota" |
150 " FROM HostQuotaTable" | 134 " FROM HostQuotaTable" |
151 " WHERE host = ? AND type = ?"; | 135 " WHERE host = ? AND type = ?"; |
152 | 136 |
153 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 137 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
154 statement.BindString(0, host); | 138 statement.BindString(0, host); |
155 statement.BindInt(1, static_cast<int>(type)); | 139 statement.BindInt(1, static_cast<int>(type)); |
156 | 140 |
157 if (!statement.Step()) | 141 if (!statement.Step()) |
158 return false; | 142 return false; |
159 | 143 |
160 *quota = statement.ColumnInt64(0); | 144 *quota = statement.ColumnInt64(0); |
161 return true; | 145 return true; |
162 } | 146 } |
163 | 147 |
164 bool QuotaDatabase::SetHostQuota( | 148 bool QuotaDatabase::SetHostQuota(const std::string& host, |
165 const std::string& host, StorageType type, int64 quota) { | 149 StorageType type, |
| 150 int64 quota) { |
166 DCHECK_GE(quota, 0); | 151 DCHECK_GE(quota, 0); |
167 if (!LazyOpen(true)) | 152 if (!LazyOpen(true)) |
168 return false; | 153 return false; |
169 | 154 |
170 const char* kSql = | 155 const char* kSql = |
171 "INSERT OR REPLACE INTO HostQuotaTable" | 156 "INSERT OR REPLACE INTO HostQuotaTable" |
172 " (quota, host, type)" | 157 " (quota, host, type)" |
173 " VALUES (?, ?, ?)"; | 158 " VALUES (?, ?, ?)"; |
174 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 159 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
175 statement.BindInt64(0, quota); | 160 statement.BindInt64(0, quota); |
176 statement.BindString(1, host); | 161 statement.BindString(1, host); |
177 statement.BindInt(2, static_cast<int>(type)); | 162 statement.BindInt(2, static_cast<int>(type)); |
178 | 163 |
179 if (!statement.Run()) | 164 if (!statement.Run()) |
180 return false; | 165 return false; |
181 | 166 |
182 ScheduleCommit(); | 167 ScheduleCommit(); |
183 return true; | 168 return true; |
184 } | 169 } |
185 | 170 |
186 bool QuotaDatabase::SetOriginLastAccessTime( | 171 bool QuotaDatabase::SetOriginLastAccessTime(const GURL& origin, |
187 const GURL& origin, StorageType type, base::Time last_access_time) { | 172 StorageType type, |
| 173 base::Time last_access_time) { |
188 if (!LazyOpen(true)) | 174 if (!LazyOpen(true)) |
189 return false; | 175 return false; |
190 | 176 |
191 sql::Statement statement; | 177 sql::Statement statement; |
192 | 178 |
193 int used_count = 1; | 179 int used_count = 1; |
194 if (FindOriginUsedCount(origin, type, &used_count)) { | 180 if (FindOriginUsedCount(origin, type, &used_count)) { |
195 ++used_count; | 181 ++used_count; |
196 const char* kSql = | 182 const char* kSql = |
197 "UPDATE OriginInfoTable" | 183 "UPDATE OriginInfoTable" |
198 " SET used_count = ?, last_access_time = ?" | 184 " SET used_count = ?, last_access_time = ?" |
199 " WHERE origin = ? AND type = ?"; | 185 " WHERE origin = ? AND type = ?"; |
200 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 186 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
201 } else { | 187 } else { |
202 const char* kSql = | 188 const char* kSql = |
203 "INSERT INTO OriginInfoTable" | 189 "INSERT INTO OriginInfoTable" |
204 " (used_count, last_access_time, origin, type)" | 190 " (used_count, last_access_time, origin, type)" |
205 " VALUES (?, ?, ?, ?)"; | 191 " VALUES (?, ?, ?, ?)"; |
206 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 192 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
207 } | 193 } |
208 statement.BindInt(0, used_count); | 194 statement.BindInt(0, used_count); |
209 statement.BindInt64(1, last_access_time.ToInternalValue()); | 195 statement.BindInt64(1, last_access_time.ToInternalValue()); |
210 statement.BindString(2, origin.spec()); | 196 statement.BindString(2, origin.spec()); |
211 statement.BindInt(3, static_cast<int>(type)); | 197 statement.BindInt(3, static_cast<int>(type)); |
212 | 198 |
213 if (!statement.Run()) | 199 if (!statement.Run()) |
214 return false; | 200 return false; |
215 | 201 |
216 ScheduleCommit(); | 202 ScheduleCommit(); |
217 return true; | 203 return true; |
218 } | 204 } |
219 | 205 |
220 bool QuotaDatabase::SetOriginLastModifiedTime( | 206 bool QuotaDatabase::SetOriginLastModifiedTime(const GURL& origin, |
221 const GURL& origin, StorageType type, base::Time last_modified_time) { | 207 StorageType type, |
| 208 base::Time last_modified_time) { |
222 if (!LazyOpen(true)) | 209 if (!LazyOpen(true)) |
223 return false; | 210 return false; |
224 | 211 |
225 sql::Statement statement; | 212 sql::Statement statement; |
226 | 213 |
227 int dummy; | 214 int dummy; |
228 if (FindOriginUsedCount(origin, type, &dummy)) { | 215 if (FindOriginUsedCount(origin, type, &dummy)) { |
229 const char* kSql = | 216 const char* kSql = |
230 "UPDATE OriginInfoTable" | 217 "UPDATE OriginInfoTable" |
231 " SET last_modified_time = ?" | 218 " SET last_modified_time = ?" |
232 " WHERE origin = ? AND type = ?"; | 219 " WHERE origin = ? AND type = ?"; |
233 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 220 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
234 } else { | 221 } else { |
235 const char* kSql = | 222 const char* kSql = |
236 "INSERT INTO OriginInfoTable" | 223 "INSERT INTO OriginInfoTable" |
237 " (last_modified_time, origin, type) VALUES (?, ?, ?)"; | 224 " (last_modified_time, origin, type) VALUES (?, ?, ?)"; |
238 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 225 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
239 } | 226 } |
240 statement.BindInt64(0, last_modified_time.ToInternalValue()); | 227 statement.BindInt64(0, last_modified_time.ToInternalValue()); |
241 statement.BindString(1, origin.spec()); | 228 statement.BindString(1, origin.spec()); |
242 statement.BindInt(2, static_cast<int>(type)); | 229 statement.BindInt(2, static_cast<int>(type)); |
243 | 230 |
244 if (!statement.Run()) | 231 if (!statement.Run()) |
245 return false; | 232 return false; |
246 | 233 |
247 ScheduleCommit(); | 234 ScheduleCommit(); |
248 return true; | 235 return true; |
249 } | 236 } |
250 | 237 |
251 bool QuotaDatabase::RegisterInitialOriginInfo( | 238 bool QuotaDatabase::RegisterInitialOriginInfo(const std::set<GURL>& origins, |
252 const std::set<GURL>& origins, StorageType type) { | 239 StorageType type) { |
253 if (!LazyOpen(true)) | 240 if (!LazyOpen(true)) |
254 return false; | 241 return false; |
255 | 242 |
256 typedef std::set<GURL>::const_iterator itr_type; | 243 typedef std::set<GURL>::const_iterator itr_type; |
257 for (itr_type itr = origins.begin(), end = origins.end(); | 244 for (itr_type itr = origins.begin(), end = origins.end(); itr != end; ++itr) { |
258 itr != end; ++itr) { | |
259 const char* kSql = | 245 const char* kSql = |
260 "INSERT OR IGNORE INTO OriginInfoTable" | 246 "INSERT OR IGNORE INTO OriginInfoTable" |
261 " (origin, type) VALUES (?, ?)"; | 247 " (origin, type) VALUES (?, ?)"; |
262 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 248 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
263 statement.BindString(0, itr->spec()); | 249 statement.BindString(0, itr->spec()); |
264 statement.BindInt(1, static_cast<int>(type)); | 250 statement.BindInt(1, static_cast<int>(type)); |
265 | 251 |
266 if (!statement.Run()) | 252 if (!statement.Run()) |
267 return false; | 253 return false; |
268 } | 254 } |
269 | 255 |
270 ScheduleCommit(); | 256 ScheduleCommit(); |
271 return true; | 257 return true; |
272 } | 258 } |
273 | 259 |
274 bool QuotaDatabase::DeleteHostQuota( | 260 bool QuotaDatabase::DeleteHostQuota(const std::string& host, StorageType type) { |
275 const std::string& host, StorageType type) { | |
276 if (!LazyOpen(false)) | 261 if (!LazyOpen(false)) |
277 return false; | 262 return false; |
278 | 263 |
279 const char* kSql = | 264 const char* kSql = |
280 "DELETE FROM HostQuotaTable" | 265 "DELETE FROM HostQuotaTable" |
281 " WHERE host = ? AND type = ?"; | 266 " WHERE host = ? AND type = ?"; |
282 | 267 |
283 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 268 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
284 statement.BindString(0, host); | 269 statement.BindString(0, host); |
285 statement.BindInt(1, static_cast<int>(type)); | 270 statement.BindInt(1, static_cast<int>(type)); |
286 | 271 |
287 if (!statement.Run()) | 272 if (!statement.Run()) |
288 return false; | 273 return false; |
289 | 274 |
290 ScheduleCommit(); | 275 ScheduleCommit(); |
291 return true; | 276 return true; |
292 } | 277 } |
293 | 278 |
294 bool QuotaDatabase::DeleteOriginInfo( | 279 bool QuotaDatabase::DeleteOriginInfo(const GURL& origin, StorageType type) { |
295 const GURL& origin, StorageType type) { | |
296 if (!LazyOpen(false)) | 280 if (!LazyOpen(false)) |
297 return false; | 281 return false; |
298 | 282 |
299 const char* kSql = | 283 const char* kSql = |
300 "DELETE FROM OriginInfoTable" | 284 "DELETE FROM OriginInfoTable" |
301 " WHERE origin = ? AND type = ?"; | 285 " WHERE origin = ? AND type = ?"; |
302 | 286 |
303 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 287 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
304 statement.BindString(0, origin.spec()); | 288 statement.BindString(0, origin.spec()); |
305 statement.BindInt(1, static_cast<int>(type)); | 289 statement.BindInt(1, static_cast<int>(type)); |
(...skipping 12 matching lines...) Expand all Loading... |
318 return meta_table_->GetValue(key, value); | 302 return meta_table_->GetValue(key, value); |
319 } | 303 } |
320 | 304 |
321 bool QuotaDatabase::SetQuotaConfigValue(const char* key, int64 value) { | 305 bool QuotaDatabase::SetQuotaConfigValue(const char* key, int64 value) { |
322 if (!LazyOpen(true)) | 306 if (!LazyOpen(true)) |
323 return false; | 307 return false; |
324 DCHECK(VerifyValidQuotaConfig(key)); | 308 DCHECK(VerifyValidQuotaConfig(key)); |
325 return meta_table_->SetValue(key, value); | 309 return meta_table_->SetValue(key, value); |
326 } | 310 } |
327 | 311 |
328 bool QuotaDatabase::GetLRUOrigin( | 312 bool QuotaDatabase::GetLRUOrigin(StorageType type, |
329 StorageType type, | 313 const std::set<GURL>& exceptions, |
330 const std::set<GURL>& exceptions, | 314 SpecialStoragePolicy* special_storage_policy, |
331 SpecialStoragePolicy* special_storage_policy, | 315 GURL* origin) { |
332 GURL* origin) { | |
333 DCHECK(origin); | 316 DCHECK(origin); |
334 if (!LazyOpen(false)) | 317 if (!LazyOpen(false)) |
335 return false; | 318 return false; |
336 | 319 |
337 const char* kSql = "SELECT origin FROM OriginInfoTable" | 320 const char* kSql = |
338 " WHERE type = ?" | 321 "SELECT origin FROM OriginInfoTable" |
339 " ORDER BY last_access_time ASC"; | 322 " WHERE type = ?" |
| 323 " ORDER BY last_access_time ASC"; |
340 | 324 |
341 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 325 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
342 statement.BindInt(0, static_cast<int>(type)); | 326 statement.BindInt(0, static_cast<int>(type)); |
343 | 327 |
344 while (statement.Step()) { | 328 while (statement.Step()) { |
345 GURL url(statement.ColumnString(0)); | 329 GURL url(statement.ColumnString(0)); |
346 if (exceptions.find(url) != exceptions.end()) | 330 if (exceptions.find(url) != exceptions.end()) |
347 continue; | 331 continue; |
348 if (special_storage_policy && | 332 if (special_storage_policy && |
349 special_storage_policy->IsStorageUnlimited(url)) | 333 special_storage_policy->IsStorageUnlimited(url)) |
350 continue; | 334 continue; |
351 *origin = url; | 335 *origin = url; |
352 return true; | 336 return true; |
353 } | 337 } |
354 | 338 |
355 *origin = GURL(); | 339 *origin = GURL(); |
356 return statement.Succeeded(); | 340 return statement.Succeeded(); |
357 } | 341 } |
358 | 342 |
359 bool QuotaDatabase::GetOriginsModifiedSince( | 343 bool QuotaDatabase::GetOriginsModifiedSince(StorageType type, |
360 StorageType type, std::set<GURL>* origins, base::Time modified_since) { | 344 std::set<GURL>* origins, |
| 345 base::Time modified_since) { |
361 DCHECK(origins); | 346 DCHECK(origins); |
362 if (!LazyOpen(false)) | 347 if (!LazyOpen(false)) |
363 return false; | 348 return false; |
364 | 349 |
365 const char* kSql = "SELECT origin FROM OriginInfoTable" | 350 const char* kSql = |
366 " WHERE type = ? AND last_modified_time >= ?"; | 351 "SELECT origin FROM OriginInfoTable" |
| 352 " WHERE type = ? AND last_modified_time >= ?"; |
367 | 353 |
368 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 354 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
369 statement.BindInt(0, static_cast<int>(type)); | 355 statement.BindInt(0, static_cast<int>(type)); |
370 statement.BindInt64(1, modified_since.ToInternalValue()); | 356 statement.BindInt64(1, modified_since.ToInternalValue()); |
371 | 357 |
372 origins->clear(); | 358 origins->clear(); |
373 while (statement.Step()) | 359 while (statement.Step()) |
374 origins->insert(GURL(statement.ColumnString(0))); | 360 origins->insert(GURL(statement.ColumnString(0))); |
375 | 361 |
376 return statement.Succeeded(); | 362 return statement.Succeeded(); |
(...skipping 21 matching lines...) Expand all Loading... |
398 if (timer_.IsRunning()) | 384 if (timer_.IsRunning()) |
399 timer_.Stop(); | 385 timer_.Stop(); |
400 | 386 |
401 db_->CommitTransaction(); | 387 db_->CommitTransaction(); |
402 db_->BeginTransaction(); | 388 db_->BeginTransaction(); |
403 } | 389 } |
404 | 390 |
405 void QuotaDatabase::ScheduleCommit() { | 391 void QuotaDatabase::ScheduleCommit() { |
406 if (timer_.IsRunning()) | 392 if (timer_.IsRunning()) |
407 return; | 393 return; |
408 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kCommitIntervalMs), | 394 timer_.Start(FROM_HERE, |
409 this, &QuotaDatabase::Commit); | 395 base::TimeDelta::FromMilliseconds(kCommitIntervalMs), |
| 396 this, |
| 397 &QuotaDatabase::Commit); |
410 } | 398 } |
411 | 399 |
412 bool QuotaDatabase::FindOriginUsedCount( | 400 bool QuotaDatabase::FindOriginUsedCount(const GURL& origin, |
413 const GURL& origin, StorageType type, int* used_count) { | 401 StorageType type, |
| 402 int* used_count) { |
414 DCHECK(used_count); | 403 DCHECK(used_count); |
415 if (!LazyOpen(false)) | 404 if (!LazyOpen(false)) |
416 return false; | 405 return false; |
417 | 406 |
418 const char* kSql = | 407 const char* kSql = |
419 "SELECT used_count FROM OriginInfoTable" | 408 "SELECT used_count FROM OriginInfoTable" |
420 " WHERE origin = ? AND type = ?"; | 409 " WHERE origin = ? AND type = ?"; |
421 | 410 |
422 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 411 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
423 statement.BindString(0, origin.spec()); | 412 statement.BindString(0, origin.spec()); |
(...skipping 23 matching lines...) Expand all Loading... |
447 | 436 |
448 db_.reset(new sql::Connection); | 437 db_.reset(new sql::Connection); |
449 meta_table_.reset(new sql::MetaTable); | 438 meta_table_.reset(new sql::MetaTable); |
450 | 439 |
451 db_->set_histogram_tag("Quota"); | 440 db_->set_histogram_tag("Quota"); |
452 | 441 |
453 bool opened = false; | 442 bool opened = false; |
454 if (in_memory_only) { | 443 if (in_memory_only) { |
455 opened = db_->OpenInMemory(); | 444 opened = db_->OpenInMemory(); |
456 } else if (!base::CreateDirectory(db_file_path_.DirName())) { | 445 } else if (!base::CreateDirectory(db_file_path_.DirName())) { |
457 LOG(ERROR) << "Failed to create quota database directory."; | 446 LOG(ERROR) << "Failed to create quota database directory."; |
458 } else { | 447 } else { |
459 opened = db_->Open(db_file_path_); | 448 opened = db_->Open(db_file_path_); |
460 if (opened) | 449 if (opened) |
461 db_->Preload(); | 450 db_->Preload(); |
462 } | 451 } |
463 | 452 |
464 if (!opened || !EnsureDatabaseVersion()) { | 453 if (!opened || !EnsureDatabaseVersion()) { |
465 LOG(ERROR) << "Failed to open the quota database."; | 454 LOG(ERROR) << "Failed to open the quota database."; |
466 is_disabled_ = true; | 455 is_disabled_ = true; |
467 db_.reset(); | 456 db_.reset(); |
468 meta_table_.reset(); | 457 meta_table_.reset(); |
469 return false; | 458 return false; |
470 } | 459 } |
471 | 460 |
472 // Start a long-running transaction. | 461 // Start a long-running transaction. |
473 db_->BeginTransaction(); | 462 db_->BeginTransaction(); |
474 | 463 |
475 return true; | 464 return true; |
476 } | 465 } |
477 | 466 |
478 bool QuotaDatabase::EnsureDatabaseVersion() { | 467 bool QuotaDatabase::EnsureDatabaseVersion() { |
479 static const size_t kTableCount = ARRAYSIZE_UNSAFE(kTables); | 468 static const size_t kTableCount = ARRAYSIZE_UNSAFE(kTables); |
480 static const size_t kIndexCount = ARRAYSIZE_UNSAFE(kIndexes); | 469 static const size_t kIndexCount = ARRAYSIZE_UNSAFE(kIndexes); |
481 if (!sql::MetaTable::DoesTableExist(db_.get())) | 470 if (!sql::MetaTable::DoesTableExist(db_.get())) |
482 return CreateSchema(db_.get(), meta_table_.get(), | 471 return CreateSchema(db_.get(), |
483 kCurrentVersion, kCompatibleVersion, | 472 meta_table_.get(), |
484 kTables, kTableCount, | 473 kCurrentVersion, |
485 kIndexes, kIndexCount); | 474 kCompatibleVersion, |
| 475 kTables, |
| 476 kTableCount, |
| 477 kIndexes, |
| 478 kIndexCount); |
486 | 479 |
487 if (!meta_table_->Init(db_.get(), kCurrentVersion, kCompatibleVersion)) | 480 if (!meta_table_->Init(db_.get(), kCurrentVersion, kCompatibleVersion)) |
488 return false; | 481 return false; |
489 | 482 |
490 if (meta_table_->GetCompatibleVersionNumber() > kCurrentVersion) { | 483 if (meta_table_->GetCompatibleVersionNumber() > kCurrentVersion) { |
491 LOG(WARNING) << "Quota database is too new."; | 484 LOG(WARNING) << "Quota database is too new."; |
492 return false; | 485 return false; |
493 } | 486 } |
494 | 487 |
495 if (meta_table_->GetVersionNumber() < kCurrentVersion) { | 488 if (meta_table_->GetVersionNumber() < kCurrentVersion) { |
496 if (!UpgradeSchema(meta_table_->GetVersionNumber())) | 489 if (!UpgradeSchema(meta_table_->GetVersionNumber())) |
497 return ResetSchema(); | 490 return ResetSchema(); |
498 } | 491 } |
499 | 492 |
500 #ifndef NDEBUG | 493 #ifndef NDEBUG |
501 DCHECK(sql::MetaTable::DoesTableExist(db_.get())); | 494 DCHECK(sql::MetaTable::DoesTableExist(db_.get())); |
502 for (size_t i = 0; i < kTableCount; ++i) { | 495 for (size_t i = 0; i < kTableCount; ++i) { |
503 DCHECK(db_->DoesTableExist(kTables[i].table_name)); | 496 DCHECK(db_->DoesTableExist(kTables[i].table_name)); |
504 } | 497 } |
505 #endif | 498 #endif |
506 | 499 |
507 return true; | 500 return true; |
508 } | 501 } |
509 | 502 |
510 // static | 503 // static |
511 bool QuotaDatabase::CreateSchema( | 504 bool QuotaDatabase::CreateSchema(sql::Connection* database, |
512 sql::Connection* database, | 505 sql::MetaTable* meta_table, |
513 sql::MetaTable* meta_table, | 506 int schema_version, |
514 int schema_version, int compatible_version, | 507 int compatible_version, |
515 const TableSchema* tables, size_t tables_size, | 508 const TableSchema* tables, |
516 const IndexSchema* indexes, size_t indexes_size) { | 509 size_t tables_size, |
| 510 const IndexSchema* indexes, |
| 511 size_t indexes_size) { |
517 // TODO(kinuko): Factor out the common code to create databases. | 512 // TODO(kinuko): Factor out the common code to create databases. |
518 sql::Transaction transaction(database); | 513 sql::Transaction transaction(database); |
519 if (!transaction.Begin()) | 514 if (!transaction.Begin()) |
520 return false; | 515 return false; |
521 | 516 |
522 if (!meta_table->Init(database, schema_version, compatible_version)) | 517 if (!meta_table->Init(database, schema_version, compatible_version)) |
523 return false; | 518 return false; |
524 | 519 |
525 for (size_t i = 0; i < tables_size; ++i) { | 520 for (size_t i = 0; i < tables_size; ++i) { |
526 std::string sql("CREATE TABLE "); | 521 std::string sql("CREATE TABLE "); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 bool QuotaDatabase::UpgradeSchema(int current_version) { | 568 bool QuotaDatabase::UpgradeSchema(int current_version) { |
574 if (current_version == 2) { | 569 if (current_version == 2) { |
575 QuotaTableImporter importer; | 570 QuotaTableImporter importer; |
576 typedef std::vector<QuotaTableEntry> QuotaTableEntries; | 571 typedef std::vector<QuotaTableEntry> QuotaTableEntries; |
577 if (!DumpQuotaTable(base::Bind(&QuotaTableImporter::Append, | 572 if (!DumpQuotaTable(base::Bind(&QuotaTableImporter::Append, |
578 base::Unretained(&importer)))) { | 573 base::Unretained(&importer)))) { |
579 return false; | 574 return false; |
580 } | 575 } |
581 ResetSchema(); | 576 ResetSchema(); |
582 for (QuotaTableEntries::const_iterator iter = importer.entries.begin(); | 577 for (QuotaTableEntries::const_iterator iter = importer.entries.begin(); |
583 iter != importer.entries.end(); ++iter) { | 578 iter != importer.entries.end(); |
| 579 ++iter) { |
584 if (!SetHostQuota(iter->host, iter->type, iter->quota)) | 580 if (!SetHostQuota(iter->host, iter->type, iter->quota)) |
585 return false; | 581 return false; |
586 } | 582 } |
587 Commit(); | 583 Commit(); |
588 return true; | 584 return true; |
589 } | 585 } |
590 return false; | 586 return false; |
591 } | 587 } |
592 | 588 |
593 bool QuotaDatabase::DumpQuotaTable(const QuotaTableCallback& callback) { | 589 bool QuotaDatabase::DumpQuotaTable(const QuotaTableCallback& callback) { |
594 if (!LazyOpen(true)) | 590 if (!LazyOpen(true)) |
595 return false; | 591 return false; |
596 | 592 |
597 const char* kSql = "SELECT * FROM HostQuotaTable"; | 593 const char* kSql = "SELECT * FROM HostQuotaTable"; |
598 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 594 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
599 | 595 |
600 while (statement.Step()) { | 596 while (statement.Step()) { |
601 QuotaTableEntry entry = QuotaTableEntry( | 597 QuotaTableEntry entry = |
602 statement.ColumnString(0), | 598 QuotaTableEntry(statement.ColumnString(0), |
603 static_cast<StorageType>(statement.ColumnInt(1)), | 599 static_cast<StorageType>(statement.ColumnInt(1)), |
604 statement.ColumnInt64(2)); | 600 statement.ColumnInt64(2)); |
605 | 601 |
606 if (!callback.Run(entry)) | 602 if (!callback.Run(entry)) |
607 return true; | 603 return true; |
608 } | 604 } |
609 | 605 |
610 return statement.Succeeded(); | 606 return statement.Succeeded(); |
611 } | 607 } |
612 | 608 |
613 bool QuotaDatabase::DumpOriginInfoTable( | 609 bool QuotaDatabase::DumpOriginInfoTable( |
614 const OriginInfoTableCallback& callback) { | 610 const OriginInfoTableCallback& callback) { |
615 | |
616 if (!LazyOpen(true)) | 611 if (!LazyOpen(true)) |
617 return false; | 612 return false; |
618 | 613 |
619 const char* kSql = "SELECT * FROM OriginInfoTable"; | 614 const char* kSql = "SELECT * FROM OriginInfoTable"; |
620 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 615 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
621 | 616 |
622 while (statement.Step()) { | 617 while (statement.Step()) { |
623 OriginInfoTableEntry entry( | 618 OriginInfoTableEntry entry( |
624 GURL(statement.ColumnString(0)), | 619 GURL(statement.ColumnString(0)), |
625 static_cast<StorageType>(statement.ColumnInt(1)), | 620 static_cast<StorageType>(statement.ColumnInt(1)), |
626 statement.ColumnInt(2), | 621 statement.ColumnInt(2), |
627 base::Time::FromInternalValue(statement.ColumnInt64(3)), | 622 base::Time::FromInternalValue(statement.ColumnInt64(3)), |
628 base::Time::FromInternalValue(statement.ColumnInt64(4))); | 623 base::Time::FromInternalValue(statement.ColumnInt64(4))); |
629 | 624 |
630 if (!callback.Run(entry)) | 625 if (!callback.Run(entry)) |
631 return true; | 626 return true; |
632 } | 627 } |
633 | 628 |
634 return statement.Succeeded(); | 629 return statement.Succeeded(); |
635 } | 630 } |
636 | 631 |
637 bool operator<(const QuotaDatabase::QuotaTableEntry& lhs, | 632 bool operator<(const QuotaDatabase::QuotaTableEntry& lhs, |
638 const QuotaDatabase::QuotaTableEntry& rhs) { | 633 const QuotaDatabase::QuotaTableEntry& rhs) { |
639 if (lhs.host < rhs.host) return true; | 634 if (lhs.host < rhs.host) |
640 if (rhs.host < lhs.host) return false; | 635 return true; |
641 if (lhs.type < rhs.type) return true; | 636 if (rhs.host < lhs.host) |
642 if (rhs.type < lhs.type) return false; | 637 return false; |
| 638 if (lhs.type < rhs.type) |
| 639 return true; |
| 640 if (rhs.type < lhs.type) |
| 641 return false; |
643 return lhs.quota < rhs.quota; | 642 return lhs.quota < rhs.quota; |
644 } | 643 } |
645 | 644 |
646 bool operator<(const QuotaDatabase::OriginInfoTableEntry& lhs, | 645 bool operator<(const QuotaDatabase::OriginInfoTableEntry& lhs, |
647 const QuotaDatabase::OriginInfoTableEntry& rhs) { | 646 const QuotaDatabase::OriginInfoTableEntry& rhs) { |
648 if (lhs.origin < rhs.origin) return true; | 647 if (lhs.origin < rhs.origin) |
649 if (rhs.origin < lhs.origin) return false; | 648 return true; |
650 if (lhs.type < rhs.type) return true; | 649 if (rhs.origin < lhs.origin) |
651 if (rhs.type < lhs.type) return false; | 650 return false; |
652 if (lhs.used_count < rhs.used_count) return true; | 651 if (lhs.type < rhs.type) |
653 if (rhs.used_count < lhs.used_count) return false; | 652 return true; |
| 653 if (rhs.type < lhs.type) |
| 654 return false; |
| 655 if (lhs.used_count < rhs.used_count) |
| 656 return true; |
| 657 if (rhs.used_count < lhs.used_count) |
| 658 return false; |
654 return lhs.last_access_time < rhs.last_access_time; | 659 return lhs.last_access_time < rhs.last_access_time; |
655 } | 660 } |
656 | 661 |
657 } // namespace quota | 662 } // namespace quota |
OLD | NEW |