| 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 "storage/browser/quota/quota_database.h" | 5 #include "storage/browser/quota/quota_database.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 180 |
| 181 *quota = statement.ColumnInt64(0); | 181 *quota = statement.ColumnInt64(0); |
| 182 return true; | 182 return true; |
| 183 } | 183 } |
| 184 | 184 |
| 185 bool QuotaDatabase::SetHostQuota( | 185 bool QuotaDatabase::SetHostQuota( |
| 186 const std::string& host, StorageType type, int64 quota) { | 186 const std::string& host, StorageType type, int64 quota) { |
| 187 DCHECK_GE(quota, 0); | 187 DCHECK_GE(quota, 0); |
| 188 if (!LazyOpen(true)) | 188 if (!LazyOpen(true)) |
| 189 return false; | 189 return false; |
| 190 | 190 if (!InsertOrReplaceHostQuota(host, type, quota)) |
| 191 const char* kSql = | |
| 192 "INSERT OR REPLACE INTO HostQuotaTable" | |
| 193 " (quota, host, type)" | |
| 194 " VALUES (?, ?, ?)"; | |
| 195 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | |
| 196 statement.BindInt64(0, quota); | |
| 197 statement.BindString(1, host); | |
| 198 statement.BindInt(2, static_cast<int>(type)); | |
| 199 | |
| 200 if (!statement.Run()) | |
| 201 return false; | 191 return false; |
| 202 | |
| 203 ScheduleCommit(); | 192 ScheduleCommit(); |
| 204 return true; | 193 return true; |
| 205 } | 194 } |
| 206 | 195 |
| 207 bool QuotaDatabase::SetOriginLastAccessTime( | 196 bool QuotaDatabase::SetOriginLastAccessTime( |
| 208 const GURL& origin, StorageType type, base::Time last_access_time) { | 197 const GURL& origin, StorageType type, base::Time last_access_time) { |
| 209 if (!LazyOpen(true)) | 198 if (!LazyOpen(true)) |
| 210 return false; | 199 return false; |
| 211 | 200 |
| 212 sql::Statement statement; | 201 sql::Statement statement; |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 return meta_table_->SetValue(kIsOriginTableBootstrapped, bootstrap_flag); | 479 return meta_table_->SetValue(kIsOriginTableBootstrapped, bootstrap_flag); |
| 491 } | 480 } |
| 492 | 481 |
| 493 void QuotaDatabase::Commit() { | 482 void QuotaDatabase::Commit() { |
| 494 if (!db_) | 483 if (!db_) |
| 495 return; | 484 return; |
| 496 | 485 |
| 497 if (timer_.IsRunning()) | 486 if (timer_.IsRunning()) |
| 498 timer_.Stop(); | 487 timer_.Stop(); |
| 499 | 488 |
| 489 DCHECK_EQ(1, db_->transaction_nesting()); |
| 500 db_->CommitTransaction(); | 490 db_->CommitTransaction(); |
| 491 DCHECK_EQ(0, db_->transaction_nesting()); |
| 501 db_->BeginTransaction(); | 492 db_->BeginTransaction(); |
| 493 DCHECK_EQ(1, db_->transaction_nesting()); |
| 502 } | 494 } |
| 503 | 495 |
| 504 void QuotaDatabase::ScheduleCommit() { | 496 void QuotaDatabase::ScheduleCommit() { |
| 505 if (timer_.IsRunning()) | 497 if (timer_.IsRunning()) |
| 506 return; | 498 return; |
| 507 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kCommitIntervalMs), | 499 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kCommitIntervalMs), |
| 508 this, &QuotaDatabase::Commit); | 500 this, &QuotaDatabase::Commit); |
| 509 } | 501 } |
| 510 | 502 |
| 511 bool QuotaDatabase::FindOriginUsedCount( | 503 bool QuotaDatabase::FindOriginUsedCount( |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 return false; | 641 return false; |
| 650 } | 642 } |
| 651 } | 643 } |
| 652 | 644 |
| 653 return transaction.Commit(); | 645 return transaction.Commit(); |
| 654 } | 646 } |
| 655 | 647 |
| 656 bool QuotaDatabase::ResetSchema() { | 648 bool QuotaDatabase::ResetSchema() { |
| 657 DCHECK(!db_file_path_.empty()); | 649 DCHECK(!db_file_path_.empty()); |
| 658 DCHECK(base::PathExists(db_file_path_)); | 650 DCHECK(base::PathExists(db_file_path_)); |
| 651 DCHECK(!db_ || !db_->transaction_nesting()); |
| 659 VLOG(1) << "Deleting existing quota data and starting over."; | 652 VLOG(1) << "Deleting existing quota data and starting over."; |
| 660 | 653 |
| 661 db_.reset(); | 654 db_.reset(); |
| 662 meta_table_.reset(); | 655 meta_table_.reset(); |
| 663 | 656 |
| 664 if (!sql::Connection::Delete(db_file_path_)) | 657 if (!sql::Connection::Delete(db_file_path_)) |
| 665 return false; | 658 return false; |
| 666 | 659 |
| 667 // So we can't go recursive. | 660 // So we can't go recursive. |
| 668 if (is_recreating_) | 661 if (is_recreating_) |
| 669 return false; | 662 return false; |
| 670 | 663 |
| 671 base::AutoReset<bool> auto_reset(&is_recreating_, true); | 664 base::AutoReset<bool> auto_reset(&is_recreating_, true); |
| 672 return LazyOpen(true); | 665 return LazyOpen(true); |
| 673 } | 666 } |
| 674 | 667 |
| 675 bool QuotaDatabase::UpgradeSchema(int current_version) { | 668 bool QuotaDatabase::UpgradeSchema(int current_version) { |
| 669 DCHECK_EQ(0, db_->transaction_nesting()); |
| 670 |
| 676 if (current_version == 2) { | 671 if (current_version == 2) { |
| 677 QuotaTableImporter importer; | 672 QuotaTableImporter importer; |
| 678 typedef std::vector<QuotaTableEntry> QuotaTableEntries; | 673 typedef std::vector<QuotaTableEntry> QuotaTableEntries; |
| 679 if (!DumpQuotaTable(base::Bind(&QuotaTableImporter::Append, | 674 if (!DumpQuotaTable(base::Bind(&QuotaTableImporter::Append, |
| 680 base::Unretained(&importer)))) { | 675 base::Unretained(&importer)))) { |
| 681 return false; | 676 return false; |
| 682 } | 677 } |
| 683 ResetSchema(); | 678 ResetSchema(); |
| 679 |
| 680 sql::Transaction transaction(db_.get()); |
| 681 if (!transaction.Begin()) |
| 682 return false; |
| 684 for (QuotaTableEntries::const_iterator iter = importer.entries.begin(); | 683 for (QuotaTableEntries::const_iterator iter = importer.entries.begin(); |
| 685 iter != importer.entries.end(); ++iter) { | 684 iter != importer.entries.end(); ++iter) { |
| 686 if (!SetHostQuota(iter->host, iter->type, iter->quota)) | 685 if (!InsertOrReplaceHostQuota(iter->host, iter->type, iter->quota)) |
| 687 return false; | 686 return false; |
| 688 } | 687 } |
| 689 Commit(); | 688 return transaction.Commit(); |
| 689 } else if (current_version < 5) { |
| 690 sql::Transaction transaction(db_.get()); |
| 691 if (!transaction.Begin()) |
| 692 return false; |
| 690 | 693 |
| 691 return true; | |
| 692 } else if (current_version < 5) { | |
| 693 const QuotaDatabase::TableSchema& eviction_table_schema = kTables[2]; | 694 const QuotaDatabase::TableSchema& eviction_table_schema = kTables[2]; |
| 694 DCHECK_EQ(strcmp(kEvictionInfoTable, eviction_table_schema.table_name), 0); | 695 DCHECK_EQ(strcmp(kEvictionInfoTable, eviction_table_schema.table_name), 0); |
| 695 | 696 |
| 696 std::string sql("CREATE TABLE "); | 697 std::string sql("CREATE TABLE "); |
| 697 sql += eviction_table_schema.table_name; | 698 sql += eviction_table_schema.table_name; |
| 698 sql += eviction_table_schema.columns; | 699 sql += eviction_table_schema.columns; |
| 699 if (!db_->Execute(sql.c_str())) { | 700 if (!db_->Execute(sql.c_str())) { |
| 700 VLOG(1) << "Failed to execute " << sql; | 701 VLOG(1) << "Failed to execute " << sql; |
| 701 return false; | 702 return false; |
| 702 } | 703 } |
| 703 | 704 |
| 704 meta_table_->SetVersionNumber(5); | 705 meta_table_->SetVersionNumber(5); |
| 705 Commit(); | 706 return transaction.Commit(); |
| 706 | |
| 707 return true; | |
| 708 } | 707 } |
| 709 return false; | 708 return false; |
| 710 } | 709 } |
| 711 | 710 |
| 711 bool QuotaDatabase::InsertOrReplaceHostQuota( |
| 712 const std::string& host, StorageType type, int64 quota) { |
| 713 DCHECK(db_.get()); |
| 714 const char* kSql = |
| 715 "INSERT OR REPLACE INTO HostQuotaTable" |
| 716 " (quota, host, type)" |
| 717 " VALUES (?, ?, ?)"; |
| 718 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 719 statement.BindInt64(0, quota); |
| 720 statement.BindString(1, host); |
| 721 statement.BindInt(2, static_cast<int>(type)); |
| 722 return statement.Run(); |
| 723 } |
| 724 |
| 712 bool QuotaDatabase::DumpQuotaTable(const QuotaTableCallback& callback) { | 725 bool QuotaDatabase::DumpQuotaTable(const QuotaTableCallback& callback) { |
| 713 if (!LazyOpen(true)) | 726 if (!LazyOpen(true)) |
| 714 return false; | 727 return false; |
| 715 | 728 |
| 716 const char* kSql = "SELECT * FROM HostQuotaTable"; | 729 const char* kSql = "SELECT * FROM HostQuotaTable"; |
| 717 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); | 730 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 718 | 731 |
| 719 while (statement.Step()) { | 732 while (statement.Step()) { |
| 720 QuotaTableEntry entry = QuotaTableEntry( | 733 QuotaTableEntry entry = QuotaTableEntry( |
| 721 statement.ColumnString(0), | 734 statement.ColumnString(0), |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 767 if (lhs.origin < rhs.origin) return true; | 780 if (lhs.origin < rhs.origin) return true; |
| 768 if (rhs.origin < lhs.origin) return false; | 781 if (rhs.origin < lhs.origin) return false; |
| 769 if (lhs.type < rhs.type) return true; | 782 if (lhs.type < rhs.type) return true; |
| 770 if (rhs.type < lhs.type) return false; | 783 if (rhs.type < lhs.type) return false; |
| 771 if (lhs.used_count < rhs.used_count) return true; | 784 if (lhs.used_count < rhs.used_count) return true; |
| 772 if (rhs.used_count < lhs.used_count) return false; | 785 if (rhs.used_count < lhs.used_count) return false; |
| 773 return lhs.last_access_time < rhs.last_access_time; | 786 return lhs.last_access_time < rhs.last_access_time; |
| 774 } | 787 } |
| 775 | 788 |
| 776 } // namespace storage | 789 } // namespace storage |
| OLD | NEW |