Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #ifndef NET_CERT_CERT_NET_FETCHER_H_ | |
| 6 #define NET_CERT_CERT_NET_FETCHER_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/time/time.h" | |
| 15 #include "net/base/net_export.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 class URLRequestContext; | |
| 21 | |
| 22 // CertNetFetcher is a helper for fetching AIA URLs and CRL URLs. | |
| 23 // | |
| 24 // It manages the outstanding requests, and applies policies specific to | |
| 25 // certificate network fetches. For instance: | |
| 26 // * Enforces a maximum timeout on responses | |
| 27 // * Enforces a maximum size on responses | |
| 28 // * Restricts URLs to http:// | |
| 29 // | |
| 30 // The CertNetFetcher is initialized by giving it a URLRequestContext for the | |
| 31 // network dependencies. Note that it does not maintain its own in-memory cache | |
| 32 // for responses. Instead it is reliant on the URLRequestContext providing an | |
| 33 // HTTP cache if this is desired. | |
| 34 // | |
| 35 // ------------------------- | |
| 36 // Cancellation of requests | |
| 37 // ------------------------- | |
| 38 // | |
| 39 // * Network requests started by the CertNetFetcher can be cancelled using | |
| 40 // CancelRequest(). Cancellation means the callback will not be invoked. | |
| 41 // | |
| 42 // * If the CertNetFetcher is deleted then any outstanding | |
| 43 // requests are automatically cancelled. | |
| 44 // | |
| 45 // * Cancelling a request more than once is an error (and may result in a | |
| 46 // use-after-free). | |
| 47 // | |
| 48 // * Cancelling a request within the execution of a callback is allowed. | |
| 49 // | |
| 50 // * Deleting the CertNetFetcher from within the execution of a callback is | |
| 51 // allowed. | |
| 52 // | |
| 53 // ------------------------- | |
| 54 // Threading | |
| 55 // ------------------------- | |
| 56 // | |
| 57 // The CertNetFetcher is expected to be operated from a single thread, which has | |
| 58 // an IO message loop. The URLRequestContext will be accessed from this same | |
| 59 // thread, and callbacks will be posted to this message loop. | |
| 60 // | |
| 61 // For more details see the early design document: | |
| 62 // https://docs.google.com/a/chromium.org/document/d/1CdS9YOnPdAyVZBJqHY7ZJ6tU lU71OCvX8kHnaVhf144/edit | |
| 63 class NET_EXPORT CertNetFetcher { | |
|
mattm
2015/02/10 05:22:14
Maybe inherit NonThreadSafe and add CalledOnValidT
eroman
2015/02/11 03:51:52
Done.
| |
| 64 private: | |
| 65 struct Request; | |
| 66 | |
| 67 public: | |
| 68 typedef base::Callback<void(int, const std::vector<uint8_t>&)> FetchCallback; | |
| 69 | |
| 70 enum RequestType { | |
| 71 REQUEST_TYPE_CA_ISSUERS, | |
| 72 REQUEST_TYPE_OCSP, | |
| 73 REQUEST_TYPE_CRL, | |
| 74 }; | |
| 75 | |
| 76 enum HttpMethod { | |
| 77 HTTP_METHOD_GET, | |
| 78 HTTP_METHOD_POST, | |
| 79 }; | |
| 80 | |
| 81 struct RequestParams { | |
| 82 RequestParams(const GURL& url, RequestType type); | |
| 83 | |
| 84 bool operator<(const RequestParams& other) const; | |
| 85 | |
| 86 GURL url; | |
| 87 HttpMethod http_method; | |
| 88 size_t max_response_size_in_bytes; | |
| 89 | |
| 90 // If set to a value <= 0 then means "no timeout". | |
| 91 base::TimeDelta timeout; | |
| 92 | |
| 93 // IMPORTANT: When adding fields to this structure, update operator<(). | |
| 94 }; | |
| 95 | |
| 96 typedef Request* RequestId; | |
| 97 | |
| 98 // Initilizes CertNetFetcher using the specified URLRequestContext for issuing | |
| 99 // requests. |context| must remain valid for the entire lifetime of the | |
| 100 // CertNetFetcher. | |
| 101 explicit CertNetFetcher(URLRequestContext* context); | |
| 102 | |
| 103 ~CertNetFetcher(); | |
| 104 | |
| 105 // Starts an asynchronous request to fetch the given URL. On completion | |
| 106 // |callback| will be invoked. | |
| 107 // | |
| 108 // Completion of the request will never occur synchronously. In other words it | |
| 109 // is guaranteed that |callback| will only be invoked once the Fetch*() method | |
| 110 // has returned. | |
| 111 RequestId Fetch(scoped_ptr<RequestParams> request_params, | |
| 112 FetchCallback callback); | |
| 113 | |
| 114 // Cancels the indicated request. It is an error to call this function on a | |
| 115 // request which has already completed (including one that was already | |
| 116 // cancelled). | |
| 117 void CancelRequest(RequestId request); | |
| 118 | |
| 119 private: | |
| 120 class Job; | |
| 121 struct JobToRequestParamsComparator; | |
| 122 | |
| 123 struct JobComparator { | |
| 124 bool operator()(const scoped_refptr<Job>& job1, | |
| 125 const scoped_refptr<Job>& job2) const; | |
| 126 }; | |
| 127 | |
| 128 typedef std::set<scoped_refptr<Job>, JobComparator> JobSet; | |
| 129 | |
| 130 // Finds a job with a matching RequestPararms or returns NULL if there was no | |
| 131 // match. | |
| 132 Job* FindJob(const RequestParams& params); | |
| 133 | |
| 134 // Removes |job| from the in progress jobs. This may cause the final reference | |
| 135 // to |job| to be released. | |
| 136 void RemoveJob(Job* job); | |
| 137 | |
| 138 // The in-progress jobs. | |
| 139 JobSet jobs_; | |
| 140 | |
| 141 // Not owned. CertNetFetcher must outlive the URLRequestContext. | |
| 142 URLRequestContext* context_; | |
| 143 | |
| 144 DISALLOW_COPY_AND_ASSIGN(CertNetFetcher); | |
| 145 }; | |
| 146 | |
| 147 } // namespace net | |
| 148 | |
| 149 #endif // NET_CERT_CERT_NET_FETCHER_H_ | |
| OLD | NEW |