| 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 } | 121 } |
| 122 | 122 |
| 123 void GenericURLRequestJob::OnFetchStartError(net::Error error) { | 123 void GenericURLRequestJob::OnFetchStartError(net::Error error) { |
| 124 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 124 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 125 DispatchStartError(error); | 125 DispatchStartError(error); |
| 126 delegate_->OnResourceLoadFailed(this, error); | 126 delegate_->OnResourceLoadFailed(this, error); |
| 127 } | 127 } |
| 128 | 128 |
| 129 void GenericURLRequestJob::OnFetchComplete( | 129 void GenericURLRequestJob::OnFetchComplete( |
| 130 const GURL& final_url, | 130 const GURL& final_url, |
| 131 int http_response_code, | |
| 132 scoped_refptr<net::HttpResponseHeaders> response_headers, | 131 scoped_refptr<net::HttpResponseHeaders> response_headers, |
| 133 const char* body, | 132 const char* body, |
| 134 size_t body_size) { | 133 size_t body_size) { |
| 135 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 134 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 136 response_time_ = base::TimeTicks::Now(); | 135 response_time_ = base::TimeTicks::Now(); |
| 137 http_response_code_ = http_response_code; | |
| 138 response_headers_ = response_headers; | 136 response_headers_ = response_headers; |
| 139 body_ = body; | 137 body_ = body; |
| 140 body_size_ = body_size; | 138 body_size_ = body_size; |
| 141 | 139 |
| 142 DispatchHeadersComplete(); | 140 DispatchHeadersComplete(); |
| 143 | 141 |
| 144 delegate_->OnResourceLoadComplete(this, final_url, http_response_code, | 142 delegate_->OnResourceLoadComplete(this, final_url, response_headers_, body_, |
| 145 response_headers_, body_, body_size_); | 143 body_size_); |
| 146 } | 144 } |
| 147 | 145 |
| 148 int GenericURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { | 146 int GenericURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
| 149 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); | 147 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 150 // TODO(skyostil): Implement ranged fetches. | 148 // TODO(skyostil): Implement ranged fetches. |
| 151 // TODO(alexclarke): Add coverage for all the cases below. | 149 // TODO(alexclarke): Add coverage for all the cases below. |
| 152 size_t bytes_available = body_size_ - read_offset_; | 150 size_t bytes_available = body_size_ - read_offset_; |
| 153 size_t bytes_to_copy = | 151 size_t bytes_to_copy = |
| 154 std::min(static_cast<size_t>(buf_size), bytes_available); | 152 std::min(static_cast<size_t>(buf_size), bytes_available); |
| 155 if (bytes_to_copy) { | 153 if (bytes_to_copy) { |
| 156 std::memcpy(buf->data(), &body_[read_offset_], bytes_to_copy); | 154 std::memcpy(buf->data(), &body_[read_offset_], bytes_to_copy); |
| 157 read_offset_ += bytes_to_copy; | 155 read_offset_ += bytes_to_copy; |
| 158 } | 156 } |
| 159 return bytes_to_copy; | 157 return bytes_to_copy; |
| 160 } | 158 } |
| 161 | 159 |
| 162 int GenericURLRequestJob::GetResponseCode() const { | |
| 163 return http_response_code_; | |
| 164 } | |
| 165 | |
| 166 void GenericURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { | 160 void GenericURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 167 info->headers = response_headers_; | 161 info->headers = response_headers_; |
| 168 } | 162 } |
| 169 | 163 |
| 170 bool GenericURLRequestJob::GetMimeType(std::string* mime_type) const { | 164 bool GenericURLRequestJob::GetMimeType(std::string* mime_type) const { |
| 171 if (!response_headers_) | 165 if (!response_headers_) |
| 172 return false; | 166 return false; |
| 173 return response_headers_->GetMimeType(mime_type); | 167 return response_headers_->GetMimeType(mime_type); |
| 174 } | 168 } |
| 175 | 169 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 origin_task_runner_->PostTask( | 308 origin_task_runner_->PostTask( |
| 315 FROM_HERE, base::Bind(&GenericURLRequestJob::MockResponse, | 309 FROM_HERE, base::Bind(&GenericURLRequestJob::MockResponse, |
| 316 weak_factory_.GetWeakPtr(), | 310 weak_factory_.GetWeakPtr(), |
| 317 base::Passed(std::move(mock_response)))); | 311 base::Passed(std::move(mock_response)))); |
| 318 return; | 312 return; |
| 319 } | 313 } |
| 320 | 314 |
| 321 mock_response_ = std::move(mock_response); | 315 mock_response_ = std::move(mock_response); |
| 322 | 316 |
| 323 OnFetchCompleteExtractHeaders(request_->url(), | 317 OnFetchCompleteExtractHeaders(request_->url(), |
| 324 mock_response_->http_response_code, | |
| 325 mock_response_->response_data.data(), | 318 mock_response_->response_data.data(), |
| 326 mock_response_->response_data.size()); | 319 mock_response_->response_data.size()); |
| 327 } | 320 } |
| 328 | 321 |
| 329 } // namespace headless | 322 } // namespace headless |
| OLD | NEW |