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