OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/net/sqlite_server_bound_cert_store.h" | 5 #include "chrome/browser/net/sqlite_server_bound_cert_store.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 : public base::RefCountedThreadSafe<SQLiteServerBoundCertStore::Backend> { | 34 : public base::RefCountedThreadSafe<SQLiteServerBoundCertStore::Backend> { |
35 public: | 35 public: |
36 Backend(const FilePath& path, ClearOnExitPolicy* clear_on_exit_policy) | 36 Backend(const FilePath& path, ClearOnExitPolicy* clear_on_exit_policy) |
37 : path_(path), | 37 : path_(path), |
38 db_(NULL), | 38 db_(NULL), |
39 num_pending_(0), | 39 num_pending_(0), |
40 force_keep_session_state_(false), | 40 force_keep_session_state_(false), |
41 clear_on_exit_policy_(clear_on_exit_policy) { | 41 clear_on_exit_policy_(clear_on_exit_policy) { |
42 } | 42 } |
43 | 43 |
44 // Creates or load the SQLite database. | 44 // Creates or loads the SQLite database. |
45 bool Load( | 45 void Load(const LoadedCallback& loaded_callback); |
46 std::vector<net::DefaultServerBoundCertStore::ServerBoundCert*>* certs); | |
47 | 46 |
48 // Batch a server bound cert addition. | 47 // Batch a server bound cert addition. |
49 void AddServerBoundCert( | 48 void AddServerBoundCert( |
50 const net::DefaultServerBoundCertStore::ServerBoundCert& cert); | 49 const net::DefaultServerBoundCertStore::ServerBoundCert& cert); |
51 | 50 |
52 // Batch a server bound cert deletion. | 51 // Batch a server bound cert deletion. |
53 void DeleteServerBoundCert( | 52 void DeleteServerBoundCert( |
54 const net::DefaultServerBoundCertStore::ServerBoundCert& cert); | 53 const net::DefaultServerBoundCertStore::ServerBoundCert& cert); |
55 | 54 |
56 // Commit pending operations as soon as possible. | 55 // Commit pending operations as soon as possible. |
57 void Flush(const base::Closure& completion_task); | 56 void Flush(const base::Closure& completion_task); |
58 | 57 |
59 // Commit any pending operations and close the database. This must be called | 58 // Commit any pending operations and close the database. This must be called |
60 // before the object is destructed. | 59 // before the object is destructed. |
61 void Close(); | 60 void Close(); |
62 | 61 |
63 void SetForceKeepSessionState(); | 62 void SetForceKeepSessionState(); |
64 | 63 |
65 private: | 64 private: |
65 bool LoadOnDBThread( | |
66 std::vector<net::DefaultServerBoundCertStore::ServerBoundCert*>* certs); | |
67 | |
66 friend class base::RefCountedThreadSafe<SQLiteServerBoundCertStore::Backend>; | 68 friend class base::RefCountedThreadSafe<SQLiteServerBoundCertStore::Backend>; |
67 | 69 |
68 // You should call Close() before destructing this object. | 70 // You should call Close() before destructing this object. |
69 ~Backend() { | 71 ~Backend() { |
70 DCHECK(!db_.get()) << "Close should have already been called."; | 72 DCHECK(!db_.get()) << "Close should have already been called."; |
71 DCHECK(num_pending_ == 0 && pending_.empty()); | 73 DCHECK(num_pending_ == 0 && pending_.empty()); |
72 } | 74 } |
73 | 75 |
74 // Database upgrade statements. | 76 // Database upgrade statements. |
75 bool EnsureDatabaseVersion(); | 77 bool EnsureDatabaseVersion(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 "expiration_time INTEGER," | 150 "expiration_time INTEGER," |
149 "creation_time INTEGER)")) | 151 "creation_time INTEGER)")) |
150 return false; | 152 return false; |
151 } | 153 } |
152 | 154 |
153 return true; | 155 return true; |
154 } | 156 } |
155 | 157 |
156 } // namespace | 158 } // namespace |
157 | 159 |
158 bool SQLiteServerBoundCertStore::Backend::Load( | 160 void SQLiteServerBoundCertStore::Backend::Load( |
159 std::vector<net::DefaultServerBoundCertStore::ServerBoundCert*>* certs) { | 161 const LoadedCallback& loaded_callback) { |
160 // This function should be called only once per instance. | 162 // This function should be called only once per instance. |
161 DCHECK(!db_.get()); | 163 DCHECK(!db_.get()); |
162 | 164 |
163 // TODO(paivanof@gmail.com): We do a lot of disk access in this function, | 165 scoped_ptr<ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert> > |
164 // thus we do an exception to allow IO on the UI thread. This code will be | 166 certs(new ScopedVector<net::DefaultServerBoundCertStore::ServerBoundCert>( |
165 // moved to the DB thread as part of http://crbug.com/89665. | 167 )); |
166 base::ThreadRestrictions::ScopedAllowIO allow_io; | 168 BrowserThread::PostTaskAndReply( |
169 BrowserThread::DB, FROM_HERE, | |
170 base::Bind(base::IgnoreResult(&Backend::LoadOnDBThread), this, | |
171 &certs->get()), | |
172 base::Bind(loaded_callback, base::Passed(&certs))); | |
173 } | |
174 | |
175 bool SQLiteServerBoundCertStore::Backend::LoadOnDBThread( | |
erikwright (departed)
2013/01/04 19:20:10
why keep the bool exit code?
Also, consider simpl
mattm
2013/01/08 04:53:21
removed.
erikwright (departed)
2013/01/10 22:03:30
I see your point. I don't mind either way.
| |
176 std::vector<net::DefaultServerBoundCertStore::ServerBoundCert*>* certs) { | |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
178 | |
179 // This method should be called only once per instance. | |
180 DCHECK(!db_.get()); | |
167 | 181 |
168 base::TimeTicks start = base::TimeTicks::Now(); | 182 base::TimeTicks start = base::TimeTicks::Now(); |
169 | 183 |
170 // Ensure the parent directory for storing certs is created before reading | 184 // Ensure the parent directory for storing certs is created before reading |
171 // from it. | 185 // from it. |
172 const FilePath dir = path_.DirName(); | 186 const FilePath dir = path_.DirName(); |
173 if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) | 187 if (!file_util::PathExists(dir) && !file_util::CreateDirectory(dir)) |
174 return false; | 188 return false; |
175 | 189 |
176 int64 db_size = 0; | 190 int64 db_size = 0; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 static_cast<net::SSLClientCertType>(smt.ColumnInt(3)), | 225 static_cast<net::SSLClientCertType>(smt.ColumnInt(3)), |
212 base::Time::FromInternalValue(smt.ColumnInt64(5)), | 226 base::Time::FromInternalValue(smt.ColumnInt64(5)), |
213 base::Time::FromInternalValue(smt.ColumnInt64(4)), | 227 base::Time::FromInternalValue(smt.ColumnInt64(4)), |
214 private_key_from_db, | 228 private_key_from_db, |
215 cert_from_db)); | 229 cert_from_db)); |
216 cert_origins_.insert(cert->server_identifier()); | 230 cert_origins_.insert(cert->server_identifier()); |
217 certs->push_back(cert.release()); | 231 certs->push_back(cert.release()); |
218 } | 232 } |
219 | 233 |
220 UMA_HISTOGRAM_COUNTS_10000("DomainBoundCerts.DBLoadedCount", certs->size()); | 234 UMA_HISTOGRAM_COUNTS_10000("DomainBoundCerts.DBLoadedCount", certs->size()); |
235 base::TimeDelta load_time = base::TimeTicks::Now() - start; | |
221 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.DBLoadTime", | 236 UMA_HISTOGRAM_CUSTOM_TIMES("DomainBoundCerts.DBLoadTime", |
222 base::TimeTicks::Now() - start, | 237 load_time, |
223 base::TimeDelta::FromMilliseconds(1), | 238 base::TimeDelta::FromMilliseconds(1), |
224 base::TimeDelta::FromMinutes(1), | 239 base::TimeDelta::FromMinutes(1), |
225 50); | 240 50); |
241 DVLOG(1) << "loaded " << certs->size() << " in " << load_time.InMilliseconds() | |
242 << " ms"; | |
226 return true; | 243 return true; |
227 } | 244 } |
228 | 245 |
229 bool SQLiteServerBoundCertStore::Backend::EnsureDatabaseVersion() { | 246 bool SQLiteServerBoundCertStore::Backend::EnsureDatabaseVersion() { |
230 // Version check. | 247 // Version check. |
231 if (!meta_table_.Init( | 248 if (!meta_table_.Init( |
232 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { | 249 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { |
233 return false; | 250 return false; |
234 } | 251 } |
235 | 252 |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
537 base::AutoLock locked(lock_); | 554 base::AutoLock locked(lock_); |
538 force_keep_session_state_ = true; | 555 force_keep_session_state_ = true; |
539 } | 556 } |
540 | 557 |
541 SQLiteServerBoundCertStore::SQLiteServerBoundCertStore( | 558 SQLiteServerBoundCertStore::SQLiteServerBoundCertStore( |
542 const FilePath& path, | 559 const FilePath& path, |
543 ClearOnExitPolicy* clear_on_exit_policy) | 560 ClearOnExitPolicy* clear_on_exit_policy) |
544 : backend_(new Backend(path, clear_on_exit_policy)) { | 561 : backend_(new Backend(path, clear_on_exit_policy)) { |
545 } | 562 } |
546 | 563 |
547 bool SQLiteServerBoundCertStore::Load( | 564 void SQLiteServerBoundCertStore::Load( |
548 std::vector<net::DefaultServerBoundCertStore::ServerBoundCert*>* certs) { | 565 const LoadedCallback& loaded_callback) { |
549 return backend_->Load(certs); | 566 backend_->Load(loaded_callback); |
550 } | 567 } |
551 | 568 |
552 void SQLiteServerBoundCertStore::AddServerBoundCert( | 569 void SQLiteServerBoundCertStore::AddServerBoundCert( |
553 const net::DefaultServerBoundCertStore::ServerBoundCert& cert) { | 570 const net::DefaultServerBoundCertStore::ServerBoundCert& cert) { |
554 if (backend_.get()) | 571 if (backend_.get()) |
555 backend_->AddServerBoundCert(cert); | 572 backend_->AddServerBoundCert(cert); |
556 } | 573 } |
557 | 574 |
558 void SQLiteServerBoundCertStore::DeleteServerBoundCert( | 575 void SQLiteServerBoundCertStore::DeleteServerBoundCert( |
559 const net::DefaultServerBoundCertStore::ServerBoundCert& cert) { | 576 const net::DefaultServerBoundCertStore::ServerBoundCert& cert) { |
(...skipping 14 matching lines...) Expand all Loading... | |
574 } | 591 } |
575 | 592 |
576 SQLiteServerBoundCertStore::~SQLiteServerBoundCertStore() { | 593 SQLiteServerBoundCertStore::~SQLiteServerBoundCertStore() { |
577 if (backend_.get()) { | 594 if (backend_.get()) { |
578 backend_->Close(); | 595 backend_->Close(); |
579 // Release our reference, it will probably still have a reference if the | 596 // Release our reference, it will probably still have a reference if the |
580 // background thread has not run Close() yet. | 597 // background thread has not run Close() yet. |
581 backend_ = NULL; | 598 backend_ = NULL; |
582 } | 599 } |
583 } | 600 } |
OLD | NEW |