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 "webkit/dom_storage/dom_storage_database.h" | 5 #include "webkit/dom_storage/dom_storage_database.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "sql/diagnostic_error_delegate.h" | 9 #include "sql/diagnostic_error_delegate.h" |
| 10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 } | 23 } |
| 24 | 24 |
| 25 } // anon namespace | 25 } // anon namespace |
| 26 | 26 |
| 27 namespace dom_storage { | 27 namespace dom_storage { |
| 28 | 28 |
| 29 DomStorageDatabase::DomStorageDatabase(const FilePath& file_path) | 29 DomStorageDatabase::DomStorageDatabase(const FilePath& file_path) |
| 30 : file_path_(file_path), | 30 : file_path_(file_path), |
| 31 db_(NULL), | 31 db_(NULL), |
| 32 failed_to_open_(false), | 32 failed_to_open_(false), |
| 33 tried_to_recreate_(false) { | 33 tried_to_recreate_(false), |
| 34 known_to_be_empty_(false) { | |
| 34 // Note: in normal use we should never get an empty backing path here. | 35 // Note: in normal use we should never get an empty backing path here. |
| 35 // However, the unit test for this class defines another constructor | 36 // However, the unit test for this class defines another constructor |
| 36 // that will bypass this check to allow an empty path that signifies | 37 // that will bypass this check to allow an empty path that signifies |
| 37 // we should operate on an in-memory database for performance/reliability | 38 // we should operate on an in-memory database for performance/reliability |
| 38 // reasons. | 39 // reasons. |
| 39 DCHECK(!file_path_.empty()); | 40 DCHECK(!file_path_.empty()); |
| 40 } | 41 } |
| 41 | 42 |
| 42 DomStorageDatabase::~DomStorageDatabase() { | 43 DomStorageDatabase::~DomStorageDatabase() { |
| 44 if (known_to_be_empty_ && !file_path_.empty()) { | |
| 45 // Delete the db from disk, it's empty. | |
| 46 Close(); | |
| 47 file_util::Delete(file_path_, false); | |
| 48 } | |
| 43 } | 49 } |
| 44 | 50 |
| 45 void DomStorageDatabase::ReadAllValues(ValuesMap* result) { | 51 void DomStorageDatabase::ReadAllValues(ValuesMap* result) { |
| 46 if (!LazyOpen(false)) | 52 if (!LazyOpen(false)) |
| 47 return; | 53 return; |
| 48 | 54 |
| 49 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, | 55 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, |
| 50 "SELECT * from ItemTable")); | 56 "SELECT * from ItemTable")); |
| 51 DCHECK(statement.is_valid()); | 57 DCHECK(statement.is_valid()); |
| 52 | 58 |
| 53 while (statement.Step()) { | 59 while (statement.Step()) { |
| 54 string16 key = statement.ColumnString16(0); | 60 string16 key = statement.ColumnString16(0); |
| 55 string16 value; | 61 string16 value; |
| 56 statement.ColumnBlobAsString16(1, &value); | 62 statement.ColumnBlobAsString16(1, &value); |
| 57 (*result)[key] = NullableString16(value, false); | 63 (*result)[key] = NullableString16(value, false); |
| 58 } | 64 } |
|
michaeln
2012/02/27 19:28:48
i think we should set known_to_be_empty_ here
sin
benm (inactive)
2012/02/27 19:36:15
Got it.
| |
| 59 } | 65 } |
| 60 | 66 |
| 61 bool DomStorageDatabase::CommitChanges(bool clear_all_first, | 67 bool DomStorageDatabase::CommitChanges(bool clear_all_first, |
| 62 const ValuesMap& changes) { | 68 const ValuesMap& changes) { |
| 63 if (!LazyOpen(!changes.empty())) | 69 if (!LazyOpen(!changes.empty())) |
| 64 return false; | 70 return false; |
| 65 | 71 |
| 72 bool old_known_to_be_empty = known_to_be_empty_; | |
| 66 sql::Transaction transaction(db_.get()); | 73 sql::Transaction transaction(db_.get()); |
| 67 if (!transaction.Begin()) | 74 if (!transaction.Begin()) |
| 68 return false; | 75 return false; |
| 69 | 76 |
| 70 if (clear_all_first) { | 77 if (clear_all_first) { |
| 71 if (!db_->Execute("DELETE FROM ItemTable")) | 78 if (!db_->Execute("DELETE FROM ItemTable")) |
| 72 return false; | 79 return false; |
| 80 known_to_be_empty_ = true; | |
| 73 } | 81 } |
| 74 | 82 |
| 83 bool did_delete = false; | |
| 84 bool did_insert = false; | |
| 75 ValuesMap::const_iterator it = changes.begin(); | 85 ValuesMap::const_iterator it = changes.begin(); |
| 76 for(; it != changes.end(); ++it) { | 86 for(; it != changes.end(); ++it) { |
| 77 sql::Statement statement; | 87 sql::Statement statement; |
| 78 string16 key = it->first; | 88 string16 key = it->first; |
| 79 NullableString16 value = it->second; | 89 NullableString16 value = it->second; |
| 80 if (value.is_null()) { | 90 if (value.is_null()) { |
| 81 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, | 91 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, |
| 82 "DELETE FROM ItemTable WHERE key=?")); | 92 "DELETE FROM ItemTable WHERE key=?")); |
| 83 statement.BindString16(0, key); | 93 statement.BindString16(0, key); |
| 94 did_delete = true; | |
| 84 } else { | 95 } else { |
| 85 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, | 96 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, |
| 86 "INSERT INTO ItemTable VALUES (?,?)")); | 97 "INSERT INTO ItemTable VALUES (?,?)")); |
| 87 statement.BindString16(0, key); | 98 statement.BindString16(0, key); |
| 88 statement.BindBlob(1, value.string().data(), | 99 statement.BindBlob(1, value.string().data(), |
| 89 value.string().length() * sizeof(char16)); | 100 value.string().length() * sizeof(char16)); |
| 101 known_to_be_empty_ = false; | |
| 102 did_insert = true; | |
| 90 } | 103 } |
| 91 DCHECK(statement.is_valid()); | 104 DCHECK(statement.is_valid()); |
| 92 statement.Run(); | 105 statement.Run(); |
| 93 } | 106 } |
| 94 return transaction.Commit(); | 107 |
| 108 if (!known_to_be_empty_ && did_delete && !did_insert) { | |
| 109 // See if the database is empty. | |
|
michaeln
2012/02/27 19:28:48
consider removing the comment, the code is clear e
benm (inactive)
2012/02/27 19:36:15
Done.
| |
| 110 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, | |
| 111 "SELECT count(key) from ItemTable")); | |
| 112 if (statement.Step()) | |
| 113 known_to_be_empty_ = statement.ColumnInt(0) == 0; | |
|
michaeln
2012/02/27 19:28:48
if you think it's more readable, consider...
know
benm (inactive)
2012/02/27 19:36:15
I think I prefer the if () then approach like it i
| |
| 114 } | |
| 115 | |
| 116 bool success = transaction.Commit(); | |
| 117 if (!success) | |
| 118 known_to_be_empty_ = old_known_to_be_empty; | |
| 119 return success; | |
| 95 } | 120 } |
| 96 | 121 |
| 97 bool DomStorageDatabase::LazyOpen(bool create_if_needed) { | 122 bool DomStorageDatabase::LazyOpen(bool create_if_needed) { |
| 98 if (failed_to_open_) { | 123 if (failed_to_open_) { |
| 99 // Don't try to open a database that we know has failed | 124 // Don't try to open a database that we know has failed |
| 100 // already. | 125 // already. |
| 101 return false; | 126 return false; |
| 102 } | 127 } |
| 103 | 128 |
| 104 if (IsOpen()) | 129 if (IsOpen()) |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 | 167 |
| 143 if (!database_exists) { | 168 if (!database_exists) { |
| 144 // This is a new database, create the table and we're done! | 169 // This is a new database, create the table and we're done! |
| 145 if (CreateTableV2()) | 170 if (CreateTableV2()) |
| 146 return true; | 171 return true; |
| 147 } else { | 172 } else { |
| 148 // The database exists already - check if we need to upgrade | 173 // The database exists already - check if we need to upgrade |
| 149 // and whether it's usable (i.e. not corrupted). | 174 // and whether it's usable (i.e. not corrupted). |
| 150 SchemaVersion current_version = DetectSchemaVersion(); | 175 SchemaVersion current_version = DetectSchemaVersion(); |
| 151 | 176 |
| 177 bool valid_database = false; | |
| 152 if (current_version == V2) { | 178 if (current_version == V2) { |
| 153 return true; | 179 valid_database = true; |
| 154 } else if (current_version == V1) { | 180 } else if (current_version == V1) { |
| 155 if (UpgradeVersion1To2()) | 181 if (UpgradeVersion1To2()) |
| 156 return true; | 182 valid_database = true; |
| 183 } | |
| 184 | |
| 185 if (!valid_database) { | |
| 186 // Try and recover by attempting to delete the file and start again. | |
| 187 Close(); | |
| 188 return DeleteFileAndRecreate(); | |
| 157 } | 189 } |
| 158 } | 190 } |
| 159 | 191 |
| 160 // This is the exceptional case - to try and recover we'll attempt | 192 return true; |
|
michaeln
2012/02/27 19:28:48
looks like this introduces a bug in the 'new datab
benm (inactive)
2012/02/27 19:36:15
Good spot.
| |
| 161 // to delete the file and start again. | |
| 162 Close(); | |
| 163 return DeleteFileAndRecreate(); | |
| 164 } | 193 } |
| 165 | 194 |
| 166 DomStorageDatabase::SchemaVersion DomStorageDatabase::DetectSchemaVersion() { | 195 DomStorageDatabase::SchemaVersion DomStorageDatabase::DetectSchemaVersion() { |
| 167 DCHECK(IsOpen()); | 196 DCHECK(IsOpen()); |
| 168 | 197 |
| 169 // Connection::Open() may succeed even if the file we try and open is not a | 198 // Connection::Open() may succeed even if the file we try and open is not a |
| 170 // database, however in the case that the database is corrupted to the point | 199 // database, however in the case that the database is corrupted to the point |
| 171 // that SQLite doesn't actually think it's a database, | 200 // that SQLite doesn't actually think it's a database, |
| 172 // sql::Connection::GetCachedStatement will DCHECK when we later try and | 201 // sql::Connection::GetCachedStatement will DCHECK when we later try and |
| 173 // run statements. So we run a query here that will not DCHECK but fail | 202 // run statements. So we run a query here that will not DCHECK but fail |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 CreateTableV2() && | 280 CreateTableV2() && |
| 252 CommitChanges(false, values) && | 281 CommitChanges(false, values) && |
| 253 migration.Commit(); | 282 migration.Commit(); |
| 254 } | 283 } |
| 255 | 284 |
| 256 void DomStorageDatabase::Close() { | 285 void DomStorageDatabase::Close() { |
| 257 db_.reset(NULL); | 286 db_.reset(NULL); |
| 258 } | 287 } |
| 259 | 288 |
| 260 } // namespace dom_storage | 289 } // namespace dom_storage |
| OLD | NEW |