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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_util_proxy.h" | 8 #include "base/files/file_util_proxy.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 CancelURLRequest(); | 324 CancelURLRequest(); |
| 325 } else { | 325 } else { |
| 326 network_task_runner_->PostTask( | 326 network_task_runner_->PostTask( |
| 327 FROM_HERE, base::Bind(&URLFetcherCore::CancelURLRequest, this)); | 327 FROM_HERE, base::Bind(&URLFetcherCore::CancelURLRequest, this)); |
| 328 } | 328 } |
| 329 } | 329 } |
| 330 | 330 |
| 331 void URLFetcherCore::SetUploadData(const std::string& upload_content_type, | 331 void URLFetcherCore::SetUploadData(const std::string& upload_content_type, |
| 332 const std::string& upload_content) { | 332 const std::string& upload_content) { |
| 333 DCHECK(!is_chunked_upload_); | 333 DCHECK(!is_chunked_upload_); |
| 334 DCHECK(!upload_content_); | 334 DCHECK(upload_content_.empty()); |
| 335 DCHECK(upload_content_type_.empty()); | 335 DCHECK(upload_content_type_.empty()); |
| 336 | 336 |
| 337 // Empty |upload_content_type| is allowed iff the |upload_content| is empty. | 337 // Empty |upload_content_type| is allowed iff the |upload_content| is empty. |
| 338 DCHECK(upload_content.empty() || !upload_content_type.empty()); | 338 DCHECK(upload_content.empty() || !upload_content_type.empty()); |
| 339 | 339 |
| 340 upload_content_type_ = upload_content_type; | 340 upload_content_type_ = upload_content_type; |
| 341 scoped_ptr<UploadElementReader> reader( | 341 upload_content_ = upload_content; |
| 342 UploadOwnedBytesElementReader::CreateWithString(upload_content)); | |
| 343 upload_content_.reset(UploadDataStream::CreateWithReader(reader.Pass(), 0)); | |
| 344 } | |
| 345 | |
| 346 void URLFetcherCore::SetUploadDataStream( | |
| 347 const std::string& upload_content_type, | |
| 348 scoped_ptr<UploadDataStream> upload_content) { | |
| 349 DCHECK(!is_chunked_upload_); | |
| 350 DCHECK(!upload_content_); | |
| 351 DCHECK(upload_content_type_.empty()); | |
| 352 | |
| 353 // Empty |upload_content_type| is not allowed here, because it is impossible | |
| 354 // to ensure non-empty |upload_content| as it may not be initialized yet. | |
| 355 DCHECK(!upload_content_type.empty()); | |
| 356 | |
| 357 upload_content_type_ = upload_content_type; | |
| 358 upload_content_ = upload_content.Pass(); | |
| 359 } | 342 } |
| 360 | 343 |
| 361 void URLFetcherCore::SetChunkedUpload(const std::string& content_type) { | 344 void URLFetcherCore::SetChunkedUpload(const std::string& content_type) { |
| 362 DCHECK(is_chunked_upload_ || | 345 DCHECK(is_chunked_upload_ || |
| 363 (upload_content_type_.empty() && | 346 (upload_content_type_.empty() && |
| 364 !upload_content_)); | 347 upload_content_.empty())); |
| 365 | 348 |
| 366 // Empty |content_type| is not allowed here, because it is impossible | 349 // Empty |content_type| is not allowed here, because it is impossible |
| 367 // to ensure non-empty upload content as it is not yet supplied. | 350 // to ensure non-empty upload content as it is not yet supplied. |
| 368 DCHECK(!content_type.empty()); | 351 DCHECK(!content_type.empty()); |
| 369 | 352 |
| 370 upload_content_type_ = content_type; | 353 upload_content_type_ = content_type; |
| 371 upload_content_.reset(); | 354 upload_content_.clear(); |
| 372 is_chunked_upload_ = true; | 355 is_chunked_upload_ = true; |
| 373 } | 356 } |
| 374 | 357 |
| 375 void URLFetcherCore::AppendChunkToUpload(const std::string& content, | 358 void URLFetcherCore::AppendChunkToUpload(const std::string& content, |
| 376 bool is_last_chunk) { | 359 bool is_last_chunk) { |
| 377 DCHECK(delegate_task_runner_); | 360 DCHECK(delegate_task_runner_); |
| 378 DCHECK(network_task_runner_.get()); | 361 DCHECK(network_task_runner_.get()); |
| 379 network_task_runner_->PostTask( | 362 network_task_runner_->PostTask( |
| 380 FROM_HERE, | 363 FROM_HERE, |
| 381 base::Bind(&URLFetcherCore::CompleteAddingUploadDataChunk, this, content, | 364 base::Bind(&URLFetcherCore::CompleteAddingUploadDataChunk, this, content, |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 743 url_request_create_data_callback_.Run()); | 726 url_request_create_data_callback_.Run()); |
| 744 } | 727 } |
| 745 | 728 |
| 746 switch (request_type_) { | 729 switch (request_type_) { |
| 747 case URLFetcher::GET: | 730 case URLFetcher::GET: |
| 748 break; | 731 break; |
| 749 | 732 |
| 750 case URLFetcher::POST: | 733 case URLFetcher::POST: |
| 751 case URLFetcher::PUT: | 734 case URLFetcher::PUT: |
| 752 case URLFetcher::PATCH: | 735 case URLFetcher::PATCH: |
| 753 // Upload content must be set. | |
| 754 DCHECK(is_chunked_upload_ || upload_content_); | |
|
rmsousa
2013/02/13 23:05:08
One of the changes that came after http://crrev.co
| |
| 755 | |
| 756 request_->set_method( | 736 request_->set_method( |
| 757 request_type_ == URLFetcher::POST ? "POST" : | 737 request_type_ == URLFetcher::POST ? "POST" : |
| 758 request_type_ == URLFetcher::PUT ? "PUT" : "PATCH"); | 738 request_type_ == URLFetcher::PUT ? "PUT" : "PATCH"); |
| 759 if (!upload_content_type_.empty()) { | 739 if (!upload_content_type_.empty()) { |
| 760 extra_request_headers_.SetHeader(HttpRequestHeaders::kContentType, | 740 extra_request_headers_.SetHeader(HttpRequestHeaders::kContentType, |
| 761 upload_content_type_); | 741 upload_content_type_); |
| 762 } | 742 } |
| 763 if (upload_content_) | 743 if (!upload_content_.empty()) { |
| 764 request_->set_upload(upload_content_.Pass()); | 744 scoped_ptr<UploadElementReader> reader(new UploadBytesElementReader( |
| 745 upload_content_.data(), upload_content_.size())); | |
| 746 request_->set_upload(make_scoped_ptr( | |
| 747 UploadDataStream::CreateWithReader(reader.Pass(), 0))); | |
| 748 } | |
| 749 | |
| 765 current_upload_bytes_ = -1; | 750 current_upload_bytes_ = -1; |
| 766 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the | 751 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the |
| 767 // layer and avoid using timer here. | 752 // layer and avoid using timer here. |
| 768 upload_progress_checker_timer_.reset( | 753 upload_progress_checker_timer_.reset( |
| 769 new base::RepeatingTimer<URLFetcherCore>()); | 754 new base::RepeatingTimer<URLFetcherCore>()); |
| 770 upload_progress_checker_timer_->Start( | 755 upload_progress_checker_timer_->Start( |
| 771 FROM_HERE, | 756 FROM_HERE, |
| 772 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), | 757 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), |
| 773 this, | 758 this, |
| 774 &URLFetcherCore::InformDelegateUploadProgress); | 759 &URLFetcherCore::InformDelegateUploadProgress); |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1019 file_writer_->DisownFile(); | 1004 file_writer_->DisownFile(); |
| 1020 } | 1005 } |
| 1021 | 1006 |
| 1022 void URLFetcherCore::InformDelegateUploadProgress() { | 1007 void URLFetcherCore::InformDelegateUploadProgress() { |
| 1023 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 1008 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 1024 if (request_.get()) { | 1009 if (request_.get()) { |
| 1025 int64 current = request_->GetUploadProgress().position(); | 1010 int64 current = request_->GetUploadProgress().position(); |
| 1026 if (current_upload_bytes_ != current) { | 1011 if (current_upload_bytes_ != current) { |
| 1027 current_upload_bytes_ = current; | 1012 current_upload_bytes_ = current; |
| 1028 int64 total = -1; | 1013 int64 total = -1; |
| 1029 if (!is_chunked_upload_) { | 1014 if (!is_chunked_upload_) |
| 1030 total = static_cast<int64>(request_->GetUploadProgress().size()); | 1015 total = static_cast<int64>(upload_content_.size()); |
| 1031 // Total may be zero if the UploadDataStream::Init has not been called | |
| 1032 // yet. Don't send the upload progress until the size is initialized. | |
| 1033 if (!total) | |
| 1034 return; | |
| 1035 } | |
| 1036 delegate_task_runner_->PostTask( | 1016 delegate_task_runner_->PostTask( |
| 1037 FROM_HERE, | 1017 FROM_HERE, |
| 1038 base::Bind( | 1018 base::Bind( |
| 1039 &URLFetcherCore::InformDelegateUploadProgressInDelegateThread, | 1019 &URLFetcherCore::InformDelegateUploadProgressInDelegateThread, |
| 1040 this, current, total)); | 1020 this, current, total)); |
| 1041 } | 1021 } |
| 1042 } | 1022 } |
| 1043 } | 1023 } |
| 1044 | 1024 |
| 1045 void URLFetcherCore::InformDelegateUploadProgressInDelegateThread( | 1025 void URLFetcherCore::InformDelegateUploadProgressInDelegateThread( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1079 } | 1059 } |
| 1080 | 1060 |
| 1081 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread( | 1061 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread( |
| 1082 scoped_ptr<std::string> download_data) { | 1062 scoped_ptr<std::string> download_data) { |
| 1083 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); | 1063 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); |
| 1084 if (delegate_) | 1064 if (delegate_) |
| 1085 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass()); | 1065 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass()); |
| 1086 } | 1066 } |
| 1087 | 1067 |
| 1088 } // namespace net | 1068 } // namespace net |
| OLD | NEW |