Chromium Code Reviews| 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 if (!UpdateLastAccessTime(pair.first, pair.second)) | |
| 430 return false; | |
| 431 } | |
| 432 if (!transaction.Commit()) | |
|
cmumford
2015/02/13 23:06:15
If we fail to update or commit the last access tim
michaeln
2015/02/14 01:35:03
Done, the method easier to read now too.
| |
| 433 return false; | |
| 434 | |
| 435 lazy_last_access_times_.clear(); | |
| 436 return true; | |
| 408 } | 437 } |
| 409 | 438 |
| 410 bool AppCacheDatabase::InsertGroup(const GroupRecord* record) { | 439 bool AppCacheDatabase::InsertGroup(const GroupRecord* record) { |
| 411 if (!LazyOpen(true)) | 440 if (!LazyOpen(true)) |
| 412 return false; | 441 return false; |
| 413 | 442 |
| 414 const char* kSql = | 443 const char* kSql = |
| 415 "INSERT INTO Groups" | 444 "INSERT INTO Groups" |
| 416 " (group_id, origin, manifest_url, creation_time, last_access_time)" | 445 " (group_id, origin, manifest_url, creation_time, last_access_time)" |
| 417 " VALUES(?, ?, ?, ?, ?)"; | 446 " VALUES(?, ?, ?, ?, ?)"; |
| (...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 917 return statement.Succeeded(); | 946 return statement.Succeeded(); |
| 918 } | 947 } |
| 919 | 948 |
| 920 void AppCacheDatabase::ReadGroupRecord( | 949 void AppCacheDatabase::ReadGroupRecord( |
| 921 const sql::Statement& statement, GroupRecord* record) { | 950 const sql::Statement& statement, GroupRecord* record) { |
| 922 record->group_id = statement.ColumnInt64(0); | 951 record->group_id = statement.ColumnInt64(0); |
| 923 record->origin = GURL(statement.ColumnString(1)); | 952 record->origin = GURL(statement.ColumnString(1)); |
| 924 record->manifest_url = GURL(statement.ColumnString(2)); | 953 record->manifest_url = GURL(statement.ColumnString(2)); |
| 925 record->creation_time = | 954 record->creation_time = |
| 926 base::Time::FromInternalValue(statement.ColumnInt64(3)); | 955 base::Time::FromInternalValue(statement.ColumnInt64(3)); |
| 927 record->last_access_time = | 956 |
| 928 base::Time::FromInternalValue(statement.ColumnInt64(4)); | 957 const auto found = lazy_last_access_times_.find(record->group_id); |
| 958 if (found != lazy_last_access_times_.end()) { | |
| 959 record->last_access_time = found->second; | |
| 960 } else { | |
| 961 record->last_access_time = | |
| 962 base::Time::FromInternalValue(statement.ColumnInt64(4)); | |
| 963 } | |
| 929 } | 964 } |
| 930 | 965 |
| 931 void AppCacheDatabase::ReadCacheRecord( | 966 void AppCacheDatabase::ReadCacheRecord( |
| 932 const sql::Statement& statement, CacheRecord* record) { | 967 const sql::Statement& statement, CacheRecord* record) { |
| 933 record->cache_id = statement.ColumnInt64(0); | 968 record->cache_id = statement.ColumnInt64(0); |
| 934 record->group_id = statement.ColumnInt64(1); | 969 record->group_id = statement.ColumnInt64(1); |
| 935 record->online_wildcard = statement.ColumnBool(2); | 970 record->online_wildcard = statement.ColumnBool(2); |
| 936 record->update_time = | 971 record->update_time = |
| 937 base::Time::FromInternalValue(statement.ColumnInt64(3)); | 972 base::Time::FromInternalValue(statement.ColumnInt64(3)); |
| 938 record->cache_size = statement.ColumnInt64(4); | 973 record->cache_size = statement.ColumnInt64(4); |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1219 } | 1254 } |
| 1220 | 1255 |
| 1221 void AppCacheDatabase::OnDatabaseError(int err, sql::Statement* stmt) { | 1256 void AppCacheDatabase::OnDatabaseError(int err, sql::Statement* stmt) { |
| 1222 was_corruption_detected_ |= sql::IsErrorCatastrophic(err); | 1257 was_corruption_detected_ |= sql::IsErrorCatastrophic(err); |
| 1223 if (!db_->ShouldIgnoreSqliteError(err)) | 1258 if (!db_->ShouldIgnoreSqliteError(err)) |
| 1224 DLOG(ERROR) << db_->GetErrorMessage(); | 1259 DLOG(ERROR) << db_->GetErrorMessage(); |
| 1225 // TODO: Maybe use non-catostrophic errors to trigger a full integrity check? | 1260 // TODO: Maybe use non-catostrophic errors to trigger a full integrity check? |
| 1226 } | 1261 } |
| 1227 | 1262 |
| 1228 } // namespace content | 1263 } // namespace content |
| OLD | NEW |