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/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 323 if (network_task_runner_->RunsTasksOnCurrentThread()) { | 323 if (network_task_runner_->RunsTasksOnCurrentThread()) { |
| 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_); | |
| 334 DCHECK(!upload_content_); | |
| 335 DCHECK(upload_content_type_.empty()); | |
| 336 | |
| 337 // Empty |upload_content_type| is allowed iff the |upload_content| is empty. | |
| 338 DCHECK(upload_content.empty() || !upload_content_type.empty()); | |
| 339 | |
| 340 upload_content_type_ = upload_content_type; | |
| 333 scoped_ptr<UploadElementReader> reader( | 341 scoped_ptr<UploadElementReader> reader( |
| 334 UploadOwnedBytesElementReader::CreateWithString(upload_content)); | 342 UploadOwnedBytesElementReader::CreateWithString(upload_content)); |
| 335 SetUploadDataStream( | 343 upload_content_.reset(UploadDataStream::CreateWithReader(reader.Pass(), 0)); |
| 336 upload_content_type, | |
| 337 make_scoped_ptr(UploadDataStream::CreateWithReader(reader.Pass(), 0))); | |
| 338 } | 344 } |
| 339 | 345 |
| 340 void URLFetcherCore::SetUploadDataStream( | 346 void URLFetcherCore::SetUploadDataStream( |
| 341 const std::string& upload_content_type, | 347 const std::string& upload_content_type, |
| 342 scoped_ptr<UploadDataStream> upload_content) { | 348 scoped_ptr<UploadDataStream> upload_content) { |
| 343 DCHECK(!is_chunked_upload_); | 349 DCHECK(!is_chunked_upload_); |
| 344 DCHECK(!upload_content_); | 350 DCHECK(!upload_content_); |
| 345 DCHECK(upload_content_type_.empty()); | 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 | |
| 346 upload_content_type_ = upload_content_type; | 357 upload_content_type_ = upload_content_type; |
| 347 upload_content_ = upload_content.Pass(); | 358 upload_content_ = upload_content.Pass(); |
| 348 } | 359 } |
| 349 | 360 |
| 350 void URLFetcherCore::SetChunkedUpload(const std::string& content_type) { | 361 void URLFetcherCore::SetChunkedUpload(const std::string& content_type) { |
| 351 DCHECK(is_chunked_upload_ || | 362 DCHECK(is_chunked_upload_ || |
| 352 (upload_content_type_.empty() && | 363 (upload_content_type_.empty() && |
| 353 !upload_content_)); | 364 !upload_content_)); |
|
mmenke
2013/02/06 17:27:07
"DCHECK(!upload_content_type.empty());" here as we
hidehiko
2013/02/06 17:55:41
Done.
| |
| 354 upload_content_type_ = content_type; | 365 upload_content_type_ = content_type; |
| 355 upload_content_.reset(); | 366 upload_content_.reset(); |
| 356 is_chunked_upload_ = true; | 367 is_chunked_upload_ = true; |
| 357 } | 368 } |
| 358 | 369 |
| 359 void URLFetcherCore::AppendChunkToUpload(const std::string& content, | 370 void URLFetcherCore::AppendChunkToUpload(const std::string& content, |
| 360 bool is_last_chunk) { | 371 bool is_last_chunk) { |
| 361 DCHECK(delegate_task_runner_); | 372 DCHECK(delegate_task_runner_); |
| 362 DCHECK(network_task_runner_.get()); | 373 DCHECK(network_task_runner_.get()); |
| 363 network_task_runner_->PostTask( | 374 network_task_runner_->PostTask( |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 727 url_request_create_data_callback_.Run()); | 738 url_request_create_data_callback_.Run()); |
| 728 } | 739 } |
| 729 | 740 |
| 730 switch (request_type_) { | 741 switch (request_type_) { |
| 731 case URLFetcher::GET: | 742 case URLFetcher::GET: |
| 732 break; | 743 break; |
| 733 | 744 |
| 734 case URLFetcher::POST: | 745 case URLFetcher::POST: |
| 735 case URLFetcher::PUT: | 746 case URLFetcher::PUT: |
| 736 case URLFetcher::PATCH: | 747 case URLFetcher::PATCH: |
| 737 DCHECK(!upload_content_type_.empty()); | 748 // Upload content must be set. |
| 749 DCHECK(is_chunked_upload_ || upload_content_); | |
| 738 | 750 |
| 739 request_->set_method( | 751 request_->set_method( |
| 740 request_type_ == URLFetcher::POST ? "POST" : | 752 request_type_ == URLFetcher::POST ? "POST" : |
| 741 request_type_ == URLFetcher::PUT ? "PUT" : "PATCH"); | 753 request_type_ == URLFetcher::PUT ? "PUT" : "PATCH"); |
| 742 extra_request_headers_.SetHeader(HttpRequestHeaders::kContentType, | 754 if (!upload_content_type_.empty()) { |
| 743 upload_content_type_); | 755 extra_request_headers_.SetHeader(HttpRequestHeaders::kContentType, |
| 756 upload_content_type_); | |
| 757 } | |
| 744 if (upload_content_) | 758 if (upload_content_) |
| 745 request_->set_upload(upload_content_.Pass()); | 759 request_->set_upload(upload_content_.Pass()); |
| 746 current_upload_bytes_ = -1; | 760 current_upload_bytes_ = -1; |
| 747 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the | 761 // TODO(kinaba): http://crbug.com/118103. Implement upload callback in the |
| 748 // layer and avoid using timer here. | 762 // layer and avoid using timer here. |
| 749 upload_progress_checker_timer_.reset( | 763 upload_progress_checker_timer_.reset( |
| 750 new base::RepeatingTimer<URLFetcherCore>()); | 764 new base::RepeatingTimer<URLFetcherCore>()); |
| 751 upload_progress_checker_timer_->Start( | 765 upload_progress_checker_timer_->Start( |
| 752 FROM_HERE, | 766 FROM_HERE, |
| 753 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), | 767 base::TimeDelta::FromMilliseconds(kUploadProgressTimerInterval), |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1064 } | 1078 } |
| 1065 | 1079 |
| 1066 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread( | 1080 void URLFetcherCore::InformDelegateDownloadDataInDelegateThread( |
| 1067 scoped_ptr<std::string> download_data) { | 1081 scoped_ptr<std::string> download_data) { |
| 1068 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); | 1082 DCHECK(delegate_task_runner_->BelongsToCurrentThread()); |
| 1069 if (delegate_) | 1083 if (delegate_) |
| 1070 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass()); | 1084 delegate_->OnURLFetchDownloadData(fetcher_, download_data.Pass()); |
| 1071 } | 1085 } |
| 1072 | 1086 |
| 1073 } // namespace net | 1087 } // namespace net |
| OLD | NEW |