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 "chrome/browser/webdata/web_database_service.h" | 5 #include "chrome/browser/webdata/web_database_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "chrome/browser/api/webdata/web_data_results.h" | 10 #include "chrome/browser/api/webdata/web_data_results.h" |
11 #include "chrome/browser/api/webdata/web_data_service_consumer.h" | 11 #include "chrome/browser/api/webdata/web_data_service_consumer.h" |
12 #include "chrome/browser/webdata/web_data_request_manager.h" | 12 #include "chrome/browser/webdata/web_data_request_manager.h" |
13 | 13 |
14 using base::Bind; | 14 using base::Bind; |
15 using base::FilePath; | 15 using base::FilePath; |
16 using content::BrowserThread; | 16 using content::BrowserThread; |
17 | 17 |
18 | 18 |
19 //////////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////////// |
20 // | 20 // |
21 // WebDataServiceBackend implementation. | 21 // WebDataServiceBackend implementation. |
22 // | 22 // |
23 //////////////////////////////////////////////////////////////////////////////// | 23 //////////////////////////////////////////////////////////////////////////////// |
24 | 24 |
25 // Refcounted to allow asynchronous destruction on the DB thread. | 25 // Refcounted to allow asynchronous destruction on the DB thread. |
26 class WebDataServiceBackend | 26 class WebDataServiceBackend |
27 : public base::RefCountedThreadSafe<WebDataServiceBackend, | 27 : public base::RefCountedThreadSafe<WebDataServiceBackend, |
28 BrowserThread::DeleteOnDBThread> { | 28 BrowserThread::DeleteOnDBThread> { |
29 public: | 29 public: |
30 explicit WebDataServiceBackend( | 30 explicit WebDataServiceBackend(const FilePath& path); |
31 const FilePath& path, const std::string app_locale); | |
32 | 31 |
33 // Must call only before InitDatabaseWithCallback. | 32 // Must call only before InitDatabaseWithCallback. |
34 void AddTable(scoped_ptr<WebDatabaseTable> table); | 33 void AddTable(scoped_ptr<WebDatabaseTable> table); |
35 | 34 |
36 // Initializes the database and notifies caller via callback when complete. | 35 // Initializes the database and notifies caller via callback when complete. |
37 // Callback is called synchronously. | 36 // Callback is called synchronously. |
38 void InitDatabaseWithCallback( | 37 void InitDatabaseWithCallback( |
39 const WebDatabaseService::InitCallback& callback); | 38 const WebDatabaseService::InitCallback& callback); |
40 | 39 |
41 // Opens the database file from the profile path if an init has not yet been | 40 // Opens the database file from the profile path if an init has not yet been |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 scoped_refptr<WebDataRequestManager> request_manager_; | 89 scoped_refptr<WebDataRequestManager> request_manager_; |
91 | 90 |
92 // State of database initialization. Used to prevent the executing of tasks | 91 // State of database initialization. Used to prevent the executing of tasks |
93 // before the db is ready. | 92 // before the db is ready. |
94 sql::InitStatus init_status_; | 93 sql::InitStatus init_status_; |
95 | 94 |
96 // True if an attempt has been made to load the database (even if the attempt | 95 // True if an attempt has been made to load the database (even if the attempt |
97 // fails), used to avoid continually trying to reinit if the db init fails. | 96 // fails), used to avoid continually trying to reinit if the db init fails. |
98 bool init_complete_; | 97 bool init_complete_; |
99 | 98 |
100 // The application locale. The locale is needed for some database migrations, | |
101 // and must be read on the UI thread. It's cached here so that we can pass it | |
102 // to the migration code on the DB thread. | |
103 const std::string app_locale_; | |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(WebDataServiceBackend); | 99 DISALLOW_COPY_AND_ASSIGN(WebDataServiceBackend); |
106 }; | 100 }; |
107 | 101 |
108 WebDataServiceBackend::WebDataServiceBackend( | 102 WebDataServiceBackend::WebDataServiceBackend( |
109 const FilePath& path, | 103 const FilePath& path) |
110 const std::string app_locale) | |
111 : db_path_(path), | 104 : db_path_(path), |
112 request_manager_(new WebDataRequestManager()), | 105 request_manager_(new WebDataRequestManager()), |
113 init_status_(sql::INIT_FAILURE), | 106 init_status_(sql::INIT_FAILURE), |
114 init_complete_(false), | 107 init_complete_(false) { |
115 app_locale_(app_locale) { | |
116 } | 108 } |
117 | 109 |
118 void WebDataServiceBackend::AddTable(scoped_ptr<WebDatabaseTable> table) { | 110 void WebDataServiceBackend::AddTable(scoped_ptr<WebDatabaseTable> table) { |
119 DCHECK(!db_.get()); | 111 DCHECK(!db_.get()); |
120 tables_.push_back(table.release()); | 112 tables_.push_back(table.release()); |
121 } | 113 } |
122 | 114 |
123 void WebDataServiceBackend::InitDatabaseWithCallback( | 115 void WebDataServiceBackend::InitDatabaseWithCallback( |
124 const WebDatabaseService::InitCallback& callback) { | 116 const WebDatabaseService::InitCallback& callback) { |
125 if (!callback.is_null()) { | 117 if (!callback.is_null()) { |
126 callback.Run(LoadDatabaseIfNecessary()); | 118 callback.Run(LoadDatabaseIfNecessary()); |
127 } | 119 } |
128 } | 120 } |
129 | 121 |
130 sql::InitStatus WebDataServiceBackend::LoadDatabaseIfNecessary() { | 122 sql::InitStatus WebDataServiceBackend::LoadDatabaseIfNecessary() { |
131 if (init_complete_ || db_path_.empty()) { | 123 if (init_complete_ || db_path_.empty()) { |
132 return init_status_; | 124 return init_status_; |
133 } | 125 } |
134 init_complete_ = true; | 126 init_complete_ = true; |
135 db_.reset(new WebDatabase()); | 127 db_.reset(new WebDatabase()); |
136 | 128 |
137 for (ScopedVector<WebDatabaseTable>::iterator it = tables_.begin(); | 129 for (ScopedVector<WebDatabaseTable>::iterator it = tables_.begin(); |
138 it != tables_.end(); | 130 it != tables_.end(); |
139 ++it) { | 131 ++it) { |
140 db_->AddTable(*it); | 132 db_->AddTable(*it); |
141 } | 133 } |
142 | 134 |
143 init_status_ = db_->Init(db_path_, app_locale_); | 135 init_status_ = db_->Init(db_path_); |
144 if (init_status_ != sql::INIT_OK) { | 136 if (init_status_ != sql::INIT_OK) { |
145 LOG(ERROR) << "Cannot initialize the web database: " << init_status_; | 137 LOG(ERROR) << "Cannot initialize the web database: " << init_status_; |
146 db_.reset(NULL); | 138 db_.reset(NULL); |
147 return init_status_; | 139 return init_status_; |
148 } | 140 } |
149 | 141 |
150 db_->BeginTransaction(); | 142 db_->BeginTransaction(); |
151 return init_status_; | 143 return init_status_; |
152 } | 144 } |
153 | 145 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 if (db_ && init_status_ == sql::INIT_OK) { | 183 if (db_ && init_status_ == sql::INIT_OK) { |
192 db_->CommitTransaction(); | 184 db_->CommitTransaction(); |
193 db_->BeginTransaction(); | 185 db_->BeginTransaction(); |
194 } else { | 186 } else { |
195 NOTREACHED() << "Commit scheduled after Shutdown()"; | 187 NOTREACHED() << "Commit scheduled after Shutdown()"; |
196 } | 188 } |
197 } | 189 } |
198 | 190 |
199 //////////////////////////////////////////////////////////////////////////////// | 191 //////////////////////////////////////////////////////////////////////////////// |
200 WebDatabaseService::WebDatabaseService( | 192 WebDatabaseService::WebDatabaseService( |
201 const base::FilePath& path, | 193 const base::FilePath& path) |
202 const std::string app_locale) | 194 : path_(path) { |
203 : path_(path), | |
204 app_locale_(app_locale) { | |
205 // WebDatabaseService should be instantiated on UI thread. | 195 // WebDatabaseService should be instantiated on UI thread. |
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
207 // WebDatabaseService requires DB thread if instantiated. | 197 // WebDatabaseService requires DB thread if instantiated. |
208 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); | 198 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); |
209 } | 199 } |
210 | 200 |
211 WebDatabaseService::~WebDatabaseService() { | 201 WebDatabaseService::~WebDatabaseService() { |
212 } | 202 } |
213 | 203 |
214 void WebDatabaseService::AddTable(scoped_ptr<WebDatabaseTable> table) { | 204 void WebDatabaseService::AddTable(scoped_ptr<WebDatabaseTable> table) { |
215 if (!wds_backend_) { | 205 if (!wds_backend_) { |
216 wds_backend_ = new WebDataServiceBackend(path_, app_locale_); | 206 wds_backend_ = new WebDataServiceBackend(path_); |
217 } | 207 } |
218 wds_backend_->AddTable(table.Pass()); | 208 wds_backend_->AddTable(table.Pass()); |
219 } | 209 } |
220 | 210 |
221 void WebDatabaseService::LoadDatabase(const InitCallback& callback) { | 211 void WebDatabaseService::LoadDatabase(const InitCallback& callback) { |
222 DCHECK(wds_backend_); | 212 DCHECK(wds_backend_); |
223 | 213 |
224 BrowserThread::PostTask( | 214 BrowserThread::PostTask( |
225 BrowserThread::DB, | 215 BrowserThread::DB, |
226 FROM_HERE, | 216 FROM_HERE, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 task, base::Passed(&request))); | 278 task, base::Passed(&request))); |
289 | 279 |
290 return handle; | 280 return handle; |
291 } | 281 } |
292 | 282 |
293 void WebDatabaseService::CancelRequest(WebDataServiceBase::Handle h) { | 283 void WebDatabaseService::CancelRequest(WebDataServiceBase::Handle h) { |
294 if (!wds_backend_) | 284 if (!wds_backend_) |
295 return; | 285 return; |
296 wds_backend_->request_manager()->CancelRequest(h); | 286 wds_backend_->request_manager()->CancelRequest(h); |
297 } | 287 } |
OLD | NEW |