| 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 defines another constructor |
| 33 // that will bypass this check to allow an empty path that signifies | 43 // that will bypass this check to allow an empty path that signifies |
| 34 // we should operate on an in-memory database for performance/reliability | 44 // we should operate on an in-memory database for performance/reliability |
| 35 // reasons. | 45 // reasons. |
| 36 DCHECK(!file_path_.empty()); | 46 DCHECK(!file_path_.empty()); |
| 37 Init(); | 47 Init(); |
| 38 } | 48 } |
| 39 | 49 |
| 40 DomStorageDatabase::DomStorageDatabase() { | 50 DomStorageDatabase::DomStorageDatabase() { |
| 41 Init(); | 51 Init(); |
| 42 } | 52 } |
| 43 | 53 |
| 44 void DomStorageDatabase::Init() { | 54 void DomStorageDatabase::Init() { |
| 45 failed_to_open_ = false; | 55 failed_to_open_ = false; |
| 46 tried_to_recreate_ = false; | 56 tried_to_recreate_ = false; |
| 47 known_to_be_empty_ = false; | 57 known_to_be_empty_ = false; |
| 48 } | 58 } |
| 49 | 59 |
| 50 DomStorageDatabase::~DomStorageDatabase() { | 60 DomStorageDatabase::~DomStorageDatabase() { |
| 51 if (known_to_be_empty_ && !file_path_.empty()) { | 61 if (known_to_be_empty_ && !file_path_.empty()) { |
| 52 // Delete the db from disk, it's empty. | 62 // Delete the db and any lingering journal file from disk. |
| 53 Close(); | 63 Close(); |
| 54 file_util::Delete(file_path_, false); | 64 file_util::Delete(file_path_, false); |
| 65 file_util::Delete(GetJournalFilePath(file_path_), false); |
| 55 } | 66 } |
| 56 } | 67 } |
| 57 | 68 |
| 58 void DomStorageDatabase::ReadAllValues(ValuesMap* result) { | 69 void DomStorageDatabase::ReadAllValues(ValuesMap* result) { |
| 59 if (!LazyOpen(false)) | 70 if (!LazyOpen(false)) |
| 60 return; | 71 return; |
| 61 | 72 |
| 62 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, | 73 sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, |
| 63 "SELECT * from ItemTable")); | 74 "SELECT * from ItemTable")); |
| 64 DCHECK(statement.is_valid()); | 75 DCHECK(statement.is_valid()); |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 CreateTableV2() && | 294 CreateTableV2() && |
| 284 CommitChanges(false, values) && | 295 CommitChanges(false, values) && |
| 285 migration.Commit(); | 296 migration.Commit(); |
| 286 } | 297 } |
| 287 | 298 |
| 288 void DomStorageDatabase::Close() { | 299 void DomStorageDatabase::Close() { |
| 289 db_.reset(NULL); | 300 db_.reset(NULL); |
| 290 } | 301 } |
| 291 | 302 |
| 292 } // namespace dom_storage | 303 } // namespace dom_storage |
| OLD | NEW |