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), |
29 should_ignore_db_errors_(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 since we own |db_|. | |
56 db_->set_error_callback(base::Bind(&WebDatabaseBackend::DatabaseErrorCallback, | |
57 base::Unretained(this))); | |
58 db_diagnostics_.clear(); | |
54 init_status_ = db_->Init(db_path_); | 59 init_status_ = db_->Init(db_path_); |
55 if (init_status_ != sql::INIT_OK) { | 60 if (init_status_ != sql::INIT_OK) { |
56 LOG(ERROR) << "Cannot initialize the web database: " << init_status_; | 61 LOG(ERROR) << "Cannot initialize the web database: " << init_status_; |
57 db_.reset(NULL); | 62 db_diagnostics_ += sql::GetCorruptFileDiagnosticsInfo(db_path_); |
Peter Kasting
2016/08/05 00:20:44
We don't need to add a newline or anything in here
afakhry
2016/08/05 16:13:22
No, we don't.
| |
63 db_.reset(); | |
58 return init_status_; | 64 return init_status_; |
59 } | 65 } |
60 | 66 |
61 db_->BeginTransaction(); | 67 db_->BeginTransaction(); |
62 return init_status_; | 68 return init_status_; |
63 } | 69 } |
64 | 70 |
65 void WebDatabaseBackend::ShutdownDatabase() { | 71 void WebDatabaseBackend::ShutdownDatabase() { |
66 if (db_ && init_status_ == sql::INIT_OK) | 72 if (db_ && init_status_ == sql::INIT_OK) |
67 db_->CommitTransaction(); | 73 db_->CommitTransaction(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 if (db_ && init_status_ == sql::INIT_OK) { | 112 if (db_ && init_status_ == sql::INIT_OK) { |
107 return task.Run(db_.get()); | 113 return task.Run(db_.get()); |
108 } | 114 } |
109 return nullptr; | 115 return nullptr; |
110 } | 116 } |
111 | 117 |
112 WebDatabaseBackend::~WebDatabaseBackend() { | 118 WebDatabaseBackend::~WebDatabaseBackend() { |
113 ShutdownDatabase(); | 119 ShutdownDatabase(); |
114 } | 120 } |
115 | 121 |
122 void WebDatabaseBackend::DatabaseErrorCallback(int error, | |
123 sql::Statement* stmt) { | |
124 // We ignore any further error callbacks on the first catastrophic error. | |
Peter Kasting
2016/08/05 00:20:44
Nit: on -> after?
afakhry
2016/08/05 16:13:22
Done.
| |
125 if (!should_ignore_db_errors_ && sql::IsErrorCatastrophic(error)) { | |
126 should_ignore_db_errors_ = true; | |
127 db_diagnostics_ = db_->GetDiagnosticInfo(error, stmt); | |
128 } | |
129 } | |
130 | |
116 void WebDatabaseBackend::Commit() { | 131 void WebDatabaseBackend::Commit() { |
117 DCHECK(db_); | 132 DCHECK(db_); |
118 DCHECK_EQ(sql::INIT_OK, init_status_); | 133 DCHECK_EQ(sql::INIT_OK, init_status_); |
119 db_->CommitTransaction(); | 134 db_->CommitTransaction(); |
120 db_->BeginTransaction(); | 135 db_->BeginTransaction(); |
121 } | 136 } |
OLD | NEW |