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

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

Issue 1130183003: Notify upload progress from batch request to its child requests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments and fix msan test. 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 1150 matching lines...) Expand 10 before | Expand all | Expand 10 after
1161 1161
1162 //========================== BatchUploadRequest ========================== 1162 //========================== BatchUploadRequest ==========================
1163 1163
1164 BatchUploadRequest::BatchUploadRequest( 1164 BatchUploadRequest::BatchUploadRequest(
1165 RequestSender* sender, 1165 RequestSender* sender,
1166 const DriveApiUrlGenerator& url_generator) 1166 const DriveApiUrlGenerator& url_generator)
1167 : UrlFetchRequestBase(sender), 1167 : UrlFetchRequestBase(sender),
1168 sender_(sender), 1168 sender_(sender),
1169 url_generator_(url_generator), 1169 url_generator_(url_generator),
1170 committed_(false), 1170 committed_(false),
1171 last_progress_value_(0),
1171 weak_ptr_factory_(this) { 1172 weak_ptr_factory_(this) {
1172 } 1173 }
1173 1174
1174 BatchUploadRequest::~BatchUploadRequest() { 1175 BatchUploadRequest::~BatchUploadRequest() {
1175 for (const auto& child : child_requests_) { 1176 for (const auto& child : child_requests_) {
1176 // Request will be deleted in |RequestFinished| method. 1177 // Request will be deleted in |RequestFinished| method.
1177 sender_->RequestFinished(child.request); 1178 sender_->RequestFinished(child.request);
1178 } 1179 }
1179 } 1180 }
1180 1181
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1248 void BatchUploadRequest::MayCompletePrepare() { 1249 void BatchUploadRequest::MayCompletePrepare() {
1249 if (!committed_ || prepare_callback_.is_null()) 1250 if (!committed_ || prepare_callback_.is_null())
1250 return; 1251 return;
1251 for (const auto& child : child_requests_) { 1252 for (const auto& child : child_requests_) {
1252 if (!child.prepared) 1253 if (!child.prepared)
1253 return; 1254 return;
1254 } 1255 }
1255 1256
1256 // Build multipart body here. 1257 // Build multipart body here.
1257 std::vector<ContentTypeAndData> parts; 1258 std::vector<ContentTypeAndData> parts;
1258 for (const auto& child : child_requests_) { 1259 for (auto& child : child_requests_) {
1259 std::string type; 1260 std::string type;
1260 std::string data; 1261 std::string data;
1261 const bool result = child.request->GetContentData(&type, &data); 1262 const bool result = child.request->GetContentData(&type, &data);
1262 // Upload request must have content data. 1263 // Upload request must have content data.
1263 DCHECK(result); 1264 DCHECK(result);
1264 1265
1265 const GURL url = child.request->GetURL(); 1266 const GURL url = child.request->GetURL();
1266 std::string method; 1267 std::string method;
1267 switch (child.request->GetRequestType()) { 1268 switch (child.request->GetRequestType()) {
1268 case net::URLFetcher::POST: 1269 case net::URLFetcher::POST:
1269 method = "POST"; 1270 method = "POST";
1270 break; 1271 break;
1271 case net::URLFetcher::PUT: 1272 case net::URLFetcher::PUT:
1272 method = "PUT"; 1273 method = "PUT";
1273 break; 1274 break;
1274 default: 1275 default:
1275 NOTREACHED(); 1276 NOTREACHED();
1276 break; 1277 break;
1277 } 1278 }
1279 const std::string header = base::StringPrintf(
1280 kBatchUploadRequestFormat, method.c_str(), url.path().c_str(),
1281 url_generator_.GetBatchUploadUrl().host().c_str(), type.c_str());
1282
1283 child.data_offset = header.size();
1284 child.data_size = data.size();
1278 1285
1279 parts.push_back(ContentTypeAndData()); 1286 parts.push_back(ContentTypeAndData());
1280 parts.back().type = kHttpContentType; 1287 parts.back().type = kHttpContentType;
1281 parts.back().data = base::StringPrintf( 1288 parts.back().data = header;
1282 kBatchUploadRequestFormat, method.c_str(), url.path().c_str(),
1283 url_generator_.GetBatchUploadUrl().host().c_str(), type.c_str());
1284 parts.back().data.append(data); 1289 parts.back().data.append(data);
1285 } 1290 }
1286 1291
1287 GenerateMultipartBody(MULTIPART_MIXED, boundary_, parts, &upload_content_); 1292 std::vector<uint64> part_data_offset;
1293 GenerateMultipartBody(MULTIPART_MIXED, boundary_, parts, &upload_content_,
1294 &part_data_offset);
1295 DCHECK(part_data_offset.size() == child_requests_.size());
1296 for (size_t i = 0; i < child_requests_.size(); ++i) {
1297 child_requests_[i].data_offset += part_data_offset[i];
1298 }
1288 prepare_callback_.Run(HTTP_SUCCESS); 1299 prepare_callback_.Run(HTTP_SUCCESS);
1289 } 1300 }
1290 1301
1291 bool BatchUploadRequest::GetContentData(std::string* upload_content_type, 1302 bool BatchUploadRequest::GetContentData(std::string* upload_content_type,
1292 std::string* upload_content_data) { 1303 std::string* upload_content_data) {
1293 upload_content_type->assign(upload_content_.type); 1304 upload_content_type->assign(upload_content_.type);
1294 upload_content_data->assign(upload_content_.data); 1305 upload_content_data->assign(upload_content_.data);
1295 return true; 1306 return true;
1296 } 1307 }
1297 1308
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1344 } 1355 }
1345 1356
1346 void BatchUploadRequest::RunCallbackOnPrematureFailure(DriveApiErrorCode code) { 1357 void BatchUploadRequest::RunCallbackOnPrematureFailure(DriveApiErrorCode code) {
1347 for (auto child : child_requests_) { 1358 for (auto child : child_requests_) {
1348 child.request->RunCallbackOnPrematureFailure(code); 1359 child.request->RunCallbackOnPrematureFailure(code);
1349 sender_->RequestFinished(child.request); 1360 sender_->RequestFinished(child.request);
1350 } 1361 }
1351 child_requests_.clear(); 1362 child_requests_.clear();
1352 } 1363 }
1353 1364
1365 void BatchUploadRequest::OnURLFetchUploadProgress(const net::URLFetcher* source,
1366 int64 current,
1367 int64 total) {
1368 for (auto child : child_requests_) {
1369 if (child.data_offset <= current &&
1370 current <= child.data_offset + child.data_size) {
1371 child.request->OnURLFetchUploadProgress(
1372 source, current - child.data_offset, child.data_size);
1373 } else if (last_progress_value_ < child.data_offset + child.data_size &&
1374 child.data_offset + child.data_size < current) {
1375 child.request->OnURLFetchUploadProgress(source, child.data_size,
1376 child.data_size);
1377 }
1378 }
1379 last_progress_value_ = current;
1380 }
1354 } // namespace drive 1381 } // namespace drive
1355 } // namespace google_apis 1382 } // 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