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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } else { | 98 } else { |
99 url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers); | 99 url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers); |
100 } | 100 } |
101 return headers; | 101 return headers; |
102 } | 102 } |
103 | 103 |
104 bool IsSuccessfulResponseCode(int response_code) { | 104 bool IsSuccessfulResponseCode(int response_code) { |
105 return 200 <= response_code && response_code <= 299; | 105 return 200 <= response_code && response_code <= 299; |
106 } | 106 } |
107 | 107 |
108 // Creates metadata JSON string for multipart uploading. | |
109 // All the values are optional. If the value is empty or null, the value does | |
110 // not appear in the metadata. | |
111 std::string CreateMultipartUploadMetadataJson( | |
112 const std::string& title, | |
113 const std::string& parent_resource_id, | |
114 const base::Time& modified_date, | |
115 const base::Time& last_viewed_by_me_date) { | |
116 base::DictionaryValue root; | |
117 if (!title.empty()) | |
118 root.SetString("title", title); | |
119 | |
120 // Fill parent link. | |
121 if (!parent_resource_id.empty()) { | |
122 scoped_ptr<base::ListValue> parents(new base::ListValue); | |
123 parents->Append( | |
124 google_apis::util::CreateParentValue(parent_resource_id).release()); | |
125 root.Set("parents", parents.release()); | |
126 } | |
127 | |
128 if (!modified_date.is_null()) | |
129 root.SetString("modifiedDate", | |
130 google_apis::util::FormatTimeAsString(modified_date)); | |
131 | |
132 if (!last_viewed_by_me_date.is_null()) { | |
133 root.SetString("lastViewedByMeDate", google_apis::util::FormatTimeAsString( | |
134 last_viewed_by_me_date)); | |
135 } | |
136 | |
137 std::string json_string; | |
138 base::JSONWriter::Write(&root, &json_string); | |
139 return json_string; | |
140 } | |
141 | |
142 // Obtains the multipart body for the metadata string and file contents. If | 108 // Obtains the multipart body for the metadata string and file contents. If |
143 // predetermined_boundary is empty, the function generates the boundary string. | 109 // predetermined_boundary is empty, the function generates the boundary string. |
144 bool GetMultipartContent(const std::string& predetermined_boundary, | 110 bool GetMultipartContent(const std::string& predetermined_boundary, |
145 const std::string& metadata_json, | 111 const std::string& metadata_json, |
146 const std::string& content_type, | 112 const std::string& content_type, |
147 const base::FilePath& path, | 113 const base::FilePath& path, |
148 std::string* upload_content_type, | 114 std::string* upload_content_type, |
149 std::string* upload_content_data) { | 115 std::string* upload_content_data) { |
150 std::string file_content; | 116 std::string file_content; |
151 if (!ReadFileToString(path, &file_content)) | 117 if (!ReadFileToString(path, &file_content)) |
(...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 headers.push_back( | 746 headers.push_back( |
781 std::string(kUploadContentRange) + "*/" + | 747 std::string(kUploadContentRange) + "*/" + |
782 base::Int64ToString(content_length_)); | 748 base::Int64ToString(content_length_)); |
783 return headers; | 749 return headers; |
784 } | 750 } |
785 | 751 |
786 //========================= MultipartUploadRequestBase ======================== | 752 //========================= MultipartUploadRequestBase ======================== |
787 | 753 |
788 MultipartUploadRequestBase::MultipartUploadRequestBase( | 754 MultipartUploadRequestBase::MultipartUploadRequestBase( |
789 RequestSender* sender, | 755 RequestSender* sender, |
790 const std::string& title, | 756 const std::string& metadata_json, |
791 const std::string& parent_resource_id, | |
792 const std::string& content_type, | 757 const std::string& content_type, |
793 int64 content_length, | 758 int64 content_length, |
794 const base::Time& modified_date, | |
795 const base::Time& last_viewed_by_me_date, | |
796 const base::FilePath& local_file_path, | 759 const base::FilePath& local_file_path, |
797 const FileResourceCallback& callback, | 760 const FileResourceCallback& callback, |
798 const ProgressCallback& progress_callback) | 761 const ProgressCallback& progress_callback) |
799 : UrlFetchRequestBase(sender), | 762 : UrlFetchRequestBase(sender), |
800 metadata_json_(CreateMultipartUploadMetadataJson(title, | 763 metadata_json_(metadata_json), |
801 parent_resource_id, | |
802 modified_date, | |
803 last_viewed_by_me_date)), | |
804 content_type_(content_type), | 764 content_type_(content_type), |
805 local_path_(local_file_path), | 765 local_path_(local_file_path), |
806 has_modified_date_(!modified_date.is_null()), | |
807 callback_(callback), | 766 callback_(callback), |
808 progress_callback_(progress_callback), | 767 progress_callback_(progress_callback), |
809 weak_ptr_factory_(this) { | 768 weak_ptr_factory_(this) { |
810 DCHECK(!content_type.empty()); | 769 DCHECK(!content_type.empty()); |
811 DCHECK_GE(content_length, 0); | 770 DCHECK_GE(content_length, 0); |
812 DCHECK(!local_file_path.empty()); | 771 DCHECK(!local_file_path.empty()); |
813 DCHECK(!callback.is_null()); | 772 DCHECK(!callback.is_null()); |
814 } | 773 } |
815 | 774 |
816 MultipartUploadRequestBase::~MultipartUploadRequestBase() { | 775 MultipartUploadRequestBase::~MultipartUploadRequestBase() { |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 download_action_callback_.Run(code, temp_file); | 917 download_action_callback_.Run(code, temp_file); |
959 OnProcessURLFetchResultsComplete(); | 918 OnProcessURLFetchResultsComplete(); |
960 } | 919 } |
961 | 920 |
962 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( | 921 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( |
963 DriveApiErrorCode code) { | 922 DriveApiErrorCode code) { |
964 download_action_callback_.Run(code, base::FilePath()); | 923 download_action_callback_.Run(code, base::FilePath()); |
965 } | 924 } |
966 | 925 |
967 } // namespace google_apis | 926 } // namespace google_apis |
OLD | NEW |