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" |
11 #include "chrome/browser/google_apis/gdata_wapi_url_generator.h" | 11 #include "chrome/browser/google_apis/gdata_wapi_url_generator.h" |
12 #include "chrome/browser/google_apis/time_util.h" | 12 #include "chrome/browser/google_apis/time_util.h" |
13 #include "chrome/common/net/url_util.h" | 13 #include "chrome/common/net/url_util.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
16 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
17 #include "net/http/http_util.h" | 17 #include "net/http/http_util.h" |
18 #include "third_party/libxml/chromium/libxml_utils.h" | 18 #include "third_party/libxml/chromium/libxml_utils.h" |
19 | 19 |
| 20 using content::BrowserThread; |
20 using net::URLFetcher; | 21 using net::URLFetcher; |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // etag matching header. | 25 // etag matching header. |
25 const char kIfMatchAllHeader[] = "If-Match: *"; | 26 const char kIfMatchAllHeader[] = "If-Match: *"; |
26 const char kIfMatchHeaderFormat[] = "If-Match: %s"; | 27 const char kIfMatchHeaderFormat[] = "If-Match: %s"; |
27 | 28 |
28 const char kUploadContentRange[] = "Content-Range: bytes "; | 29 const char kUploadContentRange[] = "Content-Range: bytes "; |
29 const char kUploadContentType[] = "X-Upload-Content-Type: "; | 30 const char kUploadContentType[] = "X-Upload-Content-Type: "; |
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 initiate_upload_url_(chrome_common_net::AppendOrReplaceQueryParameter( | 569 initiate_upload_url_(chrome_common_net::AppendOrReplaceQueryParameter( |
569 params.upload_location, | 570 params.upload_location, |
570 kUploadParamConvertKey, | 571 kUploadParamConvertKey, |
571 kUploadParamConvertValue)) { | 572 kUploadParamConvertValue)) { |
572 DCHECK(!callback_.is_null()); | 573 DCHECK(!callback_.is_null()); |
573 } | 574 } |
574 | 575 |
575 InitiateUploadOperation::~InitiateUploadOperation() {} | 576 InitiateUploadOperation::~InitiateUploadOperation() {} |
576 | 577 |
577 GURL InitiateUploadOperation::GetURL() const { | 578 GURL InitiateUploadOperation::GetURL() const { |
578 return initiate_upload_url_; | 579 return GDataWapiUrlGenerator::AddStandardUrlParams(initiate_upload_url_); |
579 } | 580 } |
580 | 581 |
581 void InitiateUploadOperation::ProcessURLFetchResults( | 582 void InitiateUploadOperation::ProcessURLFetchResults( |
582 const URLFetcher* source) { | 583 const URLFetcher* source) { |
583 GDataErrorCode code = GetErrorCode(source); | 584 GDataErrorCode code = GetErrorCode(source); |
584 | 585 |
585 std::string upload_location; | 586 std::string upload_location; |
586 if (code == HTTP_SUCCESS) { | 587 if (code == HTTP_SUCCESS) { |
587 // Retrieve value of the first "Location" header. | 588 // Retrieve value of the first "Location" header. |
588 source->GetResponseHeaders()->EnumerateHeader(NULL, | 589 source->GetResponseHeaders()->EnumerateHeader(NULL, |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 OperationRegistry* registry, | 665 OperationRegistry* registry, |
665 net::URLRequestContextGetter* url_request_context_getter, | 666 net::URLRequestContextGetter* url_request_context_getter, |
666 const ResumeUploadCallback& callback, | 667 const ResumeUploadCallback& callback, |
667 const ResumeUploadParams& params) | 668 const ResumeUploadParams& params) |
668 : UrlFetchOperationBase(registry, | 669 : UrlFetchOperationBase(registry, |
669 url_request_context_getter, | 670 url_request_context_getter, |
670 OPERATION_UPLOAD, | 671 OPERATION_UPLOAD, |
671 params.drive_file_path), | 672 params.drive_file_path), |
672 callback_(callback), | 673 callback_(callback), |
673 params_(params), | 674 params_(params), |
674 last_chunk_completed_(false) { | 675 last_chunk_completed_(false), |
| 676 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
675 DCHECK(!callback_.is_null()); | 677 DCHECK(!callback_.is_null()); |
676 } | 678 } |
677 | 679 |
678 ResumeUploadOperation::~ResumeUploadOperation() {} | 680 ResumeUploadOperation::~ResumeUploadOperation() {} |
679 | 681 |
680 GURL ResumeUploadOperation::GetURL() const { | 682 GURL ResumeUploadOperation::GetURL() const { |
| 683 // This is very tricky to get json from this operation. To do that, &alt=json |
| 684 // has to be appended not here but in InitiateUploadOperation::GetURL(). |
681 return params_.upload_location; | 685 return params_.upload_location; |
682 } | 686 } |
683 | 687 |
684 void ResumeUploadOperation::ProcessURLFetchResults(const URLFetcher* source) { | 688 void ResumeUploadOperation::ProcessURLFetchResults(const URLFetcher* source) { |
685 GDataErrorCode code = GetErrorCode(source); | 689 GDataErrorCode code = GetErrorCode(source); |
686 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); | 690 net::HttpResponseHeaders* hdrs = source->GetResponseHeaders(); |
687 int64 start_position_received = -1; | |
688 int64 end_position_received = -1; | |
689 scoped_ptr<ResourceEntry> entry; | |
690 | 691 |
691 if (code == HTTP_RESUME_INCOMPLETE) { | 692 if (code == HTTP_RESUME_INCOMPLETE) { |
692 // Retrieve value of the first "Range" header. | 693 // Retrieve value of the first "Range" header. |
| 694 int64 start_position_received = -1; |
| 695 int64 end_position_received = -1; |
693 std::string range_received; | 696 std::string range_received; |
694 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); | 697 hdrs->EnumerateHeader(NULL, kUploadResponseRange, &range_received); |
695 if (!range_received.empty()) { // Parse the range header. | 698 if (!range_received.empty()) { // Parse the range header. |
696 std::vector<net::HttpByteRange> ranges; | 699 std::vector<net::HttpByteRange> ranges; |
697 if (net::HttpUtil::ParseRangeHeader(range_received, &ranges) && | 700 if (net::HttpUtil::ParseRangeHeader(range_received, &ranges) && |
698 !ranges.empty() ) { | 701 !ranges.empty() ) { |
699 // We only care about the first start-end pair in the range. | 702 // We only care about the first start-end pair in the range. |
700 // | 703 // |
701 // Range header represents the range inclusively, while we are treating | 704 // Range header represents the range inclusively, while we are treating |
702 // ranges exclusively (i.e., end_position_received should be one passed | 705 // ranges exclusively (i.e., end_position_received should be one passed |
703 // the last valid index). So "+ 1" is added. | 706 // the last valid index). So "+ 1" is added. |
704 start_position_received = ranges[0].first_byte_position(); | 707 start_position_received = ranges[0].first_byte_position(); |
705 end_position_received = ranges[0].last_byte_position() + 1; | 708 end_position_received = ranges[0].last_byte_position() + 1; |
706 } | 709 } |
707 } | 710 } |
708 DVLOG(1) << "Got response for [" << params_.drive_file_path.value() | 711 DVLOG(1) << "Got response for [" << params_.drive_file_path.value() |
709 << "]: code=" << code | 712 << "]: code=" << code |
710 << ", range_hdr=[" << range_received | 713 << ", range_hdr=[" << range_received |
711 << "], range_parsed=" << start_position_received | 714 << "], range_parsed=" << start_position_received |
712 << "," << end_position_received; | 715 << "," << end_position_received; |
| 716 |
| 717 callback_.Run(ResumeUploadResponse(code, |
| 718 start_position_received, |
| 719 end_position_received), |
| 720 scoped_ptr<ResourceEntry>()); |
| 721 |
| 722 OnProcessURLFetchResultsComplete(true); |
713 } else { | 723 } else { |
714 // There might be explanation of unexpected error code in response. | 724 // There might be explanation of unexpected error code in response. |
715 std::string response_content; | 725 std::string response_content; |
716 source->GetResponseAsString(&response_content); | 726 source->GetResponseAsString(&response_content); |
717 DVLOG(1) << "Got response for [" << params_.drive_file_path.value() | 727 DVLOG(1) << "Got response for [" << params_.drive_file_path.value() |
718 << "]: code=" << code | 728 << "]: code=" << code |
719 << ", content=[\n" << response_content << "\n]"; | 729 << ", content=[\n" << response_content << "\n]"; |
720 | 730 |
721 // Parse entry XML. | 731 ParseJson(response_content, |
722 XmlReader xml_reader; | 732 base::Bind(&ResumeUploadOperation::OnDataParsed, |
723 if (xml_reader.Load(response_content)) { | 733 weak_ptr_factory_.GetWeakPtr(), |
724 while (xml_reader.Read()) { | 734 code)); |
725 if (xml_reader.NodeName() == ResourceEntry::GetEntryNodeName()) { | |
726 entry = ResourceEntry::CreateFromXml(&xml_reader); | |
727 break; | |
728 } | |
729 } | |
730 } | |
731 if (!entry.get()) | |
732 LOG(WARNING) << "Invalid entry received on upload:\n" << response_content; | |
733 } | 735 } |
| 736 } |
734 | 737 |
735 callback_.Run(ResumeUploadResponse(code, | 738 void ResumeUploadOperation::OnDataParsed(GDataErrorCode code, |
736 start_position_received, | 739 scoped_ptr<base::Value> value) { |
737 end_position_received), | 740 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
738 entry.Pass()); | |
739 | 741 |
740 // For a new file, HTTP_CREATED is returned. | 742 // For a new file, HTTP_CREATED is returned. |
741 // For an existing file, HTTP_SUCCESS is returned. | 743 // For an existing file, HTTP_SUCCESS is returned. |
742 if ((params_.upload_mode == UPLOAD_NEW_FILE && code == HTTP_CREATED) || | 744 if ((params_.upload_mode == UPLOAD_NEW_FILE && code == HTTP_CREATED) || |
743 (params_.upload_mode == UPLOAD_EXISTING_FILE && code == HTTP_SUCCESS)) { | 745 (params_.upload_mode == UPLOAD_EXISTING_FILE && code == HTTP_SUCCESS)) { |
744 last_chunk_completed_ = true; | 746 last_chunk_completed_ = true; |
745 } | 747 } |
746 | 748 |
747 OnProcessURLFetchResultsComplete( | 749 scoped_ptr<ResourceEntry> entry; |
748 last_chunk_completed_ || code == HTTP_RESUME_INCOMPLETE); | 750 if (value.get()) |
| 751 entry = ResourceEntry::ExtractAndParse(*(value.get())); |
| 752 |
| 753 if (!entry.get()) |
| 754 LOG(WARNING) << "Invalid entry received on upload."; |
| 755 |
| 756 callback_.Run(ResumeUploadResponse(code, -1, -1), entry.Pass()); |
| 757 OnProcessURLFetchResultsComplete(last_chunk_completed_); |
749 } | 758 } |
750 | 759 |
751 void ResumeUploadOperation::NotifyStartToOperationRegistry() { | 760 void ResumeUploadOperation::NotifyStartToOperationRegistry() { |
752 NotifyResume(); | 761 NotifyResume(); |
753 } | 762 } |
754 | 763 |
755 void ResumeUploadOperation::NotifySuccessToOperationRegistry() { | 764 void ResumeUploadOperation::NotifySuccessToOperationRegistry() { |
756 if (last_chunk_completed_) | 765 if (last_chunk_completed_) |
757 NotifyFinish(OPERATION_COMPLETED); | 766 NotifyFinish(OPERATION_COMPLETED); |
758 else | 767 else |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
804 return true; | 813 return true; |
805 } | 814 } |
806 | 815 |
807 void ResumeUploadOperation::OnURLFetchUploadProgress( | 816 void ResumeUploadOperation::OnURLFetchUploadProgress( |
808 const URLFetcher* source, int64 current, int64 total) { | 817 const URLFetcher* source, int64 current, int64 total) { |
809 // Adjust the progress values according to the range currently uploaded. | 818 // Adjust the progress values according to the range currently uploaded. |
810 NotifyProgress(params_.start_position + current, params_.content_length); | 819 NotifyProgress(params_.start_position + current, params_.content_length); |
811 } | 820 } |
812 | 821 |
813 } // namespace google_apis | 822 } // namespace google_apis |
OLD | NEW |