| 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 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 // This class is designed to be shared between any calling threads and the | 30 // This class is designed to be shared between any calling threads and the |
| 31 // database thread. It batches operations and commits them on a timer. | 31 // database thread. It batches operations and commits them on a timer. |
| 32 class SQLiteServerBoundCertStore::Backend | 32 class SQLiteServerBoundCertStore::Backend |
| 33 : public base::RefCountedThreadSafe<SQLiteServerBoundCertStore::Backend> { | 33 : public base::RefCountedThreadSafe<SQLiteServerBoundCertStore::Backend> { |
| 34 public: | 34 public: |
| 35 Backend(const FilePath& path, ClearOnExitPolicy* clear_on_exit_policy) | 35 Backend(const FilePath& path, ClearOnExitPolicy* clear_on_exit_policy) |
| 36 : path_(path), | 36 : path_(path), |
| 37 db_(NULL), | 37 db_(NULL), |
| 38 num_pending_(0), | 38 num_pending_(0), |
| 39 clear_local_state_on_exit_(false), | 39 force_keep_session_state_(false), |
| 40 clear_on_exit_policy_(clear_on_exit_policy) { | 40 clear_on_exit_policy_(clear_on_exit_policy) { |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Creates or load the SQLite database. | 43 // Creates or load the SQLite database. |
| 44 bool Load( | 44 bool Load( |
| 45 std::vector<net::DefaultServerBoundCertStore::ServerBoundCert*>* certs); | 45 std::vector<net::DefaultServerBoundCertStore::ServerBoundCert*>* certs); |
| 46 | 46 |
| 47 // Batch a server bound cert addition. | 47 // Batch a server bound cert addition. |
| 48 void AddServerBoundCert( | 48 void AddServerBoundCert( |
| 49 const net::DefaultServerBoundCertStore::ServerBoundCert& cert); | 49 const net::DefaultServerBoundCertStore::ServerBoundCert& cert); |
| 50 | 50 |
| 51 // Batch a server bound cert deletion. | 51 // Batch a server bound cert deletion. |
| 52 void DeleteServerBoundCert( | 52 void DeleteServerBoundCert( |
| 53 const net::DefaultServerBoundCertStore::ServerBoundCert& cert); | 53 const net::DefaultServerBoundCertStore::ServerBoundCert& cert); |
| 54 | 54 |
| 55 // Commit pending operations as soon as possible. | 55 // Commit pending operations as soon as possible. |
| 56 void Flush(const base::Closure& completion_task); | 56 void Flush(const base::Closure& completion_task); |
| 57 | 57 |
| 58 // 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 |
| 59 // before the object is destructed. | 59 // before the object is destructed. |
| 60 void Close(); | 60 void Close(); |
| 61 | 61 |
| 62 void SetClearLocalStateOnExit(bool clear_local_state); | 62 void SetForceKeepSessionState(); |
| 63 | 63 |
| 64 private: | 64 private: |
| 65 friend class base::RefCountedThreadSafe<SQLiteServerBoundCertStore::Backend>; | 65 friend class base::RefCountedThreadSafe<SQLiteServerBoundCertStore::Backend>; |
| 66 | 66 |
| 67 // You should call Close() before destructing this object. | 67 // You should call Close() before destructing this object. |
| 68 ~Backend() { | 68 ~Backend() { |
| 69 DCHECK(!db_.get()) << "Close should have already been called."; | 69 DCHECK(!db_.get()) << "Close should have already been called."; |
| 70 DCHECK(num_pending_ == 0 && pending_.empty()); | 70 DCHECK(num_pending_ == 0 && pending_.empty()); |
| 71 } | 71 } |
| 72 | 72 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 | 107 |
| 108 void DeleteCertificatesOnShutdown(); | 108 void DeleteCertificatesOnShutdown(); |
| 109 | 109 |
| 110 FilePath path_; | 110 FilePath path_; |
| 111 scoped_ptr<sql::Connection> db_; | 111 scoped_ptr<sql::Connection> db_; |
| 112 sql::MetaTable meta_table_; | 112 sql::MetaTable meta_table_; |
| 113 | 113 |
| 114 typedef std::list<PendingOperation*> PendingOperationsList; | 114 typedef std::list<PendingOperation*> PendingOperationsList; |
| 115 PendingOperationsList pending_; | 115 PendingOperationsList pending_; |
| 116 PendingOperationsList::size_type num_pending_; | 116 PendingOperationsList::size_type num_pending_; |
| 117 // True if the persistent store should be deleted upon destruction. | 117 // True if the persistent store should skip clear on exit rules. |
| 118 bool clear_local_state_on_exit_; | 118 bool force_keep_session_state_; |
| 119 // Guard |pending_|, |num_pending_| and |clear_local_state_on_exit_|. | 119 // Guard |pending_|, |num_pending_| and |force_keep_session_state_|. |
| 120 base::Lock lock_; | 120 base::Lock lock_; |
| 121 | 121 |
| 122 // Cache of origins we have certificates stored for. | 122 // Cache of origins we have certificates stored for. |
| 123 std::set<std::string> cert_origins_; | 123 std::set<std::string> cert_origins_; |
| 124 | 124 |
| 125 scoped_refptr<ClearOnExitPolicy> clear_on_exit_policy_; | 125 scoped_refptr<ClearOnExitPolicy> clear_on_exit_policy_; |
| 126 | 126 |
| 127 DISALLOW_COPY_AND_ASSIGN(Backend); | 127 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 128 }; | 128 }; |
| 129 | 129 |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 BrowserThread::PostTask( | 469 BrowserThread::PostTask( |
| 470 BrowserThread::DB, FROM_HERE, | 470 BrowserThread::DB, FROM_HERE, |
| 471 base::Bind(&Backend::InternalBackgroundClose, this)); | 471 base::Bind(&Backend::InternalBackgroundClose, this)); |
| 472 } | 472 } |
| 473 | 473 |
| 474 void SQLiteServerBoundCertStore::Backend::InternalBackgroundClose() { | 474 void SQLiteServerBoundCertStore::Backend::InternalBackgroundClose() { |
| 475 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 475 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 476 // Commit any pending operations | 476 // Commit any pending operations |
| 477 Commit(); | 477 Commit(); |
| 478 | 478 |
| 479 if (!clear_local_state_on_exit_ && clear_on_exit_policy_.get() && | 479 if (!force_keep_session_state_ && clear_on_exit_policy_.get() && |
| 480 clear_on_exit_policy_->HasClearOnExitOrigins()) { | 480 clear_on_exit_policy_->HasClearOnExitOrigins()) { |
| 481 DeleteCertificatesOnShutdown(); | 481 DeleteCertificatesOnShutdown(); |
| 482 } | 482 } |
| 483 | 483 |
| 484 db_.reset(); | 484 db_.reset(); |
| 485 | |
| 486 if (clear_local_state_on_exit_) | |
| 487 file_util::Delete(path_, false); | |
| 488 } | 485 } |
| 489 | 486 |
| 490 void SQLiteServerBoundCertStore::Backend::DeleteCertificatesOnShutdown() { | 487 void SQLiteServerBoundCertStore::Backend::DeleteCertificatesOnShutdown() { |
| 491 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 488 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 492 | 489 |
| 493 if (!db_.get()) | 490 if (!db_.get()) |
| 494 return; | 491 return; |
| 495 | 492 |
| 496 if (cert_origins_.empty()) | 493 if (cert_origins_.empty()) |
| 497 return; | 494 return; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 516 del_smt.Reset(true); | 513 del_smt.Reset(true); |
| 517 del_smt.BindString(0, *it); | 514 del_smt.BindString(0, *it); |
| 518 if (!del_smt.Run()) | 515 if (!del_smt.Run()) |
| 519 NOTREACHED() << "Could not delete a certificate from the DB."; | 516 NOTREACHED() << "Could not delete a certificate from the DB."; |
| 520 } | 517 } |
| 521 | 518 |
| 522 if (!transaction.Commit()) | 519 if (!transaction.Commit()) |
| 523 LOG(WARNING) << "Unable to delete certificates on shutdown."; | 520 LOG(WARNING) << "Unable to delete certificates on shutdown."; |
| 524 } | 521 } |
| 525 | 522 |
| 526 void SQLiteServerBoundCertStore::Backend::SetClearLocalStateOnExit( | 523 void SQLiteServerBoundCertStore::Backend::SetForceKeepSessionState() { |
| 527 bool clear_local_state) { | |
| 528 base::AutoLock locked(lock_); | 524 base::AutoLock locked(lock_); |
| 529 clear_local_state_on_exit_ = clear_local_state; | 525 force_keep_session_state_ = true; |
| 530 } | 526 } |
| 531 | 527 |
| 532 SQLiteServerBoundCertStore::SQLiteServerBoundCertStore( | 528 SQLiteServerBoundCertStore::SQLiteServerBoundCertStore( |
| 533 const FilePath& path, | 529 const FilePath& path, |
| 534 ClearOnExitPolicy* clear_on_exit_policy) | 530 ClearOnExitPolicy* clear_on_exit_policy) |
| 535 : backend_(new Backend(path, clear_on_exit_policy)) { | 531 : backend_(new Backend(path, clear_on_exit_policy)) { |
| 536 } | 532 } |
| 537 | 533 |
| 538 bool SQLiteServerBoundCertStore::Load( | 534 bool SQLiteServerBoundCertStore::Load( |
| 539 std::vector<net::DefaultServerBoundCertStore::ServerBoundCert*>* certs) { | 535 std::vector<net::DefaultServerBoundCertStore::ServerBoundCert*>* certs) { |
| 540 return backend_->Load(certs); | 536 return backend_->Load(certs); |
| 541 } | 537 } |
| 542 | 538 |
| 543 void SQLiteServerBoundCertStore::AddServerBoundCert( | 539 void SQLiteServerBoundCertStore::AddServerBoundCert( |
| 544 const net::DefaultServerBoundCertStore::ServerBoundCert& cert) { | 540 const net::DefaultServerBoundCertStore::ServerBoundCert& cert) { |
| 545 if (backend_.get()) | 541 if (backend_.get()) |
| 546 backend_->AddServerBoundCert(cert); | 542 backend_->AddServerBoundCert(cert); |
| 547 } | 543 } |
| 548 | 544 |
| 549 void SQLiteServerBoundCertStore::DeleteServerBoundCert( | 545 void SQLiteServerBoundCertStore::DeleteServerBoundCert( |
| 550 const net::DefaultServerBoundCertStore::ServerBoundCert& cert) { | 546 const net::DefaultServerBoundCertStore::ServerBoundCert& cert) { |
| 551 if (backend_.get()) | 547 if (backend_.get()) |
| 552 backend_->DeleteServerBoundCert(cert); | 548 backend_->DeleteServerBoundCert(cert); |
| 553 } | 549 } |
| 554 | 550 |
| 555 void SQLiteServerBoundCertStore::SetClearLocalStateOnExit( | 551 void SQLiteServerBoundCertStore::SetForceKeepSessionState() { |
| 556 bool clear_local_state) { | |
| 557 if (backend_.get()) | 552 if (backend_.get()) |
| 558 backend_->SetClearLocalStateOnExit(clear_local_state); | 553 backend_->SetForceKeepSessionState(); |
| 559 } | 554 } |
| 560 | 555 |
| 561 void SQLiteServerBoundCertStore::Flush(const base::Closure& completion_task) { | 556 void SQLiteServerBoundCertStore::Flush(const base::Closure& completion_task) { |
| 562 if (backend_.get()) | 557 if (backend_.get()) |
| 563 backend_->Flush(completion_task); | 558 backend_->Flush(completion_task); |
| 564 else if (!completion_task.is_null()) | 559 else if (!completion_task.is_null()) |
| 565 MessageLoop::current()->PostTask(FROM_HERE, completion_task); | 560 MessageLoop::current()->PostTask(FROM_HERE, completion_task); |
| 566 } | 561 } |
| 567 | 562 |
| 568 SQLiteServerBoundCertStore::~SQLiteServerBoundCertStore() { | 563 SQLiteServerBoundCertStore::~SQLiteServerBoundCertStore() { |
| 569 if (backend_.get()) { | 564 if (backend_.get()) { |
| 570 backend_->Close(); | 565 backend_->Close(); |
| 571 // Release our reference, it will probably still have a reference if the | 566 // Release our reference, it will probably still have a reference if the |
| 572 // background thread has not run Close() yet. | 567 // background thread has not run Close() yet. |
| 573 backend_ = NULL; | 568 backend_ = NULL; |
| 574 } | 569 } |
| 575 } | 570 } |
| OLD | NEW |