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