| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ | 5 #ifndef HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ |
| 6 #define HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ | 6 #define HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <functional> | 9 #include <functional> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // Intended for use in a protocol handler, this ManagedDispatchURLRequestJob has | 31 // Intended for use in a protocol handler, this ManagedDispatchURLRequestJob has |
| 32 // the following features: | 32 // the following features: |
| 33 // | 33 // |
| 34 // 1. The delegate can extension observe / cancel and redirect requests | 34 // 1. The delegate can extension observe / cancel and redirect requests |
| 35 // 2. The delegate can optionally provide the results, otherwise the specifed | 35 // 2. The delegate can optionally provide the results, otherwise the specifed |
| 36 // fetcher is invoked. | 36 // fetcher is invoked. |
| 37 class GenericURLRequestJob : public ManagedDispatchURLRequestJob, | 37 class GenericURLRequestJob : public ManagedDispatchURLRequestJob, |
| 38 public URLFetcher::ResultListener { | 38 public URLFetcher::ResultListener { |
| 39 public: | 39 public: |
| 40 enum class RewriteResult { kAllow, kDeny, kFailure }; | 40 enum class RewriteResult { kAllow, kDeny, kFailure }; |
| 41 using RewriteCallback = | 41 using RewriteCallback = std::function< |
| 42 std::function<void(RewriteResult result, const GURL& url)>; | 42 void(RewriteResult result, const GURL& url, const std::string& method)>; |
| 43 | 43 |
| 44 struct HttpResponse { | 44 struct HttpResponse { |
| 45 GURL final_url; | 45 GURL final_url; |
| 46 int http_response_code; | 46 int http_response_code; |
| 47 | 47 |
| 48 // The HTTP headers and response body. Note the lifetime of |response_data| | 48 // The HTTP headers and response body. Note the lifetime of |response_data| |
| 49 // is expected to outlive the GenericURLRequestJob. | 49 // is expected to outlive the GenericURLRequestJob. |
| 50 const char* response_data; // NOT OWNED | 50 const char* response_data; // NOT OWNED |
| 51 size_t response_data_size; | 51 size_t response_data_size; |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 class Delegate { | 54 class Delegate { |
| 55 public: | 55 public: |
| 56 // Allows the delegate to rewrite the URL for a given request. Return true | 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 | 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. | 58 // with the result, or false to indicate that no rewriting is necessary. |
| 59 // Called on an arbitrary thread. | 59 // Called on an arbitrary thread. |
| 60 virtual bool BlockOrRewriteRequest(const GURL& url, | 60 virtual bool BlockOrRewriteRequest(const GURL& url, |
| 61 const std::string& method, |
| 61 const std::string& referrer, | 62 const std::string& referrer, |
| 62 RewriteCallback callback) = 0; | 63 RewriteCallback callback) = 0; |
| 63 | 64 |
| 64 // Allows the delegate to synchronously fulfill a request with a reply. | 65 // Allows the delegate to synchronously fulfill a request with a reply. |
| 65 // Called on an arbitrary thread. | 66 // Called on an arbitrary thread. |
| 66 virtual const HttpResponse* MaybeMatchResource( | 67 virtual const HttpResponse* MaybeMatchResource( |
| 67 const GURL& url, | 68 const GURL& url, |
| 69 const std::string& method, |
| 68 const net::HttpRequestHeaders& request_headers) = 0; | 70 const net::HttpRequestHeaders& request_headers) = 0; |
| 69 | 71 |
| 70 // Signals that a resource load has finished. Called on an arbitrary thread. | 72 // Signals that a resource load has finished. Called on an arbitrary thread. |
| 71 virtual void OnResourceLoadComplete(const GURL& final_url, | 73 virtual void OnResourceLoadComplete(const GURL& final_url, |
| 72 const std::string& mime_type, | 74 const std::string& mime_type, |
| 73 int http_response_code) = 0; | 75 int http_response_code) = 0; |
| 74 | 76 |
| 75 protected: | 77 protected: |
| 76 virtual ~Delegate() {} | 78 virtual ~Delegate() {} |
| 77 }; | 79 }; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 97 // URLFetcher::FetchResultListener implementation: | 99 // URLFetcher::FetchResultListener implementation: |
| 98 void OnFetchStartError(net::Error error) override; | 100 void OnFetchStartError(net::Error error) override; |
| 99 void OnFetchComplete(const GURL& final_url, | 101 void OnFetchComplete(const GURL& final_url, |
| 100 int http_response_code, | 102 int http_response_code, |
| 101 scoped_refptr<net::HttpResponseHeaders> response_headers, | 103 scoped_refptr<net::HttpResponseHeaders> response_headers, |
| 102 const char* body, | 104 const char* body, |
| 103 size_t body_size) override; | 105 size_t body_size) override; |
| 104 | 106 |
| 105 private: | 107 private: |
| 106 void PrepareCookies(const GURL& rewritten_url, | 108 void PrepareCookies(const GURL& rewritten_url, |
| 109 const std::string& method, |
| 107 const url::Origin& site_for_cookies); | 110 const url::Origin& site_for_cookies); |
| 108 | 111 |
| 109 void OnCookiesAvailable(const GURL& rewritten_url, | 112 void OnCookiesAvailable(const GURL& rewritten_url, |
| 113 const std::string& method, |
| 110 const net::CookieList& cookie_list); | 114 const net::CookieList& cookie_list); |
| 111 | 115 |
| 112 std::unique_ptr<URLFetcher> url_fetcher_; | 116 std::unique_ptr<URLFetcher> url_fetcher_; |
| 113 net::HttpRequestHeaders extra_request_headers_; | 117 net::HttpRequestHeaders extra_request_headers_; |
| 114 scoped_refptr<net::HttpResponseHeaders> response_headers_; | 118 scoped_refptr<net::HttpResponseHeaders> response_headers_; |
| 115 Delegate* delegate_; // Not owned. | 119 Delegate* delegate_; // Not owned. |
| 116 const char* body_ = nullptr; // Not owned. | 120 const char* body_ = nullptr; // Not owned. |
| 117 int http_response_code_ = 0; | 121 int http_response_code_ = 0; |
| 118 size_t body_size_ = 0; | 122 size_t body_size_ = 0; |
| 119 size_t read_offset_ = 0; | 123 size_t read_offset_ = 0; |
| 120 | 124 |
| 121 base::WeakPtrFactory<GenericURLRequestJob> weak_factory_; | 125 base::WeakPtrFactory<GenericURLRequestJob> weak_factory_; |
| 122 | 126 |
| 123 DISALLOW_COPY_AND_ASSIGN(GenericURLRequestJob); | 127 DISALLOW_COPY_AND_ASSIGN(GenericURLRequestJob); |
| 124 }; | 128 }; |
| 125 | 129 |
| 126 } // namespace headless | 130 } // namespace headless |
| 127 | 131 |
| 128 #endif // HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ | 132 #endif // HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ |
| OLD | NEW |