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