| 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" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 typedef std::list<URLRequestTestJob*> URLRequestJobList; | 23 typedef std::list<URLRequestTestJob*> URLRequestJobList; |
| 24 base::LazyInstance<URLRequestJobList>::Leaky | 24 base::LazyInstance<URLRequestJobList>::Leaky g_pending_jobs = |
| 25 g_pending_jobs = LAZY_INSTANCE_INITIALIZER; | 25 LAZY_INSTANCE_INITIALIZER; |
| 26 | 26 |
| 27 class TestJobProtocolHandler : public URLRequestJobFactory::ProtocolHandler { | 27 class TestJobProtocolHandler : public URLRequestJobFactory::ProtocolHandler { |
| 28 public: | 28 public: |
| 29 // URLRequestJobFactory::ProtocolHandler implementation: | 29 // URLRequestJobFactory::ProtocolHandler implementation: |
| 30 virtual URLRequestJob* MaybeCreateJob( | 30 virtual URLRequestJob* MaybeCreateJob( |
| 31 URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE { | 31 URLRequest* request, |
| 32 NetworkDelegate* network_delegate) const OVERRIDE { |
| 32 return new URLRequestTestJob(request, network_delegate); | 33 return new URLRequestTestJob(request, network_delegate); |
| 33 } | 34 } |
| 34 }; | 35 }; |
| 35 | 36 |
| 36 } // namespace | 37 } // namespace |
| 37 | 38 |
| 38 // static getters for known URLs | 39 // static getters for known URLs |
| 39 GURL URLRequestTestJob::test_url_1() { | 40 GURL URLRequestTestJob::test_url_1() { |
| 40 return GURL("test:url1"); | 41 return GURL("test:url1"); |
| 41 } | 42 } |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 } | 170 } |
| 170 | 171 |
| 171 void URLRequestTestJob::SetPriority(RequestPriority priority) { | 172 void URLRequestTestJob::SetPriority(RequestPriority priority) { |
| 172 priority_ = priority; | 173 priority_ = priority; |
| 173 } | 174 } |
| 174 | 175 |
| 175 void URLRequestTestJob::Start() { | 176 void URLRequestTestJob::Start() { |
| 176 // Start reading asynchronously so that all error reporting and data | 177 // Start reading asynchronously so that all error reporting and data |
| 177 // callbacks happen as they would for network requests. | 178 // callbacks happen as they would for network requests. |
| 178 base::MessageLoop::current()->PostTask( | 179 base::MessageLoop::current()->PostTask( |
| 179 FROM_HERE, base::Bind(&URLRequestTestJob::StartAsync, | 180 FROM_HERE, |
| 180 weak_factory_.GetWeakPtr())); | 181 base::Bind(&URLRequestTestJob::StartAsync, weak_factory_.GetWeakPtr())); |
| 181 } | 182 } |
| 182 | 183 |
| 183 void URLRequestTestJob::StartAsync() { | 184 void URLRequestTestJob::StartAsync() { |
| 184 if (!response_headers_.get()) { | 185 if (!response_headers_.get()) { |
| 185 response_headers_ = new HttpResponseHeaders(test_headers()); | 186 response_headers_ = new HttpResponseHeaders(test_headers()); |
| 186 if (request_->url().spec() == test_url_1().spec()) { | 187 if (request_->url().spec() == test_url_1().spec()) { |
| 187 response_data_ = test_data_1(); | 188 response_data_ = test_data_1(); |
| 188 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. | 189 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. |
| 189 } else if (request_->url().spec() == test_url_2().spec()) { | 190 } else if (request_->url().spec() == test_url_2().spec()) { |
| 190 response_data_ = test_data_2(); | 191 response_data_ = test_data_2(); |
| 191 } else if (request_->url().spec() == test_url_3().spec()) { | 192 } else if (request_->url().spec() == test_url_3().spec()) { |
| 192 response_data_ = test_data_3(); | 193 response_data_ = test_data_3(); |
| 193 } else if (request_->url().spec() == test_url_4().spec()) { | 194 } else if (request_->url().spec() == test_url_4().spec()) { |
| 194 response_data_ = test_data_4(); | 195 response_data_ = test_data_4(); |
| 195 } else if (request_->url().spec() == test_url_redirect_to_url_2().spec()) { | 196 } else if (request_->url().spec() == test_url_redirect_to_url_2().spec()) { |
| 196 response_headers_ = | 197 response_headers_ = |
| 197 new HttpResponseHeaders(test_redirect_to_url_2_headers()); | 198 new HttpResponseHeaders(test_redirect_to_url_2_headers()); |
| 198 } else { | 199 } else { |
| 199 AdvanceJob(); | 200 AdvanceJob(); |
| 200 | 201 |
| 201 // unexpected url, return error | 202 // unexpected url, return error |
| 202 // FIXME(brettw) we may want to use WININET errors or have some more types | 203 // FIXME(brettw) we may want to use WININET errors or have some more types |
| 203 // of errors | 204 // of errors |
| 204 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 205 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, ERR_INVALID_URL)); |
| 205 ERR_INVALID_URL)); | |
| 206 // FIXME(brettw): this should emulate a network error, and not just fail | 206 // FIXME(brettw): this should emulate a network error, and not just fail |
| 207 // initiating a connection | 207 // initiating a connection |
| 208 return; | 208 return; |
| 209 } | 209 } |
| 210 } | 210 } |
| 211 | 211 |
| 212 AdvanceJob(); | 212 AdvanceJob(); |
| 213 | 213 |
| 214 this->NotifyHeadersComplete(); | 214 this->NotifyHeadersComplete(); |
| 215 } | 215 } |
| 216 | 216 |
| 217 bool URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size, | 217 bool URLRequestTestJob::ReadRawData(IOBuffer* buf, |
| 218 int *bytes_read) { | 218 int buf_size, |
| 219 int* bytes_read) { |
| 219 if (stage_ == WAITING) { | 220 if (stage_ == WAITING) { |
| 220 async_buf_ = buf; | 221 async_buf_ = buf; |
| 221 async_buf_size_ = buf_size; | 222 async_buf_size_ = buf_size; |
| 222 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 223 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 223 return false; | 224 return false; |
| 224 } | 225 } |
| 225 | 226 |
| 226 DCHECK(bytes_read); | 227 DCHECK(bytes_read); |
| 227 *bytes_read = 0; | 228 *bytes_read = 0; |
| 228 | 229 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 } | 324 } |
| 324 } | 325 } |
| 325 | 326 |
| 326 bool URLRequestTestJob::NextReadAsync() { | 327 bool URLRequestTestJob::NextReadAsync() { |
| 327 return false; | 328 return false; |
| 328 } | 329 } |
| 329 | 330 |
| 330 void URLRequestTestJob::AdvanceJob() { | 331 void URLRequestTestJob::AdvanceJob() { |
| 331 if (auto_advance_) { | 332 if (auto_advance_) { |
| 332 base::MessageLoop::current()->PostTask( | 333 base::MessageLoop::current()->PostTask( |
| 333 FROM_HERE, base::Bind(&URLRequestTestJob::ProcessNextOperation, | 334 FROM_HERE, |
| 334 weak_factory_.GetWeakPtr())); | 335 base::Bind(&URLRequestTestJob::ProcessNextOperation, |
| 336 weak_factory_.GetWeakPtr())); |
| 335 return; | 337 return; |
| 336 } | 338 } |
| 337 g_pending_jobs.Get().push_back(this); | 339 g_pending_jobs.Get().push_back(this); |
| 338 } | 340 } |
| 339 | 341 |
| 340 // static | 342 // static |
| 341 bool URLRequestTestJob::ProcessOnePendingMessage() { | 343 bool URLRequestTestJob::ProcessOnePendingMessage() { |
| 342 if (g_pending_jobs.Get().empty()) | 344 if (g_pending_jobs.Get().empty()) |
| 343 return false; | 345 return false; |
| 344 | 346 |
| 345 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); | 347 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); |
| 346 g_pending_jobs.Get().pop_front(); | 348 g_pending_jobs.Get().pop_front(); |
| 347 | 349 |
| 348 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q | 350 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q |
| 349 next_job->ProcessNextOperation(); | 351 next_job->ProcessNextOperation(); |
| 350 return true; | 352 return true; |
| 351 } | 353 } |
| 352 | 354 |
| 353 } // namespace net | 355 } // namespace net |
| OLD | NEW |