Chromium Code Reviews| Index: webkit/dom_storage/dom_storage_database.cc |
| diff --git a/webkit/dom_storage/dom_storage_database.cc b/webkit/dom_storage/dom_storage_database.cc |
| index 673ddde5380433e9b6d246b2799765cbef82a548..10d2ad08e7a157843e58d5bfd48da3d76595bfca 100644 |
| --- a/webkit/dom_storage/dom_storage_database.cc |
| +++ b/webkit/dom_storage/dom_storage_database.cc |
| @@ -30,7 +30,8 @@ DomStorageDatabase::DomStorageDatabase(const FilePath& file_path) |
| : file_path_(file_path), |
| db_(NULL), |
| failed_to_open_(false), |
| - tried_to_recreate_(false) { |
| + tried_to_recreate_(false), |
| + values_stored_(0) { |
| // Note: in normal use we should never get an empty backing path here. |
| // However, the unit test for this class defines another constructor |
| // that will bypass this check to allow an empty path that signifies |
| @@ -40,6 +41,11 @@ DomStorageDatabase::DomStorageDatabase(const FilePath& file_path) |
| } |
| DomStorageDatabase::~DomStorageDatabase() { |
| + if (IsOpen() && values_stored_ == 0) { |
| + // Delete the db from disk, it's empty. |
| + Close(); |
| + file_util::Delete(file_path_, false); |
|
michaeln
2012/02/25 02:55:55
maybe skip this if file_path.empty()?
if (known_t
benm (inactive)
2012/02/27 12:06:25
Done.
|
| + } |
| } |
| void DomStorageDatabase::ReadAllValues(ValuesMap* result) { |
| @@ -70,6 +76,7 @@ bool DomStorageDatabase::CommitChanges(bool clear_all_first, |
| if (clear_all_first) { |
| if (!db_->Execute("DELETE FROM ItemTable")) |
| return false; |
| + values_stored_ = 0; |
| } |
| ValuesMap::const_iterator it = changes.begin(); |
| @@ -81,12 +88,14 @@ bool DomStorageDatabase::CommitChanges(bool clear_all_first, |
| statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, |
| "DELETE FROM ItemTable WHERE key=?")); |
| statement.BindString16(0, key); |
| + values_stored_--; |
| } else { |
| statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, |
| "INSERT INTO ItemTable VALUES (?,?)")); |
| statement.BindString16(0, key); |
| statement.BindBlob(1, value.string().data(), |
| value.string().length() * sizeof(char16)); |
| + values_stored_++; |
|
michaeln
2012/02/25 02:55:55
This count will not be accurate, consider this is
benm (inactive)
2012/02/27 12:06:25
Good catch, i made the test a bit more robust.
|
| } |
| DCHECK(statement.is_valid()); |
| statement.Run(); |
| @@ -149,18 +158,28 @@ bool DomStorageDatabase::LazyOpen(bool create_if_needed) { |
| // and whether it's usable (i.e. not corrupted). |
| SchemaVersion current_version = DetectSchemaVersion(); |
| + bool valid_database = false; |
| if (current_version == V2) { |
| - return true; |
| + valid_database = true; |
| } else if (current_version == V1) { |
| if (UpgradeVersion1To2()) |
| - return true; |
| + valid_database = true; |
| } |
| + |
| + if (!valid_database) { |
| + // This is the exceptional case - to try and recover we'll attempt |
| + // to delete the file and start again. |
| + Close(); |
| + return DeleteFileAndRecreate(); |
| + } |
| + |
| + sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, |
| + "SELECT count(key) from ItemTable")); |
| + if (statement.Step()) |
| + values_stored_ = statement.ColumnInt(0); |
| } |
| - // This is the exceptional case - to try and recover we'll attempt |
| - // to delete the file and start again. |
| - Close(); |
| - return DeleteFileAndRecreate(); |
| + return true; |
| } |
| DomStorageDatabase::SchemaVersion DomStorageDatabase::DetectSchemaVersion() { |