| 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 std::string* upload_content_type, | 121 std::string* upload_content_type, |
| 122 std::string* upload_content_data) { | 122 std::string* upload_content_data) { |
| 123 std::vector<google_apis::ContentTypeAndData> parts(2); | 123 std::vector<google_apis::ContentTypeAndData> parts(2); |
| 124 parts[0].type = kJsonMimeType; | 124 parts[0].type = kJsonMimeType; |
| 125 parts[0].data = metadata_json; | 125 parts[0].data = metadata_json; |
| 126 parts[1].type = content_type; | 126 parts[1].type = content_type; |
| 127 if (!ReadFileToString(path, &parts[1].data)) | 127 if (!ReadFileToString(path, &parts[1].data)) |
| 128 return false; | 128 return false; |
| 129 | 129 |
| 130 google_apis::ContentTypeAndData output; | 130 google_apis::ContentTypeAndData output; |
| 131 GenerateMultipartBody( | 131 GenerateMultipartBody(google_apis::MULTIPART_RELATED, predetermined_boundary, |
| 132 google_apis::MULTIPART_RELATED, predetermined_boundary, parts, &output); | 132 parts, &output, nullptr); |
| 133 upload_content_type->swap(output.type); | 133 upload_content_type->swap(output.type); |
| 134 upload_content_data->swap(output.data); | 134 upload_content_data->swap(output.data); |
| 135 return true; | 135 return true; |
| 136 } | 136 } |
| 137 | 137 |
| 138 } // namespace | 138 } // namespace |
| 139 | 139 |
| 140 namespace google_apis { | 140 namespace google_apis { |
| 141 | 141 |
| 142 scoped_ptr<base::Value> ParseJson(const std::string& json) { | 142 scoped_ptr<base::Value> ParseJson(const std::string& json) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 159 } | 159 } |
| 160 LOG(WARNING) << "Error while parsing entry response: " << error_message | 160 LOG(WARNING) << "Error while parsing entry response: " << error_message |
| 161 << ", code: " << error_code << ", json:\n" << trimmed_json; | 161 << ", code: " << error_code << ", json:\n" << trimmed_json; |
| 162 } | 162 } |
| 163 return value.Pass(); | 163 return value.Pass(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void GenerateMultipartBody(MultipartType multipart_type, | 166 void GenerateMultipartBody(MultipartType multipart_type, |
| 167 const std::string& predetermined_boundary, | 167 const std::string& predetermined_boundary, |
| 168 const std::vector<ContentTypeAndData>& parts, | 168 const std::vector<ContentTypeAndData>& parts, |
| 169 ContentTypeAndData* output) { | 169 ContentTypeAndData* output, |
| 170 std::vector<uint64>* data_offset) { |
| 170 std::string boundary; | 171 std::string boundary; |
| 171 // Generate random boundary. | 172 // Generate random boundary. |
| 172 if (predetermined_boundary.empty()) { | 173 if (predetermined_boundary.empty()) { |
| 173 while (true) { | 174 while (true) { |
| 174 boundary.resize(kBoundarySize); | 175 boundary.resize(kBoundarySize); |
| 175 for (int i = 0; i < kBoundarySize; ++i) { | 176 for (int i = 0; i < kBoundarySize; ++i) { |
| 176 // Subtract 2 from the array size to exclude '\0', and to turn the size | 177 // Subtract 2 from the array size to exclude '\0', and to turn the size |
| 177 // into the last index. | 178 // into the last index. |
| 178 const int last_char_index = arraysize(kBoundaryCharacters) - 2; | 179 const int last_char_index = arraysize(kBoundaryCharacters) - 2; |
| 179 boundary[i] = kBoundaryCharacters[base::RandInt(0, last_char_index)]; | 180 boundary[i] = kBoundaryCharacters[base::RandInt(0, last_char_index)]; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 195 switch (multipart_type) { | 196 switch (multipart_type) { |
| 196 case MULTIPART_RELATED: | 197 case MULTIPART_RELATED: |
| 197 output->type = kMultipartRelatedMimeTypePrefix + boundary; | 198 output->type = kMultipartRelatedMimeTypePrefix + boundary; |
| 198 break; | 199 break; |
| 199 case MULTIPART_MIXED: | 200 case MULTIPART_MIXED: |
| 200 output->type = kMultipartMixedMimeTypePrefix + boundary; | 201 output->type = kMultipartMixedMimeTypePrefix + boundary; |
| 201 break; | 202 break; |
| 202 } | 203 } |
| 203 | 204 |
| 204 output->data.clear(); | 205 output->data.clear(); |
| 206 if (data_offset) |
| 207 data_offset->clear(); |
| 205 for (auto& part : parts) { | 208 for (auto& part : parts) { |
| 206 output->data.append(base::StringPrintf( | 209 output->data.append(base::StringPrintf( |
| 207 kMultipartItemHeaderFormat, boundary.c_str(), part.type.c_str())); | 210 kMultipartItemHeaderFormat, boundary.c_str(), part.type.c_str())); |
| 211 if (data_offset) |
| 212 data_offset->push_back(output->data.size()); |
| 208 output->data.append(part.data); | 213 output->data.append(part.data); |
| 209 output->data.append("\n"); | 214 output->data.append("\n"); |
| 210 } | 215 } |
| 211 output->data.append( | 216 output->data.append( |
| 212 base::StringPrintf(kMultipartFooterFormat, boundary.c_str())); | 217 base::StringPrintf(kMultipartFooterFormat, boundary.c_str())); |
| 213 } | 218 } |
| 214 | 219 |
| 215 //=========================== ResponseWriter ================================== | 220 //=========================== ResponseWriter ================================== |
| 216 ResponseWriter::ResponseWriter(base::SequencedTaskRunner* file_task_runner, | 221 ResponseWriter::ResponseWriter(base::SequencedTaskRunner* file_task_runner, |
| 217 const base::FilePath& file_path, | 222 const base::FilePath& file_path, |
| (...skipping 792 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 download_action_callback_.Run(code, temp_file); | 1015 download_action_callback_.Run(code, temp_file); |
| 1011 OnProcessURLFetchResultsComplete(); | 1016 OnProcessURLFetchResultsComplete(); |
| 1012 } | 1017 } |
| 1013 | 1018 |
| 1014 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( | 1019 void DownloadFileRequestBase::RunCallbackOnPrematureFailure( |
| 1015 DriveApiErrorCode code) { | 1020 DriveApiErrorCode code) { |
| 1016 download_action_callback_.Run(code, base::FilePath()); | 1021 download_action_callback_.Run(code, base::FilePath()); |
| 1017 } | 1022 } |
| 1018 | 1023 |
| 1019 } // namespace google_apis | 1024 } // namespace google_apis |
| OLD | NEW |