[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_CERTIFICATE_MANAGER_MODEL_H_ |
| 6 | #define CONTENT_BROWSER_CERTIFICATE_MANAGER_MODEL_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 11 | #include "base/memory/ref_counted.h" |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 12 | #include "base/string16.h" |
| 13 | #include "net/base/cert_database.h" |
| 14 | |
| 15 | // CertificateManagerModel provides the data to be displayed in the certificate |
| 16 | // manager dialog, and processes changes from the view. |
| 17 | class CertificateManagerModel { |
| 18 | public: |
| 19 | // Map from the subject organization name to the list of certs from that |
| 20 | // organization. If a cert does not have an organization name, the |
| 21 | // subject's CertPrincipal::GetDisplayName() value is used instead. |
| 22 | typedef std::map<std::string, net::CertificateList> OrgGroupingMap; |
| 23 | |
| 24 | // Enumeration of the possible columns in the certificate manager tree view. |
| 25 | enum Column { |
| 26 | COL_SUBJECT_NAME, |
| 27 | COL_CERTIFICATE_STORE, |
| 28 | COL_SERIAL_NUMBER, |
| 29 | COL_EXPIRES_ON, |
| 30 | }; |
| 31 | |
| 32 | class Observer { |
| 33 | public: |
| 34 | // Called to notify the view that the certificate list has been refreshed. |
| 35 | // TODO(mattm): do a more granular updating strategy? Maybe retrieve new |
| 36 | // list of certs, diff against past list, and then notify of the changes? |
| 37 | virtual void CertificatesRefreshed() = 0; |
| 38 | }; |
| 39 | |
| 40 | explicit CertificateManagerModel(Observer* observer); |
| 41 | ~CertificateManagerModel(); |
| 42 | |
| 43 | // Accessor for read-only access to the underlying CertDatabase. |
| 44 | const net::CertDatabase& cert_db() const { return cert_db_; } |
| 45 | |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 46 | // Trigger a refresh of the list of certs, unlock any slots if necessary. |
| 47 | // Following this call, the observer CertificatesRefreshed method will be |
| 48 | // called so the view can call FilterAndBuildOrgGroupingMap as necessary to |
| 49 | // refresh its tree views. |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 50 | void Refresh(); |
| 51 | |
| 52 | // Fill |map| with the certificates matching |filter_type|. |
| 53 | void FilterAndBuildOrgGroupingMap(net::CertType filter_type, |
| 54 | OrgGroupingMap* map) const; |
| 55 | |
| 56 | // Get the data to be displayed in |column| for the given |cert|. |
| 57 | string16 GetColumnText(const net::X509Certificate& cert, Column column) const; |
| 58 | |
| 59 | // Import certificates from PKCS #12 encoded |data|, using the given |
| 60 | // |password|. Returns a net error code on failure. |
| 61 | int ImportFromPKCS12(net::CryptoModule* module, const std::string& data, |
| 62 | const string16& password); |
| 63 | |
| 64 | // Import CA certificates. |
| 65 | // Tries to import all the certificates given. The root will be trusted |
| 66 | // according to |trust_bits|. Any certificates that could not be imported |
| 67 | // will be listed in |not_imported|. |
| 68 | // |trust_bits| should be a bit field of TRUST_* values from CertDatabase, or |
| 69 | // UNTRUSTED. |
| 70 | // Returns false if there is an internal error, otherwise true is returned and |
| 71 | // |not_imported| should be checked for any certificates that were not |
| 72 | // imported. |
| 73 | bool ImportCACerts(const net::CertificateList& certificates, |
| 74 | unsigned int trust_bits, |
| 75 | net::CertDatabase::ImportCertFailureList* not_imported); |
| 76 | |
| 77 | // Import server certificate. The first cert should be the server cert. Any |
| 78 | // additional certs should be intermediate/CA certs and will be imported but |
| 79 | // not given any trust. |
| 80 | // Any certificates that could not be imported will be listed in |
| 81 | // |not_imported|. |
| 82 | // Returns false if there is an internal error, otherwise true is returned and |
| 83 | // |not_imported| should be checked for any certificates that were not |
| 84 | // imported. |
| 85 | bool ImportServerCert( |
| 86 | const net::CertificateList& certificates, |
| 87 | net::CertDatabase::ImportCertFailureList* not_imported); |
| 88 | |
| 89 | // Set trust values for certificate. |
| 90 | // |trust_bits| should be a bit field of TRUST_* values from CertDatabase, or |
| 91 | // UNTRUSTED. |
| 92 | // Returns true on success or false on failure. |
| 93 | bool SetCertTrust(const net::X509Certificate* cert, |
| 94 | net::CertType type, |
| 95 | unsigned int trust_bits); |
| 96 | |
| 97 | // Delete the cert. Returns true on success. |cert| is still valid when this |
| 98 | // function returns. |
| 99 | bool Delete(net::X509Certificate* cert); |
| 100 | |
| 101 | private: |
[email protected] | 4c4f7cd | 2011-03-05 02:20:44 | [diff] [blame] | 102 | // Callback used by Refresh() for when the cert slots have been unlocked. |
| 103 | // This method does the actual refreshing. |
| 104 | void RefreshSlotsUnlocked(); |
| 105 | |
[email protected] | df8e899b | 2011-02-22 22:58:22 | [diff] [blame] | 106 | net::CertDatabase cert_db_; |
| 107 | net::CertificateList cert_list_; |
| 108 | |
| 109 | // The observer to notify when certificate list is refreshed. |
| 110 | Observer* observer_; |
| 111 | |
| 112 | DISALLOW_COPY_AND_ASSIGN(CertificateManagerModel); |
| 113 | }; |
| 114 | |
| 115 | #endif // CONTENT_BROWSER_CERTIFICATE_MANAGER_MODEL_H_ |