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 "content/common/net/url_fetcher_core.h" | 5 #include "content/common/net/url_fetcher_core.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util_proxy.h" | 8 #include "base/file_util_proxy.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 base::FileUtilProxy::Delete( | 235 base::FileUtilProxy::Delete( |
236 file_message_loop_proxy_, file_path_, | 236 file_message_loop_proxy_, file_path_, |
237 false, // No need to recurse, as the path is to a file. | 237 false, // No need to recurse, as the path is to a file. |
238 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. | 238 base::FileUtilProxy::StatusCallback()); // No callback: Ignore errors. |
239 DisownFile(); | 239 DisownFile(); |
240 } | 240 } |
241 } | 241 } |
242 | 242 |
243 static bool g_interception_enabled = false; | 243 static bool g_interception_enabled = false; |
244 | 244 |
245 URLFetcherCore::URLFetcherCore(URLFetcher* fetcher, | 245 URLFetcherCore::URLFetcherCore(net::URLFetcher* fetcher, |
246 const GURL& original_url, | 246 const GURL& original_url, |
247 URLFetcher::RequestType request_type, | 247 net::URLFetcher::RequestType request_type, |
248 net::URLFetcherDelegate* d) | 248 net::URLFetcherDelegate* d) |
249 : fetcher_(fetcher), | 249 : fetcher_(fetcher), |
250 original_url_(original_url), | 250 original_url_(original_url), |
251 request_type_(request_type), | 251 request_type_(request_type), |
252 delegate_(d), | 252 delegate_(d), |
253 delegate_loop_proxy_( | 253 delegate_loop_proxy_( |
254 base::MessageLoopProxy::current()), | 254 base::MessageLoopProxy::current()), |
255 request_(NULL), | 255 request_(NULL), |
256 load_flags_(net::LOAD_NORMAL), | 256 load_flags_(net::LOAD_NORMAL), |
257 response_code_(URLFetcher::RESPONSE_CODE_INVALID), | 257 response_code_(net::URLFetcher::RESPONSE_CODE_INVALID), |
258 buffer_(new net::IOBuffer(kBufferSize)), | 258 buffer_(new net::IOBuffer(kBufferSize)), |
259 url_request_data_key_(NULL), | 259 url_request_data_key_(NULL), |
260 was_fetched_via_proxy_(false), | 260 was_fetched_via_proxy_(false), |
261 is_chunked_upload_(false), | 261 is_chunked_upload_(false), |
262 num_retries_(0), | 262 num_retries_(0), |
263 was_cancelled_(false), | 263 was_cancelled_(false), |
264 response_destination_(STRING), | 264 response_destination_(STRING), |
265 automatically_retry_on_5xx_(true), | 265 automatically_retry_on_5xx_(true), |
266 max_retries_(0), | 266 max_retries_(0), |
267 current_upload_bytes_(-1), | 267 current_upload_bytes_(-1), |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 } | 400 } |
401 | 401 |
402 void URLFetcherCore::SetFirstPartyForCookies( | 402 void URLFetcherCore::SetFirstPartyForCookies( |
403 const GURL& first_party_for_cookies) { | 403 const GURL& first_party_for_cookies) { |
404 DCHECK(first_party_for_cookies_.is_empty()); | 404 DCHECK(first_party_for_cookies_.is_empty()); |
405 first_party_for_cookies_ = first_party_for_cookies; | 405 first_party_for_cookies_ = first_party_for_cookies; |
406 } | 406 } |
407 | 407 |
408 void URLFetcherCore::SetURLRequestUserData( | 408 void URLFetcherCore::SetURLRequestUserData( |
409 const void* key, | 409 const void* key, |
410 const URLFetcher::CreateDataCallback& create_data_callback) { | 410 const net::URLFetcher::CreateDataCallback& create_data_callback) { |
411 DCHECK(key); | 411 DCHECK(key); |
412 DCHECK(!create_data_callback.is_null()); | 412 DCHECK(!create_data_callback.is_null()); |
413 url_request_data_key_ = key; | 413 url_request_data_key_ = key; |
414 url_request_create_data_callback_ = create_data_callback; | 414 url_request_create_data_callback_ = create_data_callback; |
415 } | 415 } |
416 | 416 |
417 void URLFetcherCore::SetAutomaticallyRetryOn5xx(bool retry) { | 417 void URLFetcherCore::SetAutomaticallyRetryOn5xx(bool retry) { |
418 automatically_retry_on_5xx_ = retry; | 418 automatically_retry_on_5xx_ = retry; |
419 } | 419 } |
420 | 420 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 } | 631 } |
632 } while (request_->Read(buffer_, kBufferSize, &bytes_read)); | 632 } while (request_->Read(buffer_, kBufferSize, &bytes_read)); |
633 | 633 |
634 const net::URLRequestStatus status = request_->status(); | 634 const net::URLRequestStatus status = request_->status(); |
635 | 635 |
636 if (status.is_success()) | 636 if (status.is_success()) |
637 request_->GetResponseCookies(&cookies_); | 637 request_->GetResponseCookies(&cookies_); |
638 | 638 |
639 // See comments re: HEAD requests in ReadResponse(). | 639 // See comments re: HEAD requests in ReadResponse(). |
640 if ((!status.is_io_pending() && !waiting_on_write) || | 640 if ((!status.is_io_pending() && !waiting_on_write) || |
641 (request_type_ == URLFetcher::HEAD)) { | 641 (request_type_ == net::URLFetcher::HEAD)) { |
642 status_ = status; | 642 status_ = status; |
643 ReleaseRequest(); | 643 ReleaseRequest(); |
644 | 644 |
645 // If a file is open, close it. | 645 // If a file is open, close it. |
646 if (file_writer_.get()) { | 646 if (file_writer_.get()) { |
647 // If the file is open, close it. After closing the file, | 647 // If the file is open, close it. After closing the file, |
648 // RetryOrCompleteUrlFetch() will be called. | 648 // RetryOrCompleteUrlFetch() will be called. |
649 file_writer_->CloseFileAndCompleteRequest(); | 649 file_writer_->CloseFileAndCompleteRequest(); |
650 } else { | 650 } else { |
651 // Otherwise, complete or retry the URL request directly. | 651 // Otherwise, complete or retry the URL request directly. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 // should be gone too. | 694 // should be gone too. |
695 DCHECK(posted || !delegate_); | 695 DCHECK(posted || !delegate_); |
696 } | 696 } |
697 | 697 |
698 void URLFetcherCore::ReadResponse() { | 698 void URLFetcherCore::ReadResponse() { |
699 // Some servers may treat HEAD requests as GET requests. To free up the | 699 // Some servers may treat HEAD requests as GET requests. To free up the |
700 // network connection as soon as possible, signal that the request has | 700 // network connection as soon as possible, signal that the request has |
701 // completed immediately, without trying to read any data back (all we care | 701 // completed immediately, without trying to read any data back (all we care |
702 // about is the response code and headers, which we already have). | 702 // about is the response code and headers, which we already have). |
703 int bytes_read = 0; | 703 int bytes_read = 0; |
704 if (request_->status().is_success() && (request_type_ != URLFetcher::HEAD)) | 704 if (request_->status().is_success() && |
| 705 (request_type_ != net::URLFetcher::HEAD)) |
705 request_->Read(buffer_, kBufferSize, &bytes_read); | 706 request_->Read(buffer_, kBufferSize, &bytes_read); |
706 OnReadCompleted(request_.get(), bytes_read); | 707 OnReadCompleted(request_.get(), bytes_read); |
707 } | 708 } |
708 | 709 |
709 void URLFetcherCore::DisownFile() { | 710 void URLFetcherCore::DisownFile() { |
710 file_writer_->DisownFile(); | 711 file_writer_->DisownFile(); |
711 } | 712 } |
712 | 713 |
713 void URLFetcherCore::StartURLRequest() { | 714 void URLFetcherCore::StartURLRequest() { |
714 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 715 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
(...skipping 21 matching lines...) Expand all Loading... |
736 request_->set_context(request_context_getter_->GetURLRequestContext()); | 737 request_->set_context(request_context_getter_->GetURLRequestContext()); |
737 request_->set_referrer(referrer_); | 738 request_->set_referrer(referrer_); |
738 request_->set_first_party_for_cookies(first_party_for_cookies_.is_empty() ? | 739 request_->set_first_party_for_cookies(first_party_for_cookies_.is_empty() ? |
739 original_url_ : first_party_for_cookies_); | 740 original_url_ : first_party_for_cookies_); |
740 if (url_request_data_key_ && !url_request_create_data_callback_.is_null()) { | 741 if (url_request_data_key_ && !url_request_create_data_callback_.is_null()) { |
741 request_->SetUserData(url_request_data_key_, | 742 request_->SetUserData(url_request_data_key_, |
742 url_request_create_data_callback_.Run()); | 743 url_request_create_data_callback_.Run()); |
743 } | 744 } |
744 | 745 |
745 switch (request_type_) { | 746 switch (request_type_) { |
746 case URLFetcher::GET: | 747 case net::URLFetcher::GET: |
747 break; | 748 break; |
748 | 749 |
749 case URLFetcher::POST: | 750 case net::URLFetcher::POST: |
750 case URLFetcher::PUT: | 751 case net::URLFetcher::PUT: |
751 DCHECK(!upload_content_.empty() || is_chunked_upload_); | 752 DCHECK(!upload_content_.empty() || is_chunked_upload_); |
752 DCHECK(!upload_content_type_.empty()); | 753 DCHECK(!upload_content_type_.empty()); |
753 | 754 |
754 request_->set_method(request_type_ == URLFetcher::POST ? "POST" : "PUT"); | 755 request_->set_method( |
| 756 request_type_ == net::URLFetcher::POST ? "POST" : "PUT"); |
755 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, | 757 extra_request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, |
756 upload_content_type_); | 758 upload_content_type_); |
757 if (!upload_content_.empty()) { | 759 if (!upload_content_.empty()) { |
758 request_->AppendBytesToUpload( | 760 request_->AppendBytesToUpload( |
759 upload_content_.data(), static_cast<int>(upload_content_.length())); | 761 upload_content_.data(), static_cast<int>(upload_content_.length())); |
760 } | 762 } |
761 | 763 |
762 current_upload_bytes_ = -1; | 764 current_upload_bytes_ = -1; |
763 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the | 765 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the |
764 // net:: layer and avoid using timer here. | 766 // net:: layer and avoid using timer here. |
765 upload_progress_checker_timer_.reset( | 767 upload_progress_checker_timer_.reset( |
766 new base::RepeatingTimer<URLFetcherCore>()); | 768 new base::RepeatingTimer<URLFetcherCore>()); |
767 upload_progress_checker_timer_->Start( | 769 upload_progress_checker_timer_->Start( |
768 FROM_HERE, | 770 FROM_HERE, |
769 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), | 771 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), |
770 this, | 772 this, |
771 &URLFetcherCore::InformDelegateUploadProgress); | 773 &URLFetcherCore::InformDelegateUploadProgress); |
772 break; | 774 break; |
773 | 775 |
774 case URLFetcher::HEAD: | 776 case net::URLFetcher::HEAD: |
775 request_->set_method("HEAD"); | 777 request_->set_method("HEAD"); |
776 break; | 778 break; |
777 | 779 |
778 case URLFetcher::DELETE_REQUEST: | 780 case net::URLFetcher::DELETE_REQUEST: |
779 request_->set_method("DELETE"); | 781 request_->set_method("DELETE"); |
780 break; | 782 break; |
781 | 783 |
782 default: | 784 default: |
783 NOTREACHED(); | 785 NOTREACHED(); |
784 } | 786 } |
785 | 787 |
786 if (!extra_request_headers_.IsEmpty()) | 788 if (!extra_request_headers_.IsEmpty()) |
787 request_->SetExtraRequestHeaders(extra_request_headers_); | 789 request_->SetExtraRequestHeaders(extra_request_headers_); |
788 | 790 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
921 void URLFetcherCore::InformDelegateFetchIsComplete() { | 923 void URLFetcherCore::InformDelegateFetchIsComplete() { |
922 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 924 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
923 if (delegate_) | 925 if (delegate_) |
924 delegate_->OnURLFetchComplete(fetcher_); | 926 delegate_->OnURLFetchComplete(fetcher_); |
925 } | 927 } |
926 | 928 |
927 void URLFetcherCore::NotifyMalformedContent() { | 929 void URLFetcherCore::NotifyMalformedContent() { |
928 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 930 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
929 if (url_throttler_entry_ != NULL) { | 931 if (url_throttler_entry_ != NULL) { |
930 int status_code = response_code_; | 932 int status_code = response_code_; |
931 if (status_code == URLFetcher::RESPONSE_CODE_INVALID) { | 933 if (status_code == net::URLFetcher::RESPONSE_CODE_INVALID) { |
932 // The status code will generally be known by the time clients | 934 // The status code will generally be known by the time clients |
933 // call the |ReceivedContentWasMalformed()| function (which ends up | 935 // call the |ReceivedContentWasMalformed()| function (which ends up |
934 // calling the current function) but if it's not, we need to assume | 936 // calling the current function) but if it's not, we need to assume |
935 // the response was successful so that the total failure count | 937 // the response was successful so that the total failure count |
936 // used to calculate exponential back-off goes up. | 938 // used to calculate exponential back-off goes up. |
937 status_code = 200; | 939 status_code = 200; |
938 } | 940 } |
939 url_throttler_entry_->ReceivedContentWasMalformed(status_code); | 941 url_throttler_entry_->ReceivedContentWasMalformed(status_code); |
940 } | 942 } |
941 } | 943 } |
(...skipping 18 matching lines...) Expand all Loading... |
960 } | 962 } |
961 | 963 |
962 return original_url_backoff > destination_url_backoff ? | 964 return original_url_backoff > destination_url_backoff ? |
963 original_url_backoff : destination_url_backoff; | 965 original_url_backoff : destination_url_backoff; |
964 } else { | 966 } else { |
965 return base::TimeTicks(); | 967 return base::TimeTicks(); |
966 } | 968 } |
967 } | 969 } |
968 | 970 |
969 } // namespace content | 971 } // namespace content |
OLD | NEW |