Chromium Code Reviews| 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 bytes_read = url_request_->Read(read_buffer_.get(), kReadBufferSize); |
|
mmenke
2016/09/06 17:57:58
nit: bytes_read -> result
| |
| 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 (bytes_read == 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(bytes_read)) |
| 248 return; | 250 return; |
| 249 } | 251 } |
| 250 } | 252 } |
| 251 | 253 |
| 252 bool URLRequestAdapter::HandleReadResult(int bytes_read) { | 254 bool URLRequestAdapter::HandleReadResult(int bytes_read) { |
|
mmenke
2016/09/06 17:57:58
bytes_read -> result
| |
| 253 DCHECK(OnNetworkThread()); | 255 DCHECK(OnNetworkThread()); |
| 254 if (!url_request_->status().is_success()) { | 256 if (bytes_read < 0) { |
| 255 OnRequestFailed(); | 257 int net_error = bytes_read; |
| 258 OnRequestFailed(net_error); | |
| 256 return false; | 259 return false; |
| 257 } else if (bytes_read == 0) { | 260 } else if (bytes_read == 0) { |
|
mmenke
2016/09/06 17:57:58
pre-existing issue, but while you're here, mind re
maksims (do not use this acc)
2016/09/12 12:11:54
Do you mean to do this -
if (result == 0) {
..
| |
| 258 OnRequestSucceeded(); | 261 OnRequestSucceeded(); |
| 259 return false; | 262 return false; |
| 260 } | 263 } |
| 261 | 264 |
| 262 total_bytes_read_ += bytes_read; | 265 total_bytes_read_ += bytes_read; |
| 263 delegate_->OnBytesRead(this, bytes_read); | 266 delegate_->OnBytesRead(this, bytes_read); |
| 264 | 267 |
| 265 return true; | 268 return true; |
| 266 } | 269 } |
| 267 | 270 |
| 268 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, | 271 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, |
| 269 int bytes_read) { | 272 int bytes_read) { |
| 273 DCHECK_NE(net::ERR_IO_PENDING, bytes_read); | |
| 270 if (!HandleReadResult(bytes_read)) | 274 if (!HandleReadResult(bytes_read)) |
| 271 return; | 275 return; |
| 272 | 276 |
| 273 Read(); | 277 Read(); |
| 274 } | 278 } |
| 275 | 279 |
| 276 void URLRequestAdapter::OnReceivedRedirect(net::URLRequest* request, | 280 void URLRequestAdapter::OnReceivedRedirect(net::URLRequest* request, |
| 277 const net::RedirectInfo& info, | 281 const net::RedirectInfo& info, |
| 278 bool* defer_redirect) { | 282 bool* defer_redirect) { |
| 279 DCHECK(OnNetworkThread()); | 283 DCHECK(OnNetworkThread()); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 292 if (canceled_) { | 296 if (canceled_) { |
| 293 return; | 297 return; |
| 294 } | 298 } |
| 295 | 299 |
| 296 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ | 300 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ |
| 297 << ". Total bytes read: " << total_bytes_read_; | 301 << ". Total bytes read: " << total_bytes_read_; |
| 298 | 302 |
| 299 OnRequestCompleted(); | 303 OnRequestCompleted(); |
| 300 } | 304 } |
| 301 | 305 |
| 302 void URLRequestAdapter::OnRequestFailed() { | 306 void URLRequestAdapter::OnRequestFailed(int net_error) { |
|
mmenke
2016/09/06 17:57:58
Suggest adding:
DCHECK_LE(net_error, 0);
DCHECK_N
maksims (do not use this acc)
2016/09/12 12:11:54
Done.
| |
| 303 DCHECK(OnNetworkThread()); | 307 DCHECK(OnNetworkThread()); |
| 304 if (canceled_) { | 308 if (canceled_) { |
| 305 return; | 309 return; |
| 306 } | 310 } |
| 307 | 311 |
| 308 error_code_ = url_request_->status().error(); | 312 error_code_ = net_error; |
| 309 VLOG(1) << "Request failed with status: " << url_request_->status().status() | 313 VLOG(1) << "Request failed with error: " << net::ErrorToString(error_code_); |
| 310 << " and error: " << net::ErrorToString(error_code_); | |
| 311 OnRequestCompleted(); | 314 OnRequestCompleted(); |
| 312 } | 315 } |
| 313 | 316 |
| 314 void URLRequestAdapter::OnRequestCompleted() { | 317 void URLRequestAdapter::OnRequestCompleted() { |
| 315 DCHECK(OnNetworkThread()); | 318 DCHECK(OnNetworkThread()); |
| 316 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); | 319 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); |
| 317 | 320 |
| 318 DCHECK(url_request_ != nullptr); | 321 DCHECK(url_request_ != nullptr); |
| 319 | 322 |
| 320 delegate_->OnRequestFinished(this); | 323 delegate_->OnRequestFinished(this); |
| 321 url_request_.reset(); | 324 url_request_.reset(); |
| 322 } | 325 } |
| 323 | 326 |
| 324 unsigned char* URLRequestAdapter::Data() const { | 327 unsigned char* URLRequestAdapter::Data() const { |
| 325 DCHECK(OnNetworkThread()); | 328 DCHECK(OnNetworkThread()); |
| 326 return reinterpret_cast<unsigned char*>(read_buffer_->data()); | 329 return reinterpret_cast<unsigned char*>(read_buffer_->data()); |
| 327 } | 330 } |
| 328 | 331 |
| 329 bool URLRequestAdapter::OnNetworkThread() const { | 332 bool URLRequestAdapter::OnNetworkThread() const { |
| 330 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); | 333 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); |
| 331 } | 334 } |
| 332 | 335 |
| 333 } // namespace cronet | 336 } // namespace cronet |
| OLD | NEW |