| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/url_request/url_request_test_job.h" | 5 #include "net/url_request/url_request_test_job.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <list> | 8 #include <list> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 response_data_ = test_data_4(); | 197 response_data_ = test_data_4(); |
| 198 } else if (request_->url().spec() == test_url_redirect_to_url_2().spec()) { | 198 } else if (request_->url().spec() == test_url_redirect_to_url_2().spec()) { |
| 199 response_headers_ = | 199 response_headers_ = |
| 200 new HttpResponseHeaders(test_redirect_to_url_2_headers()); | 200 new HttpResponseHeaders(test_redirect_to_url_2_headers()); |
| 201 } else { | 201 } else { |
| 202 AdvanceJob(); | 202 AdvanceJob(); |
| 203 | 203 |
| 204 // unexpected url, return error | 204 // unexpected url, return error |
| 205 // FIXME(brettw) we may want to use WININET errors or have some more types | 205 // FIXME(brettw) we may want to use WININET errors or have some more types |
| 206 // of errors | 206 // of errors |
| 207 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 207 NotifyStartError( |
| 208 ERR_INVALID_URL)); | 208 URLRequestStatus(URLRequestStatus::FAILED, ERR_INVALID_URL)); |
| 209 // FIXME(brettw): this should emulate a network error, and not just fail | 209 // FIXME(brettw): this should emulate a network error, and not just fail |
| 210 // initiating a connection | 210 // initiating a connection |
| 211 return; | 211 return; |
| 212 } | 212 } |
| 213 } | 213 } |
| 214 | 214 |
| 215 AdvanceJob(); | 215 AdvanceJob(); |
| 216 | 216 |
| 217 this->NotifyHeadersComplete(); | 217 this->NotifyHeadersComplete(); |
| 218 } | 218 } |
| 219 | 219 |
| 220 bool URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size, | 220 int URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size) { |
| 221 int *bytes_read) { | |
| 222 if (stage_ == WAITING) { | 221 if (stage_ == WAITING) { |
| 223 async_buf_ = buf; | 222 async_buf_ = buf; |
| 224 async_buf_size_ = buf_size; | 223 async_buf_size_ = buf_size; |
| 225 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 224 return ERR_IO_PENDING; |
| 226 return false; | |
| 227 } | 225 } |
| 228 | 226 |
| 229 DCHECK(bytes_read); | 227 if (offset_ >= static_cast<int>(response_data_.length())) |
| 230 *bytes_read = 0; | 228 return 0; // done reading |
| 231 | |
| 232 if (offset_ >= static_cast<int>(response_data_.length())) { | |
| 233 return true; // done reading | |
| 234 } | |
| 235 | 229 |
| 236 int to_read = buf_size; | 230 int to_read = buf_size; |
| 237 if (to_read + offset_ > static_cast<int>(response_data_.length())) | 231 if (to_read + offset_ > static_cast<int>(response_data_.length())) |
| 238 to_read = static_cast<int>(response_data_.length()) - offset_; | 232 to_read = static_cast<int>(response_data_.length()) - offset_; |
| 239 | 233 |
| 240 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); | 234 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); |
| 241 offset_ += to_read; | 235 offset_ += to_read; |
| 242 | 236 |
| 243 *bytes_read = to_read; | 237 return to_read; |
| 244 return true; | |
| 245 } | 238 } |
| 246 | 239 |
| 247 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { | 240 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { |
| 248 if (response_headers_.get()) | 241 if (response_headers_.get()) |
| 249 info->headers = response_headers_; | 242 info->headers = response_headers_; |
| 250 } | 243 } |
| 251 | 244 |
| 252 void URLRequestTestJob::GetLoadTimingInfo( | 245 void URLRequestTestJob::GetLoadTimingInfo( |
| 253 LoadTimingInfo* load_timing_info) const { | 246 LoadTimingInfo* load_timing_info) const { |
| 254 // Preserve the times the URLRequest is responsible for, but overwrite all | 247 // Preserve the times the URLRequest is responsible for, but overwrite all |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 | 285 |
| 293 void URLRequestTestJob::ProcessNextOperation() { | 286 void URLRequestTestJob::ProcessNextOperation() { |
| 294 switch (stage_) { | 287 switch (stage_) { |
| 295 case WAITING: | 288 case WAITING: |
| 296 // Must call AdvanceJob() prior to NotifyReadComplete() since that may | 289 // Must call AdvanceJob() prior to NotifyReadComplete() since that may |
| 297 // delete |this|. | 290 // delete |this|. |
| 298 AdvanceJob(); | 291 AdvanceJob(); |
| 299 stage_ = DATA_AVAILABLE; | 292 stage_ = DATA_AVAILABLE; |
| 300 // OK if ReadRawData wasn't called yet. | 293 // OK if ReadRawData wasn't called yet. |
| 301 if (async_buf_) { | 294 if (async_buf_) { |
| 302 int bytes_read; | 295 int result = ReadRawData(async_buf_, async_buf_size_); |
| 303 if (!ReadRawData(async_buf_, async_buf_size_, &bytes_read)) | 296 if (result < 0) |
| 304 NOTREACHED() << "This should not return false in DATA_AVAILABLE."; | 297 NOTREACHED() << "Reads should not fail in DATA_AVAILABLE."; |
| 305 SetStatus(URLRequestStatus()); // clear the io pending flag | |
| 306 if (NextReadAsync()) { | 298 if (NextReadAsync()) { |
| 307 // Make all future reads return io pending until the next | 299 // Make all future reads return io pending until the next |
| 308 // ProcessNextOperation(). | 300 // ProcessNextOperation(). |
| 309 stage_ = WAITING; | 301 stage_ = WAITING; |
| 310 } | 302 } |
| 311 NotifyReadComplete(bytes_read); | 303 ReadRawDataComplete(result); |
| 312 } | 304 } |
| 313 break; | 305 break; |
| 314 case DATA_AVAILABLE: | 306 case DATA_AVAILABLE: |
| 315 AdvanceJob(); | 307 AdvanceJob(); |
| 316 stage_ = ALL_DATA; // done sending data | 308 stage_ = ALL_DATA; // done sending data |
| 317 break; | 309 break; |
| 318 case ALL_DATA: | 310 case ALL_DATA: |
| 319 stage_ = DONE; | 311 stage_ = DONE; |
| 320 return; | 312 return; |
| 321 case DONE: | 313 case DONE: |
| (...skipping 25 matching lines...) Expand all Loading... |
| 347 | 339 |
| 348 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); | 340 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); |
| 349 g_pending_jobs.Get().pop_front(); | 341 g_pending_jobs.Get().pop_front(); |
| 350 | 342 |
| 351 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q | 343 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q |
| 352 next_job->ProcessNextOperation(); | 344 next_job->ProcessNextOperation(); |
| 353 return true; | 345 return true; |
| 354 } | 346 } |
| 355 | 347 |
| 356 } // namespace net | 348 } // namespace net |
| OLD | NEW |