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; | |
Sami
2017/04/13 11:06:31
nit: DevTools
alex clarke (OOO till 29th)
2017/04/13 11:16:03
Done.
| |
46 | |
47 enum ResourceType { | |
Sami
2017/04/13 11:06:31
bikeshed: enum class?
alex clarke (OOO till 29th)
2017/04/13 11:16:03
Done.
| |
48 RESOURCE_TYPE_MAIN_FRAME = 0, | |
49 RESOURCE_TYPE_SUB_FRAME = 1, | |
50 RESOURCE_TYPE_STYLESHEET = 2, | |
51 RESOURCE_TYPE_SCRIPT = 3, | |
52 RESOURCE_TYPE_IMAGE = 4, | |
53 RESOURCE_TYPE_FONT_RESOURCE = 5, | |
54 RESOURCE_TYPE_SUB_RESOURCE = 6, | |
55 RESOURCE_TYPE_OBJECT = 7, | |
56 RESOURCE_TYPE_MEDIA = 8, | |
57 RESOURCE_TYPE_WORKER = 9, | |
58 RESOURCE_TYPE_SHARED_WORKER = 10, | |
59 RESOURCE_TYPE_PREFETCH = 11, | |
60 RESOURCE_TYPE_FAVICON = 12, | |
61 RESOURCE_TYPE_XHR = 13, | |
62 RESOURCE_TYPE_PING = 14, | |
63 RESOURCE_TYPE_SERVICE_WORKER = 15, | |
64 RESOURCE_TYPE_CSP_REPORT = 16, | |
65 RESOURCE_TYPE_PLUGIN_RESOURCE = 17, | |
66 RESOURCE_TYPE_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 request received by GenericURLRequestJob which must be either | |
80 // Allowed, Blocked, Modified or have it's response Mocked. | |
81 class IncomingRequest : public Request { | |
Sami
2017/04/13 11:06:31
Let's split this into its own class and have Reque
alex clarke (OOO till 29th)
2017/04/13 11:16:03
Done.
| |
82 public: | |
83 // Allows the request to proceed as normal. | |
84 virtual void AllowRequest() = 0; | |
85 | |
86 // Causes the request to fail with the specified |error|. | |
87 virtual void BlockRequest(net::Error error) = 0; | |
88 | |
89 // Allows the request to be completely re-written. | |
90 virtual void ModifyRequest( | |
91 const GURL& url, | |
92 const std::string& method, | |
93 const std::string& post_data, | |
94 const net::HttpRequestHeaders& request_headers) = 0; | |
95 | |
96 struct MockResponseData { | |
97 int http_response_code; | |
Sami
2017/04/13 11:06:31
nit: init to zero
alex clarke (OOO till 29th)
2017/04/13 11:16:03
Done.
| |
98 std::string response_data; | |
99 }; | |
100 | |
101 // Instead of fetching the request, |mock_response| is returned instead. | |
102 virtual void MockResponse( | |
103 std::unique_ptr<MockResponseData> mock_response) = 0; | |
104 | |
105 protected: | |
106 IncomingRequest() {} | |
107 ~IncomingRequest() override {} | |
108 | |
109 private: | |
110 DISALLOW_COPY_AND_ASSIGN(IncomingRequest); | |
111 }; | |
112 | |
32 // Intended for use in a protocol handler, this ManagedDispatchURLRequestJob has | 113 // Intended for use in a protocol handler, this ManagedDispatchURLRequestJob has |
33 // the following features: | 114 // the following features: |
34 // | 115 // |
35 // 1. The delegate can extension observe / cancel and redirect requests | 116 // 1. The delegate can extension observe / cancel and redirect requests |
36 // 2. The delegate can optionally provide the results, otherwise the specifed | 117 // 2. The delegate can optionally provide the results, otherwise the specifed |
37 // fetcher is invoked. | 118 // fetcher is invoked. |
38 class HEADLESS_EXPORT GenericURLRequestJob | 119 class HEADLESS_EXPORT GenericURLRequestJob |
39 : public ManagedDispatchURLRequestJob, | 120 : public ManagedDispatchURLRequestJob, |
40 public URLFetcher::ResultListener { | 121 public URLFetcher::ResultListener, |
122 public IncomingRequest { | |
41 public: | 123 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 { | 124 class Delegate { |
57 public: | 125 public: |
58 // Allows the delegate to rewrite the URL for a given request. Return true | 126 // Notifies the delegate of an IncomingRequest which must either be |
59 // to signal that the rewrite is in progress and |callback| will be called | 127 // 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. | 128 // thread. |
61 // Called on an arbitrary thread. |callback| can be called on any thread. | 129 virtual void OnIncomingRequest(IncomingRequest* incoming_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 | 130 |
68 // Allows the delegate to synchronously fulfill a request with a reply. | 131 // Notifies the delegate of any fetch failure. Called on an arbitrary |
69 // Called on an arbitrary thread. | 132 // thread. |
70 virtual const HttpResponse* MaybeMatchResource( | 133 virtual void OnResourceLoadFailed(const Request* request, |
71 const GURL& url, | 134 net::Error error) = 0; |
72 const std::string& devtools_id, | |
73 const std::string& method, | |
74 const net::HttpRequestHeaders& request_headers) = 0; | |
75 | 135 |
76 // Signals that a resource load has finished. Called on an arbitrary thread. | 136 // Signals that a resource load has finished. Called on an arbitrary thread. |
77 virtual void OnResourceLoadComplete(const GURL& final_url, | 137 virtual void OnResourceLoadComplete( |
78 const std::string& devtools_id, | 138 const Request* request, |
79 const std::string& mime_type, | 139 const GURL& final_url, |
80 int http_response_code) = 0; | 140 int http_response_code, |
141 scoped_refptr<net::HttpResponseHeaders> response_headers, | |
142 const char* body, | |
143 size_t body_size) = 0; | |
81 | 144 |
82 protected: | 145 protected: |
83 virtual ~Delegate() {} | 146 virtual ~Delegate() {} |
84 }; | 147 }; |
85 | 148 |
86 // NOTE |url_request_dispatcher| and |delegate| must outlive the | 149 // NOTE |url_request_dispatcher| and |delegate| must outlive the |
87 // GenericURLRequestJob. | 150 // GenericURLRequestJob. |
88 GenericURLRequestJob(net::URLRequest* request, | 151 GenericURLRequestJob(net::URLRequest* request, |
89 net::NetworkDelegate* network_delegate, | 152 net::NetworkDelegate* network_delegate, |
90 URLRequestDispatcher* url_request_dispatcher, | 153 URLRequestDispatcher* url_request_dispatcher, |
(...skipping 12 matching lines...) Expand all Loading... | |
103 void GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override; | 166 void GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override; |
104 | 167 |
105 // URLFetcher::FetchResultListener implementation: | 168 // URLFetcher::FetchResultListener implementation: |
106 void OnFetchStartError(net::Error error) override; | 169 void OnFetchStartError(net::Error error) override; |
107 void OnFetchComplete(const GURL& final_url, | 170 void OnFetchComplete(const GURL& final_url, |
108 int http_response_code, | 171 int http_response_code, |
109 scoped_refptr<net::HttpResponseHeaders> response_headers, | 172 scoped_refptr<net::HttpResponseHeaders> response_headers, |
110 const char* body, | 173 const char* body, |
111 size_t body_size) override; | 174 size_t body_size) override; |
112 | 175 |
176 // Request implementation: | |
177 const net::URLRequest* GetURLRequest() const override; | |
178 int GetFrameTreeNodeId() const override; | |
179 std::string GetDevtoolsAgentHostId() const override; | |
180 ResourceType GetResourceType() const override; | |
181 | |
182 // IncomingRequest implementation: | |
183 void AllowRequest() override; | |
184 void BlockRequest(net::Error error) override; | |
185 void ModifyRequest(const GURL& url, | |
186 const std::string& method, | |
187 const std::string& post_data, | |
188 const net::HttpRequestHeaders& request_headers) override; | |
189 void MockResponse(std::unique_ptr<MockResponseData> mock_response) override; | |
190 | |
113 private: | 191 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, | 192 void PrepareCookies(const GURL& rewritten_url, |
124 const std::string& method, | 193 const std::string& method, |
125 const url::Origin& site_for_cookies); | 194 const url::Origin& site_for_cookies, |
195 const base::Closure& done_callback); | |
126 void OnCookiesAvailable(const GURL& rewritten_url, | 196 void OnCookiesAvailable(const GURL& rewritten_url, |
127 const std::string& method, | 197 const std::string& method, |
198 const base::Closure& done_callback, | |
128 const net::CookieList& cookie_list); | 199 const net::CookieList& cookie_list); |
129 | 200 |
130 std::unique_ptr<URLFetcher> url_fetcher_; | 201 std::unique_ptr<URLFetcher> url_fetcher_; |
131 net::HttpRequestHeaders extra_request_headers_; | 202 net::HttpRequestHeaders extra_request_headers_; |
132 scoped_refptr<net::HttpResponseHeaders> response_headers_; | 203 scoped_refptr<net::HttpResponseHeaders> response_headers_; |
133 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; | 204 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
134 std::string devtools_request_id_; | 205 std::unique_ptr<MockResponseData> mock_response_; |
135 Delegate* delegate_; // Not owned. | 206 Delegate* delegate_; // Not owned. |
207 const content::ResourceRequestInfo* request_resource_info_; // Not owned. | |
136 const char* body_ = nullptr; // Not owned. | 208 const char* body_ = nullptr; // Not owned. |
137 int http_response_code_ = 0; | 209 int http_response_code_ = 0; |
138 size_t body_size_ = 0; | 210 size_t body_size_ = 0; |
139 size_t read_offset_ = 0; | 211 size_t read_offset_ = 0; |
140 base::TimeTicks response_time_; | 212 base::TimeTicks response_time_; |
141 | 213 |
142 base::WeakPtrFactory<GenericURLRequestJob> weak_factory_; | 214 base::WeakPtrFactory<GenericURLRequestJob> weak_factory_; |
143 | 215 |
144 DISALLOW_COPY_AND_ASSIGN(GenericURLRequestJob); | 216 DISALLOW_COPY_AND_ASSIGN(GenericURLRequestJob); |
145 }; | 217 }; |
146 | 218 |
147 } // namespace headless | 219 } // namespace headless |
148 | 220 |
149 #endif // HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ | 221 #endif // HEADLESS_PUBLIC_UTIL_GENERIC_URL_REQUEST_JOB_H_ |
OLD | NEW |