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 #include "headless/public/util/generic_url_request_job.h" | 5 #include "headless/public/util/generic_url_request_job.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 url_fetcher_(std::move(url_fetcher)), | 41 url_fetcher_(std::move(url_fetcher)), |
42 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 42 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
43 delegate_(delegate), | 43 delegate_(delegate), |
44 headless_browser_context_(headless_browser_context), | 44 headless_browser_context_(headless_browser_context), |
45 request_resource_info_( | 45 request_resource_info_( |
46 content::ResourceRequestInfo::ForRequest(request_)), | 46 content::ResourceRequestInfo::ForRequest(request_)), |
47 request_id_(next_request_id_++), | 47 request_id_(next_request_id_++), |
48 weak_factory_(this) {} | 48 weak_factory_(this) {} |
49 | 49 |
50 GenericURLRequestJob::~GenericURLRequestJob() { | 50 GenericURLRequestJob::~GenericURLRequestJob() { |
51 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 51 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
52 } | 52 } |
53 | 53 |
54 void GenericURLRequestJob::SetExtraRequestHeaders( | 54 void GenericURLRequestJob::SetExtraRequestHeaders( |
55 const net::HttpRequestHeaders& headers) { | 55 const net::HttpRequestHeaders& headers) { |
56 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 56 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
57 extra_request_headers_ = headers; | 57 extra_request_headers_ = headers; |
58 } | 58 } |
59 | 59 |
60 void GenericURLRequestJob::Start() { | 60 void GenericURLRequestJob::Start() { |
61 PrepareCookies(request_->url(), request_->method(), | 61 PrepareCookies(request_->url(), request_->method(), |
62 url::Origin(request_->first_party_for_cookies()), | 62 url::Origin(request_->first_party_for_cookies()), |
63 base::Bind(&Delegate::OnPendingRequest, | 63 base::Bind(&Delegate::OnPendingRequest, |
64 base::Unretained(delegate_), this)); | 64 base::Unretained(delegate_), this)); |
65 } | 65 } |
66 | 66 |
67 void GenericURLRequestJob::PrepareCookies(const GURL& rewritten_url, | 67 void GenericURLRequestJob::PrepareCookies(const GURL& rewritten_url, |
68 const std::string& method, | 68 const std::string& method, |
69 const url::Origin& site_for_cookies, | 69 const url::Origin& site_for_cookies, |
70 const base::Closure& done_callback) { | 70 const base::Closure& done_callback) { |
71 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 71 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
72 net::CookieStore* cookie_store = request_->context()->cookie_store(); | 72 net::CookieStore* cookie_store = request_->context()->cookie_store(); |
73 net::CookieOptions options; | 73 net::CookieOptions options; |
74 options.set_include_httponly(); | 74 options.set_include_httponly(); |
75 | 75 |
76 // See net::URLRequestHttpJob::AddCookieHeaderAndStart(). | 76 // See net::URLRequestHttpJob::AddCookieHeaderAndStart(). |
77 url::Origin requested_origin(rewritten_url); | 77 url::Origin requested_origin(rewritten_url); |
78 if (net::registry_controlled_domains::SameDomainOrHost( | 78 if (net::registry_controlled_domains::SameDomainOrHost( |
79 requested_origin, site_for_cookies, | 79 requested_origin, site_for_cookies, |
80 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { | 80 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { |
81 if (net::registry_controlled_domains::SameDomainOrHost( | 81 if (net::registry_controlled_domains::SameDomainOrHost( |
(...skipping 12 matching lines...) Expand all Loading... |
94 base::Bind(&GenericURLRequestJob::OnCookiesAvailable, | 94 base::Bind(&GenericURLRequestJob::OnCookiesAvailable, |
95 weak_factory_.GetWeakPtr(), rewritten_url, method, | 95 weak_factory_.GetWeakPtr(), rewritten_url, method, |
96 done_callback)); | 96 done_callback)); |
97 } | 97 } |
98 | 98 |
99 void GenericURLRequestJob::OnCookiesAvailable( | 99 void GenericURLRequestJob::OnCookiesAvailable( |
100 const GURL& rewritten_url, | 100 const GURL& rewritten_url, |
101 const std::string& method, | 101 const std::string& method, |
102 const base::Closure& done_callback, | 102 const base::Closure& done_callback, |
103 const net::CookieList& cookie_list) { | 103 const net::CookieList& cookie_list) { |
104 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 104 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
105 // TODO(alexclarke): Set user agent. | 105 // TODO(alexclarke): Set user agent. |
106 // Pass cookies, the referrer and any extra headers into the fetch request. | 106 // Pass cookies, the referrer and any extra headers into the fetch request. |
107 std::string cookie = net::CookieStore::BuildCookieLine(cookie_list); | 107 std::string cookie = net::CookieStore::BuildCookieLine(cookie_list); |
108 if (!cookie.empty()) | 108 if (!cookie.empty()) |
109 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kCookie, cookie); | 109 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kCookie, cookie); |
110 | 110 |
111 if (!request_->referrer().empty()) { | 111 if (!request_->referrer().empty()) { |
112 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kReferer, | 112 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kReferer, |
113 request_->referrer()); | 113 request_->referrer()); |
114 } | 114 } |
115 | 115 |
116 done_callback.Run(); | 116 done_callback.Run(); |
117 } | 117 } |
118 | 118 |
119 void GenericURLRequestJob::OnFetchStartError(net::Error error) { | 119 void GenericURLRequestJob::OnFetchStartError(net::Error error) { |
120 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 120 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
121 DispatchStartError(error); | 121 DispatchStartError(error); |
122 delegate_->OnResourceLoadFailed(this, error); | 122 delegate_->OnResourceLoadFailed(this, error); |
123 } | 123 } |
124 | 124 |
125 void GenericURLRequestJob::OnFetchComplete( | 125 void GenericURLRequestJob::OnFetchComplete( |
126 const GURL& final_url, | 126 const GURL& final_url, |
127 scoped_refptr<net::HttpResponseHeaders> response_headers, | 127 scoped_refptr<net::HttpResponseHeaders> response_headers, |
128 const char* body, | 128 const char* body, |
129 size_t body_size) { | 129 size_t body_size) { |
130 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 130 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
131 response_time_ = base::TimeTicks::Now(); | 131 response_time_ = base::TimeTicks::Now(); |
132 response_headers_ = response_headers; | 132 response_headers_ = response_headers; |
133 body_ = body; | 133 body_ = body; |
134 body_size_ = body_size; | 134 body_size_ = body_size; |
135 | 135 |
136 DispatchHeadersComplete(); | 136 DispatchHeadersComplete(); |
137 | 137 |
138 delegate_->OnResourceLoadComplete(this, final_url, response_headers_, body_, | 138 delegate_->OnResourceLoadComplete(this, final_url, response_headers_, body_, |
139 body_size_); | 139 body_size_); |
140 } | 140 } |
141 | 141 |
142 int GenericURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { | 142 int GenericURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
143 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 143 DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); |
144 // TODO(skyostil): Implement ranged fetches. | 144 // TODO(skyostil): Implement ranged fetches. |
145 // TODO(alexclarke): Add coverage for all the cases below. | 145 // TODO(alexclarke): Add coverage for all the cases below. |
146 size_t bytes_available = body_size_ - read_offset_; | 146 size_t bytes_available = body_size_ - read_offset_; |
147 size_t bytes_to_copy = | 147 size_t bytes_to_copy = |
148 std::min(static_cast<size_t>(buf_size), bytes_available); | 148 std::min(static_cast<size_t>(buf_size), bytes_available); |
149 if (bytes_to_copy) { | 149 if (bytes_to_copy) { |
150 std::memcpy(buf->data(), &body_[read_offset_], bytes_to_copy); | 150 std::memcpy(buf->data(), &body_[read_offset_], bytes_to_copy); |
151 read_offset_ += bytes_to_copy; | 151 read_offset_ += bytes_to_copy; |
152 } | 152 } |
153 return bytes_to_copy; | 153 return bytes_to_copy; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 if (!reader) | 275 if (!reader) |
276 return ""; | 276 return ""; |
277 return std::string(reader->bytes(), reader->length()); | 277 return std::string(reader->bytes(), reader->length()); |
278 } | 278 } |
279 | 279 |
280 const Request* GenericURLRequestJob::GetRequest() const { | 280 const Request* GenericURLRequestJob::GetRequest() const { |
281 return this; | 281 return this; |
282 } | 282 } |
283 | 283 |
284 void GenericURLRequestJob::AllowRequest() { | 284 void GenericURLRequestJob::AllowRequest() { |
285 if (!origin_task_runner_->RunsTasksOnCurrentThread()) { | 285 if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
286 origin_task_runner_->PostTask( | 286 origin_task_runner_->PostTask( |
287 FROM_HERE, base::Bind(&GenericURLRequestJob::AllowRequest, | 287 FROM_HERE, base::Bind(&GenericURLRequestJob::AllowRequest, |
288 weak_factory_.GetWeakPtr())); | 288 weak_factory_.GetWeakPtr())); |
289 return; | 289 return; |
290 } | 290 } |
291 | 291 |
292 url_fetcher_->StartFetch(request_->url(), request_->method(), GetPostData(), | 292 url_fetcher_->StartFetch(request_->url(), request_->method(), GetPostData(), |
293 extra_request_headers_, this); | 293 extra_request_headers_, this); |
294 } | 294 } |
295 | 295 |
296 void GenericURLRequestJob::BlockRequest(net::Error error) { | 296 void GenericURLRequestJob::BlockRequest(net::Error error) { |
297 if (!origin_task_runner_->RunsTasksOnCurrentThread()) { | 297 if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
298 origin_task_runner_->PostTask( | 298 origin_task_runner_->PostTask( |
299 FROM_HERE, base::Bind(&GenericURLRequestJob::BlockRequest, | 299 FROM_HERE, base::Bind(&GenericURLRequestJob::BlockRequest, |
300 weak_factory_.GetWeakPtr(), error)); | 300 weak_factory_.GetWeakPtr(), error)); |
301 return; | 301 return; |
302 } | 302 } |
303 | 303 |
304 DispatchStartError(error); | 304 DispatchStartError(error); |
305 } | 305 } |
306 | 306 |
307 void GenericURLRequestJob::ModifyRequest( | 307 void GenericURLRequestJob::ModifyRequest( |
308 const GURL& url, | 308 const GURL& url, |
309 const std::string& method, | 309 const std::string& method, |
310 const std::string& post_data, | 310 const std::string& post_data, |
311 const net::HttpRequestHeaders& request_headers) { | 311 const net::HttpRequestHeaders& request_headers) { |
312 if (!origin_task_runner_->RunsTasksOnCurrentThread()) { | 312 if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
313 origin_task_runner_->PostTask( | 313 origin_task_runner_->PostTask( |
314 FROM_HERE, base::Bind(&GenericURLRequestJob::ModifyRequest, | 314 FROM_HERE, base::Bind(&GenericURLRequestJob::ModifyRequest, |
315 weak_factory_.GetWeakPtr(), url, method, | 315 weak_factory_.GetWeakPtr(), url, method, |
316 post_data, request_headers)); | 316 post_data, request_headers)); |
317 return; | 317 return; |
318 } | 318 } |
319 | 319 |
320 extra_request_headers_ = request_headers; | 320 extra_request_headers_ = request_headers; |
321 PrepareCookies( | 321 PrepareCookies( |
322 request_->url(), request_->method(), | 322 request_->url(), request_->method(), |
323 url::Origin(request_->first_party_for_cookies()), | 323 url::Origin(request_->first_party_for_cookies()), |
324 base::Bind(&URLFetcher::StartFetch, base::Unretained(url_fetcher_.get()), | 324 base::Bind(&URLFetcher::StartFetch, base::Unretained(url_fetcher_.get()), |
325 url, method, post_data, request_headers, this)); | 325 url, method, post_data, request_headers, this)); |
326 } | 326 } |
327 | 327 |
328 void GenericURLRequestJob::MockResponse( | 328 void GenericURLRequestJob::MockResponse( |
329 std::unique_ptr<MockResponseData> mock_response) { | 329 std::unique_ptr<MockResponseData> mock_response) { |
330 if (!origin_task_runner_->RunsTasksOnCurrentThread()) { | 330 if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
331 origin_task_runner_->PostTask( | 331 origin_task_runner_->PostTask( |
332 FROM_HERE, base::Bind(&GenericURLRequestJob::MockResponse, | 332 FROM_HERE, base::Bind(&GenericURLRequestJob::MockResponse, |
333 weak_factory_.GetWeakPtr(), | 333 weak_factory_.GetWeakPtr(), |
334 base::Passed(std::move(mock_response)))); | 334 base::Passed(std::move(mock_response)))); |
335 return; | 335 return; |
336 } | 336 } |
337 | 337 |
338 mock_response_ = std::move(mock_response); | 338 mock_response_ = std::move(mock_response); |
339 | 339 |
340 OnFetchCompleteExtractHeaders(request_->url(), | 340 OnFetchCompleteExtractHeaders(request_->url(), |
341 mock_response_->response_data.data(), | 341 mock_response_->response_data.data(), |
342 mock_response_->response_data.size()); | 342 mock_response_->response_data.size()); |
343 } | 343 } |
344 | 344 |
345 } // namespace headless | 345 } // namespace headless |
OLD | NEW |