| 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" |
| 12 #include "base/strings/stringprintf.h" |
| 11 #include "components/webdata/common/web_data_request_manager.h" | 13 #include "components/webdata/common/web_data_request_manager.h" |
| 12 #include "components/webdata/common/web_database.h" | 14 #include "components/webdata/common/web_database.h" |
| 13 #include "components/webdata/common/web_database_table.h" | 15 #include "components/webdata/common/web_database_table.h" |
| 16 #include "sql/error_delegate_util.h" |
| 14 | 17 |
| 15 using base::Bind; | 18 using base::Bind; |
| 16 using base::FilePath; | 19 using base::FilePath; |
| 17 | 20 |
| 18 WebDatabaseBackend::WebDatabaseBackend( | 21 WebDatabaseBackend::WebDatabaseBackend( |
| 19 const FilePath& path, | 22 const FilePath& path, |
| 20 Delegate* delegate, | 23 Delegate* delegate, |
| 21 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread) | 24 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread) |
| 22 : base::RefCountedDeleteOnMessageLoop<WebDatabaseBackend>(db_thread), | 25 : base::RefCountedDeleteOnMessageLoop<WebDatabaseBackend>(db_thread), |
| 23 db_path_(path), | 26 db_path_(path), |
| 24 request_manager_(new WebDataRequestManager()), | 27 request_manager_(new WebDataRequestManager()), |
| 25 init_status_(sql::INIT_FAILURE), | 28 init_status_(sql::INIT_FAILURE), |
| 26 init_complete_(false), | 29 init_complete_(false), |
| 27 delegate_(delegate) { | 30 delegate_(delegate) { |
| 28 } | 31 } |
| 29 | 32 |
| 30 void WebDatabaseBackend::AddTable(std::unique_ptr<WebDatabaseTable> table) { | 33 void WebDatabaseBackend::AddTable(std::unique_ptr<WebDatabaseTable> table) { |
| 31 DCHECK(!db_.get()); | 34 DCHECK(!db_.get()); |
| 32 tables_.push_back(table.release()); | 35 tables_.push_back(table.release()); |
| 33 } | 36 } |
| 34 | 37 |
| 35 void WebDatabaseBackend::InitDatabase() { | 38 void WebDatabaseBackend::InitDatabase() { |
| 36 LoadDatabaseIfNecessary(); | 39 LoadDatabaseIfNecessary(); |
| 37 if (delegate_) { | 40 if (delegate_) { |
| 38 delegate_->DBLoaded(init_status_); | 41 delegate_->DBLoaded(init_status_, db_diagnostics_); |
| 39 } | 42 } |
| 40 } | 43 } |
| 41 | 44 |
| 42 sql::InitStatus WebDatabaseBackend::LoadDatabaseIfNecessary() { | 45 sql::InitStatus WebDatabaseBackend::LoadDatabaseIfNecessary() { |
| 43 if (init_complete_ || db_path_.empty()) { | 46 if (init_complete_ || db_path_.empty()) { |
| 44 return init_status_; | 47 return init_status_; |
| 45 } | 48 } |
| 46 init_complete_ = true; | 49 init_complete_ = true; |
| 47 db_.reset(new WebDatabase()); | 50 db_.reset(new WebDatabase()); |
| 48 | 51 |
| 49 for (ScopedVector<WebDatabaseTable>::iterator it = tables_.begin(); | 52 for (const auto& table : tables_) |
| 50 it != tables_.end(); ++it) { | 53 db_->AddTable(table); |
| 51 db_->AddTable(*it); | |
| 52 } | |
| 53 | 54 |
| 55 // Unretained to avoid a ref loop with db_. |
| 56 db_->set_error_callback(base::Bind(&WebDatabaseBackend::DatabaseErrorCallback, |
| 57 base::Unretained(this))); |
| 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_; |
| 61 const std::string corrupted_file_name = |
| 62 db_path_.DirName().BaseName().AsUTF8Unsafe() + "/" + |
| 63 db_path_.BaseName().AsUTF8Unsafe(); |
| 64 base::StringAppendF(&db_diagnostics_, "Corrupted file: %s\n", |
| 65 corrupted_file_name.c_str()); |
| 57 db_.reset(NULL); | 66 db_.reset(NULL); |
| 58 return init_status_; | 67 return init_status_; |
| 59 } | 68 } |
| 60 | 69 |
| 61 db_->BeginTransaction(); | 70 db_->BeginTransaction(); |
| 62 return init_status_; | 71 return init_status_; |
| 63 } | 72 } |
| 64 | 73 |
| 65 void WebDatabaseBackend::ShutdownDatabase() { | 74 void WebDatabaseBackend::ShutdownDatabase() { |
| 66 if (db_ && init_status_ == sql::INIT_OK) | 75 if (db_ && init_status_ == sql::INIT_OK) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 if (db_ && init_status_ == sql::INIT_OK) { | 115 if (db_ && init_status_ == sql::INIT_OK) { |
| 107 return task.Run(db_.get()); | 116 return task.Run(db_.get()); |
| 108 } | 117 } |
| 109 return nullptr; | 118 return nullptr; |
| 110 } | 119 } |
| 111 | 120 |
| 112 WebDatabaseBackend::~WebDatabaseBackend() { | 121 WebDatabaseBackend::~WebDatabaseBackend() { |
| 113 ShutdownDatabase(); | 122 ShutdownDatabase(); |
| 114 } | 123 } |
| 115 | 124 |
| 125 void WebDatabaseBackend::DatabaseErrorCallback(int error, |
| 126 sql::Statement* stmt) { |
| 127 // We ignore any further error callbacks on the first catastrophic error. |
| 128 static bool should_ignore_errors = false; |
| 129 if (!should_ignore_errors && sql::IsErrorCatastrophic(error)) { |
| 130 should_ignore_errors = true; |
| 131 db_diagnostics_ = db_->GetDiagnosticInfo(error, stmt); |
| 132 } |
| 133 } |
| 134 |
| 116 void WebDatabaseBackend::Commit() { | 135 void WebDatabaseBackend::Commit() { |
| 117 DCHECK(db_); | 136 DCHECK(db_); |
| 118 DCHECK_EQ(sql::INIT_OK, init_status_); | 137 DCHECK_EQ(sql::INIT_OK, init_status_); |
| 119 db_->CommitTransaction(); | 138 db_->CommitTransaction(); |
| 120 db_->BeginTransaction(); | 139 db_->BeginTransaction(); |
| 121 } | 140 } |
| OLD | NEW |