Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: google_apis/drive/drive_api_requests.cc

Issue 1132693006: Drive API: Simplify lifetime management of child requests in BatchUploadRequest. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing header file. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/drive_api_requests.h" 5 #include "google_apis/drive/drive_api_requests.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 base::StringPiece TrimTransportPadding(const base::StringPiece& piece) { 156 base::StringPiece TrimTransportPadding(const base::StringPiece& piece) {
157 size_t trim_size = 0; 157 size_t trim_size = 0;
158 while (trim_size < piece.size() && 158 while (trim_size < piece.size() &&
159 (piece[piece.size() - 1 - trim_size] == ' ' || 159 (piece[piece.size() - 1 - trim_size] == ' ' ||
160 piece[piece.size() - 1 - trim_size] == '\t')) { 160 piece[piece.size() - 1 - trim_size] == '\t')) {
161 ++trim_size; 161 ++trim_size;
162 } 162 }
163 return piece.substr(0, piece.size() - trim_size); 163 return piece.substr(0, piece.size() - trim_size);
164 } 164 }
165 165
166 void EmptyClosure(scoped_ptr<BatchableDelegate>) {
167 }
168
166 } // namespace 169 } // namespace
167 170
168 MultipartHttpResponse::MultipartHttpResponse() : code(HTTP_SUCCESS) { 171 MultipartHttpResponse::MultipartHttpResponse() : code(HTTP_SUCCESS) {
169 } 172 }
170 173
171 MultipartHttpResponse::~MultipartHttpResponse() { 174 MultipartHttpResponse::~MultipartHttpResponse() {
172 } 175 }
173 176
174 // The |response| must be multipart/mixed format that contains child HTTP 177 // The |response| must be multipart/mixed format that contains child HTTP
175 // response of drive batch request. 178 // response of drive batch request.
(...skipping 788 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 967
965 GetUploadStatusRequest::~GetUploadStatusRequest() {} 968 GetUploadStatusRequest::~GetUploadStatusRequest() {}
966 969
967 void GetUploadStatusRequest::OnRangeRequestComplete( 970 void GetUploadStatusRequest::OnRangeRequestComplete(
968 const UploadRangeResponse& response, 971 const UploadRangeResponse& response,
969 scoped_ptr<base::Value> value) { 972 scoped_ptr<base::Value> value) {
970 DCHECK(CalledOnValidThread()); 973 DCHECK(CalledOnValidThread());
971 ParseFileResourceWithUploadRangeAndRun(callback_, response, value.Pass()); 974 ParseFileResourceWithUploadRangeAndRun(callback_, response, value.Pass());
972 } 975 }
973 976
974 //======================= MultipartUploadNewFileRequest ======================= 977 //======================= MultipartUploadNewFileDelegate =======================
975 978
976 MultipartUploadNewFileRequest::MultipartUploadNewFileRequest( 979 MultipartUploadNewFileDelegate::MultipartUploadNewFileDelegate(
977 RequestSender* sender, 980 RequestSender* sender,
978 const std::string& title, 981 const std::string& title,
979 const std::string& parent_resource_id, 982 const std::string& parent_resource_id,
980 const std::string& content_type, 983 const std::string& content_type,
981 int64 content_length, 984 int64 content_length,
982 const base::Time& modified_date, 985 const base::Time& modified_date,
983 const base::Time& last_viewed_by_me_date, 986 const base::Time& last_viewed_by_me_date,
984 const base::FilePath& local_file_path, 987 const base::FilePath& local_file_path,
985 const Properties& properties, 988 const Properties& properties,
986 const DriveApiUrlGenerator& url_generator, 989 const DriveApiUrlGenerator& url_generator,
987 const FileResourceCallback& callback, 990 const FileResourceCallback& callback,
988 const ProgressCallback& progress_callback) 991 const ProgressCallback& progress_callback)
989 : MultipartUploadRequestBase( 992 : MultipartUploadRequestBase(
990 sender, 993 sender->blocking_task_runner(),
991 CreateMultipartUploadMetadataJson(title, 994 CreateMultipartUploadMetadataJson(title,
992 parent_resource_id, 995 parent_resource_id,
993 modified_date, 996 modified_date,
994 last_viewed_by_me_date, 997 last_viewed_by_me_date,
995 properties), 998 properties),
996 content_type, 999 content_type,
997 content_length, 1000 content_length,
998 local_file_path, 1001 local_file_path,
999 callback, 1002 callback,
1000 progress_callback), 1003 progress_callback),
1001 has_modified_date_(!modified_date.is_null()), 1004 has_modified_date_(!modified_date.is_null()),
1002 url_generator_(url_generator) { 1005 url_generator_(url_generator) {
1003 } 1006 }
1004 1007
1005 MultipartUploadNewFileRequest::~MultipartUploadNewFileRequest() { 1008 MultipartUploadNewFileDelegate::~MultipartUploadNewFileDelegate() {
1006 } 1009 }
1007 1010
1008 GURL MultipartUploadNewFileRequest::GetURL() const { 1011 GURL MultipartUploadNewFileDelegate::GetURL() const {
1009 return url_generator_.GetMultipartUploadNewFileUrl(has_modified_date_); 1012 return url_generator_.GetMultipartUploadNewFileUrl(has_modified_date_);
1010 } 1013 }
1011 1014
1012 net::URLFetcher::RequestType MultipartUploadNewFileRequest::GetRequestType() 1015 net::URLFetcher::RequestType MultipartUploadNewFileDelegate::GetRequestType()
1013 const { 1016 const {
1014 return net::URLFetcher::POST; 1017 return net::URLFetcher::POST;
1015 } 1018 }
1016 1019
1017 //======================= MultipartUploadExistingFileRequest =================== 1020 //====================== MultipartUploadExistingFileDelegate ===================
1018 1021
1019 MultipartUploadExistingFileRequest::MultipartUploadExistingFileRequest( 1022 MultipartUploadExistingFileDelegate::MultipartUploadExistingFileDelegate(
1020 RequestSender* sender, 1023 RequestSender* sender,
1021 const std::string& title, 1024 const std::string& title,
1022 const std::string& resource_id, 1025 const std::string& resource_id,
1023 const std::string& parent_resource_id, 1026 const std::string& parent_resource_id,
1024 const std::string& content_type, 1027 const std::string& content_type,
1025 int64 content_length, 1028 int64 content_length,
1026 const base::Time& modified_date, 1029 const base::Time& modified_date,
1027 const base::Time& last_viewed_by_me_date, 1030 const base::Time& last_viewed_by_me_date,
1028 const base::FilePath& local_file_path, 1031 const base::FilePath& local_file_path,
1029 const std::string& etag, 1032 const std::string& etag,
1030 const Properties& properties, 1033 const Properties& properties,
1031 const DriveApiUrlGenerator& url_generator, 1034 const DriveApiUrlGenerator& url_generator,
1032 const FileResourceCallback& callback, 1035 const FileResourceCallback& callback,
1033 const ProgressCallback& progress_callback) 1036 const ProgressCallback& progress_callback)
1034 : MultipartUploadRequestBase( 1037 : MultipartUploadRequestBase(
1035 sender, 1038 sender->blocking_task_runner(),
1036 CreateMultipartUploadMetadataJson(title, 1039 CreateMultipartUploadMetadataJson(title,
1037 parent_resource_id, 1040 parent_resource_id,
1038 modified_date, 1041 modified_date,
1039 last_viewed_by_me_date, 1042 last_viewed_by_me_date,
1040 properties), 1043 properties),
1041 content_type, 1044 content_type,
1042 content_length, 1045 content_length,
1043 local_file_path, 1046 local_file_path,
1044 callback, 1047 callback,
1045 progress_callback), 1048 progress_callback),
1046 resource_id_(resource_id), 1049 resource_id_(resource_id),
1047 etag_(etag), 1050 etag_(etag),
1048 has_modified_date_(!modified_date.is_null()), 1051 has_modified_date_(!modified_date.is_null()),
1049 url_generator_(url_generator) { 1052 url_generator_(url_generator) {
1050 } 1053 }
1051 1054
1052 MultipartUploadExistingFileRequest::~MultipartUploadExistingFileRequest() { 1055 MultipartUploadExistingFileDelegate::~MultipartUploadExistingFileDelegate() {
1053 } 1056 }
1054 1057
1055 std::vector<std::string> 1058 std::vector<std::string>
1056 MultipartUploadExistingFileRequest::GetExtraRequestHeaders() const { 1059 MultipartUploadExistingFileDelegate::GetExtraRequestHeaders() const {
1057 std::vector<std::string> headers( 1060 std::vector<std::string> headers(
1058 MultipartUploadRequestBase::GetExtraRequestHeaders()); 1061 MultipartUploadRequestBase::GetExtraRequestHeaders());
1059 headers.push_back(util::GenerateIfMatchHeader(etag_)); 1062 headers.push_back(util::GenerateIfMatchHeader(etag_));
1060 return headers; 1063 return headers;
1061 } 1064 }
1062 1065
1063 GURL MultipartUploadExistingFileRequest::GetURL() const { 1066 GURL MultipartUploadExistingFileDelegate::GetURL() const {
1064 return url_generator_.GetMultipartUploadExistingFileUrl(resource_id_, 1067 return url_generator_.GetMultipartUploadExistingFileUrl(resource_id_,
1065 has_modified_date_); 1068 has_modified_date_);
1066 } 1069 }
1067 1070
1068 net::URLFetcher::RequestType 1071 net::URLFetcher::RequestType
1069 MultipartUploadExistingFileRequest::GetRequestType() const { 1072 MultipartUploadExistingFileDelegate::GetRequestType() const {
1070 return net::URLFetcher::PUT; 1073 return net::URLFetcher::PUT;
1071 } 1074 }
1072 1075
1073 //========================== DownloadFileRequest ========================== 1076 //========================== DownloadFileRequest ==========================
1074 1077
1075 DownloadFileRequest::DownloadFileRequest( 1078 DownloadFileRequest::DownloadFileRequest(
1076 RequestSender* sender, 1079 RequestSender* sender,
1077 const DriveApiUrlGenerator& url_generator, 1080 const DriveApiUrlGenerator& url_generator,
1078 const std::string& resource_id, 1081 const std::string& resource_id,
1079 const base::FilePath& output_file_path, 1082 const base::FilePath& output_file_path,
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1152 list->AppendString("commenter"); 1155 list->AppendString("commenter");
1153 root.Set("additionalRoles", list); 1156 root.Set("additionalRoles", list);
1154 } 1157 }
1155 break; 1158 break;
1156 } 1159 }
1157 root.SetString("value", value_); 1160 root.SetString("value", value_);
1158 base::JSONWriter::Write(&root, upload_content); 1161 base::JSONWriter::Write(&root, upload_content);
1159 return true; 1162 return true;
1160 } 1163 }
1161 1164
1165 //======================= SingleBatchableDelegateRequest =======================
1166
1167 SingleBatchableDelegateRequest::SingleBatchableDelegateRequest(
1168 RequestSender* sender,
1169 BatchableDelegate* delegate)
1170 : UrlFetchRequestBase(sender),
1171 delegate_(delegate),
1172 weak_ptr_factory_(this) {
1173 }
1174
1175 SingleBatchableDelegateRequest::~SingleBatchableDelegateRequest() {
1176 }
1177
1178 GURL SingleBatchableDelegateRequest::GetURL() const {
1179 return delegate_->GetURL();
1180 }
1181
1182 net::URLFetcher::RequestType SingleBatchableDelegateRequest::GetRequestType()
1183 const {
1184 return delegate_->GetRequestType();
1185 }
1186
1187 std::vector<std::string>
1188 SingleBatchableDelegateRequest::GetExtraRequestHeaders() const {
1189 return delegate_->GetExtraRequestHeaders();
1190 }
1191
1192 void SingleBatchableDelegateRequest::Prepare(const PrepareCallback& callback) {
1193 delegate_->Prepare(callback);
1194 }
1195
1196 bool SingleBatchableDelegateRequest::GetContentData(
1197 std::string* upload_content_type,
1198 std::string* upload_content) {
1199 return delegate_->GetContentData(upload_content_type, upload_content);
1200 }
1201
1202 void SingleBatchableDelegateRequest::ProcessURLFetchResults(
1203 const net::URLFetcher* source) {
1204 delegate_->NotifyResult(
1205 GetErrorCode(), response_writer()->data(),
1206 base::Bind(
1207 &SingleBatchableDelegateRequest::OnProcessURLFetchResultsComplete,
1208 weak_ptr_factory_.GetWeakPtr()));
1209 }
1210
1211 void SingleBatchableDelegateRequest::RunCallbackOnPrematureFailure(
1212 DriveApiErrorCode code) {
1213 delegate_->NotifyError(code);
1214 }
1215
1216 void SingleBatchableDelegateRequest::OnURLFetchUploadProgress(
1217 const net::URLFetcher* source,
1218 int64 current,
1219 int64 total) {
1220 delegate_->NotifyUploadProgress(source, current, total);
1221 }
1222
1162 //========================== BatchUploadRequest ========================== 1223 //========================== BatchUploadRequest ==========================
1163 1224
1225 BatchUploadChildEntry::BatchUploadChildEntry(BatchableDelegate* request)
1226 : request(request), prepared(false), data_offset(0), data_size(0) {
1227 }
1228
1229 BatchUploadChildEntry::~BatchUploadChildEntry() {
1230 }
1231
1164 BatchUploadRequest::BatchUploadRequest( 1232 BatchUploadRequest::BatchUploadRequest(
1165 RequestSender* sender, 1233 RequestSender* sender,
1166 const DriveApiUrlGenerator& url_generator) 1234 const DriveApiUrlGenerator& url_generator)
1167 : UrlFetchRequestBase(sender), 1235 : UrlFetchRequestBase(sender),
1168 sender_(sender), 1236 sender_(sender),
1169 url_generator_(url_generator), 1237 url_generator_(url_generator),
1170 committed_(false), 1238 committed_(false),
1171 last_progress_value_(0), 1239 last_progress_value_(0),
1172 weak_ptr_factory_(this) { 1240 weak_ptr_factory_(this) {
1173 } 1241 }
1174 1242
1175 BatchUploadRequest::~BatchUploadRequest() { 1243 BatchUploadRequest::~BatchUploadRequest() {
1176 for (const auto& child : child_requests_) {
1177 // Request will be deleted in |RequestFinished| method.
1178 sender_->RequestFinished(child.request);
1179 }
1180 } 1244 }
1181 1245
1182 void BatchUploadRequest::SetBoundaryForTesting(const std::string& boundary) { 1246 void BatchUploadRequest::SetBoundaryForTesting(const std::string& boundary) {
1183 boundary_ = boundary; 1247 boundary_ = boundary;
1184 } 1248 }
1185 1249
1186 void BatchUploadRequest::AddRequest(BatchableRequestBase* request) { 1250 void BatchUploadRequest::AddRequest(BatchableDelegate* request) {
1187 DCHECK(CalledOnValidThread()); 1251 DCHECK(CalledOnValidThread());
1188 DCHECK(request); 1252 DCHECK(request);
1189 DCHECK(GetChildEntry(request) == child_requests_.end()); 1253 DCHECK(GetChildEntry(request) == child_requests_.end());
1190 DCHECK(!committed_); 1254 DCHECK(!committed_);
1191 child_requests_.push_back(BatchUploadChildEntry(request)); 1255 child_requests_.push_back(new BatchUploadChildEntry(request));
1192 request->Prepare(base::Bind(&BatchUploadRequest::OnChildRequestPrepared, 1256 request->Prepare(base::Bind(&BatchUploadRequest::OnChildRequestPrepared,
1193 weak_ptr_factory_.GetWeakPtr(), request)); 1257 weak_ptr_factory_.GetWeakPtr(), request));
1194 } 1258 }
1195 1259
1196 void BatchUploadRequest::OnChildRequestPrepared(RequestID request_id, 1260 void BatchUploadRequest::OnChildRequestPrepared(RequestID request_id,
1197 DriveApiErrorCode result) { 1261 DriveApiErrorCode result) {
1198 DCHECK(CalledOnValidThread()); 1262 DCHECK(CalledOnValidThread());
1199 auto const child = GetChildEntry(request_id); 1263 auto const child = GetChildEntry(request_id);
1200 DCHECK(child != child_requests_.end()); 1264 DCHECK(child != child_requests_.end());
1201 if (IsSuccessfulDriveApiErrorCode(result)) { 1265 if (IsSuccessfulDriveApiErrorCode(result)) {
1202 child->prepared = true; 1266 (*child)->prepared = true;
1203 } else { 1267 } else {
1204 child->request->RunCallbackOnPrematureFailure(result); 1268 (*child)->request->NotifyError(result);
1205 sender_->RequestFinished(child->request);
1206 child_requests_.erase(child); 1269 child_requests_.erase(child);
1207 } 1270 }
1208 MayCompletePrepare(); 1271 MayCompletePrepare();
1209 } 1272 }
1210 1273
1211 void BatchUploadRequest::Commit() { 1274 void BatchUploadRequest::Commit() {
1212 DCHECK(CalledOnValidThread()); 1275 DCHECK(CalledOnValidThread());
1213 DCHECK(!committed_); 1276 DCHECK(!committed_);
1214 if (child_requests_.empty()) { 1277 if (child_requests_.empty()) {
1215 Cancel(); 1278 Cancel();
1216 } else { 1279 } else {
1217 committed_ = true; 1280 committed_ = true;
1218 MayCompletePrepare(); 1281 MayCompletePrepare();
1219 } 1282 }
1220 } 1283 }
1221 1284
1222 void BatchUploadRequest::Prepare(const PrepareCallback& callback) { 1285 void BatchUploadRequest::Prepare(const PrepareCallback& callback) {
1223 DCHECK(CalledOnValidThread()); 1286 DCHECK(CalledOnValidThread());
1224 DCHECK(!callback.is_null()); 1287 DCHECK(!callback.is_null());
1225 prepare_callback_ = callback; 1288 prepare_callback_ = callback;
1226 MayCompletePrepare(); 1289 MayCompletePrepare();
1227 } 1290 }
1228 1291
1229 void BatchUploadRequest::Cancel() { 1292 void BatchUploadRequest::Cancel() {
1230 for (auto& child : child_requests_) {
1231 // Request cancel should delete the request instance.
1232 child.request->Cancel();
1233 }
1234 child_requests_.clear(); 1293 child_requests_.clear();
1235 UrlFetchRequestBase::Cancel(); 1294 UrlFetchRequestBase::Cancel();
1236 } 1295 }
1237 1296
1238 // Obtains corresponding child entry of |request_id|. Returns NULL if the 1297 // Obtains corresponding child entry of |request_id|. Returns NULL if the
1239 // entry is not found. 1298 // entry is not found.
1240 std::vector<BatchUploadChildEntry>::iterator BatchUploadRequest::GetChildEntry( 1299 ScopedVector<BatchUploadChildEntry>::iterator BatchUploadRequest::GetChildEntry(
1241 RequestID request_id) { 1300 RequestID request_id) {
1242 for (auto it = child_requests_.begin(); it != child_requests_.end(); ++it) { 1301 for (auto it = child_requests_.begin(); it != child_requests_.end(); ++it) {
1243 if (it->request == request_id) 1302 if ((*it)->request.get() == request_id)
1244 return it; 1303 return it;
1245 } 1304 }
1246 return child_requests_.end(); 1305 return child_requests_.end();
1247 } 1306 }
1248 1307
1249 void BatchUploadRequest::MayCompletePrepare() { 1308 void BatchUploadRequest::MayCompletePrepare() {
1250 if (!committed_ || prepare_callback_.is_null()) 1309 if (!committed_ || prepare_callback_.is_null())
1251 return; 1310 return;
1252 for (const auto& child : child_requests_) { 1311 for (const auto& child : child_requests_) {
1253 if (!child.prepared) 1312 if (!child->prepared)
1254 return; 1313 return;
1255 } 1314 }
1256 1315
1257 // Build multipart body here. 1316 // Build multipart body here.
1258 std::vector<ContentTypeAndData> parts; 1317 std::vector<ContentTypeAndData> parts;
1259 for (auto& child : child_requests_) { 1318 for (auto& child : child_requests_) {
1260 std::string type; 1319 std::string type;
1261 std::string data; 1320 std::string data;
1262 const bool result = child.request->GetContentData(&type, &data); 1321 const bool result = child->request->GetContentData(&type, &data);
1263 // Upload request must have content data. 1322 // Upload request must have content data.
1264 DCHECK(result); 1323 DCHECK(result);
1265 1324
1266 const GURL url = child.request->GetURL(); 1325 const GURL url = child->request->GetURL();
1267 std::string method; 1326 std::string method;
1268 switch (child.request->GetRequestType()) { 1327 switch (child->request->GetRequestType()) {
1269 case net::URLFetcher::POST: 1328 case net::URLFetcher::POST:
1270 method = "POST"; 1329 method = "POST";
1271 break; 1330 break;
1272 case net::URLFetcher::PUT: 1331 case net::URLFetcher::PUT:
1273 method = "PUT"; 1332 method = "PUT";
1274 break; 1333 break;
1275 default: 1334 default:
1276 NOTREACHED(); 1335 NOTREACHED();
1277 break; 1336 break;
1278 } 1337 }
1279 const std::string header = base::StringPrintf( 1338 const std::string header = base::StringPrintf(
1280 kBatchUploadRequestFormat, method.c_str(), url.path().c_str(), 1339 kBatchUploadRequestFormat, method.c_str(), url.path().c_str(),
1281 url_generator_.GetBatchUploadUrl().host().c_str(), type.c_str()); 1340 url_generator_.GetBatchUploadUrl().host().c_str(), type.c_str());
1282 1341
1283 child.data_offset = header.size(); 1342 child->data_offset = header.size();
1284 child.data_size = data.size(); 1343 child->data_size = data.size();
1285 1344
1286 parts.push_back(ContentTypeAndData()); 1345 parts.push_back(ContentTypeAndData());
1287 parts.back().type = kHttpContentType; 1346 parts.back().type = kHttpContentType;
1288 parts.back().data = header; 1347 parts.back().data = header;
1289 parts.back().data.append(data); 1348 parts.back().data.append(data);
1290 } 1349 }
1291 1350
1292 std::vector<uint64> part_data_offset; 1351 std::vector<uint64> part_data_offset;
1293 GenerateMultipartBody(MULTIPART_MIXED, boundary_, parts, &upload_content_, 1352 GenerateMultipartBody(MULTIPART_MIXED, boundary_, parts, &upload_content_,
1294 &part_data_offset); 1353 &part_data_offset);
1295 DCHECK(part_data_offset.size() == child_requests_.size()); 1354 DCHECK(part_data_offset.size() == child_requests_.size());
1296 for (size_t i = 0; i < child_requests_.size(); ++i) { 1355 for (size_t i = 0; i < child_requests_.size(); ++i) {
1297 child_requests_[i].data_offset += part_data_offset[i]; 1356 child_requests_[i]->data_offset += part_data_offset[i];
1298 } 1357 }
1299 prepare_callback_.Run(HTTP_SUCCESS); 1358 prepare_callback_.Run(HTTP_SUCCESS);
1300 } 1359 }
1301 1360
1302 bool BatchUploadRequest::GetContentData(std::string* upload_content_type, 1361 bool BatchUploadRequest::GetContentData(std::string* upload_content_type,
1303 std::string* upload_content_data) { 1362 std::string* upload_content_data) {
1304 upload_content_type->assign(upload_content_.type); 1363 upload_content_type->assign(upload_content_.type);
1305 upload_content_data->assign(upload_content_.data); 1364 upload_content_data->assign(upload_content_.data);
1306 return true; 1365 return true;
1307 } 1366 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1339 std::vector<MultipartHttpResponse> parts; 1398 std::vector<MultipartHttpResponse> parts;
1340 if (!ParseMultipartResponse(content_type, response_writer()->data(), 1399 if (!ParseMultipartResponse(content_type, response_writer()->data(),
1341 &parts) || 1400 &parts) ||
1342 child_requests_.size() != parts.size()) { 1401 child_requests_.size() != parts.size()) {
1343 RunCallbackOnPrematureFailure(DRIVE_PARSE_ERROR); 1402 RunCallbackOnPrematureFailure(DRIVE_PARSE_ERROR);
1344 sender_->RequestFinished(this); 1403 sender_->RequestFinished(this);
1345 return; 1404 return;
1346 } 1405 }
1347 1406
1348 for (size_t i = 0; i < parts.size(); ++i) { 1407 for (size_t i = 0; i < parts.size(); ++i) {
1349 child_requests_[i].request->ProcessURLFetchResults(parts[i].code, 1408 BatchableDelegate* const delegate = child_requests_[i]->request.get();
1350 parts[i].body); 1409 // Pass onwership of |delegate| so that it is deleted after notifying.
kinaba 2015/05/14 04:16:40 I think there's a room for improvement on this com
hirono 2015/05/14 04:43:12 Thank you for the suggestion. I just copied the co
1410 delegate->NotifyResult(
1411 parts[i].code, parts[i].body,
1412 base::Bind(&EmptyClosure, base::Passed(&child_requests_[i]->request)));
1351 } 1413 }
1414 child_requests_.clear();
1352 1415
1353 child_requests_.clear();
1354 sender_->RequestFinished(this); 1416 sender_->RequestFinished(this);
1355 } 1417 }
1356 1418
1357 void BatchUploadRequest::RunCallbackOnPrematureFailure(DriveApiErrorCode code) { 1419 void BatchUploadRequest::RunCallbackOnPrematureFailure(DriveApiErrorCode code) {
1358 for (auto child : child_requests_) { 1420 for (auto child : child_requests_) {
1359 child.request->RunCallbackOnPrematureFailure(code); 1421 child->request->NotifyError(code);
1360 sender_->RequestFinished(child.request);
1361 } 1422 }
1362 child_requests_.clear(); 1423 child_requests_.clear();
1363 } 1424 }
1364 1425
1365 void BatchUploadRequest::OnURLFetchUploadProgress(const net::URLFetcher* source, 1426 void BatchUploadRequest::OnURLFetchUploadProgress(const net::URLFetcher* source,
1366 int64 current, 1427 int64 current,
1367 int64 total) { 1428 int64 total) {
1368 for (auto child : child_requests_) { 1429 for (auto child : child_requests_) {
1369 if (child.data_offset <= current && 1430 if (child->data_offset <= current &&
1370 current <= child.data_offset + child.data_size) { 1431 current <= child->data_offset + child->data_size) {
1371 child.request->OnURLFetchUploadProgress( 1432 child->request->NotifyUploadProgress(source, current - child->data_offset,
1372 source, current - child.data_offset, child.data_size); 1433 child->data_size);
1373 } else if (last_progress_value_ < child.data_offset + child.data_size && 1434 } else if (last_progress_value_ < child->data_offset + child->data_size &&
1374 child.data_offset + child.data_size < current) { 1435 child->data_offset + child->data_size < current) {
1375 child.request->OnURLFetchUploadProgress(source, child.data_size, 1436 child->request->NotifyUploadProgress(source, child->data_size,
1376 child.data_size); 1437 child->data_size);
1377 } 1438 }
1378 } 1439 }
1379 last_progress_value_ = current; 1440 last_progress_value_ = current;
1380 } 1441 }
1381 } // namespace drive 1442 } // namespace drive
1382 } // namespace google_apis 1443 } // namespace google_apis
OLDNEW
« no previous file with comments | « google_apis/drive/drive_api_requests.h ('k') | google_apis/drive/drive_api_requests_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698