| 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_peer.h" | 5 #include "url_request_peer.h" |
| 6 | 6 |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "components/cronet/android/url_request_context_peer.h" | 8 #include "components/cronet/android/url_request_context_peer.h" |
| 9 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" | 9 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" |
| 10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 FROM_HERE, | 79 FROM_HERE, |
| 80 base::Bind(&URLRequestPeer::OnInitiateConnection, | 80 base::Bind(&URLRequestPeer::OnInitiateConnection, |
| 81 base::Unretained(this))); | 81 base::Unretained(this))); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void URLRequestPeer::OnInitiateConnection() { | 84 void URLRequestPeer::OnInitiateConnection() { |
| 85 if (canceled_) { | 85 if (canceled_) { |
| 86 return; | 86 return; |
| 87 } | 87 } |
| 88 | 88 |
| 89 VLOG(context_->logging_level()) | 89 VLOG(1) << "Starting chromium request: " |
| 90 << "Starting chromium request: " << url_.possibly_invalid_spec().c_str() | 90 << url_.possibly_invalid_spec().c_str() |
| 91 << " priority: " << RequestPriorityToString(priority_); | 91 << " priority: " << RequestPriorityToString(priority_); |
| 92 url_request_ = new net::URLRequest( | 92 url_request_ = new net::URLRequest( |
| 93 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext()); | 93 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext()); |
| 94 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | 94 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
| 95 net::LOAD_DO_NOT_SAVE_COOKIES | | 95 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 96 net::LOAD_DO_NOT_SEND_COOKIES); | 96 net::LOAD_DO_NOT_SEND_COOKIES); |
| 97 url_request_->set_method(method_); | 97 url_request_->set_method(method_); |
| 98 url_request_->SetExtraRequestHeaders(headers_); | 98 url_request_->SetExtraRequestHeaders(headers_); |
| 99 if (!headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { | 99 if (!headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { |
| 100 std::string user_agent; | 100 std::string user_agent; |
| 101 user_agent = context_->GetUserAgent(url_); | 101 user_agent = context_->GetUserAgent(url_); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 117 } | 117 } |
| 118 | 118 |
| 119 canceled_ = true; | 119 canceled_ = true; |
| 120 | 120 |
| 121 context_->GetNetworkTaskRunner()->PostTask( | 121 context_->GetNetworkTaskRunner()->PostTask( |
| 122 FROM_HERE, | 122 FROM_HERE, |
| 123 base::Bind(&URLRequestPeer::OnCancelRequest, base::Unretained(this))); | 123 base::Bind(&URLRequestPeer::OnCancelRequest, base::Unretained(this))); |
| 124 } | 124 } |
| 125 | 125 |
| 126 void URLRequestPeer::OnCancelRequest() { | 126 void URLRequestPeer::OnCancelRequest() { |
| 127 VLOG(context_->logging_level()) | 127 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); |
| 128 << "Canceling chromium request: " << url_.possibly_invalid_spec(); | |
| 129 | 128 |
| 130 if (url_request_ != NULL) { | 129 if (url_request_ != NULL) { |
| 131 url_request_->Cancel(); | 130 url_request_->Cancel(); |
| 132 } | 131 } |
| 133 | 132 |
| 134 OnRequestCanceled(); | 133 OnRequestCanceled(); |
| 135 } | 134 } |
| 136 | 135 |
| 137 void URLRequestPeer::Destroy() { | 136 void URLRequestPeer::Destroy() { |
| 138 context_->GetNetworkTaskRunner()->PostTask( | 137 context_->GetNetworkTaskRunner()->PostTask( |
| 139 FROM_HERE, base::Bind(&URLRequestPeer::OnDestroyRequest, this)); | 138 FROM_HERE, base::Bind(&URLRequestPeer::OnDestroyRequest, this)); |
| 140 } | 139 } |
| 141 | 140 |
| 142 // static | 141 // static |
| 143 void URLRequestPeer::OnDestroyRequest(URLRequestPeer* self) { | 142 void URLRequestPeer::OnDestroyRequest(URLRequestPeer* self) { |
| 144 VLOG(self->context_->logging_level()) | 143 VLOG(1) << "Destroying chromium request: " |
| 145 << "Destroying chromium request: " << self->url_.possibly_invalid_spec(); | 144 << self->url_.possibly_invalid_spec(); |
| 146 delete self; | 145 delete self; |
| 147 } | 146 } |
| 148 | 147 |
| 149 void URLRequestPeer::OnResponseStarted(net::URLRequest* request) { | 148 void URLRequestPeer::OnResponseStarted(net::URLRequest* request) { |
| 150 if (request->status().status() != net::URLRequestStatus::SUCCESS) { | 149 if (request->status().status() != net::URLRequestStatus::SUCCESS) { |
| 151 OnRequestFailed(); | 150 OnRequestFailed(); |
| 152 return; | 151 return; |
| 153 } | 152 } |
| 154 | 153 |
| 155 http_status_code_ = request->GetResponseCode(); | 154 http_status_code_ = request->GetResponseCode(); |
| 156 VLOG(context_->logging_level()) | 155 VLOG(1) << "Response started with status: " << http_status_code_; |
| 157 << "Response started with status: " << http_status_code_; | |
| 158 | 156 |
| 159 request->GetResponseHeaderByName("Content-Type", &content_type_); | 157 request->GetResponseHeaderByName("Content-Type", &content_type_); |
| 160 expected_size_ = request->GetExpectedContentSize(); | 158 expected_size_ = request->GetExpectedContentSize(); |
| 161 delegate_->OnResponseStarted(this); | 159 delegate_->OnResponseStarted(this); |
| 162 | 160 |
| 163 Read(); | 161 Read(); |
| 164 } | 162 } |
| 165 | 163 |
| 166 // Reads all available data or starts an asynchronous read. | 164 // Reads all available data or starts an asynchronous read. |
| 167 void URLRequestPeer::Read() { | 165 void URLRequestPeer::Read() { |
| 168 while (true) { | 166 while (true) { |
| 169 if (read_buffer_->RemainingCapacity() == 0) { | 167 if (read_buffer_->RemainingCapacity() == 0) { |
| 170 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; | 168 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; |
| 171 read_buffer_->SetCapacity(new_capacity); | 169 read_buffer_->SetCapacity(new_capacity); |
| 172 } | 170 } |
| 173 | 171 |
| 174 int bytes_read; | 172 int bytes_read; |
| 175 if (url_request_->Read( | 173 if (url_request_->Read( |
| 176 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) { | 174 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) { |
| 177 if (bytes_read == 0) { | 175 if (bytes_read == 0) { |
| 178 OnRequestSucceeded(); | 176 OnRequestSucceeded(); |
| 179 break; | 177 break; |
| 180 } | 178 } |
| 181 | 179 |
| 182 VLOG(context_->logging_level()) << "Synchronously read: " << bytes_read | 180 VLOG(1) << "Synchronously read: " << bytes_read << " bytes"; |
| 183 << " bytes"; | |
| 184 OnBytesRead(bytes_read); | 181 OnBytesRead(bytes_read); |
| 185 } else if (url_request_->status().status() == | 182 } else if (url_request_->status().status() == |
| 186 net::URLRequestStatus::IO_PENDING) { | 183 net::URLRequestStatus::IO_PENDING) { |
| 187 if (bytes_read_ != 0) { | 184 if (bytes_read_ != 0) { |
| 188 VLOG(context_->logging_level()) << "Flushing buffer: " << bytes_read_ | 185 VLOG(1) << "Flushing buffer: " << bytes_read_ << " bytes"; |
| 189 << " bytes"; | |
| 190 | 186 |
| 191 delegate_->OnBytesRead(this); | 187 delegate_->OnBytesRead(this); |
| 192 read_buffer_->set_offset(0); | 188 read_buffer_->set_offset(0); |
| 193 bytes_read_ = 0; | 189 bytes_read_ = 0; |
| 194 } | 190 } |
| 195 VLOG(context_->logging_level()) << "Started async read"; | 191 VLOG(1) << "Started async read"; |
| 196 break; | 192 break; |
| 197 } else { | 193 } else { |
| 198 OnRequestFailed(); | 194 OnRequestFailed(); |
| 199 break; | 195 break; |
| 200 } | 196 } |
| 201 } | 197 } |
| 202 } | 198 } |
| 203 | 199 |
| 204 void URLRequestPeer::OnReadCompleted(net::URLRequest* request, int bytes_read) { | 200 void URLRequestPeer::OnReadCompleted(net::URLRequest* request, int bytes_read) { |
| 205 VLOG(context_->logging_level()) << "Asynchronously read: " << bytes_read | 201 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes"; |
| 206 << " bytes"; | |
| 207 if (bytes_read < 0) { | 202 if (bytes_read < 0) { |
| 208 OnRequestFailed(); | 203 OnRequestFailed(); |
| 209 return; | 204 return; |
| 210 } else if (bytes_read == 0) { | 205 } else if (bytes_read == 0) { |
| 211 OnRequestSucceeded(); | 206 OnRequestSucceeded(); |
| 212 return; | 207 return; |
| 213 } | 208 } |
| 214 | 209 |
| 215 OnBytesRead(bytes_read); | 210 OnBytesRead(bytes_read); |
| 216 Read(); | 211 Read(); |
| 217 } | 212 } |
| 218 | 213 |
| 219 void URLRequestPeer::OnBytesRead(int bytes_read) { | 214 void URLRequestPeer::OnBytesRead(int bytes_read) { |
| 220 read_buffer_->set_offset(read_buffer_->offset() + bytes_read); | 215 read_buffer_->set_offset(read_buffer_->offset() + bytes_read); |
| 221 bytes_read_ += bytes_read; | 216 bytes_read_ += bytes_read; |
| 222 total_bytes_read_ += bytes_read; | 217 total_bytes_read_ += bytes_read; |
| 223 } | 218 } |
| 224 | 219 |
| 225 void URLRequestPeer::OnRequestSucceeded() { | 220 void URLRequestPeer::OnRequestSucceeded() { |
| 226 if (canceled_) { | 221 if (canceled_) { |
| 227 return; | 222 return; |
| 228 } | 223 } |
| 229 | 224 |
| 230 VLOG(context_->logging_level()) | 225 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ |
| 231 << "Request completed with HTTP status: " << http_status_code_ | 226 << ". Total bytes read: " << total_bytes_read_; |
| 232 << ". Total bytes read: " << total_bytes_read_; | |
| 233 | 227 |
| 234 OnRequestCompleted(); | 228 OnRequestCompleted(); |
| 235 } | 229 } |
| 236 | 230 |
| 237 void URLRequestPeer::OnRequestFailed() { | 231 void URLRequestPeer::OnRequestFailed() { |
| 238 if (canceled_) { | 232 if (canceled_) { |
| 239 return; | 233 return; |
| 240 } | 234 } |
| 241 | 235 |
| 242 error_code_ = url_request_->status().error(); | 236 error_code_ = url_request_->status().error(); |
| 243 VLOG(context_->logging_level()) | 237 VLOG(1) << "Request failed with status: " << url_request_->status().status() |
| 244 << "Request failed with status: " << url_request_->status().status() | 238 << " and error: " << net::ErrorToString(error_code_); |
| 245 << " and error: " << net::ErrorToString(error_code_); | |
| 246 OnRequestCompleted(); | 239 OnRequestCompleted(); |
| 247 } | 240 } |
| 248 | 241 |
| 249 void URLRequestPeer::OnRequestCanceled() { OnRequestCompleted(); } | 242 void URLRequestPeer::OnRequestCanceled() { OnRequestCompleted(); } |
| 250 | 243 |
| 251 void URLRequestPeer::OnRequestCompleted() { | 244 void URLRequestPeer::OnRequestCompleted() { |
| 252 VLOG(context_->logging_level()) | 245 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); |
| 253 << "Completed: " << url_.possibly_invalid_spec(); | |
| 254 if (url_request_ != NULL) { | 246 if (url_request_ != NULL) { |
| 255 delete url_request_; | 247 delete url_request_; |
| 256 url_request_ = NULL; | 248 url_request_ = NULL; |
| 257 } | 249 } |
| 258 | 250 |
| 259 delegate_->OnBytesRead(this); | 251 delegate_->OnBytesRead(this); |
| 260 delegate_->OnRequestFinished(this); | 252 delegate_->OnRequestFinished(this); |
| 261 } | 253 } |
| 262 | 254 |
| 263 unsigned char* URLRequestPeer::Data() const { | 255 unsigned char* URLRequestPeer::Data() const { |
| 264 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); | 256 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); |
| 265 } | 257 } |
| 266 | 258 |
| 267 } // namespace cronet | 259 } // namespace cronet |
| OLD | NEW |