Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/quota/quota_database.h" | 5 #include "webkit/quota/quota_database.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "app/sql/connection.h" | 9 #include "app/sql/connection.h" |
| 10 #include "app/sql/diagnostic_error_delegate.h" | 10 #include "app/sql/diagnostic_error_delegate.h" |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 bool QuotaDatabase::RegisterOrigins(const std::set<GURL>& origins, | 212 bool QuotaDatabase::RegisterOrigins(const std::set<GURL>& origins, |
| 213 StorageType type, | 213 StorageType type, |
| 214 base::Time last_access_time) { | 214 base::Time last_access_time) { |
| 215 if (!LazyOpen(true)) | 215 if (!LazyOpen(true)) |
| 216 return false; | 216 return false; |
| 217 | 217 |
| 218 sql::Transaction transaction(db_.get()); | 218 sql::Transaction transaction(db_.get()); |
| 219 if (!transaction.Begin()) | 219 if (!transaction.Begin()) |
| 220 return false; | 220 return false; |
| 221 | 221 |
| 222 sql::Statement statement; | |
| 223 | |
| 224 typedef std::set<GURL>::const_iterator itr_type; | 222 typedef std::set<GURL>::const_iterator itr_type; |
| 225 for (itr_type itr = origins.begin(), end = origins.end(); | 223 for (itr_type itr = origins.begin(), end = origins.end(); |
| 226 itr != end; ++itr) { | 224 itr != end; ++itr) { |
| 227 const char* kSql = | 225 const char* kSql = |
| 228 "INSERT OR IGNORE INTO OriginLastAccessTable" | 226 "INSERT OR IGNORE INTO OriginLastAccessTable" |
| 229 " (used_count, last_access_time, origin, type)" | 227 " (used_count, last_access_time, origin, type)" |
| 230 " VALUES (?, ?, ?, ?)"; | 228 " VALUES (?, ?, ?, ?)"; |
| 229 sql::Statement statement; | |
| 231 if (!PrepareCachedStatement(db_.get(), SQL_FROM_HERE, kSql, &statement)) | 230 if (!PrepareCachedStatement(db_.get(), SQL_FROM_HERE, kSql, &statement)) |
| 232 return false; | 231 return false; |
| 233 | 232 |
| 234 statement.BindInt(0, 0); // used_count | 233 statement.BindInt(0, 0); // used_count |
| 235 statement.BindInt64(1, last_access_time.ToInternalValue()); | 234 statement.BindInt64(1, last_access_time.ToInternalValue()); |
| 236 statement.BindString(2, itr->spec()); | 235 statement.BindString(2, itr->spec()); |
| 237 statement.BindInt(3, static_cast<int>(type)); | 236 statement.BindInt(3, static_cast<int>(type)); |
| 238 if (!statement.Run()) | 237 if (!statement.Run()) |
| 239 return false; | 238 return false; |
| 240 } | 239 } |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 return false; | 477 return false; |
| 479 | 478 |
| 480 // So we can't go recursive. | 479 // So we can't go recursive. |
| 481 if (is_recreating_) | 480 if (is_recreating_) |
| 482 return false; | 481 return false; |
| 483 | 482 |
| 484 AutoReset<bool> auto_reset(&is_recreating_, true); | 483 AutoReset<bool> auto_reset(&is_recreating_, true); |
| 485 return LazyOpen(true); | 484 return LazyOpen(true); |
| 486 } | 485 } |
| 487 | 486 |
| 487 bool QuotaDatabase::DumpQuotaTable(QuotaTableCallback* callback) { | |
| 488 if (!LazyOpen(true)) | |
| 489 return false; | |
| 490 | |
| 491 const char* kSql = "SELECT * FROM HostQuotaTable"; | |
| 492 sql::Statement statement; | |
| 493 if (!PrepareCachedStatement(db_.get(), SQL_FROM_HERE, kSql, &statement)) | |
| 494 return false; | |
| 495 | |
| 496 while (statement.Step()) { | |
| 497 QuotaTableEntry entry = { | |
| 498 statement.ColumnString(0), | |
| 499 static_cast<StorageType>(statement.ColumnInt(1)), | |
| 500 statement.ColumnInt64(2) | |
| 501 }; | |
| 502 | |
| 503 if (!callback->Run(entry)) | |
| 504 return true; | |
| 505 } | |
| 506 | |
| 507 return statement.Succeeded(); | |
| 508 } | |
| 509 | |
| 510 bool QuotaDatabase::DumpAccessTable(AccessTableCallback* callback) { | |
|
kinuko
2011/05/20 13:19:54
Can we rename s/AccessTable/LastAccessTimeTable/
tzik
2011/05/23 05:24:15
Done.
| |
| 511 if (!LazyOpen(true)) | |
| 512 return false; | |
| 513 | |
| 514 const char* kSql = "SELECT * FROM OriginLastAccessTable"; | |
| 515 sql::Statement statement; | |
| 516 if (!PrepareCachedStatement(db_.get(), SQL_FROM_HERE, kSql, &statement)) | |
| 517 return false; | |
| 518 | |
| 519 while (statement.Step()) { | |
| 520 AccessTableEntry entry = { | |
| 521 GURL(statement.ColumnString(0)), | |
| 522 static_cast<StorageType>(statement.ColumnInt(1)), | |
| 523 statement.ColumnInt(2), | |
| 524 base::Time::FromInternalValue(statement.ColumnInt64(3)) | |
| 525 }; | |
| 526 | |
| 527 if (!callback->Run(entry)) | |
| 528 return true; | |
| 529 } | |
| 530 | |
| 531 return statement.Succeeded(); | |
| 532 } | |
| 533 | |
| 534 bool QuotaDatabase::AssignQuotaTable( | |
| 535 const std::set<QuotaTableEntry>& entries) { | |
|
kinuko
2011/05/20 13:19:54
As we chatted locally, as long as it is labeled 'f
| |
| 536 if (!LazyOpen(true)) | |
| 537 return false; | |
| 538 | |
| 539 sql::Transaction transaction(db_.get()); | |
| 540 if (!transaction.Begin()) | |
| 541 return false; | |
| 542 | |
| 543 { | |
| 544 const char* kSql = "DELETE FROM HostQuotaTable"; | |
| 545 sql::Statement statement; | |
| 546 if (!PrepareCachedStatement(db_.get(), SQL_FROM_HERE, kSql, &statement)) | |
| 547 return false; | |
| 548 if (!statement.Run()) | |
| 549 return false; | |
| 550 } | |
| 551 | |
| 552 typedef std::set<QuotaTableEntry>::const_iterator itr_type; | |
| 553 for (itr_type itr = entries.begin(), end = entries.end(); | |
| 554 itr != end; ++itr) { | |
| 555 const char* kSql = | |
| 556 "INSERT INTO HostQuotaTable" | |
| 557 " (host, type, quota)" | |
| 558 " VALUES (?, ?, ?)"; | |
| 559 sql::Statement statement; | |
| 560 if (!PrepareCachedStatement(db_.get(), SQL_FROM_HERE, kSql, &statement)) | |
| 561 return false; | |
| 562 | |
| 563 statement.BindString(0, itr->host); | |
| 564 statement.BindInt(1, static_cast<int>(itr->type)); | |
| 565 statement.BindInt64(2, itr->quota); | |
| 566 if (!statement.Run()) | |
| 567 return false; | |
| 568 } | |
| 569 | |
| 570 return transaction.Commit(); | |
| 571 } | |
| 572 | |
| 573 bool QuotaDatabase::AssignAccessTable( | |
| 574 const std::set<AccessTableEntry>& entries) { | |
| 575 if (!LazyOpen(true)) | |
| 576 return false; | |
| 577 | |
| 578 sql::Transaction transaction(db_.get()); | |
| 579 if (!transaction.Begin()) | |
| 580 return false; | |
| 581 | |
| 582 { | |
| 583 const char* kSql = "DELETE FROM OriginLastAccessTable"; | |
| 584 sql::Statement statement; | |
| 585 if (!PrepareCachedStatement(db_.get(), SQL_FROM_HERE, kSql, &statement)) | |
| 586 return false; | |
| 587 if (!statement.Run()) | |
| 588 return false; | |
| 589 } | |
| 590 | |
| 591 typedef std::set<AccessTableEntry>::const_iterator itr_type; | |
| 592 for (itr_type itr = entries.begin(), end = entries.end(); | |
| 593 itr != end; ++itr) { | |
| 594 const char* kSql = | |
| 595 "INSERT INTO OriginLastAccessTable" | |
| 596 " (origin, type, used_count, last_access_time)" | |
| 597 " VALUES (?, ?, ?, ?)"; | |
| 598 sql::Statement statement; | |
| 599 if (!PrepareCachedStatement(db_.get(), SQL_FROM_HERE, kSql, &statement)) | |
| 600 return false; | |
| 601 | |
| 602 statement.BindString(0, itr->origin.spec()); | |
| 603 statement.BindInt(1, static_cast<int>(itr->type)); | |
| 604 statement.BindInt(2, itr->used_count); | |
| 605 statement.BindInt64(3, itr->last_access_time.ToInternalValue()); | |
| 606 if (!statement.Run()) | |
| 607 return false; | |
| 608 } | |
| 609 | |
| 610 return transaction.Commit(); | |
| 611 } | |
| 612 | |
| 613 | |
| 614 bool operator<(const QuotaDatabase::QuotaTableEntry& lhs, | |
| 615 const QuotaDatabase::QuotaTableEntry& rhs) { | |
| 616 if (lhs.host < rhs.host) return true; | |
| 617 if (rhs.host < lhs.host) return false; | |
| 618 if (lhs.type < rhs.type) return true; | |
| 619 if (rhs.type < lhs.type) return false; | |
| 620 return lhs.quota < rhs.quota; | |
| 621 } | |
| 622 | |
| 623 bool operator<(const QuotaDatabase::AccessTableEntry& lhs, | |
| 624 const QuotaDatabase::AccessTableEntry& rhs) { | |
| 625 if (lhs.origin < rhs.origin) return true; | |
| 626 if (rhs.origin < lhs.origin) return false; | |
| 627 if (lhs.type < rhs.type) return true; | |
| 628 if (rhs.type < lhs.type) return false; | |
| 629 if (lhs.used_count < rhs.used_count) return true; | |
| 630 if (rhs.used_count < lhs.used_count) return false; | |
| 631 return lhs.last_access_time < rhs.last_access_time; | |
| 632 } | |
| 633 | |
| 488 } // quota namespace | 634 } // quota namespace |
| OLD | NEW |