| 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 "chrome/browser/google_apis/gdata_wapi_operations.h" | 5 #include "chrome/browser/google_apis/gdata_wapi_operations.h" |
| 6 | 6 |
| 7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/google_apis/gdata_wapi_parser.h" | 10 #include "chrome/browser/google_apis/gdata_wapi_parser.h" |
| (...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 return URLFetcher::PUT; | 627 return URLFetcher::PUT; |
| 628 } | 628 } |
| 629 | 629 |
| 630 void UploadRangeOperationBase::ProcessURLFetchResults( | 630 void UploadRangeOperationBase::ProcessURLFetchResults( |
| 631 const URLFetcher* source) { | 631 const URLFetcher* source) { |
| 632 GDataErrorCode code = GetErrorCode(source); | 632 GDataErrorCode code = GetErrorCode(source); |
| 633 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); | 633 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); |
| 634 | 634 |
| 635 if (code == HTTP_RESUME_INCOMPLETE) { | 635 if (code == HTTP_RESUME_INCOMPLETE) { |
| 636 // Retrieve value of the first "Range" header. | 636 // Retrieve value of the first "Range" header. |
| 637 int64 start_position_received = -1; | 637 // The Range header is appeared only if there is at least one received |
| 638 int64 end_position_received = -1; | 638 // byte. So, initialize the positions by 0 so that the [0,0) will be |
| 639 // returned via the |callback_| for empty data case. |
| 640 int64 start_position_received = 0; |
| 641 int64 end_position_received = 0; |
| 639 std::string range_received; | 642 std::string range_received; |
| 640 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); | 643 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); |
| 641 if (!range_received.empty()) { // Parse the range header. | 644 if (!range_received.empty()) { // Parse the range header. |
| 642 std::vector<net::HttpByteRange> ranges; | 645 std::vector<net::HttpByteRange> ranges; |
| 643 if (net::HttpUtil::ParseRangeHeader(range_received, &ranges) && | 646 if (net::HttpUtil::ParseRangeHeader(range_received, &ranges) && |
| 644 !ranges.empty() ) { | 647 !ranges.empty() ) { |
| 645 // We only care about the first start-end pair in the range. | 648 // We only care about the first start-end pair in the range. |
| 646 // | 649 // |
| 647 // Range header represents the range inclusively, while we are treating | 650 // Range header represents the range inclusively, while we are treating |
| 648 // ranges exclusively (i.e., end_position_received should be one passed | 651 // ranges exclusively (i.e., end_position_received should be one passed |
| 649 // the last valid index). So "+ 1" is added. | 652 // the last valid index). So "+ 1" is added. |
| 650 start_position_received = ranges[0].first_byte_position(); | 653 start_position_received = ranges[0].first_byte_position(); |
| 651 end_position_received = ranges[0].last_byte_position() + 1; | 654 end_position_received = ranges[0].last_byte_position() + 1; |
| 652 } | 655 } |
| 653 } | 656 } |
| 657 // The Range header has the received data range, so the start position |
| 658 // should be always 0. |
| 659 DCHECK_EQ(start_position_received, 0); |
| 654 DVLOG(1) << "Got response for [" << drive_file_path_.value() | 660 DVLOG(1) << "Got response for [" << drive_file_path_.value() |
| 655 << "]: code=" << code | 661 << "]: code=" << code |
| 656 << ", range_hdr=[" << range_received | 662 << ", range_hdr=[" << range_received |
| 657 << "], range_parsed=" << start_position_received | 663 << "], range_parsed=" << start_position_received |
| 658 << "," << end_position_received; | 664 << "," << end_position_received; |
| 659 | 665 |
| 660 callback_.Run(UploadRangeResponse(code, | 666 callback_.Run(UploadRangeResponse(code, |
| 661 start_position_received, | 667 start_position_received, |
| 662 end_position_received), | 668 end_position_received), |
| 663 scoped_ptr<ResourceEntry>()); | 669 scoped_ptr<ResourceEntry>()); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 744 // For uploading an empty document, just PUT an empty content. | 750 // For uploading an empty document, just PUT an empty content. |
| 745 DCHECK_EQ(start_position_, 0); | 751 DCHECK_EQ(start_position_, 0); |
| 746 DCHECK_EQ(end_position_, 0); | 752 DCHECK_EQ(end_position_, 0); |
| 747 return std::vector<std::string>(); | 753 return std::vector<std::string>(); |
| 748 } | 754 } |
| 749 | 755 |
| 750 // The header looks like | 756 // The header looks like |
| 751 // Content-Range: bytes <start_position>-<end_position>/<content_length> | 757 // Content-Range: bytes <start_position>-<end_position>/<content_length> |
| 752 // for example: | 758 // for example: |
| 753 // Content-Range: bytes 7864320-8388607/13851821 | 759 // Content-Range: bytes 7864320-8388607/13851821 |
| 754 // Use * for unknown/streaming content length. | |
| 755 // The header takes inclusive range, so we adjust by "end_position - 1". | 760 // The header takes inclusive range, so we adjust by "end_position - 1". |
| 756 DCHECK_GE(start_position_, 0); | 761 DCHECK_GE(start_position_, 0); |
| 757 DCHECK_GT(end_position_, 0); | 762 DCHECK_GT(end_position_, 0); |
| 758 DCHECK_GE(content_length_, -1); | 763 DCHECK_GE(content_length_, 0); |
| 759 | 764 |
| 760 std::vector<std::string> headers; | 765 std::vector<std::string> headers; |
| 761 headers.push_back( | 766 headers.push_back( |
| 762 std::string(kUploadContentRange) + | 767 std::string(kUploadContentRange) + |
| 763 base::Int64ToString(start_position_) + "-" + | 768 base::Int64ToString(start_position_) + "-" + |
| 764 base::Int64ToString(end_position_ - 1) + "/" + | 769 base::Int64ToString(end_position_ - 1) + "/" + |
| 765 (content_length_ == -1 ? "*" : | 770 base::Int64ToString(content_length_)); |
| 766 base::Int64ToString(content_length_))); | |
| 767 return headers; | 771 return headers; |
| 768 } | 772 } |
| 769 | 773 |
| 770 bool ResumeUploadOperation::GetContentData(std::string* upload_content_type, | 774 bool ResumeUploadOperation::GetContentData(std::string* upload_content_type, |
| 771 std::string* upload_content) { | 775 std::string* upload_content) { |
| 772 *upload_content_type = content_type_; | 776 *upload_content_type = content_type_; |
| 773 *upload_content = std::string(buf_->data(), end_position_ - start_position_); | 777 *upload_content = std::string(buf_->data(), end_position_ - start_position_); |
| 774 return true; | 778 return true; |
| 775 } | 779 } |
| 776 | 780 |
| 777 void ResumeUploadOperation::OnURLFetchUploadProgress( | 781 void ResumeUploadOperation::OnURLFetchUploadProgress( |
| 778 const URLFetcher* source, int64 current, int64 total) { | 782 const URLFetcher* source, int64 current, int64 total) { |
| 779 // Adjust the progress values according to the range currently uploaded. | 783 // Adjust the progress values according to the range currently uploaded. |
| 780 NotifyProgress(start_position_ + current, content_length_); | 784 NotifyProgress(start_position_ + current, content_length_); |
| 781 } | 785 } |
| 782 | 786 |
| 787 //========================== GetUploadStatusOperation ========================== |
| 788 |
| 789 GetUploadStatusOperation::GetUploadStatusOperation( |
| 790 OperationRegistry* registry, |
| 791 net::URLRequestContextGetter* url_request_context_getter, |
| 792 const UploadRangeCallback& callback, |
| 793 UploadMode upload_mode, |
| 794 const FilePath& drive_file_path, |
| 795 const GURL& upload_url, |
| 796 int64 content_length) |
| 797 : UploadRangeOperationBase(registry, |
| 798 url_request_context_getter, |
| 799 callback, |
| 800 upload_mode, |
| 801 drive_file_path, |
| 802 upload_url), |
| 803 content_length_(content_length) {} |
| 804 |
| 805 GetUploadStatusOperation::~GetUploadStatusOperation() {} |
| 806 |
| 807 std::vector<std::string> |
| 808 GetUploadStatusOperation::GetExtraRequestHeaders() const { |
| 809 // The header looks like |
| 810 // Content-Range: bytes */<content_length> |
| 811 // for example: |
| 812 // Content-Range: bytes */13851821 |
| 813 DCHECK_GE(content_length_, 0); |
| 814 |
| 815 std::vector<std::string> headers; |
| 816 headers.push_back( |
| 817 std::string(kUploadContentRange) + "*/" + |
| 818 base::Int64ToString(content_length_)); |
| 819 return headers; |
| 820 } |
| 821 |
| 783 } // namespace google_apis | 822 } // namespace google_apis |
| OLD | NEW |