Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ | |
| 6 #define HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <functional> | |
| 10 #include <memory> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "headless/public/util/managed_dispatch_url_request_job.h" | |
| 17 #include "headless/public/util/url_fetcher.h" | |
| 18 #include "net/base/net_errors.h" | |
| 19 #include "net/url_request/url_request.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 namespace net { | |
| 23 class HttpResponseHeaders; | |
| 24 class IOBuffer; | |
| 25 } // namespace net | |
| 26 | |
| 27 namespace headless { | |
| 28 | |
| 29 class URLRequestDispatcher; | |
| 30 | |
| 31 /* | |
|
Sami
2016/08/19 11:54:00
nit: Could we standardize on // style comment bloc
alex clarke (OOO till 29th)
2016/08/19 12:48:57
Done.
| |
| 32 * Intended for use in a protocol handler, this ManagedDispatchURLRequestJob has | |
| 33 * the following features: | |
| 34 * | |
| 35 * 1. The delegate can extension observe / cancel and redirect requests | |
| 36 * 2. The delegate can optionally provide the results, otherwise the specifed | |
| 37 * fetcher is invoked. | |
| 38 */ | |
| 39 class GenericURLRequestJob : public ManagedDispatchURLRequestJob, | |
| 40 public URLFetcher::ResultListener { | |
| 41 public: | |
| 42 enum class RewriteResult { kAllow, kDeny, kFailure }; | |
| 43 using RewriteCallback = | |
| 44 std::function<void(RewriteResult result, const GURL& url)>; | |
| 45 | |
| 46 struct HttpResponse { | |
| 47 std::string final_url; | |
| 48 int http_response_code; | |
| 49 | |
| 50 // The HTTP headers and response body. Note the lifetime of |response_data| | |
| 51 // is expected to outlive the GenericURLRequestJob. | |
| 52 const char* response_data; // NOT OWNED | |
| 53 size_t response_data_size; | |
| 54 }; | |
| 55 | |
| 56 class Delegate { | |
| 57 public: | |
| 58 // Allows the delegate to rewrite the URL for a given request. Return true | |
| 59 // to signal that the rewrite is in progress and |callback| will be called | |
| 60 // with the result, or false to indicate that no rewriting is necessary. | |
| 61 // Called on an arbitrary thread. | |
| 62 virtual bool BlockOrRewriteRequest(const GURL& url, | |
| 63 const std::string& referrer, | |
| 64 RewriteCallback callback) = 0; | |
| 65 | |
| 66 // Allows the delegate to synchronously fulfill a request with a reply. | |
| 67 // Called on an arbitrary thread. | |
| 68 virtual const HttpResponse* MaybeMatchResource( | |
| 69 const GURL& url, | |
| 70 const net::HttpRequestHeaders& request_headers) = 0; | |
| 71 | |
| 72 // Signals that a resource load has finished. Called on an arbitrary thread. | |
| 73 virtual void OnResourceLoadComplete(const std::string& final_url, | |
| 74 const std::string& mime_type, | |
| 75 int http_response_code) = 0; | |
| 76 | |
| 77 protected: | |
| 78 virtual ~Delegate() {} | |
| 79 }; | |
| 80 | |
| 81 // NOTE |url_request_dispatcher| and |delegate| must outlive the | |
| 82 // GenericURLRequestJob. | |
| 83 GenericURLRequestJob(net::URLRequest* request, | |
| 84 net::NetworkDelegate* network_delegate, | |
| 85 URLRequestDispatcher* url_request_dispatcher, | |
| 86 std::unique_ptr<URLFetcher> url_fetcher, | |
| 87 Delegate* delegate); | |
| 88 ~GenericURLRequestJob() override; | |
| 89 | |
| 90 // net::URLRequestJob implementation: | |
| 91 void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override; | |
| 92 void Start() override; | |
| 93 int ReadRawData(net::IOBuffer* buf, int buf_size) override; | |
| 94 int GetResponseCode() const override; | |
| 95 void GetResponseInfo(net::HttpResponseInfo* info) override; | |
| 96 bool GetMimeType(std::string* mime_type) const override; | |
| 97 bool GetCharset(std::string* charset) override; | |
| 98 | |
| 99 // URLFetcher::FetchResultListener implementation: | |
| 100 void OnStartError(net::Error error) override; | |
| 101 void OnFetchComplete(const std::string& final_url, | |
| 102 int http_response_code, | |
| 103 scoped_refptr<net::HttpResponseHeaders> response_headers, | |
| 104 const char* body, | |
| 105 size_t body_size) override; | |
| 106 | |
| 107 private: | |
| 108 void PrepareCookies(const GURL& rewritten_url, | |
| 109 const url::Origin& site_for_cookies); | |
| 110 | |
| 111 void OnCookiesAvailable(const GURL& rewritten_url, | |
| 112 const net::CookieList& cookie_list); | |
| 113 | |
| 114 std::unique_ptr<URLFetcher> url_fetcher_; | |
| 115 net::HttpRequestHeaders extra_request_headers_; | |
| 116 scoped_refptr<net::HttpResponseHeaders> response_headers_; | |
| 117 Delegate* delegate_; // Not owned. | |
| 118 const char* body_ = nullptr; // Not owned. | |
| 119 int http_response_code_ = 0; | |
| 120 size_t body_size_ = 0; | |
| 121 size_t read_offset_ = 0; | |
| 122 | |
| 123 base::WeakPtrFactory<GenericURLRequestJob> weak_factory_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(GenericURLRequestJob); | |
| 126 }; | |
| 127 | |
| 128 } // namespace headless | |
| 129 | |
| 130 #endif // HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ | |
| OLD | NEW |