1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_OPTIONS_CERT_LIBRARY_H_
6 #define CHROME_BROWSER_CHROMEOS_OPTIONS_CERT_LIBRARY_H_
10 #include "base/strings/string16.h"
11 #include "chromeos/cert_loader.h"
12 #include "net/cert/x509_certificate.h"
16 class CertNameComparator
;
18 // This class is responsible for keeping track of certificates in a UI
19 // friendly manner. It observes CertLoader to receive certificate list
20 // updates and sorts them by type for the UI. All public APIs are expected
21 // to be called from the UI thread and are non blocking. Observers will also
22 // be called on the UI thread.
23 class CertLibrary
: public CertLoader::Observer
{
27 virtual ~Observer() {}
29 // Called for any Observers whenever the certificates are loaded.
30 // |initial_load| is true the first time this is called.
31 virtual void OnCertificatesLoaded(bool initial_load
) = 0;
37 DISALLOW_COPY_AND_ASSIGN(Observer
);
47 // Manage the global instance.
48 static void Initialize();
49 static void Shutdown();
50 static CertLibrary
* Get();
51 static bool IsInitialized();
53 // Add / Remove Observer
54 void AddObserver(Observer
* observer
);
55 void RemoveObserver(Observer
* observer
);
57 // Returns true when the certificate list has been requested but not loaded.
58 bool CertificatesLoading() const;
60 // Returns true when the certificate list has been initiailized.
61 bool CertificatesLoaded() const;
63 // Retruns the number of certificates available for |type|.
64 int NumCertificates(CertType type
) const;
66 // Retreives the certificate property for |type| at |index|.
67 base::string16
GetCertDisplayStringAt(CertType type
, int index
) const;
68 std::string
GetServerCACertPEMAt(int index
) const;
69 std::string
GetUserCertPkcs11IdAt(int index
, int* slot_id
) const;
70 bool IsCertHardwareBackedAt(CertType type
, int index
) const;
72 // Returns the index of a Certificate matching |pem_encoded| or -1 if none
73 // found. This function may be slow depending on the number of stored
75 // TOOD(pneubeck): Either make this more efficient, asynchronous or get rid of
77 int GetServerCACertIndexByPEM(const std::string
& pem_encoded
) const;
78 // Same as above but for a PKCS#11 id.
79 int GetUserCertIndexByPkcs11Id(const std::string
& pkcs11_id
) const;
81 // CertLoader::Observer
82 void OnCertificatesLoaded(const net::CertificateList
&,
83 bool initial_load
) override
;
87 ~CertLibrary() override
;
89 net::X509Certificate
* GetCertificateAt(CertType type
, int index
) const;
90 const net::CertificateList
& GetCertificateListForType(CertType type
) const;
92 ObserverList
<CertLibrary::Observer
> observer_list_
;
94 // Sorted certificate lists
95 net::CertificateList certs_
;
96 net::CertificateList user_certs_
;
97 net::CertificateList server_certs_
;
98 net::CertificateList server_ca_certs_
;
100 DISALLOW_COPY_AND_ASSIGN(CertLibrary
);
103 } // namespace chromeos
105 #endif // CHROME_BROWSER_CHROMEOS_OPTIONS_CERT_LIBRARY_H_