| 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/file_util_proxy.h" | 8 #include "base/file_util_proxy.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 if (network_task_runner_->RunsTasksOnCurrentThread()) { | 322 if (network_task_runner_->RunsTasksOnCurrentThread()) { |
| 323 CancelURLRequest(); | 323 CancelURLRequest(); |
| 324 } else { | 324 } else { |
| 325 network_task_runner_->PostTask( | 325 network_task_runner_->PostTask( |
| 326 FROM_HERE, base::Bind(&URLFetcherCore::CancelURLRequest, this)); | 326 FROM_HERE, base::Bind(&URLFetcherCore::CancelURLRequest, this)); |
| 327 } | 327 } |
| 328 } | 328 } |
| 329 | 329 |
| 330 void URLFetcherCore::SetUploadData(const std::string& upload_content_type, | 330 void URLFetcherCore::SetUploadData(const std::string& upload_content_type, |
| 331 const std::string& upload_content) { | 331 const std::string& upload_content) { |
| 332 scoped_ptr<UploadElementReader> reader( |
| 333 UploadOwnedBytesElementReader::CreateWithString(upload_content)); |
| 334 SetUploadDataStream( |
| 335 upload_content_type, |
| 336 make_scoped_ptr(UploadDataStream::CreateWithReader(reader.Pass(), 0))); |
| 337 } |
| 338 |
| 339 void URLFetcherCore::SetUploadDataStream( |
| 340 const std::string& upload_content_type, |
| 341 scoped_ptr<UploadDataStream> upload_content) { |
| 332 DCHECK(!is_chunked_upload_); | 342 DCHECK(!is_chunked_upload_); |
| 343 DCHECK(!upload_content_); |
| 344 DCHECK(upload_content_type_.empty()); |
| 333 upload_content_type_ = upload_content_type; | 345 upload_content_type_ = upload_content_type; |
| 334 upload_content_ = upload_content; | 346 upload_content_ = upload_content.Pass(); |
| 335 } | 347 } |
| 336 | 348 |
| 337 void URLFetcherCore::SetChunkedUpload(const std::string& content_type) { | 349 void URLFetcherCore::SetChunkedUpload(const std::string& content_type) { |
| 338 DCHECK(is_chunked_upload_ || | 350 DCHECK(is_chunked_upload_ || |
| 339 (upload_content_type_.empty() && | 351 (upload_content_type_.empty() && |
| 340 upload_content_.empty())); | 352 !upload_content_)); |
| 341 upload_content_type_ = content_type; | 353 upload_content_type_ = content_type; |
| 342 upload_content_.clear(); | 354 upload_content_.reset(); |
| 343 is_chunked_upload_ = true; | 355 is_chunked_upload_ = true; |
| 344 } | 356 } |
| 345 | 357 |
| 346 void URLFetcherCore::AppendChunkToUpload(const std::string& content, | 358 void URLFetcherCore::AppendChunkToUpload(const std::string& content, |
| 347 bool is_last_chunk) { | 359 bool is_last_chunk) { |
| 348 DCHECK(delegate_task_runner_); | 360 DCHECK(delegate_task_runner_); |
| 349 DCHECK(network_task_runner_.get()); | 361 DCHECK(network_task_runner_.get()); |
| 350 network_task_runner_->PostTask( | 362 network_task_runner_->PostTask( |
| 351 FROM_HERE, | 363 FROM_HERE, |
| 352 base::Bind(&URLFetcherCore::CompleteAddingUploadDataChunk, this, content, | 364 base::Bind(&URLFetcherCore::CompleteAddingUploadDataChunk, this, content, |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 break; | 720 break; |
| 709 | 721 |
| 710 case URLFetcher::POST: | 722 case URLFetcher::POST: |
| 711 case URLFetcher::PUT: | 723 case URLFetcher::PUT: |
| 712 DCHECK(!upload_content_type_.empty()); | 724 DCHECK(!upload_content_type_.empty()); |
| 713 | 725 |
| 714 request_->set_method( | 726 request_->set_method( |
| 715 request_type_ == URLFetcher::POST ? "POST" : "PUT"); | 727 request_type_ == URLFetcher::POST ? "POST" : "PUT"); |
| 716 extra_request_headers_.SetHeader(HttpRequestHeaders::kContentType, | 728 extra_request_headers_.SetHeader(HttpRequestHeaders::kContentType, |
| 717 upload_content_type_); | 729 upload_content_type_); |
| 718 if (!upload_content_.empty()) { | 730 if (upload_content_) |
| 719 scoped_ptr<UploadElementReader> reader(new UploadBytesElementReader( | 731 request_->set_upload(upload_content_.Pass()); |
| 720 upload_content_.data(), upload_content_.size())); | |
| 721 request_->set_upload(make_scoped_ptr( | |
| 722 UploadDataStream::CreateWithReader(reader.Pass(), 0))); | |
| 723 } | |
| 724 | |
| 725 current_upload_bytes_ = -1; | 732 current_upload_bytes_ = -1; |
| 726 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the | 733 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the |
| 727 // layer and avoid using timer here. | 734 // layer and avoid using timer here. |
| 728 upload_progress_checker_timer_.reset( | 735 upload_progress_checker_timer_.reset( |
| 729 new base::RepeatingTimer<URLFetcherCore>()); | 736 new base::RepeatingTimer<URLFetcherCore>()); |
| 730 upload_progress_checker_timer_->Start( | 737 upload_progress_checker_timer_->Start( |
| 731 FROM_HERE, | 738 FROM_HERE, |
| 732 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), | 739 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), |
| 733 this, | 740 this, |
| 734 &URLFetcherCore::InformDelegateUploadProgress); | 741 &URLFetcherCore::InformDelegateUploadProgress); |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 979 file_writer_->DisownFile(); | 986 file_writer_->DisownFile(); |
| 980 } | 987 } |
| 981 | 988 |
| 982 void URLFetcherCore::InformDelegateUploadProgress() { | 989 void URLFetcherCore::InformDelegateUploadProgress() { |
| 983 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 990 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 984 if (request_.get()) { | 991 if (request_.get()) { |
| 985 int64 current = request_->GetUploadProgress().position(); | 992 int64 current = request_->GetUploadProgress().position(); |
| 986 if (current_upload_bytes_ != current) { | 993 if (current_upload_bytes_ != current) { |
| 987 current_upload_bytes_ = current; | 994 current_upload_bytes_ = current; |
| 988 int64 total = -1; | 995 int64 total = -1; |
| 989 if (!is_chunked_upload_) | 996 if (!is_chunked_upload_) { |
| 990 total = static_cast<int64>(upload_content_.size()); | 997 total = static_cast<int64>(request_->GetUploadProgress().size()); |
| 998 // Total may be zero if the UploadDataStream::Init has not been called |
| 999 // yet. Don't send the upload progress until the size is initialized. |
| 1000 if (!total) |
| 1001 return; |
| 1002 } |
| 991 delegate_task_runner_->PostTask( | 1003 delegate_task_runner_->PostTask( |
| 992 FROM_HERE, | 1004 FROM_HERE, |
| 993 base::Bind( | 1005 base::Bind( |
| 994 &URLFetcherCore::InformDelegateUploadProgressInDelegateThread, | 1006 &URLFetcherCore::InformDelegateUploadProgressInDelegateThread, |
| 995 this, current, total)); | 1007 this, current, total)); |
| 996 } | 1008 } |
| 997 } | 1009 } |
| 998 } | 1010 } |
| 999 | 1011 |
| 1000 void URLFetcherCore::InformDelegateUploadProgressInDelegateThread( | 1012 void URLFetcherCore::InformDelegateUploadProgressInDelegateThread( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1034 } | 1046 } |
| 1035 | 1047 |
| 1036 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread( | 1048 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread( |
| 1037 scoped_ptr<std::string> download_data) { | 1049 scoped_ptr<std::string> download_data) { |
| 1038 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); | 1050 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); |
| 1039 if (delegate_) | 1051 if (delegate_) |
| 1040 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass()); | 1052 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass()); |
| 1041 } | 1053 } |
| 1042 | 1054 |
| 1043 } // namespace net | 1055 } // namespace net |
| OLD | NEW |