| 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> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "headless/public/util/managed_dispatch_url_request_job.h" | 17 #include "headless/public/util/managed_dispatch_url_request_job.h" |
| 18 #include "headless/public/util/url_fetcher.h" | 18 #include "headless/public/util/url_fetcher.h" |
| 19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 20 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
| 21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 22 | 22 |
| 23 namespace net { | 23 namespace net { |
| 24 class HttpResponseHeaders; | 24 class HttpResponseHeaders; |
| 25 class IOBuffer; | 25 class IOBuffer; |
| 26 } // namespace net | 26 } // namespace net |
| 27 | 27 |
| 28 namespace content { |
| 29 class ResourceRequestInfo; |
| 30 } // namespace content |
| 31 |
| 28 namespace headless { | 32 namespace headless { |
| 29 | 33 |
| 30 class URLRequestDispatcher; | 34 class URLRequestDispatcher; |
| 31 | 35 |
| 36 // Wrapper around net::URLRequest with helpers to access select metadata. |
| 37 class Request { |
| 38 public: |
| 39 virtual const net::URLRequest* GetURLRequest() const = 0; |
| 40 |
| 41 // The frame from which the request came from. |
| 42 virtual int GetFrameTreeNodeId() const = 0; |
| 43 |
| 44 // The devtools agent host id for the page where the request came from. |
| 45 virtual std::string GetDevToolsAgentHostId() const = 0; |
| 46 |
| 47 enum class ResourceType { |
| 48 MAIN_FRAME = 0, |
| 49 SUB_FRAME = 1, |
| 50 STYLESHEET = 2, |
| 51 SCRIPT = 3, |
| 52 IMAGE = 4, |
| 53 FONT_RESOURCE = 5, |
| 54 SUB_RESOURCE = 6, |
| 55 OBJECT = 7, |
| 56 MEDIA = 8, |
| 57 WORKER = 9, |
| 58 SHARED_WORKER = 10, |
| 59 PREFETCH = 11, |
| 60 FAVICON = 12, |
| 61 XHR = 13, |
| 62 PING = 14, |
| 63 SERVICE_WORKER = 15, |
| 64 CSP_REPORT = 16, |
| 65 PLUGIN_RESOURCE = 17, |
| 66 LAST_TYPE |
| 67 }; |
| 68 |
| 69 virtual ResourceType GetResourceType() const = 0; |
| 70 |
| 71 protected: |
| 72 Request() {} |
| 73 virtual ~Request() {} |
| 74 |
| 75 private: |
| 76 DISALLOW_COPY_AND_ASSIGN(Request); |
| 77 }; |
| 78 |
| 79 // Details of a pending request received by GenericURLRequestJob which must be |
| 80 // either Allowed, Blocked, Modified or have it's response Mocked. |
| 81 class PendingRequest { |
| 82 public: |
| 83 virtual const Request* GetRequest() const = 0; |
| 84 |
| 85 // Allows the request to proceed as normal. |
| 86 virtual void AllowRequest() = 0; |
| 87 |
| 88 // Causes the request to fail with the specified |error|. |
| 89 virtual void BlockRequest(net::Error error) = 0; |
| 90 |
| 91 // Allows the request to be completely re-written. |
| 92 virtual void ModifyRequest( |
| 93 const GURL& url, |
| 94 const std::string& method, |
| 95 const std::string& post_data, |
| 96 const net::HttpRequestHeaders& request_headers) = 0; |
| 97 |
| 98 struct MockResponseData { |
| 99 int http_response_code = 0; |
| 100 std::string response_data; |
| 101 }; |
| 102 |
| 103 // Instead of fetching the request, |mock_response| is returned instead. |
| 104 virtual void MockResponse( |
| 105 std::unique_ptr<MockResponseData> mock_response) = 0; |
| 106 |
| 107 protected: |
| 108 PendingRequest() {} |
| 109 virtual ~PendingRequest() {} |
| 110 |
| 111 private: |
| 112 DISALLOW_COPY_AND_ASSIGN(PendingRequest); |
| 113 }; |
| 114 |
| 32 // Intended for use in a protocol handler, this ManagedDispatchURLRequestJob has | 115 // Intended for use in a protocol handler, this ManagedDispatchURLRequestJob has |
| 33 // the following features: | 116 // the following features: |
| 34 // | 117 // |
| 35 // 1. The delegate can extension observe / cancel and redirect requests | 118 // 1. The delegate can extension observe / cancel and redirect requests |
| 36 // 2. The delegate can optionally provide the results, otherwise the specifed | 119 // 2. The delegate can optionally provide the results, otherwise the specifed |
| 37 // fetcher is invoked. | 120 // fetcher is invoked. |
| 38 class HEADLESS_EXPORT GenericURLRequestJob | 121 class HEADLESS_EXPORT GenericURLRequestJob |
| 39 : public ManagedDispatchURLRequestJob, | 122 : public ManagedDispatchURLRequestJob, |
| 40 public URLFetcher::ResultListener { | 123 public URLFetcher::ResultListener, |
| 124 public PendingRequest, |
| 125 public Request { |
| 41 public: | 126 public: |
| 42 enum class RewriteResult { kAllow, kDeny, kFailure }; | |
| 43 using RewriteCallback = base::Callback< | |
| 44 void(RewriteResult result, const GURL& url, const std::string& method)>; | |
| 45 | |
| 46 struct HttpResponse { | |
| 47 GURL 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 { | 127 class Delegate { |
| 57 public: | 128 public: |
| 58 // Allows the delegate to rewrite the URL for a given request. Return true | 129 // Notifies the delegate of an PendingRequest which must either be |
| 59 // to signal that the rewrite is in progress and |callback| will be called | 130 // allowed, blocked, modifed or it's response mocked. Called on an arbitrary |
| 60 // with the result, or false to indicate that no rewriting is necessary. | 131 // thread. |
| 61 // Called on an arbitrary thread. |callback| can be called on any thread. | 132 virtual void OnPendingRequest(PendingRequest* pending_request) = 0; |
| 62 virtual bool BlockOrRewriteRequest(const GURL& url, | |
| 63 const std::string& devtools_id, | |
| 64 const std::string& method, | |
| 65 const std::string& referrer, | |
| 66 RewriteCallback callback) = 0; | |
| 67 | 133 |
| 68 // Allows the delegate to synchronously fulfill a request with a reply. | 134 // Notifies the delegate of any fetch failure. Called on an arbitrary |
| 69 // Called on an arbitrary thread. | 135 // thread. |
| 70 virtual const HttpResponse* MaybeMatchResource( | 136 virtual void OnResourceLoadFailed(const Request* request, |
| 71 const GURL& url, | 137 net::Error error) = 0; |
| 72 const std::string& devtools_id, | |
| 73 const std::string& method, | |
| 74 const net::HttpRequestHeaders& request_headers) = 0; | |
| 75 | 138 |
| 76 // Signals that a resource load has finished. Called on an arbitrary thread. | 139 // Signals that a resource load has finished. Called on an arbitrary thread. |
| 77 virtual void OnResourceLoadComplete(const GURL& final_url, | 140 virtual void OnResourceLoadComplete( |
| 78 const std::string& devtools_id, | 141 const Request* request, |
| 79 const std::string& mime_type, | 142 const GURL& final_url, |
| 80 int http_response_code) = 0; | 143 int http_response_code, |
| 144 scoped_refptr<net::HttpResponseHeaders> response_headers, |
| 145 const char* body, |
| 146 size_t body_size) = 0; |
| 81 | 147 |
| 82 protected: | 148 protected: |
| 83 virtual ~Delegate() {} | 149 virtual ~Delegate() {} |
| 84 }; | 150 }; |
| 85 | 151 |
| 86 // NOTE |url_request_dispatcher| and |delegate| must outlive the | 152 // NOTE |url_request_dispatcher| and |delegate| must outlive the |
| 87 // GenericURLRequestJob. | 153 // GenericURLRequestJob. |
| 88 GenericURLRequestJob(net::URLRequest* request, | 154 GenericURLRequestJob(net::URLRequest* request, |
| 89 net::NetworkDelegate* network_delegate, | 155 net::NetworkDelegate* network_delegate, |
| 90 URLRequestDispatcher* url_request_dispatcher, | 156 URLRequestDispatcher* url_request_dispatcher, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 103 void GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override; | 169 void GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override; |
| 104 | 170 |
| 105 // URLFetcher::FetchResultListener implementation: | 171 // URLFetcher::FetchResultListener implementation: |
| 106 void OnFetchStartError(net::Error error) override; | 172 void OnFetchStartError(net::Error error) override; |
| 107 void OnFetchComplete(const GURL& final_url, | 173 void OnFetchComplete(const GURL& final_url, |
| 108 int http_response_code, | 174 int http_response_code, |
| 109 scoped_refptr<net::HttpResponseHeaders> response_headers, | 175 scoped_refptr<net::HttpResponseHeaders> response_headers, |
| 110 const char* body, | 176 const char* body, |
| 111 size_t body_size) override; | 177 size_t body_size) override; |
| 112 | 178 |
| 179 protected: |
| 180 // Request implementation: |
| 181 const net::URLRequest* GetURLRequest() const override; |
| 182 int GetFrameTreeNodeId() const override; |
| 183 std::string GetDevToolsAgentHostId() const override; |
| 184 ResourceType GetResourceType() const override; |
| 185 |
| 186 // PendingRequest implementation: |
| 187 const Request* GetRequest() const override; |
| 188 void AllowRequest() override; |
| 189 void BlockRequest(net::Error error) override; |
| 190 void ModifyRequest(const GURL& url, |
| 191 const std::string& method, |
| 192 const std::string& post_data, |
| 193 const net::HttpRequestHeaders& request_headers) override; |
| 194 void MockResponse(std::unique_ptr<MockResponseData> mock_response) override; |
| 195 |
| 113 private: | 196 private: |
| 114 static void OnRewriteResult( | |
| 115 base::WeakPtr<GenericURLRequestJob> weak_this, | |
| 116 const scoped_refptr<base::SingleThreadTaskRunner>& origin_task_runner, | |
| 117 RewriteResult result, | |
| 118 const GURL& url, | |
| 119 const std::string& method); | |
| 120 void OnRewriteResultOnOriginThread(RewriteResult result, | |
| 121 const GURL& url, | |
| 122 const std::string& method); | |
| 123 void PrepareCookies(const GURL& rewritten_url, | 197 void PrepareCookies(const GURL& rewritten_url, |
| 124 const std::string& method, | 198 const std::string& method, |
| 125 const url::Origin& site_for_cookies); | 199 const url::Origin& site_for_cookies, |
| 200 const base::Closure& done_callback); |
| 126 void OnCookiesAvailable(const GURL& rewritten_url, | 201 void OnCookiesAvailable(const GURL& rewritten_url, |
| 127 const std::string& method, | 202 const std::string& method, |
| 203 const base::Closure& done_callback, |
| 128 const net::CookieList& cookie_list); | 204 const net::CookieList& cookie_list); |
| 129 | 205 |
| 130 std::unique_ptr<URLFetcher> url_fetcher_; | 206 std::unique_ptr<URLFetcher> url_fetcher_; |
| 131 net::HttpRequestHeaders extra_request_headers_; | 207 net::HttpRequestHeaders extra_request_headers_; |
| 132 scoped_refptr<net::HttpResponseHeaders> response_headers_; | 208 scoped_refptr<net::HttpResponseHeaders> response_headers_; |
| 133 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; | 209 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
| 134 std::string devtools_request_id_; | 210 std::unique_ptr<MockResponseData> mock_response_; |
| 135 Delegate* delegate_; // Not owned. | 211 Delegate* delegate_; // Not owned. |
| 212 const content::ResourceRequestInfo* request_resource_info_; // Not owned. |
| 136 const char* body_ = nullptr; // Not owned. | 213 const char* body_ = nullptr; // Not owned. |
| 137 int http_response_code_ = 0; | 214 int http_response_code_ = 0; |
| 138 size_t body_size_ = 0; | 215 size_t body_size_ = 0; |
| 139 size_t read_offset_ = 0; | 216 size_t read_offset_ = 0; |
| 140 base::TimeTicks response_time_; | 217 base::TimeTicks response_time_; |
| 141 | 218 |
| 142 base::WeakPtrFactory<GenericURLRequestJob> weak_factory_; | 219 base::WeakPtrFactory<GenericURLRequestJob> weak_factory_; |
| 143 | 220 |
| 144 DISALLOW_COPY_AND_ASSIGN(GenericURLRequestJob); | 221 DISALLOW_COPY_AND_ASSIGN(GenericURLRequestJob); |
| 145 }; | 222 }; |
| 146 | 223 |
| 147 } // namespace headless | 224 } // namespace headless |
| 148 | 225 |
| 149 #endif // HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ | 226 #endif // HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ |
| OLD | NEW |