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_. | |
sky
2016/08/02 13:27:21
The interesting part to document is why unretained
afakhry
2016/08/02 23:58:24
Done.
| |
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_; |
62 const std::string corrupted_file_name = | |
sky
2016/08/02 13:27:21
Same comment about using shared function here.
afakhry
2016/08/02 23:58:24
Done.
| |
63 db_path_.DirName().BaseName().AsUTF8Unsafe() + "/" + | |
64 db_path_.BaseName().AsUTF8Unsafe(); | |
65 base::StringAppendF(&db_diagnostics_, "Corrupted file: %s\n", | |
66 corrupted_file_name.c_str()); | |
57 db_.reset(NULL); | 67 db_.reset(NULL); |
58 return init_status_; | 68 return init_status_; |
59 } | 69 } |
60 | 70 |
61 db_->BeginTransaction(); | 71 db_->BeginTransaction(); |
62 return init_status_; | 72 return init_status_; |
63 } | 73 } |
64 | 74 |
65 void WebDatabaseBackend::ShutdownDatabase() { | 75 void WebDatabaseBackend::ShutdownDatabase() { |
66 if (db_ && init_status_ == sql::INIT_OK) | 76 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) { | 116 if (db_ && init_status_ == sql::INIT_OK) { |
107 return task.Run(db_.get()); | 117 return task.Run(db_.get()); |
108 } | 118 } |
109 return nullptr; | 119 return nullptr; |
110 } | 120 } |
111 | 121 |
112 WebDatabaseBackend::~WebDatabaseBackend() { | 122 WebDatabaseBackend::~WebDatabaseBackend() { |
113 ShutdownDatabase(); | 123 ShutdownDatabase(); |
114 } | 124 } |
115 | 125 |
126 void WebDatabaseBackend::DatabaseErrorCallback(int error, | |
127 sql::Statement* stmt) { | |
128 // We ignore any further error callbacks on the first catastrophic error. | |
129 static bool should_ignore_errors = false; | |
sky
2016/08/02 13:27:21
Why is this static and not a member variable? I wo
afakhry
2016/08/02 23:58:24
Hmm, you're right. Thanks! Done.
| |
130 if (!should_ignore_errors && sql::IsErrorCatastrophic(error)) { | |
131 should_ignore_errors = true; | |
132 db_diagnostics_ = db_->GetDiagnosticInfo(error, stmt); | |
133 } | |
134 } | |
135 | |
116 void WebDatabaseBackend::Commit() { | 136 void WebDatabaseBackend::Commit() { |
117 DCHECK(db_); | 137 DCHECK(db_); |
118 DCHECK_EQ(sql::INIT_OK, init_status_); | 138 DCHECK_EQ(sql::INIT_OK, init_status_); |
119 db_->CommitTransaction(); | 139 db_->CommitTransaction(); |
120 db_->BeginTransaction(); | 140 db_->BeginTransaction(); |
121 } | 141 } |
OLD | NEW |