| 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" |
| 11 #include "sql/transaction.h" | 11 #include "sql/transaction.h" |
| 12 #include "third_party/sqlite/sqlite3.h" | 12 #include "third_party/sqlite/sqlite3.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 const FilePath::CharType kJournal[] = FILE_PATH_LITERAL("-journal"); |
| 17 |
| 16 class HistogramUniquifier { | 18 class HistogramUniquifier { |
| 17 public: | 19 public: |
| 18 static const char* name() { return "Sqlite.DomStorageDatabase.Error"; } | 20 static const char* name() { return "Sqlite.DomStorageDatabase.Error"; } |
| 19 }; | 21 }; |
| 20 | 22 |
| 21 sql::ErrorDelegate* GetErrorHandlerForDomStorageDatabase() { | 23 sql::ErrorDelegate* GetErrorHandlerForDomStorageDatabase() { |
| 22 return new sql::DiagnosticErrorDelegate<HistogramUniquifier>(); | 24 return new sql::DiagnosticErrorDelegate<HistogramUniquifier>(); |
| 23 } | 25 } |
| 24 | 26 |
| 25 } // anon namespace | 27 } // anon namespace |
| 26 | 28 |
| 27 namespace dom_storage { | 29 namespace dom_storage { |
| 28 | 30 |
| 31 // static |
| 32 FilePath DomStorageDatabase::GetJournalFilePath( |
| 33 const FilePath& database_path) { |
| 34 FilePath::StringType journal_file_name = |
| 35 database_path.BaseName().value() + kJournal; |
| 36 return database_path.DirName().Append(journal_file_name); |
| 37 } |
| 38 |
| 29 DomStorageDatabase::DomStorageDatabase(const FilePath& file_path) | 39 DomStorageDatabase::DomStorageDatabase(const FilePath& file_path) |
| 30 : file_path_(file_path) { | 40 : file_path_(file_path) { |
| 31 // Note: in normal use we should never get an empty backing path here. | 41 // Note: in normal use we should never get an empty backing path here. |
| 32 // However, the unit test for this class defines another constructor | 42 // However, the unit test for this class can contruct an instance |
| 33 // that will bypass this check to allow an empty path that signifies | 43 // with an empty path. |
| 34 // we should operate on an in-memory database for performance/reliability | |
| 35 // reasons. | |
| 36 DCHECK(!file_path_.empty()); | |
| 37 Init(); | 44 Init(); |
| 38 } | 45 } |
| 39 | 46 |
| 40 DomStorageDatabase::DomStorageDatabase() { | 47 DomStorageDatabase::DomStorageDatabase() { |
| 41 Init(); | 48 Init(); |
| 42 } | 49 } |
| 43 | 50 |
| 44 void DomStorageDatabase::Init() { | 51 void DomStorageDatabase::Init() { |
| 45 failed_to_open_ = false; | 52 failed_to_open_ = false; |
| 46 tried_to_recreate_ = false; | 53 tried_to_recreate_ = false; |
| 47 known_to_be_empty_ = false; | 54 known_to_be_empty_ = false; |
| 48 } | 55 } |
| 49 | 56 |
| 50 DomStorageDatabase::~DomStorageDatabase() { | 57 DomStorageDatabase::~DomStorageDatabase() { |
| 51 if (known_to_be_empty_ && !file_path_.empty()) { | 58 if (known_to_be_empty_ && !file_path_.empty()) { |
| 52 // Delete the db from disk, it's empty. | 59 // Delete the db and any lingering journal file from disk. |
| 53 Close(); | 60 Close(); |
| 54 file_util::Delete(file_path_, false); | 61 file_util::Delete(file_path_, false); |
| 62 file_util::Delete(GetJournalFilePath(file_path_), false); |
| 55 } | 63 } |
| 56 } | 64 } |
| 57 | 65 |
| 58 void DomStorageDatabase::ReadAllValues(ValuesMap* result) { | 66 void DomStorageDatabase::ReadAllValues(ValuesMap* result) { |
| 59 if (!LazyOpen(false)) | 67 if (!LazyOpen(false)) |
| 60 return; | 68 return; |
| 61 | 69 |
| 62 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, | 70 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, |
| 63 "SELECT * from ItemTable")); | 71 "SELECT * from ItemTable")); |
| 64 DCHECK(statement.is_valid()); | 72 DCHECK(statement.is_valid()); |
| 65 | 73 |
| 66 while (statement.Step()) { | 74 while (statement.Step()) { |
| 67 string16 key = statement.ColumnString16(0); | 75 string16 key = statement.ColumnString16(0); |
| 68 string16 value; | 76 string16 value; |
| 69 statement.ColumnBlobAsString16(1, &value); | 77 statement.ColumnBlobAsString16(1, &value); |
| 70 (*result)[key] = NullableString16(value, false); | 78 (*result)[key] = NullableString16(value, false); |
| 71 } | 79 } |
| 72 known_to_be_empty_ = result->empty(); | 80 known_to_be_empty_ = result->empty(); |
| 73 } | 81 } |
| 74 | 82 |
| 75 bool DomStorageDatabase::CommitChanges(bool clear_all_first, | 83 bool DomStorageDatabase::CommitChanges(bool clear_all_first, |
| 76 const ValuesMap& changes) { | 84 const ValuesMap& changes) { |
| 77 if (!LazyOpen(!changes.empty())) | 85 if (!LazyOpen(!changes.empty())) { |
| 78 return false; | 86 // If we're being asked to commit changes that will result in an |
| 87 // empty database, we return true if the database file doesn't exist. |
| 88 return clear_all_first && changes.empty() && |
| 89 !file_util::PathExists(file_path_); |
| 90 } |
| 79 | 91 |
| 80 bool old_known_to_be_empty = known_to_be_empty_; | 92 bool old_known_to_be_empty = known_to_be_empty_; |
| 81 sql::Transaction transaction(db_.get()); | 93 sql::Transaction transaction(db_.get()); |
| 82 if (!transaction.Begin()) | 94 if (!transaction.Begin()) |
| 83 return false; | 95 return false; |
| 84 | 96 |
| 85 if (clear_all_first) { | 97 if (clear_all_first) { |
| 86 if (!db_->Execute("DELETE FROM ItemTable")) | 98 if (!db_->Execute("DELETE FROM ItemTable")) |
| 87 return false; | 99 return false; |
| 88 known_to_be_empty_ = true; | 100 known_to_be_empty_ = true; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 CreateTableV2() && | 295 CreateTableV2() && |
| 284 CommitChanges(false, values) && | 296 CommitChanges(false, values) && |
| 285 migration.Commit(); | 297 migration.Commit(); |
| 286 } | 298 } |
| 287 | 299 |
| 288 void DomStorageDatabase::Close() { | 300 void DomStorageDatabase::Close() { |
| 289 db_.reset(NULL); | 301 db_.reset(NULL); |
| 290 } | 302 } |
| 291 | 303 |
| 292 } // namespace dom_storage | 304 } // namespace dom_storage |
| OLD | NEW |