| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/webdata/common/web_database_backend.h" | 5 #include "components/webdata/common/web_database_backend.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/location.h" | 11 #include "base/location.h" |
| 11 #include "components/webdata/common/web_data_request_manager.h" | 12 #include "components/webdata/common/web_data_request_manager.h" |
| 12 #include "components/webdata/common/web_database.h" | 13 #include "components/webdata/common/web_database.h" |
| 13 #include "components/webdata/common/web_database_table.h" | 14 #include "components/webdata/common/web_database_table.h" |
| 15 #include "sql/error_delegate_util.h" |
| 14 | 16 |
| 15 using base::Bind; | 17 using base::Bind; |
| 16 using base::FilePath; | 18 using base::FilePath; |
| 17 | 19 |
| 18 WebDatabaseBackend::WebDatabaseBackend( | 20 WebDatabaseBackend::WebDatabaseBackend( |
| 19 const FilePath& path, | 21 const FilePath& path, |
| 20 Delegate* delegate, | 22 Delegate* delegate, |
| 21 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread) | 23 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread) |
| 22 : base::RefCountedDeleteOnMessageLoop<WebDatabaseBackend>(db_thread), | 24 : base::RefCountedDeleteOnMessageLoop<WebDatabaseBackend>(db_thread), |
| 23 db_path_(path), | 25 db_path_(path), |
| 24 request_manager_(new WebDataRequestManager()), | 26 request_manager_(new WebDataRequestManager()), |
| 25 init_status_(sql::INIT_FAILURE), | 27 init_status_(sql::INIT_FAILURE), |
| 26 init_complete_(false), | 28 init_complete_(false), |
| 27 delegate_(delegate) { | 29 catastrophic_error_occurred_(false), |
| 28 } | 30 delegate_(delegate) {} |
| 29 | 31 |
| 30 void WebDatabaseBackend::AddTable(std::unique_ptr<WebDatabaseTable> table) { | 32 void WebDatabaseBackend::AddTable(std::unique_ptr<WebDatabaseTable> table) { |
| 31 DCHECK(!db_.get()); | 33 DCHECK(!db_.get()); |
| 32 tables_.push_back(table.release()); | 34 tables_.push_back(table.release()); |
| 33 } | 35 } |
| 34 | 36 |
| 35 void WebDatabaseBackend::InitDatabase() { | 37 void WebDatabaseBackend::InitDatabase() { |
| 36 LoadDatabaseIfNecessary(); | 38 LoadDatabaseIfNecessary(); |
| 37 if (delegate_) { | 39 if (delegate_) { |
| 38 delegate_->DBLoaded(init_status_); | 40 delegate_->DBLoaded(init_status_, diagnostics_); |
| 39 } | 41 } |
| 40 } | 42 } |
| 41 | 43 |
| 42 sql::InitStatus WebDatabaseBackend::LoadDatabaseIfNecessary() { | 44 sql::InitStatus WebDatabaseBackend::LoadDatabaseIfNecessary() { |
| 43 if (init_complete_ || db_path_.empty()) { | 45 if (init_complete_ || db_path_.empty()) { |
| 44 return init_status_; | 46 return init_status_; |
| 45 } | 47 } |
| 46 init_complete_ = true; | 48 init_complete_ = true; |
| 47 db_.reset(new WebDatabase()); | 49 db_.reset(new WebDatabase()); |
| 48 | 50 |
| 49 for (ScopedVector<WebDatabaseTable>::iterator it = tables_.begin(); | 51 for (const auto& table : tables_) |
| 50 it != tables_.end(); ++it) { | 52 db_->AddTable(table); |
| 51 db_->AddTable(*it); | |
| 52 } | |
| 53 | 53 |
| 54 // Unretained to avoid a ref loop since we own |db_|. |
| 55 db_->set_error_callback(base::Bind(&WebDatabaseBackend::DatabaseErrorCallback, |
| 56 base::Unretained(this))); |
| 57 diagnostics_.clear(); |
| 54 init_status_ = db_->Init(db_path_); | 58 init_status_ = db_->Init(db_path_); |
| 55 if (init_status_ != sql::INIT_OK) { | 59 if (init_status_ != sql::INIT_OK) { |
| 56 LOG(ERROR) << "Cannot initialize the web database: " << init_status_; | 60 LOG(ERROR) << "Cannot initialize the web database: " << init_status_; |
| 57 db_.reset(NULL); | 61 diagnostics_ += sql::GetCorruptFileDiagnosticsInfo(db_path_); |
| 62 db_.reset(); |
| 58 return init_status_; | 63 return init_status_; |
| 59 } | 64 } |
| 60 | 65 |
| 61 db_->BeginTransaction(); | 66 db_->BeginTransaction(); |
| 62 return init_status_; | 67 return init_status_; |
| 63 } | 68 } |
| 64 | 69 |
| 65 void WebDatabaseBackend::ShutdownDatabase() { | 70 void WebDatabaseBackend::ShutdownDatabase() { |
| 66 if (db_ && init_status_ == sql::INIT_OK) | 71 if (db_ && init_status_ == sql::INIT_OK) |
| 67 db_->CommitTransaction(); | 72 db_->CommitTransaction(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 if (db_ && init_status_ == sql::INIT_OK) { | 111 if (db_ && init_status_ == sql::INIT_OK) { |
| 107 return task.Run(db_.get()); | 112 return task.Run(db_.get()); |
| 108 } | 113 } |
| 109 return nullptr; | 114 return nullptr; |
| 110 } | 115 } |
| 111 | 116 |
| 112 WebDatabaseBackend::~WebDatabaseBackend() { | 117 WebDatabaseBackend::~WebDatabaseBackend() { |
| 113 ShutdownDatabase(); | 118 ShutdownDatabase(); |
| 114 } | 119 } |
| 115 | 120 |
| 121 void WebDatabaseBackend::DatabaseErrorCallback(int error, |
| 122 sql::Statement* statement) { |
| 123 // We ignore any further error callbacks after the first catastrophic error. |
| 124 if (!catastrophic_error_occurred_ && sql::IsErrorCatastrophic(error)) { |
| 125 catastrophic_error_occurred_ = true; |
| 126 diagnostics_ = db_->GetDiagnosticInfo(error, statement); |
| 127 } |
| 128 } |
| 129 |
| 116 void WebDatabaseBackend::Commit() { | 130 void WebDatabaseBackend::Commit() { |
| 117 DCHECK(db_); | 131 DCHECK(db_); |
| 118 DCHECK_EQ(sql::INIT_OK, init_status_); | 132 DCHECK_EQ(sql::INIT_OK, init_status_); |
| 119 db_->CommitTransaction(); | 133 db_->CommitTransaction(); |
| 120 db_->BeginTransaction(); | 134 db_->BeginTransaction(); |
| 121 } | 135 } |
| OLD | NEW |