OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/webdata/common/web_data_service_backend.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "components/webdata/common/web_data_request_manager.h" |
| 10 #include "components/webdata/common/web_database.h" |
| 11 #include "components/webdata/common/web_database_table.h" |
| 12 |
| 13 |
| 14 using base::Bind; |
| 15 using base::FilePath; |
| 16 using content::BrowserThread; |
| 17 |
| 18 WebDataServiceBackend::WebDataServiceBackend( |
| 19 const FilePath& path, Delegate* delegate) |
| 20 : db_path_(path), |
| 21 request_manager_(new WebDataRequestManager()), |
| 22 init_status_(sql::INIT_FAILURE), |
| 23 init_complete_(false), |
| 24 delegate_(delegate) { |
| 25 } |
| 26 |
| 27 void WebDataServiceBackend::AddTable(scoped_ptr<WebDatabaseTable> table) { |
| 28 DCHECK(!db_.get()); |
| 29 tables_.push_back(table.release()); |
| 30 } |
| 31 |
| 32 void WebDataServiceBackend::InitDatabase() { |
| 33 LoadDatabaseIfNecessary(); |
| 34 if (delegate_) { |
| 35 delegate_->DBLoaded(init_status_); |
| 36 } |
| 37 } |
| 38 |
| 39 sql::InitStatus WebDataServiceBackend::LoadDatabaseIfNecessary() { |
| 40 if (init_complete_ || db_path_.empty()) { |
| 41 return init_status_; |
| 42 } |
| 43 init_complete_ = true; |
| 44 db_.reset(new WebDatabase()); |
| 45 |
| 46 for (ScopedVector<WebDatabaseTable>::iterator it = tables_.begin(); |
| 47 it != tables_.end(); |
| 48 ++it) { |
| 49 db_->AddTable(*it); |
| 50 } |
| 51 |
| 52 init_status_ = db_->Init(db_path_); |
| 53 if (init_status_ != sql::INIT_OK) { |
| 54 LOG(ERROR) << "Cannot initialize the web database: " << init_status_; |
| 55 db_.reset(NULL); |
| 56 return init_status_; |
| 57 } |
| 58 |
| 59 db_->BeginTransaction(); |
| 60 return init_status_; |
| 61 } |
| 62 |
| 63 void WebDataServiceBackend::ShutdownDatabase(bool should_reinit) { |
| 64 if (db_ && init_status_ == sql::INIT_OK) |
| 65 db_->CommitTransaction(); |
| 66 db_.reset(NULL); |
| 67 init_complete_ = !should_reinit; // Setting init_complete_ to true will ensure |
| 68 // that the init sequence is not re-run. |
| 69 |
| 70 init_status_ = sql::INIT_FAILURE; |
| 71 } |
| 72 |
| 73 void WebDataServiceBackend::DBWriteTaskWrapper( |
| 74 const WebDatabaseService::WriteTask& task, |
| 75 scoped_ptr<WebDataRequest> request) { |
| 76 LoadDatabaseIfNecessary(); |
| 77 if (db_ && init_status_ == sql::INIT_OK && !request->IsCancelled()) { |
| 78 WebDatabase::State state = task.Run(db_.get()); |
| 79 if (state == WebDatabase::COMMIT_NEEDED) |
| 80 Commit(); |
| 81 } |
| 82 request_manager_->RequestCompleted(request.Pass()); |
| 83 } |
| 84 |
| 85 void WebDataServiceBackend::DBReadTaskWrapper( |
| 86 const WebDatabaseService::ReadTask& task, |
| 87 scoped_ptr<WebDataRequest> request) { |
| 88 LoadDatabaseIfNecessary(); |
| 89 if (db_ && init_status_ == sql::INIT_OK && !request->IsCancelled()) { |
| 90 request->SetResult(task.Run(db_.get()).Pass()); |
| 91 } |
| 92 request_manager_->RequestCompleted(request.Pass()); |
| 93 } |
| 94 |
| 95 WebDataServiceBackend::~WebDataServiceBackend() { |
| 96 ShutdownDatabase(false); |
| 97 } |
| 98 |
| 99 void WebDataServiceBackend::Commit() { |
| 100 if (db_ && init_status_ == sql::INIT_OK) { |
| 101 db_->CommitTransaction(); |
| 102 db_->BeginTransaction(); |
| 103 } else { |
| 104 NOTREACHED() << "Commit scheduled after Shutdown()"; |
| 105 } |
| 106 } |
OLD | NEW |