Chromium Code Reviews| Index: net/cert/cert_net_fetcher.h |
| diff --git a/net/cert/cert_net_fetcher.h b/net/cert/cert_net_fetcher.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8f532bdf4f5d3ca4e872f9cadfa338c2feff1068 |
| --- /dev/null |
| +++ b/net/cert/cert_net_fetcher.h |
| @@ -0,0 +1,165 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef NET_CERT_CERT_NET_FETCHER_H_ |
| +#define NET_CERT_CERT_NET_FETCHER_H_ |
| + |
| +#include <set> |
| +#include <vector> |
| + |
| +#include "base/callback.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/net_export.h" |
| +#include "url/gurl.h" |
|
Ryan Sleevi
2015/03/25 06:16:37
Can forward declare
eroman
2015/03/26 03:50:49
Done.
|
| + |
| +namespace net { |
| + |
| +class URLRequestContext; |
| + |
| +// CertNetFetcher is a helper for fetching AIA URLs and CRL URLs. |
| +// |
| +// It manages the outstanding requests, and applies policies specific to |
| +// certificate network fetches. For instance: |
| +// * Enforces a maximum timeout on responses |
| +// * Enforces a maximum size on responses |
| +// * Restricts URLs to http:// |
| +// |
| +// The CertNetFetcher is initialized by giving it a URLRequestContext for the |
| +// network dependencies. Note that it does not maintain its own in-memory cache |
| +// for responses. Instead it is reliant on the URLRequestContext providing an |
| +// HTTP cache if this is desired. |
| +// |
| +// ------------------------- |
| +// Cancellation of requests |
| +// ------------------------- |
| +// |
| +// * Network requests started by the CertNetFetcher can be cancelled using |
| +// CancelRequest(). Cancellation means the callback will not be invoked. |
| +// |
| +// * If the CertNetFetcher is deleted then any outstanding |
| +// requests are automatically cancelled. |
| +// |
| +// * Cancelling a request more than once is an error (and may result in a |
| +// use-after-free). |
| +// |
| +// * Cancelling a request within the execution of a callback is allowed. |
| +// |
| +// * Deleting the CertNetFetcher from within the execution of a callback is |
| +// allowed. |
| +// |
| +// ------------------------- |
| +// Threading |
| +// ------------------------- |
| +// |
| +// The CertNetFetcher is expected to be operated from a single thread, which has |
| +// an IO message loop. The URLRequestContext will be accessed from this same |
| +// thread, and callbacks will be posted to this message loop. |
| +// |
| +// For more details see the early design document: |
| +// https://docs.google.com/a/chromium.org/document/d/1CdS9YOnPdAyVZBJqHY7ZJ6tUlU71OCvX8kHnaVhf144/edit |
| +class NET_EXPORT CertNetFetcher { |
| + private: |
| + struct Request; |
| + |
| + public: |
| + using FetchCallback = |
| + base::Callback<void(Error, const std::vector<uint8_t>&)>; |
| + using RequestId = Request*; |
| + |
| + // This value can be used in place of timeout or max size limits. |
| + static const int DEFAULT = -1; |
|
Ryan Sleevi
2015/03/25 06:16:37
So this syntax is super sketch (there's some old c
eroman
2015/03/26 03:50:49
Interesting, I have gotten the opposite feedback f
|
| + |
| + // Initializes CertNetFetcher using the specified URLRequestContext for |
| + // issuing requests. |context| must remain valid for the entire lifetime of |
| + // the CertNetFetcher. |
| + explicit CertNetFetcher(URLRequestContext* context); |
| + |
| + // Deletion implicitly cancels any outstanding requests. |
| + ~CertNetFetcher(); |
| + |
| + // Cancels the indicated request. It is an error to call this function on a |
| + // request which has already completed (including one that was already |
| + // cancelled). |
| + void CancelRequest(RequestId request); |
| + |
| + // The Fetch*() methods start an asynchronous request which can be cancelled |
| + // using RequestId. Here is the meaning of the common parameters: |
| + // |
| + // * url -- The http:// URL to fetch. |
| + // * timeout_seconds -- The maximum allowed duration for the fetch job. If |
| + // this delay is exceeded then the request will fail. To use a default |
| + // timeout pass DEFAULT. |
| + // * max_response_bytes -- The maximum size of the response body. If this |
| + // size is exceeded then the request will fail. To use a default timeout |
| + // pass DEFAULT. |
| + // * callback -- The callback that will be invoked on completion of the job. |
| + |
| + RequestId FetchCaIssuers(const GURL& url, |
| + int timeout_milliseconds, |
| + int max_response_bytes, |
| + const FetchCallback& callback); |
| + |
| + RequestId FetchCrl(const GURL& url, |
| + int timeout_milliseconds, |
| + int max_response_bytes, |
| + const FetchCallback& callback); |
| + |
| + RequestId FetchOcsp(const GURL& url, |
| + int timeout_milliseconds, |
| + int max_response_bytes, |
| + const FetchCallback& callback); |
| + |
| + private: |
| + class Job; |
| + struct JobToRequestParamsComparator; |
| + struct RequestParams; |
| + |
| + struct JobComparator { |
| + bool operator()(const Job* job1, const Job* job2) const; |
| + }; |
| + |
| + using JobSet = std::set<Job*, JobComparator>; |
| + |
| + // Starts an asynchronous request to fetch the given URL. On completion |
| + // |callback| will be invoked. |
| + // |
| + // Completion of the request will never occur synchronously. In other words it |
| + // is guaranteed that |callback| will only be invoked once the Fetch*() method |
| + // has returned. |
| + RequestId Fetch(scoped_ptr<RequestParams> request_params, |
| + const FetchCallback& callback); |
| + |
| + // Finds a job with a matching RequestPararms or returns NULL if there was no |
| + // match. |
| + Job* FindJob(const RequestParams& params); |
| + |
| + // Removes |job| from the in progress jobs and transfers ownership to the |
| + // caller. |
| + scoped_ptr<Job> RemoveJob(Job* job); |
| + |
| + // Indicates which Job is currently executing inside of OnJobCompleted(). |
| + void SetCurrentlyCompletingJob(Job* job); |
| + |
| + // The in-progress jobs. This set does not contain the job which is actively |
| + // invoking callbacks (OnJobCompleted). Instead that is tracked by |
| + // |currently_completing_job_|. |
| + JobSet jobs_; |
| + |
| + // The Job that is currently executing OnJobCompleted(). There can be at most |
| + // one such job. This pointer is not owned. |
| + Job* currently_completing_job_; |
| + |
| + // Not owned. CertNetFetcher must outlive the URLRequestContext. |
| + URLRequestContext* context_; |
| + |
| + base::ThreadChecker thread_checker_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CertNetFetcher); |
| +}; |
| + |
| +} // namespace net |
| + |
| +#endif // NET_CERT_CERT_NET_FETCHER_H_ |