| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/appcache/appcache_database.h" | 5 #include "content/browser/appcache/appcache_database.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 | 203 |
| 204 | 204 |
| 205 AppCacheDatabase::AppCacheDatabase(const base::FilePath& path) | 205 AppCacheDatabase::AppCacheDatabase(const base::FilePath& path) |
| 206 : db_file_path_(path), | 206 : db_file_path_(path), |
| 207 is_disabled_(false), | 207 is_disabled_(false), |
| 208 is_recreating_(false), | 208 is_recreating_(false), |
| 209 was_corruption_detected_(false) { | 209 was_corruption_detected_(false) { |
| 210 } | 210 } |
| 211 | 211 |
| 212 AppCacheDatabase::~AppCacheDatabase() { | 212 AppCacheDatabase::~AppCacheDatabase() { |
| 213 CommitLazyLastAccessTimes(); |
| 213 } | 214 } |
| 214 | 215 |
| 215 void AppCacheDatabase::Disable() { | 216 void AppCacheDatabase::Disable() { |
| 216 VLOG(1) << "Disabling appcache database."; | 217 VLOG(1) << "Disabling appcache database."; |
| 217 is_disabled_ = true; | 218 is_disabled_ = true; |
| 218 ResetConnectionAndTables(); | 219 ResetConnectionAndTables(); |
| 219 } | 220 } |
| 220 | 221 |
| 221 int64 AppCacheDatabase::GetOriginUsage(const GURL& origin) { | 222 int64 AppCacheDatabase::GetOriginUsage(const GURL& origin) { |
| 222 std::vector<CacheRecord> records; | 223 std::vector<CacheRecord> records; |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 386 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 386 statement.BindInt64(0, cache_id); | 387 statement.BindInt64(0, cache_id); |
| 387 | 388 |
| 388 if (!statement.Step()) | 389 if (!statement.Step()) |
| 389 return false; | 390 return false; |
| 390 | 391 |
| 391 ReadGroupRecord(statement, record); | 392 ReadGroupRecord(statement, record); |
| 392 return true; | 393 return true; |
| 393 } | 394 } |
| 394 | 395 |
| 395 bool AppCacheDatabase::UpdateGroupLastAccessTime( | 396 bool AppCacheDatabase::UpdateLastAccessTime( |
| 396 int64 group_id, base::Time time) { | 397 int64 group_id, base::Time time) { |
| 397 if (!LazyOpen(true)) | 398 if (!LazyOpen(true)) |
| 398 return false; | 399 return false; |
| 399 | 400 |
| 400 const char* kSql = | 401 const char* kSql = |
| 401 "UPDATE Groups SET last_access_time = ? WHERE group_id = ?"; | 402 "UPDATE Groups SET last_access_time = ? WHERE group_id = ?"; |
| 402 | 403 |
| 403 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 404 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 404 statement.BindInt64(0, time.ToInternalValue()); | 405 statement.BindInt64(0, time.ToInternalValue()); |
| 405 statement.BindInt64(1, group_id); | 406 statement.BindInt64(1, group_id); |
| 406 | 407 |
| 407 return statement.Run() && db_->GetLastChangeCount(); | 408 return statement.Run(); |
| 409 } |
| 410 |
| 411 bool AppCacheDatabase::LazyUpdateLastAccessTime( |
| 412 int64 group_id, base::Time time) { |
| 413 if (!LazyOpen(true)) |
| 414 return false; |
| 415 lazy_last_access_times_[group_id] = time; |
| 416 return true; |
| 417 } |
| 418 |
| 419 bool AppCacheDatabase::CommitLazyLastAccessTimes() { |
| 420 if (lazy_last_access_times_.empty()) |
| 421 return true; |
| 422 if (!LazyOpen(false)) |
| 423 return false; |
| 424 |
| 425 sql::Transaction transaction(db_.get()); |
| 426 if (!transaction.Begin()) |
| 427 return false; |
| 428 for (const auto& pair : lazy_last_access_times_) |
| 429 UpdateLastAccessTime(pair.first, pair.second); |
| 430 lazy_last_access_times_.clear(); |
| 431 return transaction.Commit(); |
| 408 } | 432 } |
| 409 | 433 |
| 410 bool AppCacheDatabase::InsertGroup(const GroupRecord* record) { | 434 bool AppCacheDatabase::InsertGroup(const GroupRecord* record) { |
| 411 if (!LazyOpen(true)) | 435 if (!LazyOpen(true)) |
| 412 return false; | 436 return false; |
| 413 | 437 |
| 414 const char* kSql = | 438 const char* kSql = |
| 415 "INSERT INTO Groups" | 439 "INSERT INTO Groups" |
| 416 " (group_id, origin, manifest_url, creation_time, last_access_time)" | 440 " (group_id, origin, manifest_url, creation_time, last_access_time)" |
| 417 " VALUES(?, ?, ?, ?, ?)"; | 441 " VALUES(?, ?, ?, ?, ?)"; |
| (...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 917 return statement.Succeeded(); | 941 return statement.Succeeded(); |
| 918 } | 942 } |
| 919 | 943 |
| 920 void AppCacheDatabase::ReadGroupRecord( | 944 void AppCacheDatabase::ReadGroupRecord( |
| 921 const sql::Statement& statement, GroupRecord* record) { | 945 const sql::Statement& statement, GroupRecord* record) { |
| 922 record->group_id = statement.ColumnInt64(0); | 946 record->group_id = statement.ColumnInt64(0); |
| 923 record->origin = GURL(statement.ColumnString(1)); | 947 record->origin = GURL(statement.ColumnString(1)); |
| 924 record->manifest_url = GURL(statement.ColumnString(2)); | 948 record->manifest_url = GURL(statement.ColumnString(2)); |
| 925 record->creation_time = | 949 record->creation_time = |
| 926 base::Time::FromInternalValue(statement.ColumnInt64(3)); | 950 base::Time::FromInternalValue(statement.ColumnInt64(3)); |
| 927 record->last_access_time = | 951 |
| 928 base::Time::FromInternalValue(statement.ColumnInt64(4)); | 952 const auto found = lazy_last_access_times_.find(record->group_id); |
| 953 if (found != lazy_last_access_times_.end()) { |
| 954 record->last_access_time = found->second; |
| 955 } else { |
| 956 record->last_access_time = |
| 957 base::Time::FromInternalValue(statement.ColumnInt64(4)); |
| 958 } |
| 929 } | 959 } |
| 930 | 960 |
| 931 void AppCacheDatabase::ReadCacheRecord( | 961 void AppCacheDatabase::ReadCacheRecord( |
| 932 const sql::Statement& statement, CacheRecord* record) { | 962 const sql::Statement& statement, CacheRecord* record) { |
| 933 record->cache_id = statement.ColumnInt64(0); | 963 record->cache_id = statement.ColumnInt64(0); |
| 934 record->group_id = statement.ColumnInt64(1); | 964 record->group_id = statement.ColumnInt64(1); |
| 935 record->online_wildcard = statement.ColumnBool(2); | 965 record->online_wildcard = statement.ColumnBool(2); |
| 936 record->update_time = | 966 record->update_time = |
| 937 base::Time::FromInternalValue(statement.ColumnInt64(3)); | 967 base::Time::FromInternalValue(statement.ColumnInt64(3)); |
| 938 record->cache_size = statement.ColumnInt64(4); | 968 record->cache_size = statement.ColumnInt64(4); |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1219 } | 1249 } |
| 1220 | 1250 |
| 1221 void AppCacheDatabase::OnDatabaseError(int err, sql::Statement* stmt) { | 1251 void AppCacheDatabase::OnDatabaseError(int err, sql::Statement* stmt) { |
| 1222 was_corruption_detected_ |= sql::IsErrorCatastrophic(err); | 1252 was_corruption_detected_ |= sql::IsErrorCatastrophic(err); |
| 1223 if (!db_->ShouldIgnoreSqliteError(err)) | 1253 if (!db_->ShouldIgnoreSqliteError(err)) |
| 1224 DLOG(ERROR) << db_->GetErrorMessage(); | 1254 DLOG(ERROR) << db_->GetErrorMessage(); |
| 1225 // TODO: Maybe use non-catostrophic errors to trigger a full integrity check? | 1255 // TODO: Maybe use non-catostrophic errors to trigger a full integrity check? |
| 1226 } | 1256 } |
| 1227 | 1257 |
| 1228 } // namespace content | 1258 } // namespace content |
| OLD | NEW |