| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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.h" | 5 #include "net/url_request/url_request.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/process_util.h" | 8 #include "base/process_util.h" |
| 9 #include "base/singleton.h" | 9 #include "base/singleton.h" |
| 10 #include "base/stats_counters.h" | 10 #include "base/stats_counters.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 /////////////////////////////////////////////////////////////////////////////// | 37 /////////////////////////////////////////////////////////////////////////////// |
| 38 // URLRequest | 38 // URLRequest |
| 39 | 39 |
| 40 URLRequest::URLRequest(const GURL& url, Delegate* delegate) | 40 URLRequest::URLRequest(const GURL& url, Delegate* delegate) |
| 41 : url_(url), | 41 : url_(url), |
| 42 original_url_(url), | 42 original_url_(url), |
| 43 method_("GET"), | 43 method_("GET"), |
| 44 load_flags_(net::LOAD_NORMAL), | 44 load_flags_(net::LOAD_NORMAL), |
| 45 delegate_(delegate), | 45 delegate_(delegate), |
| 46 is_pending_(false), | 46 is_pending_(false), |
| 47 user_data_(NULL), | |
| 48 enable_profiling_(false), | 47 enable_profiling_(false), |
| 49 redirect_limit_(kMaxRedirects), | 48 redirect_limit_(kMaxRedirects), |
| 50 final_upload_progress_(0), | 49 final_upload_progress_(0), |
| 51 priority_(0) { | 50 priority_(0) { |
| 52 URLREQUEST_COUNT_CTOR(); | 51 URLREQUEST_COUNT_CTOR(); |
| 53 SIMPLE_STATS_COUNTER("URLRequestCount"); | 52 SIMPLE_STATS_COUNTER("URLRequestCount"); |
| 54 origin_pid_ = base::GetCurrentProcId(); | 53 origin_pid_ = base::GetCurrentProcId(); |
| 55 | 54 |
| 56 // Sanity check out environment. | 55 // Sanity check out environment. |
| 57 DCHECK(MessageLoop::current()) << | 56 DCHECK(MessageLoop::current()) << |
| 58 "The current MessageLoop must exist"; | 57 "The current MessageLoop must exist"; |
| 59 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << | 58 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << |
| 60 "The current MessageLoop must be TYPE_IO"; | 59 "The current MessageLoop must be TYPE_IO"; |
| 61 } | 60 } |
| 62 | 61 |
| 63 URLRequest::~URLRequest() { | 62 URLRequest::~URLRequest() { |
| 64 URLREQUEST_COUNT_DTOR(); | 63 URLREQUEST_COUNT_DTOR(); |
| 65 | 64 |
| 66 Cancel(); | 65 Cancel(); |
| 67 | 66 |
| 68 if (job_) | 67 if (job_) |
| 69 OrphanJob(); | 68 OrphanJob(); |
| 70 | |
| 71 delete user_data_; // NULL check unnecessary for delete | |
| 72 } | 69 } |
| 73 | 70 |
| 74 // static | 71 // static |
| 75 URLRequest::ProtocolFactory* URLRequest::RegisterProtocolFactory( | 72 URLRequest::ProtocolFactory* URLRequest::RegisterProtocolFactory( |
| 76 const string& scheme, ProtocolFactory* factory) { | 73 const string& scheme, ProtocolFactory* factory) { |
| 77 return GetJobManager()->RegisterProtocolFactory(scheme, factory); | 74 return GetJobManager()->RegisterProtocolFactory(scheme, factory); |
| 78 } | 75 } |
| 79 | 76 |
| 80 // static | 77 // static |
| 81 void URLRequest::RegisterRequestInterceptor(Interceptor* interceptor) { | 78 void URLRequest::RegisterRequestInterceptor(Interceptor* interceptor) { |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 DCHECK(!is_pending_); | 230 DCHECK(!is_pending_); |
| 234 method_ = method; | 231 method_ = method; |
| 235 } | 232 } |
| 236 | 233 |
| 237 void URLRequest::set_referrer(const std::string& referrer) { | 234 void URLRequest::set_referrer(const std::string& referrer) { |
| 238 DCHECK(!is_pending_); | 235 DCHECK(!is_pending_); |
| 239 referrer_ = referrer; | 236 referrer_ = referrer; |
| 240 } | 237 } |
| 241 | 238 |
| 242 void URLRequest::Start() { | 239 void URLRequest::Start() { |
| 240 StartJob(GetJobManager()->CreateJob(this)); |
| 241 } |
| 242 |
| 243 void URLRequest::StartJob(URLRequestJob* job) { |
| 243 DCHECK(!is_pending_); | 244 DCHECK(!is_pending_); |
| 244 DCHECK(!job_); | 245 DCHECK(!job_); |
| 245 | 246 |
| 246 job_ = GetJobManager()->CreateJob(this); | 247 job_ = job; |
| 247 job_->SetExtraRequestHeaders(extra_request_headers_); | 248 job_->SetExtraRequestHeaders(extra_request_headers_); |
| 248 | 249 |
| 249 if (upload_.get()) | 250 if (upload_.get()) |
| 250 job_->SetUpload(upload_.get()); | 251 job_->SetUpload(upload_.get()); |
| 251 | 252 |
| 252 is_pending_ = true; | 253 is_pending_ = true; |
| 253 response_info_.request_time = Time::Now(); | 254 response_info_.request_time = Time::Now(); |
| 254 response_info_.was_cached = false; | 255 response_info_.was_cached = false; |
| 255 | 256 |
| 256 // Don't allow errors to be sent from within Start(). | 257 // Don't allow errors to be sent from within Start(). |
| 257 // TODO(brettw) this may cause NotifyDone to be sent synchronously, | 258 // TODO(brettw) this may cause NotifyDone to be sent synchronously, |
| 258 // we probably don't want this: they should be sent asynchronously so | 259 // we probably don't want this: they should be sent asynchronously so |
| 259 // the caller does not get reentered. | 260 // the caller does not get reentered. |
| 260 job_->Start(); | 261 job_->Start(); |
| 261 } | 262 } |
| 262 | 263 |
| 264 void URLRequest::Restart() { |
| 265 // Should only be called if the original job didn't make any progress. |
| 266 DCHECK(job_ && !job_->has_response_started()); |
| 267 RestartWithJob(GetJobManager()->CreateJob(this)); |
| 268 } |
| 269 |
| 270 void URLRequest::RestartWithJob(URLRequestJob *job) { |
| 271 DCHECK(job->request() == this); |
| 272 OrphanJob(); |
| 273 status_ = URLRequestStatus(); |
| 274 is_pending_ = false; |
| 275 StartJob(job); |
| 276 } |
| 277 |
| 263 void URLRequest::Cancel() { | 278 void URLRequest::Cancel() { |
| 264 DoCancel(net::ERR_ABORTED, net::SSLInfo()); | 279 DoCancel(net::ERR_ABORTED, net::SSLInfo()); |
| 265 } | 280 } |
| 266 | 281 |
| 267 void URLRequest::SimulateError(int os_error) { | 282 void URLRequest::SimulateError(int os_error) { |
| 268 DoCancel(os_error, net::SSLInfo()); | 283 DoCancel(os_error, net::SSLInfo()); |
| 269 } | 284 } |
| 270 | 285 |
| 271 void URLRequest::SimulateSSLError(int os_error, const net::SSLInfo& ssl_info) { | 286 void URLRequest::SimulateSSLError(int os_error, const net::SSLInfo& ssl_info) { |
| 272 // This should only be called on a started request. | 287 // This should only be called on a started request. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 | 327 |
| 313 // Once the request fails or is cancelled, read will just return 0 bytes | 328 // Once the request fails or is cancelled, read will just return 0 bytes |
| 314 // to indicate end of stream. | 329 // to indicate end of stream. |
| 315 if (!status_.is_success()) { | 330 if (!status_.is_success()) { |
| 316 return true; | 331 return true; |
| 317 } | 332 } |
| 318 | 333 |
| 319 return job_->Read(dest, dest_size, bytes_read); | 334 return job_->Read(dest, dest_size, bytes_read); |
| 320 } | 335 } |
| 321 | 336 |
| 337 void URLRequest::ReceivedRedirect(const GURL& location) { |
| 338 URLRequestJob* job = GetJobManager()->MaybeInterceptRedirect(this, location); |
| 339 if (job) { |
| 340 RestartWithJob(job); |
| 341 } else if (delegate_) { |
| 342 delegate_->OnReceivedRedirect(this, location); |
| 343 } |
| 344 } |
| 345 |
| 346 void URLRequest::ResponseStarted() { |
| 347 URLRequestJob* job = GetJobManager()->MaybeInterceptResponse(this); |
| 348 if (job) { |
| 349 RestartWithJob(job); |
| 350 } else if (delegate_) { |
| 351 delegate_->OnResponseStarted(this); |
| 352 } |
| 353 } |
| 354 |
| 322 void URLRequest::SetAuth(const wstring& username, const wstring& password) { | 355 void URLRequest::SetAuth(const wstring& username, const wstring& password) { |
| 323 DCHECK(job_); | 356 DCHECK(job_); |
| 324 DCHECK(job_->NeedsAuth()); | 357 DCHECK(job_->NeedsAuth()); |
| 325 | 358 |
| 326 job_->SetAuth(username, password); | 359 job_->SetAuth(username, password); |
| 327 } | 360 } |
| 328 | 361 |
| 329 void URLRequest::CancelAuth() { | 362 void URLRequest::CancelAuth() { |
| 330 DCHECK(job_); | 363 DCHECK(job_); |
| 331 DCHECK(job_->NeedsAuth()); | 364 DCHECK(job_->NeedsAuth()); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 } | 448 } |
| 416 | 449 |
| 417 int64 URLRequest::GetExpectedContentSize() const { | 450 int64 URLRequest::GetExpectedContentSize() const { |
| 418 int64 expected_content_size = -1; | 451 int64 expected_content_size = -1; |
| 419 if (job_) | 452 if (job_) |
| 420 expected_content_size = job_->expected_content_size(); | 453 expected_content_size = job_->expected_content_size(); |
| 421 | 454 |
| 422 return expected_content_size; | 455 return expected_content_size; |
| 423 } | 456 } |
| 424 | 457 |
| 458 URLRequest::UserData* URLRequest::GetUserData(void* key) const { |
| 459 UserDataMap::const_iterator found = user_data_.find(key); |
| 460 if (found != user_data_.end()) |
| 461 return found->second.get(); |
| 462 return NULL; |
| 463 } |
| 464 |
| 465 void URLRequest::SetUserData(void* key, UserData* data) { |
| 466 user_data_[key] = linked_ptr<UserData>(data); |
| 467 } |
| 468 |
| 425 #ifndef NDEBUG | 469 #ifndef NDEBUG |
| 426 | 470 |
| 427 URLRequestMetrics::~URLRequestMetrics() { | 471 URLRequestMetrics::~URLRequestMetrics() { |
| 428 DLOG_IF(WARNING, object_count != 0) << | 472 DLOG_IF(WARNING, object_count != 0) << |
| 429 "Leaking " << object_count << " URLRequest object(s)"; | 473 "Leaking " << object_count << " URLRequest object(s)"; |
| 430 } | 474 } |
| 431 | 475 |
| 432 #endif | 476 #endif |
| OLD | NEW |