| 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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 128 |
| 129 URLRequestTestJob::~URLRequestTestJob() { | 129 URLRequestTestJob::~URLRequestTestJob() { |
| 130 g_pending_jobs.Get().erase( | 130 g_pending_jobs.Get().erase( |
| 131 std::remove( | 131 std::remove( |
| 132 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this), | 132 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this), |
| 133 g_pending_jobs.Get().end()); | 133 g_pending_jobs.Get().end()); |
| 134 } | 134 } |
| 135 | 135 |
| 136 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { | 136 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { |
| 137 DCHECK(mime_type); | 137 DCHECK(mime_type); |
| 138 if (!response_headers_) | 138 if (!response_headers_.get()) |
| 139 return false; | 139 return false; |
| 140 return response_headers_->GetMimeType(mime_type); | 140 return response_headers_->GetMimeType(mime_type); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void URLRequestTestJob::SetPriority(RequestPriority priority) { | 143 void URLRequestTestJob::SetPriority(RequestPriority priority) { |
| 144 priority_ = priority; | 144 priority_ = priority; |
| 145 } | 145 } |
| 146 | 146 |
| 147 void URLRequestTestJob::Start() { | 147 void URLRequestTestJob::Start() { |
| 148 // Start reading asynchronously so that all error reporting and data | 148 // Start reading asynchronously so that all error reporting and data |
| 149 // callbacks happen as they would for network requests. | 149 // callbacks happen as they would for network requests. |
| 150 base::MessageLoop::current()->PostTask( | 150 base::MessageLoop::current()->PostTask( |
| 151 FROM_HERE, base::Bind(&URLRequestTestJob::StartAsync, | 151 FROM_HERE, base::Bind(&URLRequestTestJob::StartAsync, |
| 152 weak_factory_.GetWeakPtr())); | 152 weak_factory_.GetWeakPtr())); |
| 153 } | 153 } |
| 154 | 154 |
| 155 void URLRequestTestJob::StartAsync() { | 155 void URLRequestTestJob::StartAsync() { |
| 156 if (!response_headers_) { | 156 if (!response_headers_.get()) { |
| 157 response_headers_ = new HttpResponseHeaders(test_headers()); | 157 response_headers_ = new HttpResponseHeaders(test_headers()); |
| 158 if (request_->url().spec() == test_url_1().spec()) { | 158 if (request_->url().spec() == test_url_1().spec()) { |
| 159 response_data_ = test_data_1(); | 159 response_data_ = test_data_1(); |
| 160 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. | 160 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. |
| 161 } else if (request_->url().spec() == test_url_2().spec()) { | 161 } else if (request_->url().spec() == test_url_2().spec()) { |
| 162 response_data_ = test_data_2(); | 162 response_data_ = test_data_2(); |
| 163 } else if (request_->url().spec() == test_url_3().spec()) { | 163 } else if (request_->url().spec() == test_url_3().spec()) { |
| 164 response_data_ = test_data_3(); | 164 response_data_ = test_data_3(); |
| 165 } else { | 165 } else { |
| 166 AdvanceJob(); | 166 AdvanceJob(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 to_read = static_cast<int>(response_data_.length()) - offset_; | 202 to_read = static_cast<int>(response_data_.length()) - offset_; |
| 203 | 203 |
| 204 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); | 204 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); |
| 205 offset_ += to_read; | 205 offset_ += to_read; |
| 206 | 206 |
| 207 *bytes_read = to_read; | 207 *bytes_read = to_read; |
| 208 return true; | 208 return true; |
| 209 } | 209 } |
| 210 | 210 |
| 211 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { | 211 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { |
| 212 if (response_headers_) | 212 if (response_headers_.get()) |
| 213 info->headers = response_headers_; | 213 info->headers = response_headers_; |
| 214 } | 214 } |
| 215 | 215 |
| 216 void URLRequestTestJob::GetLoadTimingInfo( | 216 void URLRequestTestJob::GetLoadTimingInfo( |
| 217 LoadTimingInfo* load_timing_info) const { | 217 LoadTimingInfo* load_timing_info) const { |
| 218 // Preserve the times the URLRequest is responsible for, but overwrite all | 218 // Preserve the times the URLRequest is responsible for, but overwrite all |
| 219 // the others. | 219 // the others. |
| 220 base::TimeTicks request_start = load_timing_info->request_start; | 220 base::TimeTicks request_start = load_timing_info->request_start; |
| 221 base::Time request_start_time = load_timing_info->request_start_time; | 221 base::Time request_start_time = load_timing_info->request_start_time; |
| 222 *load_timing_info = load_timing_info_; | 222 *load_timing_info = load_timing_info_; |
| 223 load_timing_info->request_start = request_start; | 223 load_timing_info->request_start = request_start; |
| 224 load_timing_info->request_start_time = request_start_time; | 224 load_timing_info->request_start_time = request_start_time; |
| 225 } | 225 } |
| 226 | 226 |
| 227 int URLRequestTestJob::GetResponseCode() const { | 227 int URLRequestTestJob::GetResponseCode() const { |
| 228 if (response_headers_) | 228 if (response_headers_.get()) |
| 229 return response_headers_->response_code(); | 229 return response_headers_->response_code(); |
| 230 return -1; | 230 return -1; |
| 231 } | 231 } |
| 232 | 232 |
| 233 bool URLRequestTestJob::IsRedirectResponse(GURL* location, | 233 bool URLRequestTestJob::IsRedirectResponse(GURL* location, |
| 234 int* http_status_code) { | 234 int* http_status_code) { |
| 235 if (!response_headers_) | 235 if (!response_headers_.get()) |
| 236 return false; | 236 return false; |
| 237 | 237 |
| 238 std::string value; | 238 std::string value; |
| 239 if (!response_headers_->IsRedirect(&value)) | 239 if (!response_headers_->IsRedirect(&value)) |
| 240 return false; | 240 return false; |
| 241 | 241 |
| 242 *location = request_->url().Resolve(value); | 242 *location = request_->url().Resolve(value); |
| 243 *http_status_code = response_headers_->response_code(); | 243 *http_status_code = response_headers_->response_code(); |
| 244 return true; | 244 return true; |
| 245 } | 245 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 311 |
| 312 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); | 312 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); |
| 313 g_pending_jobs.Get().pop_front(); | 313 g_pending_jobs.Get().pop_front(); |
| 314 | 314 |
| 315 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q | 315 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q |
| 316 next_job->ProcessNextOperation(); | 316 next_job->ProcessNextOperation(); |
| 317 return true; | 317 return true; |
| 318 } | 318 } |
| 319 | 319 |
| 320 } // namespace net | 320 } // namespace net |
| OLD | NEW |