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 "google_apis/drive/base_requests.h" | 5 #include "google_apis/drive/base_requests.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 | 554 |
555 void UrlFetchRequestBase::OnAuthFailed(DriveApiErrorCode code) { | 555 void UrlFetchRequestBase::OnAuthFailed(DriveApiErrorCode code) { |
556 CompleteRequestWithError(code); | 556 CompleteRequestWithError(code); |
557 } | 557 } |
558 | 558 |
559 base::WeakPtr<AuthenticatedRequestInterface> | 559 base::WeakPtr<AuthenticatedRequestInterface> |
560 UrlFetchRequestBase::GetWeakPtr() { | 560 UrlFetchRequestBase::GetWeakPtr() { |
561 return weak_ptr_factory_.GetWeakPtr(); | 561 return weak_ptr_factory_.GetWeakPtr(); |
562 } | 562 } |
563 | 563 |
564 //============================ BatchableRequestBase ============================ | |
565 | |
566 net::URLFetcher::RequestType BatchableRequestBase::GetRequestType() const { | |
567 return UrlFetchRequestBase::GetRequestType(); | |
568 } | |
569 | |
570 std::vector<std::string> BatchableRequestBase::GetExtraRequestHeaders() const { | |
571 return UrlFetchRequestBase::GetExtraRequestHeaders(); | |
572 } | |
573 | |
574 void BatchableRequestBase::Prepare(const PrepareCallback& callback) { | |
575 return UrlFetchRequestBase::Prepare(callback); | |
576 } | |
577 | |
578 bool BatchableRequestBase::GetContentData( | |
579 std::string* upload_content_type, std::string* upload_content) { | |
580 return UrlFetchRequestBase::GetContentData( | |
581 upload_content_type, upload_content); | |
582 } | |
583 | |
584 void BatchableRequestBase::ProcessURLFetchResults( | |
585 const net::URLFetcher* source) { | |
586 ProcessURLFetchResults(GetErrorCode(), response_writer()->data()); | |
587 } | |
588 | |
589 //============================ EntryActionRequest ============================ | 564 //============================ EntryActionRequest ============================ |
590 | 565 |
591 EntryActionRequest::EntryActionRequest(RequestSender* sender, | 566 EntryActionRequest::EntryActionRequest(RequestSender* sender, |
592 const EntryActionCallback& callback) | 567 const EntryActionCallback& callback) |
593 : UrlFetchRequestBase(sender), | 568 : UrlFetchRequestBase(sender), |
594 callback_(callback) { | 569 callback_(callback) { |
595 DCHECK(!callback_.is_null()); | 570 DCHECK(!callback_.is_null()); |
596 } | 571 } |
597 | 572 |
598 EntryActionRequest::~EntryActionRequest() {} | 573 EntryActionRequest::~EntryActionRequest() {} |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
848 std::vector<std::string> headers; | 823 std::vector<std::string> headers; |
849 headers.push_back( | 824 headers.push_back( |
850 std::string(kUploadContentRange) + "*/" + | 825 std::string(kUploadContentRange) + "*/" + |
851 base::Int64ToString(content_length_)); | 826 base::Int64ToString(content_length_)); |
852 return headers; | 827 return headers; |
853 } | 828 } |
854 | 829 |
855 //========================= MultipartUploadRequestBase ======================== | 830 //========================= MultipartUploadRequestBase ======================== |
856 | 831 |
857 MultipartUploadRequestBase::MultipartUploadRequestBase( | 832 MultipartUploadRequestBase::MultipartUploadRequestBase( |
858 RequestSender* sender, | 833 base::SequencedTaskRunner* blocking_task_runner, |
859 const std::string& metadata_json, | 834 const std::string& metadata_json, |
860 const std::string& content_type, | 835 const std::string& content_type, |
861 int64 content_length, | 836 int64 content_length, |
862 const base::FilePath& local_file_path, | 837 const base::FilePath& local_file_path, |
863 const FileResourceCallback& callback, | 838 const FileResourceCallback& callback, |
864 const ProgressCallback& progress_callback) | 839 const ProgressCallback& progress_callback) |
865 : BatchableRequestBase(sender), | 840 : blocking_task_runner_(blocking_task_runner), |
866 metadata_json_(metadata_json), | 841 metadata_json_(metadata_json), |
867 content_type_(content_type), | 842 content_type_(content_type), |
868 local_path_(local_file_path), | 843 local_path_(local_file_path), |
869 callback_(callback), | 844 callback_(callback), |
870 progress_callback_(progress_callback), | 845 progress_callback_(progress_callback), |
871 weak_ptr_factory_(this) { | 846 weak_ptr_factory_(this) { |
872 DCHECK(!content_type.empty()); | 847 DCHECK(!content_type.empty()); |
873 DCHECK_GE(content_length, 0); | 848 DCHECK_GE(content_length, 0); |
874 DCHECK(!local_file_path.empty()); | 849 DCHECK(!local_file_path.empty()); |
875 DCHECK(!callback.is_null()); | 850 DCHECK(!callback.is_null()); |
876 } | 851 } |
877 | 852 |
878 MultipartUploadRequestBase::~MultipartUploadRequestBase() { | 853 MultipartUploadRequestBase::~MultipartUploadRequestBase() { |
879 } | 854 } |
880 | 855 |
| 856 std::vector<std::string> MultipartUploadRequestBase::GetExtraRequestHeaders() |
| 857 const { |
| 858 return std::vector<std::string>(); |
| 859 } |
| 860 |
881 void MultipartUploadRequestBase::Prepare(const PrepareCallback& callback) { | 861 void MultipartUploadRequestBase::Prepare(const PrepareCallback& callback) { |
882 // If the request is cancelled, the request instance will be deleted in | 862 // If the request is cancelled, the request instance will be deleted in |
883 // |UrlFetchRequestBase::Cancel| and OnPrepareUploadContent won't be called. | 863 // |UrlFetchRequestBase::Cancel| and OnPrepareUploadContent won't be called. |
884 std::string* const upload_content_type = new std::string(); | 864 std::string* const upload_content_type = new std::string(); |
885 std::string* const upload_content_data = new std::string(); | 865 std::string* const upload_content_data = new std::string(); |
886 PostTaskAndReplyWithResult( | 866 PostTaskAndReplyWithResult( |
887 blocking_task_runner(), FROM_HERE, | 867 blocking_task_runner_.get(), FROM_HERE, |
888 base::Bind(&GetMultipartContent, boundary_, metadata_json_, content_type_, | 868 base::Bind(&GetMultipartContent, boundary_, metadata_json_, content_type_, |
889 local_path_, base::Unretained(upload_content_type), | 869 local_path_, base::Unretained(upload_content_type), |
890 base::Unretained(upload_content_data)), | 870 base::Unretained(upload_content_data)), |
891 base::Bind(&MultipartUploadRequestBase::OnPrepareUploadContent, | 871 base::Bind(&MultipartUploadRequestBase::OnPrepareUploadContent, |
892 weak_ptr_factory_.GetWeakPtr(), callback, | 872 weak_ptr_factory_.GetWeakPtr(), callback, |
893 base::Owned(upload_content_type), | 873 base::Owned(upload_content_type), |
894 base::Owned(upload_content_data))); | 874 base::Owned(upload_content_data))); |
895 } | 875 } |
896 | 876 |
897 void MultipartUploadRequestBase::OnPrepareUploadContent( | 877 void MultipartUploadRequestBase::OnPrepareUploadContent( |
(...skipping 17 matching lines...) Expand all Loading... |
915 | 895 |
916 bool MultipartUploadRequestBase::GetContentData( | 896 bool MultipartUploadRequestBase::GetContentData( |
917 std::string* upload_content_type, | 897 std::string* upload_content_type, |
918 std::string* upload_content_data) { | 898 std::string* upload_content_data) { |
919 // TODO(hirono): Pass stream instead of actual data to reduce memory usage. | 899 // TODO(hirono): Pass stream instead of actual data to reduce memory usage. |
920 upload_content_type->swap(upload_content_type_); | 900 upload_content_type->swap(upload_content_type_); |
921 upload_content_data->swap(upload_content_data_); | 901 upload_content_data->swap(upload_content_data_); |
922 return true; | 902 return true; |
923 } | 903 } |
924 | 904 |
925 void MultipartUploadRequestBase::ProcessURLFetchResults( | 905 void MultipartUploadRequestBase::NotifyResult( |
926 DriveApiErrorCode code, const std::string& body) { | 906 DriveApiErrorCode code, |
| 907 const std::string& body, |
| 908 const base::Closure& notify_complete_callback) { |
927 // The upload is successfully done. Parse the response which should be | 909 // The upload is successfully done. Parse the response which should be |
928 // the entry's metadata. | 910 // the entry's metadata. |
929 if (code == HTTP_CREATED || code == HTTP_SUCCESS) { | 911 if (code == HTTP_CREATED || code == HTTP_SUCCESS) { |
930 ParseJsonOnBlockingPool( | 912 ParseJsonOnBlockingPool( |
931 blocking_task_runner(), body, | 913 blocking_task_runner_.get(), body, |
932 base::Bind(&MultipartUploadRequestBase::OnDataParsed, | 914 base::Bind(&MultipartUploadRequestBase::OnDataParsed, |
933 weak_ptr_factory_.GetWeakPtr(), code)); | 915 weak_ptr_factory_.GetWeakPtr(), code, |
| 916 notify_complete_callback)); |
934 } else { | 917 } else { |
935 OnDataParsed(code, scoped_ptr<base::Value>()); | 918 NotifyError(code); |
| 919 notify_complete_callback.Run(); |
936 } | 920 } |
937 } | 921 } |
938 | 922 |
939 void MultipartUploadRequestBase::RunCallbackOnPrematureFailure( | 923 void MultipartUploadRequestBase::NotifyError(DriveApiErrorCode code) { |
940 DriveApiErrorCode code) { | |
941 callback_.Run(code, scoped_ptr<FileResource>()); | 924 callback_.Run(code, scoped_ptr<FileResource>()); |
942 } | 925 } |
943 | 926 |
944 void MultipartUploadRequestBase::OnURLFetchUploadProgress( | 927 void MultipartUploadRequestBase::NotifyUploadProgress( |
945 const net::URLFetcher* source, | 928 const net::URLFetcher* source, |
946 int64 current, | 929 int64 current, |
947 int64 total) { | 930 int64 total) { |
948 if (!progress_callback_.is_null()) | 931 if (!progress_callback_.is_null()) |
949 progress_callback_.Run(current, total); | 932 progress_callback_.Run(current, total); |
950 } | 933 } |
951 | 934 |
952 void MultipartUploadRequestBase::OnDataParsed(DriveApiErrorCode code, | 935 void MultipartUploadRequestBase::OnDataParsed( |
953 scoped_ptr<base::Value> value) { | 936 DriveApiErrorCode code, |
954 DCHECK(CalledOnValidThread()); | 937 const base::Closure& notify_complete_callback, |
| 938 scoped_ptr<base::Value> value) { |
| 939 DCHECK(thread_checker_.CalledOnValidThread()); |
955 if (value) | 940 if (value) |
956 callback_.Run(code, google_apis::FileResource::CreateFrom(*value)); | 941 callback_.Run(code, google_apis::FileResource::CreateFrom(*value)); |
957 else | 942 else |
958 callback_.Run(DRIVE_PARSE_ERROR, scoped_ptr<FileResource>()); | 943 NotifyError(DRIVE_PARSE_ERROR); |
959 OnProcessURLFetchResultsComplete(); | 944 notify_complete_callback.Run(); |
960 } | 945 } |
961 | 946 |
962 //============================ DownloadFileRequestBase ========================= | 947 //============================ DownloadFileRequestBase ========================= |
963 | 948 |
964 DownloadFileRequestBase::DownloadFileRequestBase( | 949 DownloadFileRequestBase::DownloadFileRequestBase( |
965 RequestSender* sender, | 950 RequestSender* sender, |
966 const DownloadActionCallback& download_action_callback, | 951 const DownloadActionCallback& download_action_callback, |
967 const GetContentCallback& get_content_callback, | 952 const GetContentCallback& get_content_callback, |
968 const ProgressCallback& progress_callback, | 953 const ProgressCallback& progress_callback, |
969 const GURL& download_url, | 954 const GURL& download_url, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1015 download_action_callback_.Run(code, temp_file); | 1000 download_action_callback_.Run(code, temp_file); |
1016 OnProcessURLFetchResultsComplete(); | 1001 OnProcessURLFetchResultsComplete(); |
1017 } | 1002 } |
1018 | 1003 |
1019 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( | 1004 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( |
1020 DriveApiErrorCode code) { | 1005 DriveApiErrorCode code) { |
1021 download_action_callback_.Run(code, base::FilePath()); | 1006 download_action_callback_.Run(code, base::FilePath()); |
1022 } | 1007 } |
1023 | 1008 |
1024 } // namespace google_apis | 1009 } // namespace google_apis |
OLD | NEW |