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 "chrome/browser/api/webdata/web_data_results.h" | 10 #include "chrome/browser/api/webdata/web_data_results.h" |
10 #include "chrome/browser/api/webdata/web_data_service_consumer.h" | 11 #include "chrome/browser/api/webdata/web_data_service_consumer.h" |
11 #include "chrome/browser/webdata/autofill_table.h" | |
12 #include "chrome/browser/webdata/keyword_table.h" | |
13 #include "chrome/browser/webdata/logins_table.h" | |
14 #include "chrome/browser/webdata/token_service_table.h" | |
15 #include "chrome/browser/webdata/web_apps_table.h" | |
16 #include "chrome/browser/webdata/web_data_request_manager.h" | 12 #include "chrome/browser/webdata/web_data_request_manager.h" |
17 #include "chrome/browser/webdata/web_data_service.h" | 13 #include "chrome/browser/webdata/web_data_service.h" |
18 #include "chrome/browser/webdata/web_intents_table.h" | |
19 // TODO(caitkp): Remove this autofill dependency. | 14 // TODO(caitkp): Remove this autofill dependency. |
20 #include "components/autofill/browser/autofill_country.h" | 15 #include "components/autofill/browser/autofill_country.h" |
21 | 16 |
22 using base::Bind; | 17 using base::Bind; |
23 using base::FilePath; | 18 using base::FilePath; |
24 using content::BrowserThread; | 19 using content::BrowserThread; |
25 | 20 |
26 | 21 |
27 //////////////////////////////////////////////////////////////////////////////// | 22 //////////////////////////////////////////////////////////////////////////////// |
28 // | 23 // |
29 // WebDataServiceBackend implementation. | 24 // WebDataServiceBackend implementation. |
30 // | 25 // |
31 //////////////////////////////////////////////////////////////////////////////// | 26 //////////////////////////////////////////////////////////////////////////////// |
32 | 27 |
33 // Refcounted to allow asynchronous destruction on the DB thread. | 28 // Refcounted to allow asynchronous destruction on the DB thread. |
34 class WebDataServiceBackend | 29 class WebDataServiceBackend |
35 : public base::RefCountedThreadSafe<WebDataServiceBackend, | 30 : public base::RefCountedThreadSafe<WebDataServiceBackend, |
36 BrowserThread::DeleteOnDBThread> { | 31 BrowserThread::DeleteOnDBThread> { |
37 public: | 32 public: |
38 explicit WebDataServiceBackend(const FilePath& path); | 33 explicit WebDataServiceBackend(const FilePath& path); |
39 | 34 |
| 35 // Must call only before InitDatabaseWithCallback. |
| 36 void AddTable(scoped_ptr<WebDatabaseTable> table); |
| 37 |
40 // Initializes the database and notifies caller via callback when complete. | 38 // Initializes the database and notifies caller via callback when complete. |
41 // Callback is called synchronously. | 39 // Callback is called synchronously. |
42 void InitDatabaseWithCallback( | 40 void InitDatabaseWithCallback( |
43 const WebDatabaseService::InitCallback& callback); | 41 const WebDatabaseService::InitCallback& callback); |
44 | 42 |
45 // Opens the database file from the profile path if an init has not yet been | 43 // Opens the database file from the profile path if an init has not yet been |
46 // attempted. Separated from the constructor to ease construction/destruction | 44 // attempted. Separated from the constructor to ease construction/destruction |
47 // of this object on one thread but database access on the DB thread. Returns | 45 // of this object on one thread but database access on the DB thread. Returns |
48 // the status of the database. | 46 // the status of the database. |
49 sql::InitStatus LoadDatabaseIfNecessary(); | 47 sql::InitStatus LoadDatabaseIfNecessary(); |
(...skipping 21 matching lines...) Expand all Loading... |
71 friend class base::DeleteHelper<WebDataServiceBackend>; | 69 friend class base::DeleteHelper<WebDataServiceBackend>; |
72 | 70 |
73 virtual ~WebDataServiceBackend(); | 71 virtual ~WebDataServiceBackend(); |
74 | 72 |
75 // Commit the current transaction. | 73 // Commit the current transaction. |
76 void Commit(); | 74 void Commit(); |
77 | 75 |
78 // Path to database file. | 76 // Path to database file. |
79 FilePath db_path_; | 77 FilePath db_path_; |
80 | 78 |
81 scoped_ptr<AutofillTable> autofill_table_; | 79 // The tables that participate in managing the database. These are |
82 scoped_ptr<KeywordTable> keyword_table_; | 80 // owned here but other than that this class does nothing with |
83 scoped_ptr<LoginsTable> logins_table_; | 81 // them. Their initialization is in whatever factory creates |
84 scoped_ptr<TokenServiceTable> token_service_table_; | 82 // WebDatabaseService, and lookup by type is provided by the |
85 scoped_ptr<WebAppsTable> web_apps_table_; | 83 // WebDatabase class. The tables need to be owned by this refcounted |
86 scoped_ptr<WebIntentsTable> web_intents_table_; | 84 // object, or they themselves would need to be refcounted. Owning |
| 85 // them here rather than having WebDatabase own them makes for |
| 86 // easier unit testing of WebDatabase. |
| 87 ScopedVector<WebDatabaseTable> tables_; |
87 | 88 |
88 scoped_ptr<WebDatabase> db_; | 89 scoped_ptr<WebDatabase> db_; |
89 | 90 |
90 // Keeps track of all pending requests made to the db. | 91 // Keeps track of all pending requests made to the db. |
91 scoped_refptr<WebDataRequestManager> request_manager_; | 92 scoped_refptr<WebDataRequestManager> request_manager_; |
92 | 93 |
93 // State of database initialization. Used to prevent the executing of tasks | 94 // State of database initialization. Used to prevent the executing of tasks |
94 // before the db is ready. | 95 // before the db is ready. |
95 sql::InitStatus init_status_; | 96 sql::InitStatus init_status_; |
96 | 97 |
(...skipping 10 matching lines...) Expand all Loading... |
107 }; | 108 }; |
108 | 109 |
109 WebDataServiceBackend::WebDataServiceBackend(const FilePath& path) | 110 WebDataServiceBackend::WebDataServiceBackend(const FilePath& path) |
110 : db_path_(path), | 111 : db_path_(path), |
111 request_manager_(new WebDataRequestManager()), | 112 request_manager_(new WebDataRequestManager()), |
112 init_status_(sql::INIT_FAILURE), | 113 init_status_(sql::INIT_FAILURE), |
113 init_complete_(false), | 114 init_complete_(false), |
114 app_locale_(AutofillCountry::ApplicationLocale()) { | 115 app_locale_(AutofillCountry::ApplicationLocale()) { |
115 } | 116 } |
116 | 117 |
| 118 void WebDataServiceBackend::AddTable(scoped_ptr<WebDatabaseTable> table) { |
| 119 DCHECK(!db_.get()); |
| 120 tables_.push_back(table.release()); |
| 121 } |
| 122 |
117 void WebDataServiceBackend::InitDatabaseWithCallback( | 123 void WebDataServiceBackend::InitDatabaseWithCallback( |
118 const WebDatabaseService::InitCallback& callback) { | 124 const WebDatabaseService::InitCallback& callback) { |
119 if (!callback.is_null()) { | 125 if (!callback.is_null()) { |
120 callback.Run(LoadDatabaseIfNecessary()); | 126 callback.Run(LoadDatabaseIfNecessary()); |
121 } | 127 } |
122 } | 128 } |
123 | 129 |
124 sql::InitStatus WebDataServiceBackend::LoadDatabaseIfNecessary() { | 130 sql::InitStatus WebDataServiceBackend::LoadDatabaseIfNecessary() { |
125 if (init_complete_ || db_path_.empty()) { | 131 if (init_complete_ || db_path_.empty()) { |
126 return init_status_; | 132 return init_status_; |
127 } | 133 } |
128 init_complete_ = true; | 134 init_complete_ = true; |
129 db_.reset(new WebDatabase()); | 135 db_.reset(new WebDatabase()); |
130 | 136 |
131 // All tables objects that participate in managing the database must | 137 for (ScopedVector<WebDatabaseTable>::iterator it = tables_.begin(); |
132 // be added here. | 138 it != tables_.end(); |
133 autofill_table_.reset(new AutofillTable()); | 139 ++it) { |
134 db_->AddTable(autofill_table_.get()); | 140 db_->AddTable(*it); |
135 | 141 } |
136 keyword_table_.reset(new KeywordTable()); | |
137 db_->AddTable(keyword_table_.get()); | |
138 | |
139 // TODO(mdm): We only really need the LoginsTable on Windows for IE7 password | |
140 // access, but for now, we still create it on all platforms since it deletes | |
141 // the old logins table. We can remove this after a while, e.g. in M22 or so. | |
142 logins_table_.reset(new LoginsTable()); | |
143 db_->AddTable(logins_table_.get()); | |
144 | |
145 token_service_table_.reset(new TokenServiceTable()); | |
146 db_->AddTable(token_service_table_.get()); | |
147 | |
148 web_apps_table_.reset(new WebAppsTable()); | |
149 db_->AddTable(web_apps_table_.get()); | |
150 | |
151 // TODO(thakis): Add a migration to delete the SQL table used by | |
152 // WebIntentsTable, then remove this. | |
153 web_intents_table_.reset(new WebIntentsTable()); | |
154 db_->AddTable(web_intents_table_.get()); | |
155 | 142 |
156 init_status_ = db_->Init(db_path_, app_locale_); | 143 init_status_ = db_->Init(db_path_, app_locale_); |
157 if (init_status_ != sql::INIT_OK) { | 144 if (init_status_ != sql::INIT_OK) { |
158 LOG(ERROR) << "Cannot initialize the web database: " << init_status_; | 145 LOG(ERROR) << "Cannot initialize the web database: " << init_status_; |
159 db_.reset(NULL); | 146 db_.reset(NULL); |
160 return init_status_; | 147 return init_status_; |
161 } | 148 } |
162 | 149 |
163 db_->BeginTransaction(); | 150 db_->BeginTransaction(); |
164 return init_status_; | 151 return init_status_; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 : path_(path) { | 201 : path_(path) { |
215 // WebDatabaseService should be instantiated on UI thread. | 202 // WebDatabaseService should be instantiated on UI thread. |
216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
217 // WebDatabaseService requires DB thread if instantiated. | 204 // WebDatabaseService requires DB thread if instantiated. |
218 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); | 205 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); |
219 } | 206 } |
220 | 207 |
221 WebDatabaseService::~WebDatabaseService() { | 208 WebDatabaseService::~WebDatabaseService() { |
222 } | 209 } |
223 | 210 |
| 211 void WebDatabaseService::AddTable(scoped_ptr<WebDatabaseTable> table) { |
| 212 if (!wds_backend_) { |
| 213 wds_backend_ = new WebDataServiceBackend(path_); |
| 214 } |
| 215 wds_backend_->AddTable(table.Pass()); |
| 216 } |
| 217 |
224 void WebDatabaseService::LoadDatabase(const InitCallback& callback) { | 218 void WebDatabaseService::LoadDatabase(const InitCallback& callback) { |
225 if (!wds_backend_) | 219 DCHECK(wds_backend_); |
226 wds_backend_ = new WebDataServiceBackend(path_); | |
227 | 220 |
228 BrowserThread::PostTask( | 221 BrowserThread::PostTask( |
229 BrowserThread::DB, | 222 BrowserThread::DB, |
230 FROM_HERE, | 223 FROM_HERE, |
231 Bind(&WebDataServiceBackend::InitDatabaseWithCallback, | 224 Bind(&WebDataServiceBackend::InitDatabaseWithCallback, |
232 wds_backend_, callback)); | 225 wds_backend_, callback)); |
233 } | 226 } |
234 | 227 |
235 void WebDatabaseService::UnloadDatabase() { | 228 void WebDatabaseService::UnloadDatabase() { |
236 if (!wds_backend_) | 229 if (!wds_backend_) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 task, base::Passed(&request))); | 285 task, base::Passed(&request))); |
293 | 286 |
294 return handle; | 287 return handle; |
295 } | 288 } |
296 | 289 |
297 void WebDatabaseService::CancelRequest(WebDataServiceBase::Handle h) { | 290 void WebDatabaseService::CancelRequest(WebDataServiceBase::Handle h) { |
298 if (!wds_backend_) | 291 if (!wds_backend_) |
299 return; | 292 return; |
300 wds_backend_->request_manager()->CancelRequest(h); | 293 wds_backend_->request_manager()->CancelRequest(h); |
301 } | 294 } |
OLD | NEW |