Chromium Code Reviews| 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 "net/base/cert_database.h" | 5 #include "net/base/cert_database.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | |
| 7 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 8 #include "base/observer_list_threadsafe.h" | 9 #include "base/observer_list_threadsafe.h" |
| 9 | 10 |
| 11 #if defined(USE_NSS) | |
| 12 #include "net/base/nss_cert_database.h" | |
| 13 #endif | |
| 14 | |
| 10 namespace net { | 15 namespace net { |
| 11 | 16 |
| 12 CertDatabase::ImportCertFailure::ImportCertFailure( | 17 namespace { |
| 13 X509Certificate* cert, int err) | |
| 14 : certificate(cert), net_error(err) { | |
| 15 } | |
| 16 | |
| 17 CertDatabase::ImportCertFailure::~ImportCertFailure() { | |
| 18 } | |
| 19 | 18 |
| 20 // CertDatabaseNotifier notifies registered observers when new user certificates | 19 // CertDatabaseNotifier notifies registered observers when new user certificates |
| 21 // are added to the database. | 20 // are added to the database. |
| 21 #if defined(USE_NSS) | |
| 22 class CertDatabaseNotifier : public NSSCertDatabase::Observer { | |
|
Ryan Sleevi
2012/09/05 21:44:17
nit: Add a comment about why this inheritance (tha
Ryan Sleevi
2012/09/05 21:44:17
nit: I'm generally not a fan of conditional inheri
Joao da Silva
2012/09/06 15:11:41
Done.
Joao da Silva
2012/09/06 15:11:41
Done.
| |
| 23 #else | |
| 22 class CertDatabaseNotifier { | 24 class CertDatabaseNotifier { |
| 25 #endif | |
| 23 public: | 26 public: |
| 24 CertDatabaseNotifier() | 27 CertDatabaseNotifier() |
| 25 : observer_list_(new ObserverListThreadSafe<CertDatabase::Observer>) { | 28 : observer_list_(new ObserverListThreadSafe<CertDatabase::Observer>) { |
| 29 #if defined(USE_NSS) | |
| 30 // Observe events from the NSSCertDatabase to propagate them to the | |
| 31 // observers of CertDatabase. | |
| 32 NSSCertDatabase nss_cert_db; | |
| 33 nss_cert_db.AddObserver(this); | |
| 34 #endif | |
| 26 } | 35 } |
| 27 | 36 |
| 28 static CertDatabaseNotifier* GetInstance() { | 37 static CertDatabaseNotifier* GetInstance() { |
| 29 return Singleton<CertDatabaseNotifier>::get(); | 38 return Singleton<CertDatabaseNotifier>::get(); |
| 30 } | 39 } |
| 31 | 40 |
| 32 friend struct DefaultSingletonTraits<CertDatabaseNotifier>; | 41 #if defined(USE_NSS) |
|
Ryan Sleevi
2012/09/05 21:44:17
nit:
// NSSCertDatabase::Observer implementation
Joao da Silva
2012/09/06 15:11:41
Done (though the #ifdef is gone)
| |
| 33 friend class CertDatabase; | 42 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE { |
| 43 NotifyObserversOfCertAdded(cert); | |
| 44 } | |
| 45 | |
| 46 virtual void OnCertRemoved(const X509Certificate* cert) OVERRIDE { | |
| 47 NotifyObserversOfCertRemoved(cert); | |
| 48 } | |
| 49 | |
| 50 virtual void OnCertTrustChanged(const X509Certificate* cert) OVERRIDE { | |
| 51 NotifyObserversOfCertTrustChanged(cert); | |
| 52 } | |
| 53 #endif | |
| 54 | |
| 55 void NotifyObserversOfCertAdded(const X509Certificate* cert) { | |
| 56 observer_list_->Notify( | |
| 57 &CertDatabase::Observer::OnCertAdded, make_scoped_refptr(cert)); | |
| 58 } | |
| 59 | |
| 60 void NotifyObserversOfCertRemoved(const X509Certificate* cert) { | |
| 61 observer_list_->Notify( | |
| 62 &CertDatabase::Observer::OnCertRemoved, make_scoped_refptr(cert)); | |
| 63 } | |
| 64 | |
| 65 void NotifyObserversOfCertTrustChanged(const X509Certificate* cert) { | |
| 66 observer_list_->Notify( | |
| 67 &CertDatabase::Observer::OnCertTrustChanged, make_scoped_refptr(cert)); | |
| 68 } | |
| 34 | 69 |
| 35 private: | 70 private: |
| 71 friend struct DefaultSingletonTraits<CertDatabaseNotifier>; | |
| 72 friend class net::CertDatabase; | |
| 73 | |
| 36 const scoped_refptr<ObserverListThreadSafe<CertDatabase::Observer> > | 74 const scoped_refptr<ObserverListThreadSafe<CertDatabase::Observer> > |
| 37 observer_list_; | 75 observer_list_; |
| 38 }; | 76 }; |
| 39 | 77 |
| 78 } // namespace | |
| 79 | |
| 80 CertDatabase::CertDatabase() { | |
| 81 #if defined(USE_NSS) | |
| 82 // Make sure the NSS initialization has been performed. | |
| 83 NSSCertDatabase::EnsureInit(); | |
| 84 #endif | |
| 85 } | |
| 86 | |
| 40 void CertDatabase::AddObserver(Observer* observer) { | 87 void CertDatabase::AddObserver(Observer* observer) { |
| 41 CertDatabaseNotifier::GetInstance()->observer_list_->AddObserver(observer); | 88 CertDatabaseNotifier::GetInstance()->observer_list_->AddObserver(observer); |
| 42 } | 89 } |
| 43 | 90 |
| 44 void CertDatabase::RemoveObserver(Observer* observer) { | 91 void CertDatabase::RemoveObserver(Observer* observer) { |
| 45 CertDatabaseNotifier::GetInstance()->observer_list_->RemoveObserver(observer); | 92 CertDatabaseNotifier::GetInstance()->observer_list_->RemoveObserver(observer); |
| 46 } | 93 } |
| 47 | 94 |
| 48 void CertDatabase::NotifyObserversOfUserCertAdded(const X509Certificate* cert) { | 95 void CertDatabase::NotifyObserversOfCertAdded(const X509Certificate* cert) { |
| 49 CertDatabaseNotifier::GetInstance()->observer_list_->Notify( | 96 CertDatabaseNotifier::GetInstance()->NotifyObserversOfCertAdded(cert); |
| 50 &CertDatabase::Observer::OnUserCertAdded, make_scoped_refptr(cert)); | |
| 51 } | 97 } |
| 52 | 98 |
| 53 void CertDatabase::NotifyObserversOfUserCertRemoved( | 99 void CertDatabase::NotifyObserversOfCertRemoved(const X509Certificate* cert) { |
| 54 const X509Certificate* cert) { | 100 CertDatabaseNotifier::GetInstance()->NotifyObserversOfCertRemoved(cert); |
| 55 CertDatabaseNotifier::GetInstance()->observer_list_->Notify( | |
| 56 &CertDatabase::Observer::OnUserCertRemoved, make_scoped_refptr(cert)); | |
| 57 } | 101 } |
| 58 | 102 |
| 59 void CertDatabase::NotifyObserversOfCertTrustChanged( | 103 void CertDatabase::NotifyObserversOfCertTrustChanged( |
| 60 const X509Certificate* cert) { | 104 const X509Certificate* cert) { |
| 61 CertDatabaseNotifier::GetInstance()->observer_list_->Notify( | 105 CertDatabaseNotifier::GetInstance()->NotifyObserversOfCertTrustChanged(cert); |
| 62 &CertDatabase::Observer::OnCertTrustChanged, make_scoped_refptr(cert)); | |
| 63 } | 106 } |
| 64 | 107 |
| 65 } // namespace net | 108 } // namespace net |
| OLD | NEW |