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