| 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_fetcher_core.h" | 5 #include "net/url_request/url_fetcher_core.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 : fetcher_(fetcher), | 76 : fetcher_(fetcher), |
| 77 original_url_(original_url), | 77 original_url_(original_url), |
| 78 request_type_(request_type), | 78 request_type_(request_type), |
| 79 delegate_(d), | 79 delegate_(d), |
| 80 delegate_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 80 delegate_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 81 load_flags_(LOAD_NORMAL), | 81 load_flags_(LOAD_NORMAL), |
| 82 response_code_(URLFetcher::RESPONSE_CODE_INVALID), | 82 response_code_(URLFetcher::RESPONSE_CODE_INVALID), |
| 83 buffer_(new IOBuffer(kBufferSize)), | 83 buffer_(new IOBuffer(kBufferSize)), |
| 84 url_request_data_key_(NULL), | 84 url_request_data_key_(NULL), |
| 85 was_fetched_via_proxy_(false), | 85 was_fetched_via_proxy_(false), |
| 86 upload_content_set_(false), | |
| 87 upload_range_offset_(0), | 86 upload_range_offset_(0), |
| 88 upload_range_length_(0), | 87 upload_range_length_(0), |
| 89 referrer_policy_( | 88 referrer_policy_( |
| 90 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE), | 89 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE), |
| 91 is_chunked_upload_(false), | 90 is_chunked_upload_(false), |
| 92 was_cancelled_(false), | 91 was_cancelled_(false), |
| 93 stop_on_redirect_(false), | 92 stop_on_redirect_(false), |
| 94 stopped_on_redirect_(false), | 93 stopped_on_redirect_(false), |
| 95 automatically_retry_on_5xx_(true), | 94 automatically_retry_on_5xx_(true), |
| 96 num_retries_on_5xx_(0), | 95 num_retries_on_5xx_(0), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 } else { | 130 } else { |
| 132 network_task_runner_->PostTask( | 131 network_task_runner_->PostTask( |
| 133 FROM_HERE, | 132 FROM_HERE, |
| 134 base::Bind(&URLFetcherCore::CancelURLRequest, this, ERR_ABORTED)); | 133 base::Bind(&URLFetcherCore::CancelURLRequest, this, ERR_ABORTED)); |
| 135 } | 134 } |
| 136 } | 135 } |
| 137 | 136 |
| 138 void URLFetcherCore::SetUploadData(const std::string& upload_content_type, | 137 void URLFetcherCore::SetUploadData(const std::string& upload_content_type, |
| 139 const std::string& upload_content) { | 138 const std::string& upload_content) { |
| 140 DCHECK(!is_chunked_upload_); | 139 DCHECK(!is_chunked_upload_); |
| 141 DCHECK(!upload_content_set_); | 140 DCHECK(!IsUploadDataSet()); |
| 142 DCHECK(upload_content_.empty()); | |
| 143 DCHECK(upload_file_path_.empty()); | |
| 144 DCHECK(upload_content_type_.empty()); | 141 DCHECK(upload_content_type_.empty()); |
| 145 | 142 |
| 146 // Empty |upload_content_type| is allowed iff the |upload_content| is empty. | 143 // Empty |upload_content_type| is allowed iff the |upload_content| is empty. |
| 147 DCHECK(upload_content.empty() || !upload_content_type.empty()); | 144 DCHECK(upload_content.empty() || !upload_content_type.empty()); |
| 148 | 145 |
| 149 upload_content_type_ = upload_content_type; | 146 upload_content_type_ = upload_content_type; |
| 150 upload_content_ = upload_content; | 147 upload_content_ = upload_content; |
| 151 upload_content_set_ = true; | |
| 152 } | 148 } |
| 153 | 149 |
| 154 void URLFetcherCore::SetUploadFilePath( | 150 void URLFetcherCore::SetUploadFilePath( |
| 155 const std::string& upload_content_type, | 151 const std::string& upload_content_type, |
| 156 const base::FilePath& file_path, | 152 const base::FilePath& file_path, |
| 157 uint64 range_offset, | 153 uint64 range_offset, |
| 158 uint64 range_length, | 154 uint64 range_length, |
| 159 scoped_refptr<base::TaskRunner> file_task_runner) { | 155 scoped_refptr<base::TaskRunner> file_task_runner) { |
| 160 DCHECK(!is_chunked_upload_); | 156 DCHECK(!is_chunked_upload_); |
| 161 DCHECK(!upload_content_set_); | 157 DCHECK(!IsUploadDataSet()); |
| 162 DCHECK(upload_content_.empty()); | |
| 163 DCHECK(upload_file_path_.empty()); | |
| 164 DCHECK_EQ(upload_range_offset_, 0ULL); | 158 DCHECK_EQ(upload_range_offset_, 0ULL); |
| 165 DCHECK_EQ(upload_range_length_, 0ULL); | 159 DCHECK_EQ(upload_range_length_, 0ULL); |
| 166 DCHECK(upload_content_type_.empty()); | 160 DCHECK(upload_content_type_.empty()); |
| 167 DCHECK(!upload_content_type.empty()); | 161 DCHECK(!upload_content_type.empty()); |
| 168 | 162 |
| 169 upload_content_type_ = upload_content_type; | 163 upload_content_type_ = upload_content_type; |
| 170 upload_file_path_ = file_path; | 164 upload_file_path_ = file_path; |
| 171 upload_range_offset_ = range_offset; | 165 upload_range_offset_ = range_offset; |
| 172 upload_range_length_ = range_length; | 166 upload_range_length_ = range_length; |
| 173 upload_file_task_runner_ = file_task_runner; | 167 upload_file_task_runner_ = file_task_runner; |
| 174 upload_content_set_ = true; | 168 } |
| 169 |
| 170 void URLFetcherCore::SetUploadStreamFactory( |
| 171 const std::string& upload_content_type, |
| 172 const URLFetcher::CreateUploadStreamCallback& factory) { |
| 173 DCHECK(!is_chunked_upload_); |
| 174 DCHECK(!IsUploadDataSet()); |
| 175 DCHECK(upload_content_type_.empty()); |
| 176 |
| 177 upload_content_type_ = upload_content_type; |
| 178 upload_stream_factory_ = factory; |
| 175 } | 179 } |
| 176 | 180 |
| 177 void URLFetcherCore::SetChunkedUpload(const std::string& content_type) { | 181 void URLFetcherCore::SetChunkedUpload(const std::string& content_type) { |
| 178 DCHECK(is_chunked_upload_ || | 182 DCHECK(is_chunked_upload_ || |
| 179 (upload_content_type_.empty() && | 183 (upload_content_type_.empty() && upload_content_.empty() && |
| 180 upload_content_.empty())); | 184 upload_file_path_.empty() && upload_stream_factory_.is_null())); |
| 181 | 185 |
| 182 // Empty |content_type| is not allowed here, because it is impossible | 186 // Empty |content_type| is not allowed here, because it is impossible |
| 183 // to ensure non-empty upload content as it is not yet supplied. | 187 // to ensure non-empty upload content as it is not yet supplied. |
| 184 DCHECK(!content_type.empty()); | 188 DCHECK(!content_type.empty()); |
| 185 | 189 |
| 186 upload_content_type_ = content_type; | 190 upload_content_type_ = content_type; |
| 187 upload_content_.clear(); | 191 upload_content_.clear(); |
| 188 is_chunked_upload_ = true; | 192 is_chunked_upload_ = true; |
| 189 } | 193 } |
| 190 | 194 |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 } | 579 } |
| 576 | 580 |
| 577 switch (request_type_) { | 581 switch (request_type_) { |
| 578 case URLFetcher::GET: | 582 case URLFetcher::GET: |
| 579 break; | 583 break; |
| 580 | 584 |
| 581 case URLFetcher::POST: | 585 case URLFetcher::POST: |
| 582 case URLFetcher::PUT: | 586 case URLFetcher::PUT: |
| 583 case URLFetcher::PATCH: | 587 case URLFetcher::PATCH: |
| 584 // Upload content must be set. | 588 // Upload content must be set. |
| 585 DCHECK(is_chunked_upload_ || upload_content_set_); | 589 DCHECK(is_chunked_upload_ || IsUploadDataSet()); |
| 586 | 590 |
| 587 request_->set_method( | 591 request_->set_method( |
| 588 request_type_ == URLFetcher::POST ? "POST" : | 592 request_type_ == URLFetcher::POST ? "POST" : |
| 589 request_type_ == URLFetcher::PUT ? "PUT" : "PATCH"); | 593 request_type_ == URLFetcher::PUT ? "PUT" : "PATCH"); |
| 590 if (!upload_content_type_.empty()) { | |
| 591 extra_request_headers_.SetHeader(HttpRequestHeaders::kContentType, | |
| 592 upload_content_type_); | |
| 593 } | |
| 594 if (!upload_content_.empty()) { | 594 if (!upload_content_.empty()) { |
| 595 scoped_ptr<UploadElementReader> reader(new UploadBytesElementReader( | 595 scoped_ptr<UploadElementReader> reader(new UploadBytesElementReader( |
| 596 upload_content_.data(), upload_content_.size())); | 596 upload_content_.data(), upload_content_.size())); |
| 597 request_->set_upload( | 597 request_->set_upload( |
| 598 ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0)); | 598 ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0)); |
| 599 } else if (!upload_file_path_.empty()) { | 599 } else if (!upload_file_path_.empty()) { |
| 600 scoped_ptr<UploadElementReader> reader( | 600 scoped_ptr<UploadElementReader> reader( |
| 601 new UploadFileElementReader(upload_file_task_runner_.get(), | 601 new UploadFileElementReader(upload_file_task_runner_.get(), |
| 602 upload_file_path_, | 602 upload_file_path_, |
| 603 upload_range_offset_, | 603 upload_range_offset_, |
| 604 upload_range_length_, | 604 upload_range_length_, |
| 605 base::Time())); | 605 base::Time())); |
| 606 request_->set_upload( | 606 request_->set_upload( |
| 607 ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0)); | 607 ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0)); |
| 608 } else if (!upload_stream_factory_.is_null()) { |
| 609 scoped_ptr<UploadDataStream> stream = upload_stream_factory_.Run(); |
| 610 DCHECK(stream); |
| 611 request_->set_upload(stream.Pass()); |
| 608 } | 612 } |
| 609 | 613 if (!upload_content_type_.empty()) { |
| 614 extra_request_headers_.SetHeader(HttpRequestHeaders::kContentType, |
| 615 upload_content_type_); |
| 616 } |
| 610 current_upload_bytes_ = -1; | 617 current_upload_bytes_ = -1; |
| 611 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the | 618 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the |
| 612 // layer and avoid using timer here. | 619 // layer and avoid using timer here. |
| 613 upload_progress_checker_timer_.reset( | 620 upload_progress_checker_timer_.reset( |
| 614 new base::RepeatingTimer<URLFetcherCore>()); | 621 new base::RepeatingTimer<URLFetcherCore>()); |
| 615 upload_progress_checker_timer_->Start( | 622 upload_progress_checker_timer_->Start( |
| 616 FROM_HERE, | 623 FROM_HERE, |
| 617 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), | 624 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), |
| 618 this, | 625 this, |
| 619 &URLFetcherCore::InformDelegateUploadProgress); | 626 &URLFetcherCore::InformDelegateUploadProgress); |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 950 this, current_response_bytes_, total_response_bytes_)); | 957 this, current_response_bytes_, total_response_bytes_)); |
| 951 } | 958 } |
| 952 | 959 |
| 953 void URLFetcherCore::InformDelegateDownloadProgressInDelegateThread( | 960 void URLFetcherCore::InformDelegateDownloadProgressInDelegateThread( |
| 954 int64 current, int64 total) { | 961 int64 current, int64 total) { |
| 955 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); | 962 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); |
| 956 if (delegate_) | 963 if (delegate_) |
| 957 delegate_->OnURLFetchDownloadProgress(fetcher_, current, total); | 964 delegate_->OnURLFetchDownloadProgress(fetcher_, current, total); |
| 958 } | 965 } |
| 959 | 966 |
| 967 bool URLFetcherCore::IsUploadDataSet() const { |
| 968 return !(upload_content_.empty() && upload_file_path_.empty() && |
| 969 upload_stream_factory_.is_null()); |
| 970 } |
| 971 |
| 960 } // namespace net | 972 } // namespace net |
| OLD | NEW |