| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/common/net/url_fetcher.h" | 5 #include "content/common/net/url_fetcher.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 base::PlatformFileError error_code, | 344 base::PlatformFileError error_code, |
| 345 base::PassPlatformFile file_handle, | 345 base::PassPlatformFile file_handle, |
| 346 FilePath file_path) { | 346 FilePath file_path) { |
| 347 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 347 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 348 | 348 |
| 349 if (base::PLATFORM_FILE_OK != error_code) { | 349 if (base::PLATFORM_FILE_OK != error_code) { |
| 350 error_code_ = error_code; | 350 error_code_ = error_code; |
| 351 RemoveTempFile(); | 351 RemoveTempFile(); |
| 352 core_->delegate_loop_proxy_->PostTask( | 352 core_->delegate_loop_proxy_->PostTask( |
| 353 FROM_HERE, | 353 FROM_HERE, |
| 354 NewRunnableMethod(core_, &Core::InformDelegateFetchIsComplete)); | 354 base::Bind(&Core::InformDelegateFetchIsComplete, core_)); |
| 355 return; | 355 return; |
| 356 } | 356 } |
| 357 | 357 |
| 358 temp_file_ = file_path; | 358 temp_file_ = file_path; |
| 359 temp_file_handle_ = file_handle.ReleaseValue(); | 359 temp_file_handle_ = file_handle.ReleaseValue(); |
| 360 total_bytes_written_ = 0; | 360 total_bytes_written_ = 0; |
| 361 | 361 |
| 362 core_->io_message_loop_proxy_->PostTask( | 362 core_->io_message_loop_proxy_->PostTask( |
| 363 FROM_HERE, | 363 FROM_HERE, |
| 364 NewRunnableMethod(core_, &Core::StartURLRequestWhenAppropriate)); | 364 base::Bind(&Core::StartURLRequestWhenAppropriate, core_)); |
| 365 } | 365 } |
| 366 | 366 |
| 367 void URLFetcher::Core::TempFileWriter::WriteBuffer(int num_bytes) { | 367 void URLFetcher::Core::TempFileWriter::WriteBuffer(int num_bytes) { |
| 368 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 368 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 369 | 369 |
| 370 // Start writing to the temp file by setting the initial state | 370 // Start writing to the temp file by setting the initial state |
| 371 // of |pending_bytes_| and |buffer_offset_| to indicate that the | 371 // of |pending_bytes_| and |buffer_offset_| to indicate that the |
| 372 // entire buffer has not yet been written. | 372 // entire buffer has not yet been written. |
| 373 pending_bytes_ = num_bytes; | 373 pending_bytes_ = num_bytes; |
| 374 buffer_offset_ = 0; | 374 buffer_offset_ = 0; |
| 375 ContinueWrite(base::PLATFORM_FILE_OK, 0); | 375 ContinueWrite(base::PLATFORM_FILE_OK, 0); |
| 376 } | 376 } |
| 377 | 377 |
| 378 void URLFetcher::Core::TempFileWriter::ContinueWrite( | 378 void URLFetcher::Core::TempFileWriter::ContinueWrite( |
| 379 base::PlatformFileError error_code, | 379 base::PlatformFileError error_code, |
| 380 int bytes_written) { | 380 int bytes_written) { |
| 381 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 381 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 382 | 382 |
| 383 if (base::PLATFORM_FILE_OK != error_code) { | 383 if (base::PLATFORM_FILE_OK != error_code) { |
| 384 error_code_ = error_code; | 384 error_code_ = error_code; |
| 385 RemoveTempFile(); | 385 RemoveTempFile(); |
| 386 core_->delegate_loop_proxy_->PostTask( | 386 core_->delegate_loop_proxy_->PostTask( |
| 387 FROM_HERE, | 387 FROM_HERE, |
| 388 NewRunnableMethod(core_, &Core::InformDelegateFetchIsComplete)); | 388 base::Bind(&Core::InformDelegateFetchIsComplete, core_)); |
| 389 return; | 389 return; |
| 390 } | 390 } |
| 391 | 391 |
| 392 total_bytes_written_ += bytes_written; | 392 total_bytes_written_ += bytes_written; |
| 393 buffer_offset_ += bytes_written; | 393 buffer_offset_ += bytes_written; |
| 394 pending_bytes_ -= bytes_written; | 394 pending_bytes_ -= bytes_written; |
| 395 | 395 |
| 396 if (pending_bytes_ > 0) { | 396 if (pending_bytes_ > 0) { |
| 397 base::FileUtilProxy::Write( | 397 base::FileUtilProxy::Write( |
| 398 file_message_loop_proxy_, temp_file_handle_, | 398 file_message_loop_proxy_, temp_file_handle_, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 | 431 |
| 432 void URLFetcher::Core::TempFileWriter::DidCloseTempFile( | 432 void URLFetcher::Core::TempFileWriter::DidCloseTempFile( |
| 433 base::PlatformFileError error_code) { | 433 base::PlatformFileError error_code) { |
| 434 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 434 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 435 | 435 |
| 436 if (base::PLATFORM_FILE_OK != error_code) { | 436 if (base::PLATFORM_FILE_OK != error_code) { |
| 437 error_code_ = error_code; | 437 error_code_ = error_code; |
| 438 RemoveTempFile(); | 438 RemoveTempFile(); |
| 439 core_->delegate_loop_proxy_->PostTask( | 439 core_->delegate_loop_proxy_->PostTask( |
| 440 FROM_HERE, | 440 FROM_HERE, |
| 441 NewRunnableMethod(core_, &Core::InformDelegateFetchIsComplete)); | 441 base::Bind(&Core::InformDelegateFetchIsComplete, core_)); |
| 442 return; | 442 return; |
| 443 } | 443 } |
| 444 | 444 |
| 445 // If the file was successfully closed, then the URL request is complete. | 445 // If the file was successfully closed, then the URL request is complete. |
| 446 core_->RetryOrCompleteUrlFetch(); | 446 core_->RetryOrCompleteUrlFetch(); |
| 447 } | 447 } |
| 448 | 448 |
| 449 void URLFetcher::Core::TempFileWriter::RemoveTempFile() { | 449 void URLFetcher::Core::TempFileWriter::RemoveTempFile() { |
| 450 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); | 450 DCHECK(core_->io_message_loop_proxy_->BelongsToCurrentThread()); |
| 451 | 451 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 if (io_message_loop_proxy_) { | 556 if (io_message_loop_proxy_) { |
| 557 DCHECK_EQ(io_message_loop_proxy_, | 557 DCHECK_EQ(io_message_loop_proxy_, |
| 558 request_context_getter_->GetIOMessageLoopProxy()); | 558 request_context_getter_->GetIOMessageLoopProxy()); |
| 559 } else { | 559 } else { |
| 560 io_message_loop_proxy_ = request_context_getter_->GetIOMessageLoopProxy(); | 560 io_message_loop_proxy_ = request_context_getter_->GetIOMessageLoopProxy(); |
| 561 } | 561 } |
| 562 CHECK(io_message_loop_proxy_.get()) << "We need an IO message loop proxy"; | 562 CHECK(io_message_loop_proxy_.get()) << "We need an IO message loop proxy"; |
| 563 | 563 |
| 564 io_message_loop_proxy_->PostTask( | 564 io_message_loop_proxy_->PostTask( |
| 565 FROM_HERE, | 565 FROM_HERE, |
| 566 NewRunnableMethod(this, &Core::StartOnIOThread)); | 566 base::Bind(&Core::StartOnIOThread, this)); |
| 567 } | 567 } |
| 568 | 568 |
| 569 void URLFetcher::Core::StartOnIOThread() { | 569 void URLFetcher::Core::StartOnIOThread() { |
| 570 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 570 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 571 | 571 |
| 572 switch (response_destination_) { | 572 switch (response_destination_) { |
| 573 case STRING: | 573 case STRING: |
| 574 StartURLRequestWhenAppropriate(); | 574 StartURLRequestWhenAppropriate(); |
| 575 break; | 575 break; |
| 576 | 576 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 590 NOTREACHED(); | 590 NOTREACHED(); |
| 591 } | 591 } |
| 592 } | 592 } |
| 593 | 593 |
| 594 void URLFetcher::Core::Stop() { | 594 void URLFetcher::Core::Stop() { |
| 595 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 595 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
| 596 delegate_ = NULL; | 596 delegate_ = NULL; |
| 597 fetcher_ = NULL; | 597 fetcher_ = NULL; |
| 598 if (io_message_loop_proxy_.get()) { | 598 if (io_message_loop_proxy_.get()) { |
| 599 io_message_loop_proxy_->PostTask( | 599 io_message_loop_proxy_->PostTask( |
| 600 FROM_HERE, NewRunnableMethod(this, &Core::CancelURLRequest)); | 600 FROM_HERE, base::Bind(&Core::CancelURLRequest, this)); |
| 601 } | 601 } |
| 602 } | 602 } |
| 603 | 603 |
| 604 void URLFetcher::Core::ReceivedContentWasMalformed() { | 604 void URLFetcher::Core::ReceivedContentWasMalformed() { |
| 605 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); | 605 DCHECK(delegate_loop_proxy_->BelongsToCurrentThread()); |
| 606 if (io_message_loop_proxy_.get()) { | 606 if (io_message_loop_proxy_.get()) { |
| 607 io_message_loop_proxy_->PostTask( | 607 io_message_loop_proxy_->PostTask( |
| 608 FROM_HERE, NewRunnableMethod(this, &Core::NotifyMalformedContent)); | 608 FROM_HERE, base::Bind(&Core::NotifyMalformedContent, this)); |
| 609 } | 609 } |
| 610 } | 610 } |
| 611 | 611 |
| 612 void URLFetcher::Core::CancelAll() { | 612 void URLFetcher::Core::CancelAll() { |
| 613 g_registry.Get().CancelAll(); | 613 g_registry.Get().CancelAll(); |
| 614 } | 614 } |
| 615 | 615 |
| 616 void URLFetcher::Core::OnResponseStarted(net::URLRequest* request) { | 616 void URLFetcher::Core::OnResponseStarted(net::URLRequest* request) { |
| 617 DCHECK_EQ(request, request_.get()); | 617 DCHECK_EQ(request, request_.get()); |
| 618 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 618 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 635 static_cast<int>(content.length()), | 635 static_cast<int>(content.length()), |
| 636 is_last_chunk); | 636 is_last_chunk); |
| 637 } | 637 } |
| 638 | 638 |
| 639 void URLFetcher::Core::AppendChunkToUpload(const std::string& content, | 639 void URLFetcher::Core::AppendChunkToUpload(const std::string& content, |
| 640 bool is_last_chunk) { | 640 bool is_last_chunk) { |
| 641 DCHECK(delegate_loop_proxy_); | 641 DCHECK(delegate_loop_proxy_); |
| 642 CHECK(io_message_loop_proxy_.get()); | 642 CHECK(io_message_loop_proxy_.get()); |
| 643 io_message_loop_proxy_->PostTask( | 643 io_message_loop_proxy_->PostTask( |
| 644 FROM_HERE, | 644 FROM_HERE, |
| 645 NewRunnableMethod(this, &Core::CompleteAddingUploadDataChunk, content, | 645 base::Bind(&Core::CompleteAddingUploadDataChunk, this, |
| 646 is_last_chunk)); | 646 content, is_last_chunk)); |
| 647 } | 647 } |
| 648 | 648 |
| 649 // Return true if the write was done and reading may continue. | 649 // Return true if the write was done and reading may continue. |
| 650 // Return false if the write is pending, and the next read will | 650 // Return false if the write is pending, and the next read will |
| 651 // be done later. | 651 // be done later. |
| 652 bool URLFetcher::Core::WriteBuffer(int num_bytes) { | 652 bool URLFetcher::Core::WriteBuffer(int num_bytes) { |
| 653 bool write_complete = false; | 653 bool write_complete = false; |
| 654 switch (response_destination_) { | 654 switch (response_destination_) { |
| 655 case STRING: | 655 case STRING: |
| 656 data_.append(buffer_->data(), num_bytes); | 656 data_.append(buffer_->data(), num_bytes); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 738 num_retries_ <= max_retries_) { | 738 num_retries_ <= max_retries_) { |
| 739 StartOnIOThread(); | 739 StartOnIOThread(); |
| 740 return; | 740 return; |
| 741 } | 741 } |
| 742 } else { | 742 } else { |
| 743 backoff_delay = base::TimeDelta(); | 743 backoff_delay = base::TimeDelta(); |
| 744 } | 744 } |
| 745 request_context_getter_ = NULL; | 745 request_context_getter_ = NULL; |
| 746 bool posted = delegate_loop_proxy_->PostTask( | 746 bool posted = delegate_loop_proxy_->PostTask( |
| 747 FROM_HERE, | 747 FROM_HERE, |
| 748 NewRunnableMethod(this, | 748 base::Bind(&Core::OnCompletedURLRequest, this, |
| 749 &Core::OnCompletedURLRequest, | 749 backoff_delay)); |
| 750 backoff_delay)); | |
| 751 | 750 |
| 752 // If the delegate message loop does not exist any more, then the delegate | 751 // If the delegate message loop does not exist any more, then the delegate |
| 753 // should be gone too. | 752 // should be gone too. |
| 754 DCHECK(posted || !delegate_); | 753 DCHECK(posted || !delegate_); |
| 755 } | 754 } |
| 756 | 755 |
| 757 void URLFetcher::Core::ReadResponse() { | 756 void URLFetcher::Core::ReadResponse() { |
| 758 // Some servers may treat HEAD requests as GET requests. To free up the | 757 // Some servers may treat HEAD requests as GET requests. To free up the |
| 759 // network connection as soon as possible, signal that the request has | 758 // network connection as soon as possible, signal that the request has |
| 760 // completed immediately, without trying to read any data back (all we care | 759 // completed immediately, without trying to read any data back (all we care |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 844 original_url_); | 843 original_url_); |
| 845 } | 844 } |
| 846 | 845 |
| 847 int64 delay = original_url_throttler_entry_->ReserveSendingTimeForNextRequest( | 846 int64 delay = original_url_throttler_entry_->ReserveSendingTimeForNextRequest( |
| 848 GetBackoffReleaseTime()); | 847 GetBackoffReleaseTime()); |
| 849 if (delay == 0) { | 848 if (delay == 0) { |
| 850 StartURLRequest(); | 849 StartURLRequest(); |
| 851 } else { | 850 } else { |
| 852 MessageLoop::current()->PostDelayedTask( | 851 MessageLoop::current()->PostDelayedTask( |
| 853 FROM_HERE, | 852 FROM_HERE, |
| 854 NewRunnableMethod(this, &Core::StartURLRequest), | 853 base::Bind(&Core::StartURLRequest, this), |
| 855 delay); | 854 delay); |
| 856 } | 855 } |
| 857 } | 856 } |
| 858 | 857 |
| 859 void URLFetcher::Core::CancelURLRequest() { | 858 void URLFetcher::Core::CancelURLRequest() { |
| 860 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 859 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 861 | 860 |
| 862 if (request_.get()) { | 861 if (request_.get()) { |
| 863 request_->Cancel(); | 862 request_->Cancel(); |
| 864 ReleaseRequest(); | 863 ReleaseRequest(); |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1111 DCHECK(core_->delegate_loop_proxy_->BelongsToCurrentThread()); | 1110 DCHECK(core_->delegate_loop_proxy_->BelongsToCurrentThread()); |
| 1112 if (core_->response_destination_ != TEMP_FILE || | 1111 if (core_->response_destination_ != TEMP_FILE || |
| 1113 !core_->temp_file_writer_.get()) | 1112 !core_->temp_file_writer_.get()) |
| 1114 return false; | 1113 return false; |
| 1115 | 1114 |
| 1116 *out_response_path = core_->temp_file_writer_->temp_file(); | 1115 *out_response_path = core_->temp_file_writer_->temp_file(); |
| 1117 | 1116 |
| 1118 if (take_ownership) { | 1117 if (take_ownership) { |
| 1119 core_->io_message_loop_proxy_->PostTask( | 1118 core_->io_message_loop_proxy_->PostTask( |
| 1120 FROM_HERE, | 1119 FROM_HERE, |
| 1121 NewRunnableMethod(core_.get(), &Core::DisownTempFile)); | 1120 base::Bind(&Core::DisownTempFile, core_)); |
| 1122 } | 1121 } |
| 1123 return true; | 1122 return true; |
| 1124 } | 1123 } |
| 1125 | 1124 |
| 1126 // static | 1125 // static |
| 1127 void URLFetcher::CancelAll() { | 1126 void URLFetcher::CancelAll() { |
| 1128 Core::CancelAll(); | 1127 Core::CancelAll(); |
| 1129 } | 1128 } |
| 1130 | 1129 |
| 1131 // static | 1130 // static |
| 1132 int URLFetcher::GetNumFetcherCores() { | 1131 int URLFetcher::GetNumFetcherCores() { |
| 1133 return Core::g_registry.Get().size(); | 1132 return Core::g_registry.Get().size(); |
| 1134 } | 1133 } |
| 1135 | 1134 |
| 1136 URLFetcher::Delegate* URLFetcher::delegate() const { | 1135 URLFetcher::Delegate* URLFetcher::delegate() const { |
| 1137 return core_->delegate(); | 1136 return core_->delegate(); |
| 1138 } | 1137 } |
| OLD | NEW |