cert_database.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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
  3. // found in the LICENSE file.
  4. #ifndef NET_CERT_CERT_DATABASE_H_
  5. #define NET_CERT_CERT_DATABASE_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "build/build_config.h"
  9. #include "net/base/net_export.h"
  10. namespace base {
  11. template <typename T> struct DefaultSingletonTraits;
  12. template <class ObserverType>
  13. class ObserverListThreadSafe;
  14. }
  15. namespace net {
  16. // This class allows callers to observe changes to the underlying certificate
  17. // stores.
  18. //
  19. // TODO(davidben): This class is really just a giant global ObserverList. It
  20. // does not do anything with the platform certificate and, in principle, //net's
  21. // dependency on the platform is abstracted behind the CertVerifier and
  22. // ClientCertStore interfaces. Ideally these signals would originate out of
  23. // those interfaces' platform implementations.
  24. class NET_EXPORT CertDatabase {
  25. public:
  26. // A CertDatabase::Observer will be notified on certificate database changes.
  27. // The change could be either a user certificate is added/removed or trust on
  28. // a certificate is changed. Observers can be registered via
  29. // CertDatabase::AddObserver, and can un-register with
  30. // CertDatabase::RemoveObserver.
  31. class NET_EXPORT Observer {
  32. public:
  33. Observer(const Observer&) = delete;
  34. Observer& operator=(const Observer&) = delete;
  35. virtual ~Observer() = default;
  36. // Called whenever the Cert Database is known to have changed.
  37. // Typically, this will be in response to a CA certificate being added,
  38. // removed, or its trust changed, but may also signal on client
  39. // certificate events when they can be reliably detected.
  40. virtual void OnCertDBChanged() {}
  41. protected:
  42. Observer() = default;
  43. };
  44. // Returns the CertDatabase singleton.
  45. static CertDatabase* GetInstance();
  46. CertDatabase(const CertDatabase&) = delete;
  47. CertDatabase& operator=(const CertDatabase&) = delete;
  48. // Registers |observer| to receive notifications of certificate changes. The
  49. // thread on which this is called is the thread on which |observer| will be
  50. // called back with notifications.
  51. void AddObserver(Observer* observer);
  52. // Unregisters |observer| from receiving notifications. This must be called
  53. // on the same thread on which AddObserver() was called.
  54. void RemoveObserver(Observer* observer);
  55. #if BUILDFLAG(IS_MAC)
  56. // Start observing and forwarding events from Keychain services on the
  57. // current thread. Current thread must have an associated CFRunLoop,
  58. // which means that this must be called from a MessageLoop of TYPE_UI.
  59. void StartListeningForKeychainEvents();
  60. #endif
  61. // Synthetically injects notifications to all observers. In general, this
  62. // should only be called by the creator of the CertDatabase. Used to inject
  63. // notifications from other DB interfaces.
  64. void NotifyObserversCertDBChanged();
  65. private:
  66. friend struct base::DefaultSingletonTraits<CertDatabase>;
  67. CertDatabase();
  68. ~CertDatabase();
  69. const scoped_refptr<base::ObserverListThreadSafe<Observer>> observer_list_;
  70. #if BUILDFLAG(IS_MAC)
  71. void ReleaseNotifier();
  72. class Notifier;
  73. friend class Notifier;
  74. raw_ptr<Notifier> notifier_ = nullptr;
  75. #endif
  76. };
  77. } // namespace net
  78. #endif // NET_CERT_CERT_DATABASE_H_