| 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 } |
| 65 known_to_be_empty_ = result->empty(); |
| 59 } | 66 } |
| 60 | 67 |
| 61 bool DomStorageDatabase::CommitChanges(bool clear_all_first, | 68 bool DomStorageDatabase::CommitChanges(bool clear_all_first, |
| 62 const ValuesMap& changes) { | 69 const ValuesMap& changes) { |
| 63 if (!LazyOpen(!changes.empty())) | 70 if (!LazyOpen(!changes.empty())) |
| 64 return false; | 71 return false; |
| 65 | 72 |
| 73 bool old_known_to_be_empty = known_to_be_empty_; |
| 66 sql::Transaction transaction(db_.get()); | 74 sql::Transaction transaction(db_.get()); |
| 67 if (!transaction.Begin()) | 75 if (!transaction.Begin()) |
| 68 return false; | 76 return false; |
| 69 | 77 |
| 70 if (clear_all_first) { | 78 if (clear_all_first) { |
| 71 if (!db_->Execute("DELETE FROM ItemTable")) | 79 if (!db_->Execute("DELETE FROM ItemTable")) |
| 72 return false; | 80 return false; |
| 81 known_to_be_empty_ = true; |
| 73 } | 82 } |
| 74 | 83 |
| 84 bool did_delete = false; |
| 85 bool did_insert = false; |
| 75 ValuesMap::const_iterator it = changes.begin(); | 86 ValuesMap::const_iterator it = changes.begin(); |
| 76 for(; it != changes.end(); ++it) { | 87 for(; it != changes.end(); ++it) { |
| 77 sql::Statement statement; | 88 sql::Statement statement; |
| 78 string16 key = it->first; | 89 string16 key = it->first; |
| 79 NullableString16 value = it->second; | 90 NullableString16 value = it->second; |
| 80 if (value.is_null()) { | 91 if (value.is_null()) { |
| 81 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, | 92 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, |
| 82 "DELETE FROM ItemTable WHERE key=?")); | 93 "DELETE FROM ItemTable WHERE key=?")); |
| 83 statement.BindString16(0, key); | 94 statement.BindString16(0, key); |
| 95 did_delete = true; |
| 84 } else { | 96 } else { |
| 85 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, | 97 statement.Assign(db_->GetCachedStatement(SQL_FROM_HERE, |
| 86 "INSERT INTO ItemTable VALUES (?,?)")); | 98 "INSERT INTO ItemTable VALUES (?,?)")); |
| 87 statement.BindString16(0, key); | 99 statement.BindString16(0, key); |
| 88 statement.BindBlob(1, value.string().data(), | 100 statement.BindBlob(1, value.string().data(), |
| 89 value.string().length() * sizeof(char16)); | 101 value.string().length() * sizeof(char16)); |
| 102 known_to_be_empty_ = false; |
| 103 did_insert = true; |
| 90 } | 104 } |
| 91 DCHECK(statement.is_valid()); | 105 DCHECK(statement.is_valid()); |
| 92 statement.Run(); | 106 statement.Run(); |
| 93 } | 107 } |
| 94 return transaction.Commit(); | 108 |
| 109 if (!known_to_be_empty_ && did_delete && !did_insert) { |
| 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; |
| 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 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 CreateTableV2() && | 276 CreateTableV2() && |
| 252 CommitChanges(false, values) && | 277 CommitChanges(false, values) && |
| 253 migration.Commit(); | 278 migration.Commit(); |
| 254 } | 279 } |
| 255 | 280 |
| 256 void DomStorageDatabase::Close() { | 281 void DomStorageDatabase::Close() { |
| 257 db_.reset(NULL); | 282 db_.reset(NULL); |
| 258 } | 283 } |
| 259 | 284 |
| 260 } // namespace dom_storage | 285 } // namespace dom_storage |
| OLD | NEW |