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