coalescing_cert_verifier.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2019 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_COALESCING_CERT_VERIFIER_H_
  5. #define NET_CERT_COALESCING_CERT_VERIFIER_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <vector>
  10. #include "net/base/net_export.h"
  11. #include "net/cert/cert_verifier.h"
  12. namespace net {
  13. // CoalescingCertVerifier is a CertVerifier that keeps track of in-flight
  14. // CertVerifier Verify() requests. If a new call to Verify() is started that
  15. // matches the same parameters as an in-progress verification, the new
  16. // Verify() call will be joined to the existing, in-progress verification,
  17. // completing when it does. If no in-flight requests match, a new request to
  18. // the underlying verifier will be started.
  19. //
  20. // If the underlying configuration changes, existing requests are allowed to
  21. // complete, but any new requests will not be seen as matching, even if they
  22. // share the same parameters. This ensures configuration changes propagate
  23. // "immediately" for all new requests.
  24. class NET_EXPORT CoalescingCertVerifier : public CertVerifier {
  25. public:
  26. // Create a new verifier that will forward calls to |verifier|, coalescing
  27. // any in-flight, not-yet-completed calls to Verify().
  28. explicit CoalescingCertVerifier(std::unique_ptr<CertVerifier> verifier);
  29. CoalescingCertVerifier(const CoalescingCertVerifier&) = delete;
  30. CoalescingCertVerifier& operator=(const CoalescingCertVerifier&) = delete;
  31. ~CoalescingCertVerifier() override;
  32. // CertVerifier implementation:
  33. int Verify(const RequestParams& params,
  34. CertVerifyResult* verify_result,
  35. CompletionOnceCallback callback,
  36. std::unique_ptr<CertVerifier::Request>* out_req,
  37. const NetLogWithSource& net_log) override;
  38. void SetConfig(const CertVerifier::Config& config) override;
  39. uint64_t requests_for_testing() const { return requests_; }
  40. uint64_t inflight_joins_for_testing() const { return inflight_joins_; }
  41. private:
  42. class Job;
  43. class Request;
  44. // If there is a pending request that matches |params|, and which can be
  45. // joined (it shares the same config), returns that Job.
  46. // Otherwise, returns nullptr, meaning a new Job should be started.
  47. Job* FindJob(const RequestParams& params);
  48. void RemoveJob(Job* job);
  49. // Contains the set of Jobs for which an active verification is taking
  50. // place and which can be used for new requests (e.g. the config is the
  51. // same).
  52. std::map<CertVerifier::RequestParams, std::unique_ptr<Job>> joinable_jobs_;
  53. // Contains all pending Jobs that are in-flight, but cannot be joined, due
  54. // to the configuration having changed since they were started.
  55. std::vector<std::unique_ptr<Job>> inflight_jobs_;
  56. std::unique_ptr<CertVerifier> verifier_;
  57. uint32_t config_id_ = 0;
  58. uint64_t requests_ = 0;
  59. uint64_t inflight_joins_ = 0;
  60. };
  61. } // namespace net
  62. #endif // NET_CERT_COALESCING_CERT_VERIFIER_H_