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

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

Issue 1084523002: Files.app: Implement sending batch requests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix memory leak in the test. Created 5 years, 8 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"
11 #include "base/sequenced_task_runner.h" 11 #include "base/sequenced_task_runner.h"
12 #include "base/strings/stringprintf.h"
12 #include "base/task_runner_util.h" 13 #include "base/task_runner_util.h"
13 #include "base/values.h" 14 #include "base/values.h"
14 #include "google_apis/drive/request_sender.h" 15 #include "google_apis/drive/request_sender.h"
15 #include "google_apis/drive/request_util.h" 16 #include "google_apis/drive/request_util.h"
16 #include "google_apis/drive/time_util.h" 17 #include "google_apis/drive/time_util.h"
17 #include "net/base/url_util.h" 18 #include "net/base/url_util.h"
18 19
19 namespace google_apis { 20 namespace google_apis {
20 namespace drive { 21 namespace drive {
21 namespace { 22 namespace {
22 23
24 // Format of one request in batch uploading request.
25 const char kBatchUploadRequestFormat[] =
26 "%s %s HTTP/1.1\n"
27 "Host: %s\n"
28 "X-Goog-Upload-Protocol: multipart\n"
29 "Content-Type: %s\n"
30 "\n"
31 "%s";
32
33 // Request header for specifying batch upload.
34 const char kBatchUploadHeader[] = "X-Goog-Upload-Protocol: batch";
35
36 // Content type of HTTP request.
37 const char kHttpContentType[] = "application/http";
38
23 // Parses the JSON value to FileResource instance and runs |callback| on the 39 // Parses the JSON value to FileResource instance and runs |callback| on the
24 // UI thread once parsing is done. 40 // UI thread once parsing is done.
25 // This is customized version of ParseJsonAndRun defined above to adapt the 41 // This is customized version of ParseJsonAndRun defined above to adapt the
26 // remaining response type. 42 // remaining response type.
27 void ParseFileResourceWithUploadRangeAndRun(const UploadRangeCallback& callback, 43 void ParseFileResourceWithUploadRangeAndRun(const UploadRangeCallback& callback,
28 const UploadRangeResponse& response, 44 const UploadRangeResponse& response,
29 scoped_ptr<base::Value> value) { 45 scoped_ptr<base::Value> value) {
30 DCHECK(!callback.is_null()); 46 DCHECK(!callback.is_null());
31 47
32 scoped_ptr<FileResource> file_resource; 48 scoped_ptr<FileResource> file_resource;
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after
970 } 986 }
971 987
972 //========================== BatchUploadRequest ========================== 988 //========================== BatchUploadRequest ==========================
973 989
974 BatchUploadRequest::BatchUploadRequest( 990 BatchUploadRequest::BatchUploadRequest(
975 RequestSender* sender, 991 RequestSender* sender,
976 const DriveApiUrlGenerator& url_generator) 992 const DriveApiUrlGenerator& url_generator)
977 : UrlFetchRequestBase(sender), 993 : UrlFetchRequestBase(sender),
978 sender_(sender), 994 sender_(sender),
979 url_generator_(url_generator), 995 url_generator_(url_generator),
996 committed_(false),
980 weak_ptr_factory_(this) { 997 weak_ptr_factory_(this) {
981 } 998 }
982 999
983 BatchUploadRequest::~BatchUploadRequest() { 1000 BatchUploadRequest::~BatchUploadRequest() {
984 for (const auto& child : child_requests_) { 1001 for (const auto& child : child_requests_) {
985 // Request will be deleted in RequestFinished method. 1002 // Request will be deleted in |RequestFinished| method.
986 sender_->RequestFinished(child.request); 1003 sender_->RequestFinished(child.request);
987 } 1004 }
988 } 1005 }
989 1006
990 void BatchUploadRequest::AddRequest(UrlFetchRequestBase* request) { 1007 void BatchUploadRequest::SetBoundaryForTesting(const std::string& boundary) {
1008 boundary_ = boundary;
1009 }
1010
1011 void BatchUploadRequest::AddRequest(BatchableRequestBase* request) {
1012 DCHECK(CalledOnValidThread());
991 DCHECK(request); 1013 DCHECK(request);
1014 DCHECK(GetChildEntry(request) == child_requests_.end());
1015 DCHECK(!committed_);
992 child_requests_.push_back(BatchUploadChildEntry(request)); 1016 child_requests_.push_back(BatchUploadChildEntry(request));
1017 request->Prepare(
1018 base::Bind(&BatchUploadRequest::OnChildRequestPrepared,
1019 weak_ptr_factory_.GetWeakPtr(),
1020 request));
1021 }
1022
1023 void BatchUploadRequest::OnChildRequestPrepared(
1024 RequestID request_id, DriveApiErrorCode result) {
1025 DCHECK(CalledOnValidThread());
1026 auto const child = GetChildEntry(request_id);
1027 DCHECK(child != child_requests_.end());
1028 if (IsSuccessfulDriveApiErrorCode(result)) {
1029 child->prepared = true;
1030 } else {
1031 child->request->RunCallbackOnPrematureFailure(result);
1032 sender_->RequestFinished(child->request);
1033 child_requests_.erase(child);
1034 }
1035 MayCompletePrepare();
993 } 1036 }
994 1037
995 void BatchUploadRequest::Commit() { 1038 void BatchUploadRequest::Commit() {
996 NOTIMPLEMENTED(); 1039 DCHECK(CalledOnValidThread());
1040 DCHECK(!committed_);
1041 CHECK(!child_requests_.empty());
1042 committed_ = true;
1043 MayCompletePrepare();
1044 }
1045
1046 void BatchUploadRequest::Prepare(const PrepareCallback& callback) {
1047 DCHECK(CalledOnValidThread());
1048 DCHECK(!callback.is_null());
1049 prepare_callback_ = callback;
1050 MayCompletePrepare();
1051 }
1052
1053 void BatchUploadRequest::Cancel() {
1054 for (auto& child : child_requests_) {
1055 // Request cancel should delete the request instance.
1056 child.request->Cancel();
1057 }
1058 child_requests_.clear();
1059 UrlFetchRequestBase::Cancel();
1060 }
1061
1062 // Obtains corresponding child entry of |request_id|. Returns NULL if the
1063 // entry is not found.
1064 std::vector<BatchUploadChildEntry>::iterator
1065 BatchUploadRequest::GetChildEntry(RequestID request_id) {
1066 for (auto it = child_requests_.begin(); it != child_requests_.end(); ++it) {
1067 if (it->request == request_id)
1068 return it;
1069 }
1070 return child_requests_.end();
1071 }
1072
1073 void BatchUploadRequest::MayCompletePrepare() {
1074 if (!committed_ || prepare_callback_.is_null())
1075 return;
1076 for (const auto& child : child_requests_) {
1077 if (!child.prepared)
1078 return;
1079 }
1080
1081 // Build multipart body here.
1082 std::vector<ContentTypeAndData> parts;
1083 for (const auto& child : child_requests_) {
1084 std::string type;
1085 std::string data;
1086 const bool result = child.request->GetContentData(&type, &data);
1087 // Upload request must have content data.
1088 DCHECK(result);
1089
1090 const GURL url = child.request->GetURL();
1091 std::string method;
1092 switch (child.request->GetRequestType()) {
1093 case net::URLFetcher::POST:
1094 method = "POST";
1095 break;
1096 case net::URLFetcher::PUT:
1097 method = "PUT";
1098 break;
1099 default:
1100 NOTREACHED();
1101 break;
1102 }
1103
1104 parts.push_back(ContentTypeAndData());
1105 parts.back().type = kHttpContentType;
1106 parts.back().data = base::StringPrintf(
1107 kBatchUploadRequestFormat,
1108 method.c_str(),
1109 url.path().c_str(),
1110 url_generator_.GetBatchUploadUrl().host().c_str(),
1111 type.c_str(),
1112 data.c_str());
1113 }
1114
1115 GenerateMultipartBody(MULTIPART_MIXED, boundary_, parts, &upload_content_);
1116 prepare_callback_.Run(HTTP_SUCCESS);
1117 }
1118
1119 bool BatchUploadRequest::GetContentData(std::string* upload_content_type,
1120 std::string* upload_content_data) {
1121 upload_content_type->assign(upload_content_.type);
1122 upload_content_data->assign(upload_content_.data);
1123 return true;
997 } 1124 }
998 1125
999 base::WeakPtr<BatchUploadRequest> 1126 base::WeakPtr<BatchUploadRequest>
1000 BatchUploadRequest::GetWeakPtrAsBatchUploadRequest() { 1127 BatchUploadRequest::GetWeakPtrAsBatchUploadRequest() {
1001 return weak_ptr_factory_.GetWeakPtr(); 1128 return weak_ptr_factory_.GetWeakPtr();
1002 } 1129 }
1003 1130
1004 GURL BatchUploadRequest::GetURL() const { 1131 GURL BatchUploadRequest::GetURL() const {
1005 NOTIMPLEMENTED(); 1132 return url_generator_.GetBatchUploadUrl();
1006 return GURL(); 1133 }
1134
1135 net::URLFetcher::RequestType BatchUploadRequest::GetRequestType() const {
1136 return net::URLFetcher::PUT;
1137 }
1138
1139 std::vector<std::string> BatchUploadRequest::GetExtraRequestHeaders() const {
1140 std::vector<std::string> headers;
1141 headers.push_back(kBatchUploadHeader);
1142 return headers;
1007 } 1143 }
1008 1144
1009 void BatchUploadRequest::ProcessURLFetchResults(const net::URLFetcher* source) { 1145 void BatchUploadRequest::ProcessURLFetchResults(const net::URLFetcher* source) {
1010 NOTIMPLEMENTED(); 1146 if (!IsSuccessfulDriveApiErrorCode(GetErrorCode())) {
1147 RunCallbackOnPrematureFailure(GetErrorCode());
1148 sender_->RequestFinished(this);
1149 return;
1150 }
1151
1152 for (auto& child : child_requests_) {
1153 // TODO(hirono): Split the mutlipart result and return the correct code and
1154 // body.
1155 child.request->ProcessURLFetchResults(HTTP_SERVICE_UNAVAILABLE, "");
1156 // Request deletes itself after processing.
1157 }
1158
1159 child_requests_.clear();
1011 } 1160 }
1012 1161
1013 void BatchUploadRequest::RunCallbackOnPrematureFailure(DriveApiErrorCode code) { 1162 void BatchUploadRequest::RunCallbackOnPrematureFailure(DriveApiErrorCode code) {
1014 NOTIMPLEMENTED(); 1163 for (auto child : child_requests_) {
1164 child.request->RunCallbackOnPrematureFailure(code);
1165 sender_->RequestFinished(child.request);
1166 }
1167 child_requests_.clear();
1015 } 1168 }
1016 1169
1017 } // namespace drive 1170 } // namespace drive
1018 } // namespace google_apis 1171 } // namespace google_apis
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698