| 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/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 13 #include "components/cronet/android/url_request_context_adapter.h" | 14 #include "components/cronet/android/url_request_context_adapter.h" |
| 14 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" | 15 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" |
| 15 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 16 #include "net/base/upload_bytes_element_reader.h" | 17 #include "net/base/upload_bytes_element_reader.h" |
| 17 #include "net/http/http_status_code.h" | 18 #include "net/http/http_status_code.h" |
| 18 | 19 |
| 19 namespace cronet { | 20 namespace cronet { |
| 20 | 21 |
| 21 static const size_t kBufferSizeIncrement = 8192; | 22 static const size_t kBufferSizeIncrement = 8192; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 33 canceled_(false), | 34 canceled_(false), |
| 34 expected_size_(0), | 35 expected_size_(0), |
| 35 chunked_upload_(false) { | 36 chunked_upload_(false) { |
| 36 context_ = context; | 37 context_ = context; |
| 37 delegate_ = delegate; | 38 delegate_ = delegate; |
| 38 url_ = url; | 39 url_ = url; |
| 39 priority_ = priority; | 40 priority_ = priority; |
| 40 } | 41 } |
| 41 | 42 |
| 42 URLRequestAdapter::~URLRequestAdapter() { | 43 URLRequestAdapter::~URLRequestAdapter() { |
| 44 DCHECK(OnNetworkThread()); |
| 43 CHECK(url_request_ == NULL); | 45 CHECK(url_request_ == NULL); |
| 44 } | 46 } |
| 45 | 47 |
| 46 void URLRequestAdapter::SetMethod(const std::string& method) { | 48 void URLRequestAdapter::SetMethod(const std::string& method) { |
| 47 method_ = method; | 49 method_ = method; |
| 48 } | 50 } |
| 49 | 51 |
| 50 void URLRequestAdapter::AddHeader(const std::string& name, | 52 void URLRequestAdapter::AddHeader(const std::string& name, |
| 51 const std::string& value) { | 53 const std::string& value) { |
| 52 headers_.SetHeader(name, value); | 54 headers_.SetHeader(name, value); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 69 | 71 |
| 70 void URLRequestAdapter::EnableChunkedUpload() { | 72 void URLRequestAdapter::EnableChunkedUpload() { |
| 71 chunked_upload_ = true; | 73 chunked_upload_ = true; |
| 72 } | 74 } |
| 73 | 75 |
| 74 void URLRequestAdapter::AppendChunk(const char* bytes, int bytes_len, | 76 void URLRequestAdapter::AppendChunk(const char* bytes, int bytes_len, |
| 75 bool is_last_chunk) { | 77 bool is_last_chunk) { |
| 76 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk; | 78 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk; |
| 77 scoped_ptr<char[]> buf(new char[bytes_len]); | 79 scoped_ptr<char[]> buf(new char[bytes_len]); |
| 78 memcpy(buf.get(), bytes, bytes_len); | 80 memcpy(buf.get(), bytes, bytes_len); |
| 79 context_->GetNetworkTaskRunner()->PostTask( | 81 context_->PostTaskToNetworkThread( |
| 80 FROM_HERE, | 82 FROM_HERE, |
| 81 base::Bind(&URLRequestAdapter::OnAppendChunk, | 83 base::Bind(&URLRequestAdapter::OnAppendChunk, |
| 82 base::Unretained(this), | 84 base::Unretained(this), |
| 83 Passed(buf.Pass()), | 85 Passed(buf.Pass()), |
| 84 bytes_len, | 86 bytes_len, |
| 85 is_last_chunk)); | 87 is_last_chunk)); |
| 86 } | 88 } |
| 87 | 89 |
| 88 std::string URLRequestAdapter::GetHeader(const std::string& name) const { | 90 std::string URLRequestAdapter::GetHeader(const std::string& name) const { |
| 89 std::string value; | 91 std::string value; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 100 return url_request_->response_headers(); | 102 return url_request_->response_headers(); |
| 101 } | 103 } |
| 102 | 104 |
| 103 std::string URLRequestAdapter::GetNegotiatedProtocol() const { | 105 std::string URLRequestAdapter::GetNegotiatedProtocol() const { |
| 104 if (url_request_ == NULL) | 106 if (url_request_ == NULL) |
| 105 return std::string(); | 107 return std::string(); |
| 106 return url_request_->response_info().npn_negotiated_protocol; | 108 return url_request_->response_info().npn_negotiated_protocol; |
| 107 } | 109 } |
| 108 | 110 |
| 109 void URLRequestAdapter::Start() { | 111 void URLRequestAdapter::Start() { |
| 110 context_->GetNetworkTaskRunner()->PostTask( | 112 context_->PostTaskToNetworkThread( |
| 111 FROM_HERE, | 113 FROM_HERE, |
| 112 base::Bind(&URLRequestAdapter::OnInitiateConnection, | 114 base::Bind(&URLRequestAdapter::OnInitiateConnection, |
| 113 base::Unretained(this))); | 115 base::Unretained(this))); |
| 114 } | 116 } |
| 115 | 117 |
| 116 void URLRequestAdapter::OnAppendChunk(const scoped_ptr<char[]> bytes, | 118 void URLRequestAdapter::OnAppendChunk(const scoped_ptr<char[]> bytes, |
| 117 int bytes_len, bool is_last_chunk) { | 119 int bytes_len, bool is_last_chunk) { |
| 120 DCHECK(OnNetworkThread()); |
| 118 url_request_->AppendChunkToUpload(bytes.get(), bytes_len, is_last_chunk); | 121 url_request_->AppendChunkToUpload(bytes.get(), bytes_len, is_last_chunk); |
| 119 } | 122 } |
| 120 | 123 |
| 121 void URLRequestAdapter::OnInitiateConnection() { | 124 void URLRequestAdapter::OnInitiateConnection() { |
| 125 DCHECK(OnNetworkThread()); |
| 122 if (canceled_) { | 126 if (canceled_) { |
| 123 return; | 127 return; |
| 124 } | 128 } |
| 125 | 129 |
| 126 VLOG(1) << "Starting chromium request: " | 130 VLOG(1) << "Starting chromium request: " |
| 127 << url_.possibly_invalid_spec().c_str() | 131 << url_.possibly_invalid_spec().c_str() |
| 128 << " priority: " << RequestPriorityToString(priority_); | 132 << " priority: " << RequestPriorityToString(priority_); |
| 129 url_request_ = context_->GetURLRequestContext()->CreateRequest( | 133 url_request_ = context_->GetURLRequestContext()->CreateRequest( |
| 130 url_, net::DEFAULT_PRIORITY, this, NULL); | 134 url_, net::DEFAULT_PRIORITY, this, NULL); |
| 131 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | 135 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
| (...skipping 19 matching lines...) Expand all Loading... |
| 151 url_request_->Start(); | 155 url_request_->Start(); |
| 152 } | 156 } |
| 153 | 157 |
| 154 void URLRequestAdapter::Cancel() { | 158 void URLRequestAdapter::Cancel() { |
| 155 if (canceled_) { | 159 if (canceled_) { |
| 156 return; | 160 return; |
| 157 } | 161 } |
| 158 | 162 |
| 159 canceled_ = true; | 163 canceled_ = true; |
| 160 | 164 |
| 161 context_->GetNetworkTaskRunner()->PostTask( | 165 context_->PostTaskToNetworkThread( |
| 162 FROM_HERE, | 166 FROM_HERE, |
| 163 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this))); | 167 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this))); |
| 164 } | 168 } |
| 165 | 169 |
| 166 void URLRequestAdapter::OnCancelRequest() { | 170 void URLRequestAdapter::OnCancelRequest() { |
| 171 DCHECK(OnNetworkThread()); |
| 167 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); | 172 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); |
| 168 | 173 |
| 169 if (url_request_ != NULL) { | 174 if (url_request_ != NULL) { |
| 170 url_request_->Cancel(); | 175 url_request_->Cancel(); |
| 171 } | 176 } |
| 172 | 177 |
| 173 OnRequestCanceled(); | 178 OnRequestCanceled(); |
| 174 } | 179 } |
| 175 | 180 |
| 176 void URLRequestAdapter::Destroy() { | 181 void URLRequestAdapter::Destroy() { |
| 177 context_->GetNetworkTaskRunner()->PostTask( | 182 context_->PostTaskToNetworkThread( |
| 178 FROM_HERE, base::Bind(&URLRequestAdapter::OnDestroyRequest, this)); | 183 FROM_HERE, base::Bind(&URLRequestAdapter::OnDestroyRequest, this)); |
| 179 } | 184 } |
| 180 | 185 |
| 181 // static | 186 // static |
| 182 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { | 187 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { |
| 188 DCHECK(self->OnNetworkThread()); |
| 183 VLOG(1) << "Destroying chromium request: " | 189 VLOG(1) << "Destroying chromium request: " |
| 184 << self->url_.possibly_invalid_spec(); | 190 << self->url_.possibly_invalid_spec(); |
| 185 delete self; | 191 delete self; |
| 186 } | 192 } |
| 187 | 193 |
| 194 // static |
| 188 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) { | 195 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) { |
| 196 DCHECK(OnNetworkThread()); |
| 189 if (request->status().status() != net::URLRequestStatus::SUCCESS) { | 197 if (request->status().status() != net::URLRequestStatus::SUCCESS) { |
| 190 OnRequestFailed(); | 198 OnRequestFailed(); |
| 191 return; | 199 return; |
| 192 } | 200 } |
| 193 | 201 |
| 194 http_status_code_ = request->GetResponseCode(); | 202 http_status_code_ = request->GetResponseCode(); |
| 195 VLOG(1) << "Response started with status: " << http_status_code_; | 203 VLOG(1) << "Response started with status: " << http_status_code_; |
| 196 | 204 |
| 197 request->GetResponseHeaderByName("Content-Type", &content_type_); | 205 request->GetResponseHeaderByName("Content-Type", &content_type_); |
| 198 expected_size_ = request->GetExpectedContentSize(); | 206 expected_size_ = request->GetExpectedContentSize(); |
| 199 delegate_->OnResponseStarted(this); | 207 delegate_->OnResponseStarted(this); |
| 200 | 208 |
| 201 Read(); | 209 Read(); |
| 202 } | 210 } |
| 203 | 211 |
| 204 // Reads all available data or starts an asynchronous read. | 212 // Reads all available data or starts an asynchronous read. |
| 205 void URLRequestAdapter::Read() { | 213 void URLRequestAdapter::Read() { |
| 214 DCHECK(OnNetworkThread()); |
| 206 while (true) { | 215 while (true) { |
| 207 if (read_buffer_->RemainingCapacity() == 0) { | 216 if (read_buffer_->RemainingCapacity() == 0) { |
| 208 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; | 217 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; |
| 209 read_buffer_->SetCapacity(new_capacity); | 218 read_buffer_->SetCapacity(new_capacity); |
| 210 } | 219 } |
| 211 | 220 |
| 212 int bytes_read; | 221 int bytes_read; |
| 213 if (url_request_->Read( | 222 if (url_request_->Read( |
| 214 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) { | 223 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) { |
| 215 if (bytes_read == 0) { | 224 if (bytes_read == 0) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 232 break; | 241 break; |
| 233 } else { | 242 } else { |
| 234 OnRequestFailed(); | 243 OnRequestFailed(); |
| 235 break; | 244 break; |
| 236 } | 245 } |
| 237 } | 246 } |
| 238 } | 247 } |
| 239 | 248 |
| 240 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, | 249 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, |
| 241 int bytes_read) { | 250 int bytes_read) { |
| 251 DCHECK(OnNetworkThread()); |
| 242 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes"; | 252 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes"; |
| 243 if (bytes_read < 0) { | 253 if (bytes_read < 0) { |
| 244 OnRequestFailed(); | 254 OnRequestFailed(); |
| 245 return; | 255 return; |
| 246 } else if (bytes_read == 0) { | 256 } else if (bytes_read == 0) { |
| 247 OnRequestSucceeded(); | 257 OnRequestSucceeded(); |
| 248 return; | 258 return; |
| 249 } | 259 } |
| 250 | 260 |
| 251 OnBytesRead(bytes_read); | 261 OnBytesRead(bytes_read); |
| 252 Read(); | 262 Read(); |
| 253 } | 263 } |
| 254 | 264 |
| 255 void URLRequestAdapter::OnBytesRead(int bytes_read) { | 265 void URLRequestAdapter::OnBytesRead(int bytes_read) { |
| 266 DCHECK(OnNetworkThread()); |
| 256 read_buffer_->set_offset(read_buffer_->offset() + bytes_read); | 267 read_buffer_->set_offset(read_buffer_->offset() + bytes_read); |
| 257 bytes_read_ += bytes_read; | 268 bytes_read_ += bytes_read; |
| 258 total_bytes_read_ += bytes_read; | 269 total_bytes_read_ += bytes_read; |
| 259 } | 270 } |
| 260 | 271 |
| 261 void URLRequestAdapter::OnRequestSucceeded() { | 272 void URLRequestAdapter::OnRequestSucceeded() { |
| 273 DCHECK(OnNetworkThread()); |
| 262 if (canceled_) { | 274 if (canceled_) { |
| 263 return; | 275 return; |
| 264 } | 276 } |
| 265 | 277 |
| 266 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ | 278 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ |
| 267 << ". Total bytes read: " << total_bytes_read_; | 279 << ". Total bytes read: " << total_bytes_read_; |
| 268 | 280 |
| 269 OnRequestCompleted(); | 281 OnRequestCompleted(); |
| 270 } | 282 } |
| 271 | 283 |
| 272 void URLRequestAdapter::OnRequestFailed() { | 284 void URLRequestAdapter::OnRequestFailed() { |
| 285 DCHECK(OnNetworkThread()); |
| 273 if (canceled_) { | 286 if (canceled_) { |
| 274 return; | 287 return; |
| 275 } | 288 } |
| 276 | 289 |
| 277 error_code_ = url_request_->status().error(); | 290 error_code_ = url_request_->status().error(); |
| 278 VLOG(1) << "Request failed with status: " << url_request_->status().status() | 291 VLOG(1) << "Request failed with status: " << url_request_->status().status() |
| 279 << " and error: " << net::ErrorToString(error_code_); | 292 << " and error: " << net::ErrorToString(error_code_); |
| 280 OnRequestCompleted(); | 293 OnRequestCompleted(); |
| 281 } | 294 } |
| 282 | 295 |
| 283 void URLRequestAdapter::OnRequestCanceled() { | 296 void URLRequestAdapter::OnRequestCanceled() { |
| 297 DCHECK(OnNetworkThread()); |
| 284 OnRequestCompleted(); | 298 OnRequestCompleted(); |
| 285 } | 299 } |
| 286 | 300 |
| 287 void URLRequestAdapter::OnRequestCompleted() { | 301 void URLRequestAdapter::OnRequestCompleted() { |
| 302 DCHECK(OnNetworkThread()); |
| 288 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); | 303 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); |
| 289 url_request_.reset(); | 304 url_request_.reset(); |
| 290 | 305 |
| 291 delegate_->OnBytesRead(this); | 306 delegate_->OnBytesRead(this); |
| 292 delegate_->OnRequestFinished(this); | 307 delegate_->OnRequestFinished(this); |
| 293 } | 308 } |
| 294 | 309 |
| 295 unsigned char* URLRequestAdapter::Data() const { | 310 unsigned char* URLRequestAdapter::Data() const { |
| 311 DCHECK(OnNetworkThread()); |
| 296 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); | 312 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); |
| 297 } | 313 } |
| 298 | 314 |
| 315 bool URLRequestAdapter::OnNetworkThread() const { |
| 316 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); |
| 317 } |
| 318 |
| 299 } // namespace cronet | 319 } // namespace cronet |
| OLD | NEW |