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