| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/chromeos/drive/drive_url_request_job.h" | 5 #include "chrome/browser/chromeos/drive/drive_url_request_job.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 // Keep track of the buffer. | 409 // Keep track of the buffer. |
| 410 DCHECK(!read_buf_); | 410 DCHECK(!read_buf_); |
| 411 read_buf_ = dest; | 411 read_buf_ = dest; |
| 412 read_buf_remaining_.set(dest->data(), dest_size); | 412 read_buf_remaining_.set(dest->data(), dest_size); |
| 413 | 413 |
| 414 bool rc = false; | 414 bool rc = false; |
| 415 if (streaming_download_) | 415 if (streaming_download_) |
| 416 rc = ContinueReadFromDownloadData(bytes_read); | 416 rc = ContinueReadFromDownloadData(bytes_read); |
| 417 else | 417 else |
| 418 rc = ContinueReadFromFile(bytes_read); | 418 rc = ContinueReadFromFile(bytes_read); |
| 419 |
| 419 DVLOG(1) << "ReadRawData: out with " | 420 DVLOG(1) << "ReadRawData: out with " |
| 420 << (rc ? "has" : "no") | 421 << (rc ? "has" : "no") |
| 421 << "_data, bytes_read=" << *bytes_read | 422 << "_data, bytes_read=" << *bytes_read |
| 422 << ", buf_remaining=" | 423 << ", buf_remaining=" |
| 423 << (read_buf_ ? read_buf_remaining_.size() : 0) | 424 << (read_buf_ ? read_buf_remaining_.size() : 0) |
| 424 << ", " << (streaming_download_ ? "download" : "file") | 425 << ", " << (streaming_download_ ? "download" : "file") |
| 425 << "_remaining=" << remaining_bytes_; | 426 << "_remaining=" << remaining_bytes_; |
| 426 return rc; | 427 return rc; |
| 427 } | 428 } |
| 428 | 429 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 | 612 |
| 612 // Otherwise, we have data in read buffer, return true with number of bytes | 613 // Otherwise, we have data in read buffer, return true with number of bytes |
| 613 // read. | 614 // read. |
| 614 *bytes_read = BytesReadCompleted(); | 615 *bytes_read = BytesReadCompleted(); |
| 615 DVLOG(1) << "Has data: bytes_read=" << *bytes_read | 616 DVLOG(1) << "Has data: bytes_read=" << *bytes_read |
| 616 << ", buf_remaining=0, file_remaining=" << remaining_bytes_; | 617 << ", buf_remaining=0, file_remaining=" << remaining_bytes_; |
| 617 return true; | 618 return true; |
| 618 } | 619 } |
| 619 | 620 |
| 620 void DriveURLRequestJob::ReadFromFile() { | 621 void DriveURLRequestJob::ReadFromFile() { |
| 621 int bytes_to_read = std::min(read_buf_remaining_.size(), | |
| 622 static_cast<size_t>(remaining_bytes_)); | |
| 623 | |
| 624 // If the stream already exists, keep reading from it. | 622 // If the stream already exists, keep reading from it. |
| 625 if (stream_.get()) { | 623 if (stream_.get()) { |
| 626 ReadFileStream(bytes_to_read); | 624 ReadFileStream(); |
| 627 return; | 625 return; |
| 628 } | 626 } |
| 629 | 627 |
| 630 // Otherwise, open the stream for file. | 628 // Otherwise, open the stream for file. |
| 631 stream_.reset(new net::FileStream(NULL)); | 629 stream_.reset(new net::FileStream(NULL)); |
| 632 int result = stream_->Open( | 630 int result = stream_->Open( |
| 633 local_file_path_, | 631 local_file_path_, |
| 634 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | | 632 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | |
| 635 base::PLATFORM_FILE_ASYNC, | 633 base::PLATFORM_FILE_ASYNC, |
| 636 base::Bind(&DriveURLRequestJob::OnFileOpen, | 634 base::Bind(&DriveURLRequestJob::OnFileOpen, |
| 637 weak_ptr_factory_.GetWeakPtr(), | 635 weak_ptr_factory_.GetWeakPtr())); |
| 638 bytes_to_read)); | |
| 639 | 636 |
| 640 if (result == net::ERR_IO_PENDING) { | 637 if (result == net::ERR_IO_PENDING) { |
| 641 DVLOG(1) << "IO is pending for opening " | 638 DVLOG(1) << "IO is pending for opening " |
| 642 << local_file_path_.BaseName().RemoveExtension().value(); | 639 << local_file_path_.BaseName().RemoveExtension().value(); |
| 643 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | 640 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 644 } else { | 641 } else { |
| 645 DCHECK_NE(result, net::OK); | 642 DCHECK_NE(result, net::OK); |
| 646 LOG(WARNING) << "Failed to open " << local_file_path_.value(); | 643 LOG(WARNING) << "Failed to open " << local_file_path_.value(); |
| 647 NotifyFailure(net::ERR_FILE_NOT_FOUND); | 644 NotifyFailure(net::ERR_FILE_NOT_FOUND); |
| 648 } | 645 } |
| 649 } | 646 } |
| 650 | 647 |
| 651 void DriveURLRequestJob::OnFileOpen(int bytes_to_read, int open_result) { | 648 void DriveURLRequestJob::OnFileOpen(int open_result) { |
| 652 if (open_result != net::OK) { | 649 if (open_result != net::OK) { |
| 653 LOG(WARNING) << "Failed to open " << local_file_path_.value(); | 650 LOG(WARNING) << "Failed to open " << local_file_path_.value(); |
| 654 NotifyFailure(net::ERR_FILE_NOT_FOUND); | 651 NotifyFailure(net::ERR_FILE_NOT_FOUND); |
| 655 return; | 652 return; |
| 656 } | 653 } |
| 657 | 654 |
| 658 DVLOG(1) << "Successfully opened " << local_file_path_.value(); | 655 DVLOG(1) << "Successfully opened " << local_file_path_.value(); |
| 659 | 656 |
| 660 // Read from opened file stream. | 657 // Read from opened file stream. |
| 661 DCHECK(stream_.get()); | 658 DCHECK(stream_.get()); |
| 662 ReadFileStream(bytes_to_read); | 659 ReadFileStream(); |
| 663 } | 660 } |
| 664 | 661 |
| 665 void DriveURLRequestJob::ReadFileStream(int bytes_to_read) { | 662 void DriveURLRequestJob::ReadFileStream() { |
| 666 DCHECK(stream_.get()); | 663 DCHECK(stream_.get()); |
| 667 DCHECK(stream_->IsOpen()); | 664 DCHECK(stream_->IsOpen()); |
| 668 DCHECK_GE(static_cast<int>(read_buf_remaining_.size()), bytes_to_read); | |
| 669 | 665 |
| 666 int bytes_to_read = std::min( |
| 667 read_buf_remaining_.size(), static_cast<size_t>(remaining_bytes_)); |
| 670 int result = stream_->Read(read_buf_, bytes_to_read, | 668 int result = stream_->Read(read_buf_, bytes_to_read, |
| 671 base::Bind(&DriveURLRequestJob::OnReadFileStream, | 669 base::Bind(&DriveURLRequestJob::OnReadFileStream, |
| 672 weak_ptr_factory_.GetWeakPtr())); | 670 weak_ptr_factory_.GetWeakPtr())); |
| 673 | 671 |
| 674 // If IO is pending, we just need to wait. | 672 // If IO is pending, we just need to wait. |
| 675 if (result == net::ERR_IO_PENDING) { | 673 if (result == net::ERR_IO_PENDING) { |
| 676 DVLOG(1) << "IO pending: bytes_to_read=" << bytes_to_read | 674 DVLOG(1) << "IO pending: bytes_to_read=" << bytes_to_read |
| 677 << ", buf_remaining=" << read_buf_remaining_.size() | 675 << ", buf_remaining=" << read_buf_remaining_.size() |
| 678 << ", file_remaining=" << remaining_bytes_; | 676 << ", file_remaining=" << remaining_bytes_; |
| 679 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | 677 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 680 } else { // For all other errors, bail out. | 678 } else { // For all other errors, bail out. |
| 681 // Asynchronous read should not return result >= 0; | 679 // Asynchronous read should not return result >= 0; |
| 682 // refer to net/base/file_stream_posix.cc. | 680 // refer to net/base/file_stream_posix.cc. |
| 683 DCHECK(result < 0); | 681 DCHECK(result < 0); |
| 684 LOG(WARNING) << "Failed to read " << local_file_path_.value(); | 682 LOG(WARNING) << "Failed to read " << local_file_path_.value(); |
| 685 NotifyFailure(net::ERR_FAILED); | 683 NotifyFailure(net::ERR_FAILED); |
| 686 } | 684 } |
| 687 } | 685 } |
| 688 | 686 |
| 689 void DriveURLRequestJob::OnReadFileStream(int bytes_read) { | 687 void DriveURLRequestJob::OnReadFileStream(int bytes_read) { |
| 690 if (bytes_read <= 0) { | 688 if (bytes_read <= 0) { |
| 691 LOG(WARNING) << "Failed to read " << local_file_path_.value(); | 689 LOG(WARNING) << "Failed to read " << local_file_path_.value(); |
| 692 NotifyFailure(net::ERR_FAILED); | 690 NotifyFailure(net::ERR_FAILED); |
| 693 return; | 691 return; |
| 694 } | 692 } |
| 695 | 693 |
| 696 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status. | 694 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status. |
| 697 | |
| 698 RecordBytesRead(bytes_read); | 695 RecordBytesRead(bytes_read); |
| 699 | |
| 700 DVLOG(1) << "Cleared IO pending: bytes_read=" << bytes_read | 696 DVLOG(1) << "Cleared IO pending: bytes_read=" << bytes_read |
| 701 << ", buf_remaining=" << read_buf_remaining_.size() | 697 << ", buf_remaining=" << read_buf_remaining_.size() |
| 702 << ", file_remaining=" << remaining_bytes_; | 698 << ", file_remaining=" << remaining_bytes_; |
| 703 | 699 |
| 704 // If the read buffer is completely filled, we're done. | 700 // Regardless of whether the buffer is still remaining or not, |
| 705 if (read_buf_remaining_.empty()) { | 701 // notify the client that some data is available in the buffer. |
| 706 int bytes_read = BytesReadCompleted(); | 702 BytesReadCompleted(); // Release |read_buf_|. |
| 707 DVLOG(1) << "Completed read: bytes_read=" << bytes_read | 703 DVLOG(1) << "Completed read: bytes_read=" << bytes_read |
| 708 << ", file_remaining=" << remaining_bytes_; | 704 << ", file_remaining=" << remaining_bytes_; |
| 709 NotifyReadComplete(bytes_read); | 705 NotifyReadComplete(bytes_read); |
| 710 return; | |
| 711 } | |
| 712 | |
| 713 // Otherwise, continue the reading. | |
| 714 int new_bytes_read = 0; | |
| 715 if (ContinueReadFromFile(&new_bytes_read)) { | |
| 716 DVLOG(1) << "Completed read: bytes_read=" << bytes_read | |
| 717 << ", file_remaining=" << remaining_bytes_; | |
| 718 NotifyReadComplete(new_bytes_read); | |
| 719 } | |
| 720 } | 706 } |
| 721 | 707 |
| 722 int DriveURLRequestJob::BytesReadCompleted() { | 708 int DriveURLRequestJob::BytesReadCompleted() { |
| 723 int bytes_read = read_buf_remaining_.data() - read_buf_->data(); | 709 int bytes_read = read_buf_remaining_.data() - read_buf_->data(); |
| 724 read_buf_ = NULL; | 710 read_buf_ = NULL; |
| 725 read_buf_remaining_.clear(); | 711 read_buf_remaining_.clear(); |
| 726 return bytes_read; | 712 return bytes_read; |
| 727 } | 713 } |
| 728 | 714 |
| 729 void DriveURLRequestJob::RecordBytesRead(int bytes_read) { | 715 void DriveURLRequestJob::RecordBytesRead(int bytes_read) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 807 response_info_.reset(new net::HttpResponseInfo()); | 793 response_info_.reset(new net::HttpResponseInfo()); |
| 808 response_info_->headers = headers; | 794 response_info_->headers = headers; |
| 809 | 795 |
| 810 set_expected_content_size(remaining_bytes_); | 796 set_expected_content_size(remaining_bytes_); |
| 811 headers_set_ = true; | 797 headers_set_ = true; |
| 812 | 798 |
| 813 NotifyHeadersComplete(); | 799 NotifyHeadersComplete(); |
| 814 } | 800 } |
| 815 | 801 |
| 816 } // namespace drive | 802 } // namespace drive |
| OLD | NEW |