crl_set_distributor.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2018 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 SERVICES_NETWORK_CRL_SET_DISTRIBUTOR_H_
  5. #define SERVICES_NETWORK_CRL_SET_DISTRIBUTOR_H_
  6. #include <stdint.h>
  7. #include "base/callback.h"
  8. #include "base/component_export.h"
  9. #include "base/containers/span.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/observer_list.h"
  13. #include "net/cert/crl_set.h"
  14. namespace network {
  15. // CRLSetDistributor is a helper class to handle fan-out distribution of
  16. // new CRLSets. As new encoded CRLSets are received (via OnNewCRLSet), they
  17. // will be parsed and, if successful and a later sequence than the current
  18. // CRLSet, dispatched to CRLSetDistributor::Observers' OnNewCRLSet().
  19. class COMPONENT_EXPORT(NETWORK_SERVICE) CRLSetDistributor {
  20. public:
  21. class Observer {
  22. public:
  23. // Called whenever a new CRLSet, |crl_set|, has been received.
  24. virtual void OnNewCRLSet(scoped_refptr<net::CRLSet> crl_set) = 0;
  25. protected:
  26. virtual ~Observer() = default;
  27. };
  28. CRLSetDistributor();
  29. ~CRLSetDistributor();
  30. // Adds an observer to be notified when new CRLSets are available.
  31. // Note: Newly-added observers are not notified on the current |crl_set()|,
  32. // only newly configured CRLSets after the AddObserver call.
  33. void AddObserver(Observer* observer);
  34. // Removes a previously registered observer.
  35. void RemoveObserver(Observer* observer);
  36. // Returns the currently configured CRLSet, or nullptr if one has not yet
  37. // been configured.
  38. scoped_refptr<net::CRLSet> crl_set() const { return crl_set_; }
  39. // Notifies the distributor that a new encoded CRLSet, |crl_set|, has been
  40. // received. If the CRLSet successfully decodes and is newer than the
  41. // current CRLSet, all observers will be notified. |callback| will be
  42. // notified once all observers have been notified. |callback| is guaranteed
  43. // to run (e.g. even if this object is deleted prior to it being run).
  44. void OnNewCRLSet(base::span<const uint8_t> crl_set,
  45. base::OnceClosure callback);
  46. private:
  47. void OnCRLSetParsed(scoped_refptr<net::CRLSet> crl_set);
  48. base::ObserverList<Observer,
  49. true /*check_empty*/,
  50. false /*allow_reentrancy*/>::Unchecked observers_;
  51. scoped_refptr<net::CRLSet> crl_set_;
  52. base::WeakPtrFactory<CRLSetDistributor> weak_factory_{this};
  53. };
  54. } // namespace network
  55. #endif // SERVICES_NETWORK_CRL_SET_DISTRIBUTOR_H_