| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/cronet/android/url_request_adapter.h" | 5 #include "components/cronet/android/url_request_adapter.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 | 203 |
| 204 // static | 204 // static |
| 205 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { | 205 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { |
| 206 DCHECK(self->OnNetworkThread()); | 206 DCHECK(self->OnNetworkThread()); |
| 207 VLOG(1) << "Destroying chromium request: " | 207 VLOG(1) << "Destroying chromium request: " |
| 208 << self->url_.possibly_invalid_spec(); | 208 << self->url_.possibly_invalid_spec(); |
| 209 delete self; | 209 delete self; |
| 210 } | 210 } |
| 211 | 211 |
| 212 // static | 212 // static |
| 213 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) { | 213 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request, |
| 214 int net_error) { |
| 215 DCHECK_NE(net::ERR_IO_PENDING, net_error); |
| 214 DCHECK(OnNetworkThread()); | 216 DCHECK(OnNetworkThread()); |
| 215 if (request->status().status() != net::URLRequestStatus::SUCCESS) { | 217 |
| 216 OnRequestFailed(); | 218 if (net_error != net::OK) { |
| 219 OnRequestFailed(net_error); |
| 217 return; | 220 return; |
| 218 } | 221 } |
| 219 | 222 |
| 220 http_status_code_ = request->GetResponseCode(); | 223 http_status_code_ = request->GetResponseCode(); |
| 221 VLOG(1) << "Response started with status: " << http_status_code_; | 224 VLOG(1) << "Response started with status: " << http_status_code_; |
| 222 | 225 |
| 223 net::HttpResponseHeaders* headers = request->response_headers(); | 226 net::HttpResponseHeaders* headers = request->response_headers(); |
| 224 if (headers) | 227 if (headers) |
| 225 http_status_text_ = headers->GetStatusText(); | 228 http_status_text_ = headers->GetStatusText(); |
| 226 | 229 |
| 227 request->GetResponseHeaderByName("Content-Type", &content_type_); | 230 request->GetResponseHeaderByName("Content-Type", &content_type_); |
| 228 expected_size_ = request->GetExpectedContentSize(); | 231 expected_size_ = request->GetExpectedContentSize(); |
| 229 delegate_->OnResponseStarted(this); | 232 delegate_->OnResponseStarted(this); |
| 230 | 233 |
| 231 Read(); | 234 Read(); |
| 232 } | 235 } |
| 233 | 236 |
| 234 // Reads all available data or starts an asynchronous read. | 237 // Reads all available data or starts an asynchronous read. |
| 235 void URLRequestAdapter::Read() { | 238 void URLRequestAdapter::Read() { |
| 236 DCHECK(OnNetworkThread()); | 239 DCHECK(OnNetworkThread()); |
| 237 if (!read_buffer_.get()) | 240 if (!read_buffer_.get()) |
| 238 read_buffer_ = new net::IOBufferWithSize(kReadBufferSize); | 241 read_buffer_ = new net::IOBufferWithSize(kReadBufferSize); |
| 239 | 242 |
| 240 while(true) { | 243 while(true) { |
| 241 int bytes_read = 0; | 244 int result = url_request_->Read(read_buffer_.get(), kReadBufferSize); |
| 242 url_request_->Read(read_buffer_.get(), kReadBufferSize, &bytes_read); | |
| 243 // If IO is pending, wait for the URLRequest to call OnReadCompleted. | 245 // If IO is pending, wait for the URLRequest to call OnReadCompleted. |
| 244 if (url_request_->status().is_io_pending()) | 246 if (result == net::ERR_IO_PENDING) |
| 245 return; | 247 return; |
| 246 // Stop when request has failed or succeeded. | 248 // Stop when request has failed or succeeded. |
| 247 if (!HandleReadResult(bytes_read)) | 249 if (!HandleReadResult(result)) |
| 248 return; | 250 return; |
| 249 } | 251 } |
| 250 } | 252 } |
| 251 | 253 |
| 252 bool URLRequestAdapter::HandleReadResult(int bytes_read) { | 254 bool URLRequestAdapter::HandleReadResult(int result) { |
| 253 DCHECK(OnNetworkThread()); | 255 DCHECK(OnNetworkThread()); |
| 254 if (!url_request_->status().is_success()) { | 256 if (result < 0) { |
| 255 OnRequestFailed(); | 257 OnRequestFailed(result); |
| 256 return false; | 258 return false; |
| 257 } else if (bytes_read == 0) { | 259 } |
| 260 |
| 261 if (result == 0) { |
| 258 OnRequestSucceeded(); | 262 OnRequestSucceeded(); |
| 259 return false; | 263 return false; |
| 260 } | 264 } |
| 261 | 265 |
| 262 total_bytes_read_ += bytes_read; | 266 total_bytes_read_ += result; |
| 263 delegate_->OnBytesRead(this, bytes_read); | 267 delegate_->OnBytesRead(this, result); |
| 264 | 268 |
| 265 return true; | 269 return true; |
| 266 } | 270 } |
| 267 | 271 |
| 268 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, | 272 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, |
| 269 int bytes_read) { | 273 int bytes_read) { |
| 274 DCHECK_NE(net::ERR_IO_PENDING, bytes_read); |
| 270 if (!HandleReadResult(bytes_read)) | 275 if (!HandleReadResult(bytes_read)) |
| 271 return; | 276 return; |
| 272 | 277 |
| 273 Read(); | 278 Read(); |
| 274 } | 279 } |
| 275 | 280 |
| 276 void URLRequestAdapter::OnReceivedRedirect(net::URLRequest* request, | 281 void URLRequestAdapter::OnReceivedRedirect(net::URLRequest* request, |
| 277 const net::RedirectInfo& info, | 282 const net::RedirectInfo& info, |
| 278 bool* defer_redirect) { | 283 bool* defer_redirect) { |
| 279 DCHECK(OnNetworkThread()); | 284 DCHECK(OnNetworkThread()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 292 if (canceled_) { | 297 if (canceled_) { |
| 293 return; | 298 return; |
| 294 } | 299 } |
| 295 | 300 |
| 296 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ | 301 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ |
| 297 << ". Total bytes read: " << total_bytes_read_; | 302 << ". Total bytes read: " << total_bytes_read_; |
| 298 | 303 |
| 299 OnRequestCompleted(); | 304 OnRequestCompleted(); |
| 300 } | 305 } |
| 301 | 306 |
| 302 void URLRequestAdapter::OnRequestFailed() { | 307 void URLRequestAdapter::OnRequestFailed(int net_error) { |
| 308 DCHECK_LE(net_error, 0); |
| 309 DCHECK_NE(net::ERR_IO_PENDING, net_error); |
| 303 DCHECK(OnNetworkThread()); | 310 DCHECK(OnNetworkThread()); |
| 304 if (canceled_) { | 311 if (canceled_) { |
| 305 return; | 312 return; |
| 306 } | 313 } |
| 307 | 314 |
| 308 error_code_ = url_request_->status().error(); | 315 error_code_ = net_error; |
| 309 VLOG(1) << "Request failed with status: " << url_request_->status().status() | 316 VLOG(1) << "Request failed with error: " << net::ErrorToString(error_code_); |
| 310 << " and error: " << net::ErrorToString(error_code_); | |
| 311 OnRequestCompleted(); | 317 OnRequestCompleted(); |
| 312 } | 318 } |
| 313 | 319 |
| 314 void URLRequestAdapter::OnRequestCompleted() { | 320 void URLRequestAdapter::OnRequestCompleted() { |
| 315 DCHECK(OnNetworkThread()); | 321 DCHECK(OnNetworkThread()); |
| 316 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); | 322 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); |
| 317 | 323 |
| 318 DCHECK(url_request_ != nullptr); | 324 DCHECK(url_request_ != nullptr); |
| 319 | 325 |
| 320 delegate_->OnRequestFinished(this); | 326 delegate_->OnRequestFinished(this); |
| 321 url_request_.reset(); | 327 url_request_.reset(); |
| 322 } | 328 } |
| 323 | 329 |
| 324 unsigned char* URLRequestAdapter::Data() const { | 330 unsigned char* URLRequestAdapter::Data() const { |
| 325 DCHECK(OnNetworkThread()); | 331 DCHECK(OnNetworkThread()); |
| 326 return reinterpret_cast<unsigned char*>(read_buffer_->data()); | 332 return reinterpret_cast<unsigned char*>(read_buffer_->data()); |
| 327 } | 333 } |
| 328 | 334 |
| 329 bool URLRequestAdapter::OnNetworkThread() const { | 335 bool URLRequestAdapter::OnNetworkThread() const { |
| 330 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); | 336 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); |
| 331 } | 337 } |
| 332 | 338 |
| 333 } // namespace cronet | 339 } // namespace cronet |
| OLD | NEW |