Chromium Code Reviews| 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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 | 137 |
| 138 void URLFetcherCore::SetUploadData(const std::string& upload_content_type, | 138 void URLFetcherCore::SetUploadData(const std::string& upload_content_type, |
| 139 const std::string& upload_content) { | 139 const std::string& upload_content) { |
| 140 DCHECK(!is_chunked_upload_); | 140 DCHECK(!is_chunked_upload_); |
| 141 DCHECK(!upload_content_set_); | 141 DCHECK(!upload_content_set_); |
| 142 DCHECK(upload_content_.empty()); | 142 DCHECK(upload_content_.empty()); |
| 143 DCHECK(upload_file_path_.empty()); | 143 DCHECK(upload_file_path_.empty()); |
| 144 DCHECK(upload_content_type_.empty()); | 144 DCHECK(upload_content_type_.empty()); |
| 145 DCHECK(upload_stream_factory_.is_null()); | |
| 145 | 146 |
| 146 // Empty |upload_content_type| is allowed iff the |upload_content| is empty. | 147 // Empty |upload_content_type| is allowed iff the |upload_content| is empty. |
| 147 DCHECK(upload_content.empty() || !upload_content_type.empty()); | 148 DCHECK(upload_content.empty() || !upload_content_type.empty()); |
| 148 | 149 |
| 149 upload_content_type_ = upload_content_type; | 150 upload_content_type_ = upload_content_type; |
| 150 upload_content_ = upload_content; | 151 upload_content_ = upload_content; |
| 151 upload_content_set_ = true; | 152 upload_content_set_ = true; |
| 152 } | 153 } |
| 153 | 154 |
| 154 void URLFetcherCore::SetUploadFilePath( | 155 void URLFetcherCore::SetUploadFilePath( |
| 155 const std::string& upload_content_type, | 156 const std::string& upload_content_type, |
| 156 const base::FilePath& file_path, | 157 const base::FilePath& file_path, |
| 157 uint64 range_offset, | 158 uint64 range_offset, |
| 158 uint64 range_length, | 159 uint64 range_length, |
| 159 scoped_refptr<base::TaskRunner> file_task_runner) { | 160 scoped_refptr<base::TaskRunner> file_task_runner) { |
| 160 DCHECK(!is_chunked_upload_); | 161 DCHECK(!is_chunked_upload_); |
| 161 DCHECK(!upload_content_set_); | 162 DCHECK(!upload_content_set_); |
| 162 DCHECK(upload_content_.empty()); | 163 DCHECK(upload_content_.empty()); |
| 163 DCHECK(upload_file_path_.empty()); | 164 DCHECK(upload_file_path_.empty()); |
| 164 DCHECK_EQ(upload_range_offset_, 0ULL); | 165 DCHECK_EQ(upload_range_offset_, 0ULL); |
| 165 DCHECK_EQ(upload_range_length_, 0ULL); | 166 DCHECK_EQ(upload_range_length_, 0ULL); |
| 166 DCHECK(upload_content_type_.empty()); | 167 DCHECK(upload_content_type_.empty()); |
| 167 DCHECK(!upload_content_type.empty()); | 168 DCHECK(!upload_content_type.empty()); |
| 169 DCHECK(upload_stream_factory_.is_null()); | |
| 168 | 170 |
| 169 upload_content_type_ = upload_content_type; | 171 upload_content_type_ = upload_content_type; |
| 170 upload_file_path_ = file_path; | 172 upload_file_path_ = file_path; |
| 171 upload_range_offset_ = range_offset; | 173 upload_range_offset_ = range_offset; |
| 172 upload_range_length_ = range_length; | 174 upload_range_length_ = range_length; |
| 173 upload_file_task_runner_ = file_task_runner; | 175 upload_file_task_runner_ = file_task_runner; |
| 174 upload_content_set_ = true; | 176 upload_content_set_ = true; |
| 175 } | 177 } |
| 176 | 178 |
| 179 void URLFetcherCore::SetUploadStreamFactory( | |
| 180 const std::string& upload_content_type, | |
| 181 const URLFetcher::CreateUploadStreamCallback& factory) { | |
| 182 DCHECK(!is_chunked_upload_); | |
| 183 DCHECK(!upload_content_set_); | |
| 184 DCHECK(upload_content_.empty()); | |
| 185 DCHECK(upload_file_path_.empty()); | |
| 186 DCHECK(upload_content_type_.empty()); | |
| 187 DCHECK(upload_stream_factory_.is_null()); | |
|
mmenke
2015/01/09 17:42:25
Mind making a private method for all these shared
hirono
2015/01/15 08:14:27
I added IsUploadDataSet to check upload_content_,
| |
| 188 | |
| 189 upload_content_type_ = upload_content_type; | |
| 190 upload_stream_factory_ = factory; | |
| 191 upload_content_set_ = true; | |
| 192 } | |
| 193 | |
| 177 void URLFetcherCore::SetChunkedUpload(const std::string& content_type) { | 194 void URLFetcherCore::SetChunkedUpload(const std::string& content_type) { |
| 178 DCHECK(is_chunked_upload_ || | 195 DCHECK(is_chunked_upload_ || |
| 179 (upload_content_type_.empty() && | 196 (upload_content_type_.empty() && upload_content_.empty() && |
| 180 upload_content_.empty())); | 197 upload_file_path_.empty() && upload_stream_factory_.is_null())); |
| 181 | 198 |
| 182 // Empty |content_type| is not allowed here, because it is impossible | 199 // Empty |content_type| is not allowed here, because it is impossible |
| 183 // to ensure non-empty upload content as it is not yet supplied. | 200 // to ensure non-empty upload content as it is not yet supplied. |
| 184 DCHECK(!content_type.empty()); | 201 DCHECK(!content_type.empty()); |
| 185 | 202 |
| 186 upload_content_type_ = content_type; | 203 upload_content_type_ = content_type; |
| 187 upload_content_.clear(); | 204 upload_content_.clear(); |
| 188 is_chunked_upload_ = true; | 205 is_chunked_upload_ = true; |
| 189 } | 206 } |
| 190 | 207 |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 580 | 597 |
| 581 case URLFetcher::POST: | 598 case URLFetcher::POST: |
| 582 case URLFetcher::PUT: | 599 case URLFetcher::PUT: |
| 583 case URLFetcher::PATCH: | 600 case URLFetcher::PATCH: |
| 584 // Upload content must be set. | 601 // Upload content must be set. |
| 585 DCHECK(is_chunked_upload_ || upload_content_set_); | 602 DCHECK(is_chunked_upload_ || upload_content_set_); |
| 586 | 603 |
| 587 request_->set_method( | 604 request_->set_method( |
| 588 request_type_ == URLFetcher::POST ? "POST" : | 605 request_type_ == URLFetcher::POST ? "POST" : |
| 589 request_type_ == URLFetcher::PUT ? "PUT" : "PATCH"); | 606 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()) { | 607 if (!upload_content_.empty()) { |
| 595 scoped_ptr<UploadElementReader> reader(new UploadBytesElementReader( | 608 scoped_ptr<UploadElementReader> reader(new UploadBytesElementReader( |
| 596 upload_content_.data(), upload_content_.size())); | 609 upload_content_.data(), upload_content_.size())); |
| 597 request_->set_upload( | 610 request_->set_upload( |
| 598 ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0)); | 611 ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0)); |
| 599 } else if (!upload_file_path_.empty()) { | 612 } else if (!upload_file_path_.empty()) { |
| 600 scoped_ptr<UploadElementReader> reader( | 613 scoped_ptr<UploadElementReader> reader( |
| 601 new UploadFileElementReader(upload_file_task_runner_.get(), | 614 new UploadFileElementReader(upload_file_task_runner_.get(), |
| 602 upload_file_path_, | 615 upload_file_path_, |
| 603 upload_range_offset_, | 616 upload_range_offset_, |
| 604 upload_range_length_, | 617 upload_range_length_, |
| 605 base::Time())); | 618 base::Time())); |
| 606 request_->set_upload( | 619 request_->set_upload( |
| 607 ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0)); | 620 ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0)); |
| 621 } else if (!upload_stream_factory_.is_null()) { | |
| 622 request_->set_upload(upload_stream_factory_.Run().Pass()); | |
|
mmenke
2015/01/09 18:13:51
We should DCHECK that we don't get a null stream.
hirono
2015/01/15 08:14:27
Done.
| |
| 608 } | 623 } |
| 609 | 624 if (!upload_content_type_.empty()) { |
| 625 extra_request_headers_.SetHeader(HttpRequestHeaders::kContentType, | |
| 626 upload_content_type_); | |
| 627 } | |
| 610 current_upload_bytes_ = -1; | 628 current_upload_bytes_ = -1; |
| 611 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the | 629 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the |
| 612 // layer and avoid using timer here. | 630 // layer and avoid using timer here. |
| 613 upload_progress_checker_timer_.reset( | 631 upload_progress_checker_timer_.reset( |
| 614 new base::RepeatingTimer<URLFetcherCore>()); | 632 new base::RepeatingTimer<URLFetcherCore>()); |
| 615 upload_progress_checker_timer_->Start( | 633 upload_progress_checker_timer_->Start( |
| 616 FROM_HERE, | 634 FROM_HERE, |
| 617 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), | 635 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), |
| 618 this, | 636 this, |
| 619 &URLFetcherCore::InformDelegateUploadProgress); | 637 &URLFetcherCore::InformDelegateUploadProgress); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 940 } | 958 } |
| 941 | 959 |
| 942 void URLFetcherCore::InformDelegateDownloadProgressInDelegateThread( | 960 void URLFetcherCore::InformDelegateDownloadProgressInDelegateThread( |
| 943 int64 current, int64 total) { | 961 int64 current, int64 total) { |
| 944 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); | 962 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); |
| 945 if (delegate_) | 963 if (delegate_) |
| 946 delegate_->OnURLFetchDownloadProgress(fetcher_, current, total); | 964 delegate_->OnURLFetchDownloadProgress(fetcher_, current, total); |
| 947 } | 965 } |
| 948 | 966 |
| 949 } // namespace net | 967 } // namespace net |
| OLD | NEW |