Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: net/base/cert_database.cc

Issue 10916094: Move the NSS functions out of CertDatabase into a new NSSCertDatabase class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/singleton.h" 7 #include "base/memory/singleton.h"
8 #include "base/observer_list_threadsafe.h" 8 #include "base/observer_list_threadsafe.h"
9 9
10 #if defined(USE_NSS)
11 #include "net/base/nss_cert_database.h"
12 #endif
13
10 namespace net { 14 namespace net {
11 15
12 CertDatabase::ImportCertFailure::ImportCertFailure( 16 #if defined(USE_NSS)
13 X509Certificate* cert, int err) 17 // Helper that observes events from the NSSCertDatabase and forwards them to
14 : certificate(cert), net_error(err) { 18 // the given CertDatabase.
19 class CertDatabase::Notifier : public NSSCertDatabase::Observer {
wtc 2012/09/10 22:11:01 Ideally the USE_NSS code in this file should be mo
20 public:
21 explicit Notifier(CertDatabase* cert_db) : cert_db_(cert_db) {
22 NSSCertDatabase::GetInstance()->AddObserver(this);
23 }
24
25 virtual ~Notifier() {
26 NSSCertDatabase::GetInstance()->RemoveObserver(this);
27 }
28
29 // NSSCertDatabase::Observer implementation:
30 virtual void OnCertAdded(const X509Certificate* cert) OVERRIDE {
31 cert_db_->NotifyObserversOfCertAdded(cert);
32 }
33
34 virtual void OnCertRemoved(const X509Certificate* cert) OVERRIDE {
35 cert_db_->NotifyObserversOfCertRemoved(cert);
36 }
37
38 virtual void OnCertTrustChanged(const X509Certificate* cert) OVERRIDE {
39 cert_db_->NotifyObserversOfCertTrustChanged(cert);
40 }
41
42 private:
43 CertDatabase* cert_db_;
44
45 DISALLOW_COPY_AND_ASSIGN(Notifier);
46 };
47 #endif
48
49 // static
50 CertDatabase* CertDatabase::GetInstance() {
51 return Singleton<CertDatabase>::get();
15 } 52 }
16 53
17 CertDatabase::ImportCertFailure::~ImportCertFailure() { 54 CertDatabase::CertDatabase()
55 : observer_list_(new ObserverListThreadSafe<Observer>) {
56 #if defined(USE_NSS)
57 // Observe NSSCertDatabase events and forward them to observers of
58 // CertDatabase. This also makes sure that NSS has been initialized.
59 notifier_.reset(new Notifier(this));
60 #endif
18 } 61 }
19 62
20 // CertDatabaseNotifier notifies registered observers when new user certificates 63 CertDatabase::~CertDatabase() {}
21 // are added to the database.
22 class CertDatabaseNotifier {
23 public:
24 CertDatabaseNotifier()
25 : observer_list_(new ObserverListThreadSafe<CertDatabase::Observer>) {
26 }
27
28 static CertDatabaseNotifier* GetInstance() {
29 return Singleton<CertDatabaseNotifier>::get();
30 }
31
32 friend struct DefaultSingletonTraits<CertDatabaseNotifier>;
33 friend class CertDatabase;
34
35 private:
36 const scoped_refptr<ObserverListThreadSafe<CertDatabase::Observer> >
37 observer_list_;
38 };
39 64
40 void CertDatabase::AddObserver(Observer* observer) { 65 void CertDatabase::AddObserver(Observer* observer) {
41 CertDatabaseNotifier::GetInstance()->observer_list_->AddObserver(observer); 66 observer_list_->AddObserver(observer);
42 } 67 }
43 68
44 void CertDatabase::RemoveObserver(Observer* observer) { 69 void CertDatabase::RemoveObserver(Observer* observer) {
45 CertDatabaseNotifier::GetInstance()->observer_list_->RemoveObserver(observer); 70 observer_list_->RemoveObserver(observer);
46 } 71 }
47 72
48 void CertDatabase::NotifyObserversOfUserCertAdded(const X509Certificate* cert) { 73 void CertDatabase::NotifyObserversOfCertAdded(const X509Certificate* cert) {
49 CertDatabaseNotifier::GetInstance()->observer_list_->Notify( 74 observer_list_->Notify(&Observer::OnCertAdded, make_scoped_refptr(cert));
50 &CertDatabase::Observer::OnUserCertAdded, make_scoped_refptr(cert));
51 } 75 }
52 76
53 void CertDatabase::NotifyObserversOfUserCertRemoved( 77 void CertDatabase::NotifyObserversOfCertRemoved(const X509Certificate* cert) {
54 const X509Certificate* cert) { 78 observer_list_->Notify(&Observer::OnCertRemoved, make_scoped_refptr(cert));
55 CertDatabaseNotifier::GetInstance()->observer_list_->Notify(
56 &CertDatabase::Observer::OnUserCertRemoved, make_scoped_refptr(cert));
57 } 79 }
58 80
59 void CertDatabase::NotifyObserversOfCertTrustChanged( 81 void CertDatabase::NotifyObserversOfCertTrustChanged(
60 const X509Certificate* cert) { 82 const X509Certificate* cert) {
61 CertDatabaseNotifier::GetInstance()->observer_list_->Notify( 83 observer_list_->Notify(
62 &CertDatabase::Observer::OnCertTrustChanged, make_scoped_refptr(cert)); 84 &Observer::OnCertTrustChanged, make_scoped_refptr(cert));
63 } 85 }
64 86
65 } // namespace net 87 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698