cert_net_fetcher.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2015 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_NET_FETCHER_H_
  5. #define NET_CERT_CERT_NET_FETCHER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/memory/ref_counted.h"
  10. #include "net/base/net_errors.h"
  11. #include "net/base/net_export.h"
  12. class GURL;
  13. namespace net {
  14. // CertNetFetcher is a synchronous interface for fetching AIA URLs and CRL
  15. // URLs. It is shared between a caller thread (which starts and waits for
  16. // fetches), and a network thread (which does the actual fetches). It can be
  17. // shutdown from the network thread to cancel outstanding requests.
  18. //
  19. // A Request object is returned when starting a fetch. The consumer can
  20. // use this as a handle for aborting the request (by freeing it), or reading
  21. // the result of the request (WaitForResult)
  22. class NET_EXPORT CertNetFetcher
  23. : public base::RefCountedThreadSafe<CertNetFetcher> {
  24. public:
  25. class Request {
  26. public:
  27. virtual ~Request() = default;
  28. // WaitForResult() can be called at most once.
  29. //
  30. // It will block and wait for the (network) request to complete, and
  31. // then write the result into the provided out-parameters.
  32. virtual void WaitForResult(Error* error, std::vector<uint8_t>* bytes) = 0;
  33. };
  34. // This value can be used in place of timeout or max size limits.
  35. enum { DEFAULT = -1 };
  36. CertNetFetcher() = default;
  37. CertNetFetcher(const CertNetFetcher&) = delete;
  38. CertNetFetcher& operator=(const CertNetFetcher&) = delete;
  39. // Shuts down the CertNetFetcher and cancels outstanding network requests. It
  40. // is not guaranteed that any outstanding or subsequent
  41. // Request::WaitForResult() calls will be completed. Shutdown() must be called
  42. // from the network thread. It can be called more than once, but must be
  43. // called before the CertNetFetcher is destroyed.
  44. virtual void Shutdown() = 0;
  45. // The Fetch*() methods start a request which can be cancelled by
  46. // deleting the returned Request. Here is the meaning of the common
  47. // parameters:
  48. //
  49. // * url -- The http:// URL to fetch.
  50. // * timeout_seconds -- The maximum allowed duration for the fetch job. If
  51. // this delay is exceeded then the request will fail. To use a default
  52. // timeout pass DEFAULT.
  53. // * max_response_bytes -- The maximum size of the response body. If this
  54. // size is exceeded then the request will fail. To use a default timeout
  55. // pass DEFAULT.
  56. [[nodiscard]] virtual std::unique_ptr<Request> FetchCaIssuers(
  57. const GURL& url,
  58. int timeout_milliseconds,
  59. int max_response_bytes) = 0;
  60. [[nodiscard]] virtual std::unique_ptr<Request> FetchCrl(
  61. const GURL& url,
  62. int timeout_milliseconds,
  63. int max_response_bytes) = 0;
  64. [[nodiscard]] virtual std::unique_ptr<Request> FetchOcsp(
  65. const GURL& url,
  66. int timeout_milliseconds,
  67. int max_response_bytes) = 0;
  68. protected:
  69. virtual ~CertNetFetcher() = default;
  70. private:
  71. friend class base::RefCountedThreadSafe<CertNetFetcher>;
  72. };
  73. } // namespace net
  74. #endif // NET_CERT_CERT_NET_FETCHER_H_