| 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 "url_request_adapter.h" | 5 #include "url_request_adapter.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "components/cronet/android/url_request_context_adapter.h" | 14 #include "components/cronet/android/url_request_context_adapter.h" |
| 15 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" | 15 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" |
| 16 #include "net/base/elements_upload_data_stream.h" | 16 #include "net/base/elements_upload_data_stream.h" |
| 17 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/base/upload_bytes_element_reader.h" | 19 #include "net/base/upload_bytes_element_reader.h" |
| 20 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
| 21 #include "net/http/http_status_code.h" | 21 #include "net/http/http_status_code.h" |
| 22 | 22 |
| 23 namespace cronet { | 23 namespace cronet { |
| 24 | 24 |
| 25 static const size_t kBufferSizeIncrement = 8192; | 25 static const size_t kReadBufferSize = 32768; |
| 26 | 26 |
| 27 URLRequestAdapter::URLRequestAdapter(URLRequestContextAdapter* context, | 27 URLRequestAdapter::URLRequestAdapter(URLRequestContextAdapter* context, |
| 28 URLRequestAdapterDelegate* delegate, | 28 URLRequestAdapterDelegate* delegate, |
| 29 GURL url, | 29 GURL url, |
| 30 net::RequestPriority priority) | 30 net::RequestPriority priority) |
| 31 : method_("GET"), | 31 : method_("GET"), |
| 32 read_buffer_(new net::GrowableIOBuffer()), | |
| 33 bytes_read_(0), | |
| 34 total_bytes_read_(0), | 32 total_bytes_read_(0), |
| 35 error_code_(0), | 33 error_code_(0), |
| 36 http_status_code_(0), | 34 http_status_code_(0), |
| 37 canceled_(false), | 35 canceled_(false), |
| 38 expected_size_(0), | 36 expected_size_(0), |
| 39 chunked_upload_(false), | 37 chunked_upload_(false), |
| 40 disable_redirect_(false) { | 38 disable_redirect_(false) { |
| 41 context_ = context; | 39 context_ = context; |
| 42 delegate_ = delegate; | 40 delegate_ = delegate; |
| 43 url_ = url; | 41 url_ = url; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 } else if (chunked_upload_) { | 162 } else if (chunked_upload_) { |
| 165 url_request_->EnableChunkedUpload(); | 163 url_request_->EnableChunkedUpload(); |
| 166 } | 164 } |
| 167 | 165 |
| 168 url_request_->SetPriority(priority_); | 166 url_request_->SetPriority(priority_); |
| 169 | 167 |
| 170 url_request_->Start(); | 168 url_request_->Start(); |
| 171 } | 169 } |
| 172 | 170 |
| 173 void URLRequestAdapter::Cancel() { | 171 void URLRequestAdapter::Cancel() { |
| 174 if (canceled_) { | |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 canceled_ = true; | |
| 179 | |
| 180 context_->PostTaskToNetworkThread( | 172 context_->PostTaskToNetworkThread( |
| 181 FROM_HERE, | 173 FROM_HERE, |
| 182 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this))); | 174 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this))); |
| 183 } | 175 } |
| 184 | 176 |
| 185 void URLRequestAdapter::OnCancelRequest() { | 177 void URLRequestAdapter::OnCancelRequest() { |
| 186 DCHECK(OnNetworkThread()); | 178 DCHECK(OnNetworkThread()); |
| 179 DCHECK(!canceled_); |
| 187 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); | 180 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); |
| 181 canceled_ = true; |
| 182 // Check whether request has already completed. |
| 183 if (url_request_ == nullptr) |
| 184 return; |
| 188 | 185 |
| 189 if (url_request_ != NULL) { | 186 url_request_->Cancel(); |
| 190 url_request_->Cancel(); | 187 OnRequestCompleted(); |
| 191 } | |
| 192 | |
| 193 OnRequestCanceled(); | |
| 194 } | 188 } |
| 195 | 189 |
| 196 void URLRequestAdapter::Destroy() { | 190 void URLRequestAdapter::Destroy() { |
| 197 context_->PostTaskToNetworkThread( | 191 context_->PostTaskToNetworkThread( |
| 198 FROM_HERE, base::Bind(&URLRequestAdapter::OnDestroyRequest, this)); | 192 FROM_HERE, base::Bind(&URLRequestAdapter::OnDestroyRequest, this)); |
| 199 } | 193 } |
| 200 | 194 |
| 201 // static | 195 // static |
| 202 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { | 196 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { |
| 203 DCHECK(self->OnNetworkThread()); | 197 DCHECK(self->OnNetworkThread()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 224 request->GetResponseHeaderByName("Content-Type", &content_type_); | 218 request->GetResponseHeaderByName("Content-Type", &content_type_); |
| 225 expected_size_ = request->GetExpectedContentSize(); | 219 expected_size_ = request->GetExpectedContentSize(); |
| 226 delegate_->OnResponseStarted(this); | 220 delegate_->OnResponseStarted(this); |
| 227 | 221 |
| 228 Read(); | 222 Read(); |
| 229 } | 223 } |
| 230 | 224 |
| 231 // Reads all available data or starts an asynchronous read. | 225 // Reads all available data or starts an asynchronous read. |
| 232 void URLRequestAdapter::Read() { | 226 void URLRequestAdapter::Read() { |
| 233 DCHECK(OnNetworkThread()); | 227 DCHECK(OnNetworkThread()); |
| 234 while (true) { | 228 if (!read_buffer_.get()) |
| 235 if (read_buffer_->RemainingCapacity() == 0) { | 229 read_buffer_ = new net::IOBufferWithSize(kReadBufferSize); |
| 236 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; | |
| 237 read_buffer_->SetCapacity(new_capacity); | |
| 238 } | |
| 239 | 230 |
| 240 int bytes_read; | 231 while(true) { |
| 241 if (url_request_->Read(read_buffer_.get(), | 232 int bytes_read = 0; |
| 242 read_buffer_->RemainingCapacity(), | 233 url_request_->Read(read_buffer_.get(), kReadBufferSize, &bytes_read); |
| 243 &bytes_read)) { | 234 // If IO is pending, wait for the URLRequest to call OnReadCompleted. |
| 244 if (bytes_read == 0) { | 235 if (url_request_->status().is_io_pending()) |
| 245 OnRequestSucceeded(); | 236 return; |
| 246 break; | 237 // Stop when request has failed or succeeded. |
| 247 } | 238 if (!HandleReadResult(bytes_read)) |
| 239 return; |
| 240 } |
| 241 } |
| 248 | 242 |
| 249 VLOG(1) << "Synchronously read: " << bytes_read << " bytes"; | 243 bool URLRequestAdapter::HandleReadResult(int bytes_read) { |
| 250 OnBytesRead(bytes_read); | 244 DCHECK(OnNetworkThread()); |
| 251 } else if (url_request_->status().status() == | 245 if (!url_request_->status().is_success()) { |
| 252 net::URLRequestStatus::IO_PENDING) { | 246 OnRequestFailed(); |
| 253 if (bytes_read_ != 0) { | 247 return false; |
| 254 VLOG(1) << "Flushing buffer: " << bytes_read_ << " bytes"; | 248 } else if (bytes_read == 0) { |
| 249 OnRequestSucceeded(); |
| 250 return false; |
| 251 } |
| 255 | 252 |
| 256 delegate_->OnBytesRead(this); | 253 total_bytes_read_ += bytes_read; |
| 257 read_buffer_->set_offset(0); | 254 delegate_->OnBytesRead(this, bytes_read); |
| 258 bytes_read_ = 0; | 255 |
| 259 } | 256 return true; |
| 260 VLOG(1) << "Started async read"; | |
| 261 break; | |
| 262 } else { | |
| 263 OnRequestFailed(); | |
| 264 break; | |
| 265 } | |
| 266 } | |
| 267 } | 257 } |
| 268 | 258 |
| 269 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, | 259 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, |
| 270 int bytes_read) { | 260 int bytes_read) { |
| 271 DCHECK(OnNetworkThread()); | 261 if (!HandleReadResult(bytes_read)) |
| 272 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes"; | |
| 273 if (bytes_read < 0) { | |
| 274 OnRequestFailed(); | |
| 275 return; | 262 return; |
| 276 } else if (bytes_read == 0) { | |
| 277 OnRequestSucceeded(); | |
| 278 return; | |
| 279 } | |
| 280 | 263 |
| 281 OnBytesRead(bytes_read); | |
| 282 Read(); | 264 Read(); |
| 283 } | 265 } |
| 284 | 266 |
| 285 void URLRequestAdapter::OnReceivedRedirect(net::URLRequest* request, | 267 void URLRequestAdapter::OnReceivedRedirect(net::URLRequest* request, |
| 286 const net::RedirectInfo& info, | 268 const net::RedirectInfo& info, |
| 287 bool* defer_redirect) { | 269 bool* defer_redirect) { |
| 288 DCHECK(OnNetworkThread()); | 270 DCHECK(OnNetworkThread()); |
| 289 if (disable_redirect_) { | 271 if (disable_redirect_) { |
| 290 http_status_code_ = request->GetResponseCode(); | 272 http_status_code_ = request->GetResponseCode(); |
| 291 request->CancelWithError(net::ERR_TOO_MANY_REDIRECTS); | 273 request->CancelWithError(net::ERR_TOO_MANY_REDIRECTS); |
| 292 error_code_ = net::ERR_TOO_MANY_REDIRECTS; | 274 error_code_ = net::ERR_TOO_MANY_REDIRECTS; |
| 293 canceled_ = true; | 275 canceled_ = true; |
| 294 *defer_redirect = false; | 276 *defer_redirect = false; |
| 295 OnRequestCompleted(); | 277 OnRequestCompleted(); |
| 296 } | 278 } |
| 297 } | 279 } |
| 298 | 280 |
| 299 void URLRequestAdapter::OnBytesRead(int bytes_read) { | |
| 300 DCHECK(OnNetworkThread()); | |
| 301 read_buffer_->set_offset(read_buffer_->offset() + bytes_read); | |
| 302 bytes_read_ += bytes_read; | |
| 303 total_bytes_read_ += bytes_read; | |
| 304 } | |
| 305 | |
| 306 void URLRequestAdapter::OnRequestSucceeded() { | 281 void URLRequestAdapter::OnRequestSucceeded() { |
| 307 DCHECK(OnNetworkThread()); | 282 DCHECK(OnNetworkThread()); |
| 308 if (canceled_) { | 283 if (canceled_) { |
| 309 return; | 284 return; |
| 310 } | 285 } |
| 311 | 286 |
| 312 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ | 287 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ |
| 313 << ". Total bytes read: " << total_bytes_read_; | 288 << ". Total bytes read: " << total_bytes_read_; |
| 314 | 289 |
| 315 OnRequestCompleted(); | 290 OnRequestCompleted(); |
| 316 } | 291 } |
| 317 | 292 |
| 318 void URLRequestAdapter::OnRequestFailed() { | 293 void URLRequestAdapter::OnRequestFailed() { |
| 319 DCHECK(OnNetworkThread()); | 294 DCHECK(OnNetworkThread()); |
| 320 if (canceled_) { | 295 if (canceled_) { |
| 321 return; | 296 return; |
| 322 } | 297 } |
| 323 | 298 |
| 324 error_code_ = url_request_->status().error(); | 299 error_code_ = url_request_->status().error(); |
| 325 VLOG(1) << "Request failed with status: " << url_request_->status().status() | 300 VLOG(1) << "Request failed with status: " << url_request_->status().status() |
| 326 << " and error: " << net::ErrorToString(error_code_); | 301 << " and error: " << net::ErrorToString(error_code_); |
| 327 OnRequestCompleted(); | 302 OnRequestCompleted(); |
| 328 } | 303 } |
| 329 | 304 |
| 330 void URLRequestAdapter::OnRequestCanceled() { | |
| 331 DCHECK(OnNetworkThread()); | |
| 332 OnRequestCompleted(); | |
| 333 } | |
| 334 | |
| 335 void URLRequestAdapter::OnRequestCompleted() { | 305 void URLRequestAdapter::OnRequestCompleted() { |
| 336 DCHECK(OnNetworkThread()); | 306 DCHECK(OnNetworkThread()); |
| 337 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); | 307 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); |
| 338 | 308 |
| 339 delegate_->OnBytesRead(this); | 309 DCHECK(url_request_ != nullptr); |
| 310 |
| 340 delegate_->OnRequestFinished(this); | 311 delegate_->OnRequestFinished(this); |
| 341 url_request_.reset(); | 312 url_request_.reset(); |
| 342 } | 313 } |
| 343 | 314 |
| 344 unsigned char* URLRequestAdapter::Data() const { | 315 unsigned char* URLRequestAdapter::Data() const { |
| 345 DCHECK(OnNetworkThread()); | 316 DCHECK(OnNetworkThread()); |
| 346 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); | 317 return reinterpret_cast<unsigned char*>(read_buffer_->data()); |
| 347 } | 318 } |
| 348 | 319 |
| 349 bool URLRequestAdapter::OnNetworkThread() const { | 320 bool URLRequestAdapter::OnNetworkThread() const { |
| 350 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); | 321 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); |
| 351 } | 322 } |
| 352 | 323 |
| 353 } // namespace cronet | 324 } // namespace cronet |
| OLD | NEW |