| 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 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 network_task_runner_ = request_context_getter_->GetNetworkTaskRunner(); | 134 network_task_runner_ = request_context_getter_->GetNetworkTaskRunner(); |
| 135 } | 135 } |
| 136 DCHECK(network_task_runner_.get()) << "We need an IO task runner"; | 136 DCHECK(network_task_runner_.get()) << "We need an IO task runner"; |
| 137 | 137 |
| 138 network_task_runner_->PostTask( | 138 network_task_runner_->PostTask( |
| 139 FROM_HERE, base::Bind(&URLFetcherCore::StartOnIOThread, this)); | 139 FROM_HERE, base::Bind(&URLFetcherCore::StartOnIOThread, this)); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void URLFetcherCore::Stop() { | 142 void URLFetcherCore::Stop() { |
| 143 if (delegate_task_runner_) // May be NULL in tests. | 143 if (delegate_task_runner_) // May be NULL in tests. |
| 144 DCHECK(delegate_task_runner_->RunsTasksOnCurrentThread()); | 144 DCHECK(delegate_task_runner_->RunsTasksInCurrentSequence()); |
| 145 | 145 |
| 146 delegate_ = NULL; | 146 delegate_ = NULL; |
| 147 fetcher_ = NULL; | 147 fetcher_ = NULL; |
| 148 if (!network_task_runner_.get()) | 148 if (!network_task_runner_.get()) |
| 149 return; | 149 return; |
| 150 if (network_task_runner_->RunsTasksOnCurrentThread()) { | 150 if (network_task_runner_->RunsTasksInCurrentSequence()) { |
| 151 CancelURLRequest(ERR_ABORTED); | 151 CancelURLRequest(ERR_ABORTED); |
| 152 } else { | 152 } else { |
| 153 network_task_runner_->PostTask( | 153 network_task_runner_->PostTask( |
| 154 FROM_HERE, | 154 FROM_HERE, |
| 155 base::Bind(&URLFetcherCore::CancelURLRequest, this, ERR_ABORTED)); | 155 base::Bind(&URLFetcherCore::CancelURLRequest, this, ERR_ABORTED)); |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 | 158 |
| 159 void URLFetcherCore::SetUploadData(const std::string& upload_content_type, | 159 void URLFetcherCore::SetUploadData(const std::string& upload_content_type, |
| 160 const std::string& upload_content) { | 160 const std::string& upload_content) { |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 return backoff_delay_; | 298 return backoff_delay_; |
| 299 } | 299 } |
| 300 | 300 |
| 301 void URLFetcherCore::SetAutomaticallyRetryOnNetworkChanges(int max_retries) { | 301 void URLFetcherCore::SetAutomaticallyRetryOnNetworkChanges(int max_retries) { |
| 302 max_retries_on_network_changes_ = max_retries; | 302 max_retries_on_network_changes_ = max_retries; |
| 303 } | 303 } |
| 304 | 304 |
| 305 void URLFetcherCore::SaveResponseToFileAtPath( | 305 void URLFetcherCore::SaveResponseToFileAtPath( |
| 306 const base::FilePath& file_path, | 306 const base::FilePath& file_path, |
| 307 scoped_refptr<base::SequencedTaskRunner> file_task_runner) { | 307 scoped_refptr<base::SequencedTaskRunner> file_task_runner) { |
| 308 DCHECK(delegate_task_runner_->RunsTasksOnCurrentThread()); | 308 DCHECK(delegate_task_runner_->RunsTasksInCurrentSequence()); |
| 309 SaveResponseWithWriter(std::unique_ptr<URLFetcherResponseWriter>( | 309 SaveResponseWithWriter(std::unique_ptr<URLFetcherResponseWriter>( |
| 310 new URLFetcherFileWriter(file_task_runner, file_path))); | 310 new URLFetcherFileWriter(file_task_runner, file_path))); |
| 311 } | 311 } |
| 312 | 312 |
| 313 void URLFetcherCore::SaveResponseToTemporaryFile( | 313 void URLFetcherCore::SaveResponseToTemporaryFile( |
| 314 scoped_refptr<base::SequencedTaskRunner> file_task_runner) { | 314 scoped_refptr<base::SequencedTaskRunner> file_task_runner) { |
| 315 DCHECK(delegate_task_runner_->RunsTasksOnCurrentThread()); | 315 DCHECK(delegate_task_runner_->RunsTasksInCurrentSequence()); |
| 316 SaveResponseWithWriter(std::unique_ptr<URLFetcherResponseWriter>( | 316 SaveResponseWithWriter(std::unique_ptr<URLFetcherResponseWriter>( |
| 317 new URLFetcherFileWriter(file_task_runner, base::FilePath()))); | 317 new URLFetcherFileWriter(file_task_runner, base::FilePath()))); |
| 318 } | 318 } |
| 319 | 319 |
| 320 void URLFetcherCore::SaveResponseWithWriter( | 320 void URLFetcherCore::SaveResponseWithWriter( |
| 321 std::unique_ptr<URLFetcherResponseWriter> response_writer) { | 321 std::unique_ptr<URLFetcherResponseWriter> response_writer) { |
| 322 DCHECK(delegate_task_runner_->RunsTasksOnCurrentThread()); | 322 DCHECK(delegate_task_runner_->RunsTasksInCurrentSequence()); |
| 323 response_writer_ = std::move(response_writer); | 323 response_writer_ = std::move(response_writer); |
| 324 } | 324 } |
| 325 | 325 |
| 326 HttpResponseHeaders* URLFetcherCore::GetResponseHeaders() const { | 326 HttpResponseHeaders* URLFetcherCore::GetResponseHeaders() const { |
| 327 return response_headers_.get(); | 327 return response_headers_.get(); |
| 328 } | 328 } |
| 329 | 329 |
| 330 // TODO(panayiotis): socket_address_ is written in the IO thread, | 330 // TODO(panayiotis): socket_address_ is written in the IO thread, |
| 331 // if this is accessed in the UI thread, this could result in a race. | 331 // if this is accessed in the UI thread, this could result in a race. |
| 332 // Same for response_headers_ above and was_fetched_via_proxy_ below. | 332 // Same for response_headers_ above and was_fetched_via_proxy_ below. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 360 | 360 |
| 361 const URLRequestStatus& URLFetcherCore::GetStatus() const { | 361 const URLRequestStatus& URLFetcherCore::GetStatus() const { |
| 362 return status_; | 362 return status_; |
| 363 } | 363 } |
| 364 | 364 |
| 365 int URLFetcherCore::GetResponseCode() const { | 365 int URLFetcherCore::GetResponseCode() const { |
| 366 return response_code_; | 366 return response_code_; |
| 367 } | 367 } |
| 368 | 368 |
| 369 void URLFetcherCore::ReceivedContentWasMalformed() { | 369 void URLFetcherCore::ReceivedContentWasMalformed() { |
| 370 DCHECK(delegate_task_runner_->RunsTasksOnCurrentThread()); | 370 DCHECK(delegate_task_runner_->RunsTasksInCurrentSequence()); |
| 371 if (network_task_runner_.get()) { | 371 if (network_task_runner_.get()) { |
| 372 network_task_runner_->PostTask( | 372 network_task_runner_->PostTask( |
| 373 FROM_HERE, base::Bind(&URLFetcherCore::NotifyMalformedContent, this)); | 373 FROM_HERE, base::Bind(&URLFetcherCore::NotifyMalformedContent, this)); |
| 374 } | 374 } |
| 375 } | 375 } |
| 376 | 376 |
| 377 bool URLFetcherCore::GetResponseAsString( | 377 bool URLFetcherCore::GetResponseAsString( |
| 378 std::string* out_response_string) const { | 378 std::string* out_response_string) const { |
| 379 URLFetcherStringWriter* string_writer = | 379 URLFetcherStringWriter* string_writer = |
| 380 response_writer_ ? response_writer_->AsStringWriter() : NULL; | 380 response_writer_ ? response_writer_->AsStringWriter() : NULL; |
| 381 if (!string_writer) | 381 if (!string_writer) |
| 382 return false; | 382 return false; |
| 383 | 383 |
| 384 *out_response_string = string_writer->data(); | 384 *out_response_string = string_writer->data(); |
| 385 UMA_HISTOGRAM_MEMORY_KB("UrlFetcher.StringResponseSize", | 385 UMA_HISTOGRAM_MEMORY_KB("UrlFetcher.StringResponseSize", |
| 386 (string_writer->data().length() / 1024)); | 386 (string_writer->data().length() / 1024)); |
| 387 return true; | 387 return true; |
| 388 } | 388 } |
| 389 | 389 |
| 390 bool URLFetcherCore::GetResponseAsFilePath(bool take_ownership, | 390 bool URLFetcherCore::GetResponseAsFilePath(bool take_ownership, |
| 391 base::FilePath* out_response_path) { | 391 base::FilePath* out_response_path) { |
| 392 DCHECK(delegate_task_runner_->RunsTasksOnCurrentThread()); | 392 DCHECK(delegate_task_runner_->RunsTasksInCurrentSequence()); |
| 393 | 393 |
| 394 URLFetcherFileWriter* file_writer = | 394 URLFetcherFileWriter* file_writer = |
| 395 response_writer_ ? response_writer_->AsFileWriter() : NULL; | 395 response_writer_ ? response_writer_->AsFileWriter() : NULL; |
| 396 if (!file_writer) | 396 if (!file_writer) |
| 397 return false; | 397 return false; |
| 398 | 398 |
| 399 *out_response_path = file_writer->file_path(); | 399 *out_response_path = file_writer->file_path(); |
| 400 | 400 |
| 401 if (take_ownership) { | 401 if (take_ownership) { |
| 402 // Intentionally calling a file_writer_ method directly without posting | 402 // Intentionally calling a file_writer_ method directly without posting |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 728 // context. | 728 // context. |
| 729 request_context_getter_ = NULL; | 729 request_context_getter_ = NULL; |
| 730 initiator_.reset(); | 730 initiator_.reset(); |
| 731 url_request_data_key_ = NULL; | 731 url_request_data_key_ = NULL; |
| 732 url_request_create_data_callback_.Reset(); | 732 url_request_create_data_callback_.Reset(); |
| 733 was_cancelled_ = true; | 733 was_cancelled_ = true; |
| 734 } | 734 } |
| 735 | 735 |
| 736 void URLFetcherCore::OnCompletedURLRequest( | 736 void URLFetcherCore::OnCompletedURLRequest( |
| 737 base::TimeDelta backoff_delay) { | 737 base::TimeDelta backoff_delay) { |
| 738 DCHECK(delegate_task_runner_->RunsTasksOnCurrentThread()); | 738 DCHECK(delegate_task_runner_->RunsTasksInCurrentSequence()); |
| 739 | 739 |
| 740 // Save the status and backoff_delay so that delegates can read it. | 740 // Save the status and backoff_delay so that delegates can read it. |
| 741 if (delegate_) { | 741 if (delegate_) { |
| 742 backoff_delay_ = backoff_delay; | 742 backoff_delay_ = backoff_delay; |
| 743 InformDelegateFetchIsComplete(); | 743 InformDelegateFetchIsComplete(); |
| 744 } | 744 } |
| 745 } | 745 } |
| 746 | 746 |
| 747 void URLFetcherCore::InformDelegateFetchIsComplete() { | 747 void URLFetcherCore::InformDelegateFetchIsComplete() { |
| 748 DCHECK(delegate_task_runner_->RunsTasksOnCurrentThread()); | 748 DCHECK(delegate_task_runner_->RunsTasksInCurrentSequence()); |
| 749 if (delegate_) | 749 if (delegate_) |
| 750 delegate_->OnURLFetchComplete(fetcher_); | 750 delegate_->OnURLFetchComplete(fetcher_); |
| 751 } | 751 } |
| 752 | 752 |
| 753 void URLFetcherCore::NotifyMalformedContent() { | 753 void URLFetcherCore::NotifyMalformedContent() { |
| 754 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 754 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 755 if (url_throttler_entry_.get()) { | 755 if (url_throttler_entry_.get()) { |
| 756 int status_code = response_code_; | 756 int status_code = response_code_; |
| 757 if (status_code == URLFetcher::RESPONSE_CODE_INVALID) { | 757 if (status_code == URLFetcher::RESPONSE_CODE_INVALID) { |
| 758 // The status code will generally be known by the time clients | 758 // The status code will generally be known by the time clients |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 942 base::Bind( | 942 base::Bind( |
| 943 &URLFetcherCore::InformDelegateUploadProgressInDelegateSequence, | 943 &URLFetcherCore::InformDelegateUploadProgressInDelegateSequence, |
| 944 this, current, total)); | 944 this, current, total)); |
| 945 } | 945 } |
| 946 } | 946 } |
| 947 } | 947 } |
| 948 | 948 |
| 949 void URLFetcherCore::InformDelegateUploadProgressInDelegateSequence( | 949 void URLFetcherCore::InformDelegateUploadProgressInDelegateSequence( |
| 950 int64_t current, | 950 int64_t current, |
| 951 int64_t total) { | 951 int64_t total) { |
| 952 DCHECK(delegate_task_runner_->RunsTasksOnCurrentThread()); | 952 DCHECK(delegate_task_runner_->RunsTasksInCurrentSequence()); |
| 953 if (delegate_) | 953 if (delegate_) |
| 954 delegate_->OnURLFetchUploadProgress(fetcher_, current, total); | 954 delegate_->OnURLFetchUploadProgress(fetcher_, current, total); |
| 955 } | 955 } |
| 956 | 956 |
| 957 void URLFetcherCore::InformDelegateDownloadProgress() { | 957 void URLFetcherCore::InformDelegateDownloadProgress() { |
| 958 DCHECK(network_task_runner_->BelongsToCurrentThread()); | 958 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 959 | 959 |
| 960 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455952 is fixed. | 960 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455952 is fixed. |
| 961 tracked_objects::ScopedTracker tracking_profile2( | 961 tracked_objects::ScopedTracker tracking_profile2( |
| 962 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 962 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 963 "455952 delegate_task_runner_->PostTask()")); | 963 "455952 delegate_task_runner_->PostTask()")); |
| 964 | 964 |
| 965 delegate_task_runner_->PostTask( | 965 delegate_task_runner_->PostTask( |
| 966 FROM_HERE, | 966 FROM_HERE, |
| 967 base::Bind( | 967 base::Bind( |
| 968 &URLFetcherCore::InformDelegateDownloadProgressInDelegateSequence, | 968 &URLFetcherCore::InformDelegateDownloadProgressInDelegateSequence, |
| 969 this, current_response_bytes_, total_response_bytes_, | 969 this, current_response_bytes_, total_response_bytes_, |
| 970 request_->GetTotalReceivedBytes())); | 970 request_->GetTotalReceivedBytes())); |
| 971 } | 971 } |
| 972 | 972 |
| 973 void URLFetcherCore::InformDelegateDownloadProgressInDelegateSequence( | 973 void URLFetcherCore::InformDelegateDownloadProgressInDelegateSequence( |
| 974 int64_t current, | 974 int64_t current, |
| 975 int64_t total, | 975 int64_t total, |
| 976 int64_t current_network_bytes) { | 976 int64_t current_network_bytes) { |
| 977 DCHECK(delegate_task_runner_->RunsTasksOnCurrentThread()); | 977 DCHECK(delegate_task_runner_->RunsTasksInCurrentSequence()); |
| 978 if (delegate_) | 978 if (delegate_) |
| 979 delegate_->OnURLFetchDownloadProgress(fetcher_, current, total, | 979 delegate_->OnURLFetchDownloadProgress(fetcher_, current, total, |
| 980 current_network_bytes); | 980 current_network_bytes); |
| 981 } | 981 } |
| 982 | 982 |
| 983 void URLFetcherCore::AssertHasNoUploadData() const { | 983 void URLFetcherCore::AssertHasNoUploadData() const { |
| 984 DCHECK(!upload_content_set_); | 984 DCHECK(!upload_content_set_); |
| 985 DCHECK(upload_content_.empty()); | 985 DCHECK(upload_content_.empty()); |
| 986 DCHECK(upload_file_path_.empty()); | 986 DCHECK(upload_file_path_.empty()); |
| 987 DCHECK(upload_stream_factory_.is_null()); | 987 DCHECK(upload_stream_factory_.is_null()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1001 memset(instruction_pointers_copy, 0xAB, sizeof(instruction_pointers_copy)); | 1001 memset(instruction_pointers_copy, 0xAB, sizeof(instruction_pointers_copy)); |
| 1002 stack_size = std::min(kMaxStackSize, stack_size); | 1002 stack_size = std::min(kMaxStackSize, stack_size); |
| 1003 std::memcpy(&instruction_pointers_copy[1], instruction_pointers, | 1003 std::memcpy(&instruction_pointers_copy[1], instruction_pointers, |
| 1004 stack_size * sizeof(const void*)); | 1004 stack_size * sizeof(const void*)); |
| 1005 base::debug::Alias(&stack_size); | 1005 base::debug::Alias(&stack_size); |
| 1006 base::debug::Alias(&instruction_pointers_copy); | 1006 base::debug::Alias(&instruction_pointers_copy); |
| 1007 base::debug::DumpWithoutCrashing(); | 1007 base::debug::DumpWithoutCrashing(); |
| 1008 } | 1008 } |
| 1009 | 1009 |
| 1010 } // namespace net | 1010 } // namespace net |
| OLD | NEW |