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

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

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: Improve comment. 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
« no previous file with comments | « google_apis/drive/base_requests_unittest.cc ('k') | google_apis/drive/drive_api_requests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #ifndef GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_ 5 #ifndef GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_
6 #define GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_ 6 #define GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 950 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 // UploadRangeRequestBase overrides. 961 // UploadRangeRequestBase overrides.
962 void OnRangeRequestComplete(const UploadRangeResponse& response, 962 void OnRangeRequestComplete(const UploadRangeResponse& response,
963 scoped_ptr<base::Value> value) override; 963 scoped_ptr<base::Value> value) override;
964 964
965 private: 965 private:
966 const UploadRangeCallback callback_; 966 const UploadRangeCallback callback_;
967 967
968 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequest); 968 DISALLOW_COPY_AND_ASSIGN(GetUploadStatusRequest);
969 }; 969 };
970 970
971 //======================= MultipartUploadNewFileRequest ======================= 971 //======================= MultipartUploadNewFileDelegate =======================
972 972
973 // This class performs the request for initiating the upload of a new file. 973 // This class performs the request for initiating the upload of a new file.
974 class MultipartUploadNewFileRequest : public MultipartUploadRequestBase { 974 class MultipartUploadNewFileDelegate : public MultipartUploadRequestBase {
975 public: 975 public:
976 // |parent_resource_id| should be the resource id of the parent directory. 976 // |parent_resource_id| should be the resource id of the parent directory.
977 // |title| should be set. 977 // |title| should be set.
978 // See also the comments of MultipartUploadRequestBase for more details 978 // See also the comments of MultipartUploadRequestBase for more details
979 // about the other parameters. 979 // about the other parameters.
980 MultipartUploadNewFileRequest(RequestSender* sender, 980 MultipartUploadNewFileDelegate(RequestSender* sender,
981 const std::string& title, 981 const std::string& title,
982 const std::string& parent_resource_id, 982 const std::string& parent_resource_id,
983 const std::string& content_type, 983 const std::string& content_type,
984 int64 content_length, 984 int64 content_length,
985 const base::Time& modified_date, 985 const base::Time& modified_date,
986 const base::Time& last_viewed_by_me_date, 986 const base::Time& last_viewed_by_me_date,
987 const base::FilePath& local_file_path, 987 const base::FilePath& local_file_path,
988 const Properties& properties, 988 const Properties& properties,
989 const DriveApiUrlGenerator& url_generator, 989 const DriveApiUrlGenerator& url_generator,
990 const FileResourceCallback& callback, 990 const FileResourceCallback& callback,
991 const ProgressCallback& progress_callback); 991 const ProgressCallback& progress_callback);
992 ~MultipartUploadNewFileRequest() override; 992 ~MultipartUploadNewFileDelegate() override;
993 993
994 protected: 994 protected:
995 // UrlFetchRequestBase overrides. 995 // UrlFetchRequestBase overrides.
996 GURL GetURL() const override; 996 GURL GetURL() const override;
997 net::URLFetcher::RequestType GetRequestType() const override; 997 net::URLFetcher::RequestType GetRequestType() const override;
998 998
999 private: 999 private:
1000 const bool has_modified_date_; 1000 const bool has_modified_date_;
1001 const DriveApiUrlGenerator url_generator_; 1001 const DriveApiUrlGenerator url_generator_;
1002 1002
1003 DISALLOW_COPY_AND_ASSIGN(MultipartUploadNewFileRequest); 1003 DISALLOW_COPY_AND_ASSIGN(MultipartUploadNewFileDelegate);
1004 }; 1004 };
1005 1005
1006 //======================= MultipartUploadExistingFileRequest =================== 1006 //====================== MultipartUploadExistingFileDelegate ===================
1007 1007
1008 // This class performs the request for initiating the upload of a new file. 1008 // This class performs the request for initiating the upload of a new file.
1009 class MultipartUploadExistingFileRequest : public MultipartUploadRequestBase { 1009 class MultipartUploadExistingFileDelegate : public MultipartUploadRequestBase {
1010 public: 1010 public:
1011 // |parent_resource_id| should be the resource id of the parent directory. 1011 // |parent_resource_id| should be the resource id of the parent directory.
1012 // |title| should be set. 1012 // |title| should be set.
1013 // See also the comments of MultipartUploadRequestBase for more details 1013 // See also the comments of MultipartUploadRequestBase for more details
1014 // about the other parameters. 1014 // about the other parameters.
1015 MultipartUploadExistingFileRequest(RequestSender* sender, 1015 MultipartUploadExistingFileDelegate(
1016 const std::string& title, 1016 RequestSender* sender,
1017 const std::string& resource_id, 1017 const std::string& title,
1018 const std::string& parent_resource_id, 1018 const std::string& resource_id,
1019 const std::string& content_type, 1019 const std::string& parent_resource_id,
1020 int64 content_length, 1020 const std::string& content_type,
1021 const base::Time& modified_date, 1021 int64 content_length,
1022 const base::Time& last_viewed_by_me_date, 1022 const base::Time& modified_date,
1023 const base::FilePath& local_file_path, 1023 const base::Time& last_viewed_by_me_date,
1024 const std::string& etag, 1024 const base::FilePath& local_file_path,
1025 const Properties& properties, 1025 const std::string& etag,
1026 const DriveApiUrlGenerator& url_generator, 1026 const Properties& properties,
1027 const FileResourceCallback& callback, 1027 const DriveApiUrlGenerator& url_generator,
1028 const ProgressCallback& progress_callback); 1028 const FileResourceCallback& callback,
1029 ~MultipartUploadExistingFileRequest() override; 1029 const ProgressCallback& progress_callback);
1030 ~MultipartUploadExistingFileDelegate() override;
1030 1031
1031 protected: 1032 protected:
1032 // UrlFetchRequestBase overrides. 1033 // UrlFetchRequestBase overrides.
1033 std::vector<std::string> GetExtraRequestHeaders() const override; 1034 std::vector<std::string> GetExtraRequestHeaders() const override;
1034 GURL GetURL() const override; 1035 GURL GetURL() const override;
1035 net::URLFetcher::RequestType GetRequestType() const override; 1036 net::URLFetcher::RequestType GetRequestType() const override;
1036 1037
1037 private: 1038 private:
1038 const std::string resource_id_; 1039 const std::string resource_id_;
1039 const std::string etag_; 1040 const std::string etag_;
1040 const bool has_modified_date_; 1041 const bool has_modified_date_;
1041 const DriveApiUrlGenerator url_generator_; 1042 const DriveApiUrlGenerator url_generator_;
1042 1043
1043 DISALLOW_COPY_AND_ASSIGN(MultipartUploadExistingFileRequest); 1044 DISALLOW_COPY_AND_ASSIGN(MultipartUploadExistingFileDelegate);
1044 }; 1045 };
1045 1046
1046 //========================== DownloadFileRequest ========================== 1047 //========================== DownloadFileRequest ==========================
1047 1048
1048 // This class performs the request for downloading of a specified file. 1049 // This class performs the request for downloading of a specified file.
1049 class DownloadFileRequest : public DownloadFileRequestBase { 1050 class DownloadFileRequest : public DownloadFileRequestBase {
1050 public: 1051 public:
1051 // See also DownloadFileRequestBase's comment for parameters meaning. 1052 // See also DownloadFileRequestBase's comment for parameters meaning.
1052 DownloadFileRequest(RequestSender* sender, 1053 DownloadFileRequest(RequestSender* sender,
1053 const DriveApiUrlGenerator& url_generator, 1054 const DriveApiUrlGenerator& url_generator,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 private: 1103 private:
1103 const DriveApiUrlGenerator url_generator_; 1104 const DriveApiUrlGenerator url_generator_;
1104 std::string id_; 1105 std::string id_;
1105 PermissionType type_; 1106 PermissionType type_;
1106 PermissionRole role_; 1107 PermissionRole role_;
1107 std::string value_; 1108 std::string value_;
1108 1109
1109 DISALLOW_COPY_AND_ASSIGN(PermissionsInsertRequest); 1110 DISALLOW_COPY_AND_ASSIGN(PermissionsInsertRequest);
1110 }; 1111 };
1111 1112
1113 //======================= SingleBatchableDelegateRequest =======================
1114
1115 // Request that is operated by single BatchableDelegate.
1116 class SingleBatchableDelegateRequest : public UrlFetchRequestBase {
1117 public:
1118 // The instance takes ownership of |delegate|.
1119 SingleBatchableDelegateRequest(RequestSender* sender,
1120 BatchableDelegate* delegate);
1121 ~SingleBatchableDelegateRequest() override;
1122
1123 private:
1124 GURL GetURL() const override;
1125 net::URLFetcher::RequestType GetRequestType() const override;
1126 std::vector<std::string> GetExtraRequestHeaders() const override;
1127 void Prepare(const PrepareCallback& callback) override;
1128 bool GetContentData(std::string* upload_content_type,
1129 std::string* upload_content) override;
1130 void RunCallbackOnPrematureFailure(DriveApiErrorCode code) override;
1131 void ProcessURLFetchResults(const net::URLFetcher* source) override;
1132 void OnURLFetchUploadProgress(const net::URLFetcher* source,
1133 int64 current,
1134 int64 total) override;
1135 scoped_ptr<BatchableDelegate> delegate_;
1136
1137 // Note: This should remain the last member so it'll be destroyed and
1138 // invalidate its weak pointers before any other members are destroyed.
1139 base::WeakPtrFactory<SingleBatchableDelegateRequest> weak_ptr_factory_;
1140
1141 DISALLOW_COPY_AND_ASSIGN(SingleBatchableDelegateRequest);
1142 };
1143
1112 //========================== BatchUploadRequest ========================== 1144 //========================== BatchUploadRequest ==========================
1113 1145
1114 struct BatchUploadChildEntry { 1146 class BatchUploadChildEntry {
1115 BatchUploadChildEntry() : request(NULL), prepared(false) {} 1147 public:
1116 explicit BatchUploadChildEntry(BatchableRequestBase* request) 1148 explicit BatchUploadChildEntry(BatchableDelegate* request);
1117 : request(request), prepared(false), data_offset(0), data_size(0) {} 1149 ~BatchUploadChildEntry();
1118 BatchableRequestBase* request; 1150 scoped_ptr<BatchableDelegate> request;
1119 bool prepared; 1151 bool prepared;
1120 int64 data_offset; 1152 int64 data_offset;
1121 int64 data_size; 1153 int64 data_size;
1154
1155 private:
1156 DISALLOW_COPY_AND_ASSIGN(BatchUploadChildEntry);
1122 }; 1157 };
1123 1158
1124 class BatchUploadRequest : public UrlFetchRequestBase { 1159 class BatchUploadRequest : public UrlFetchRequestBase {
1125 public: 1160 public:
1126 BatchUploadRequest(RequestSender* sender, 1161 BatchUploadRequest(RequestSender* sender,
1127 const DriveApiUrlGenerator& url_generator); 1162 const DriveApiUrlGenerator& url_generator);
1128 ~BatchUploadRequest() override; 1163 ~BatchUploadRequest() override;
1129 1164
1130 // Adds request to the batch request. 1165 // Adds request to the batch request. The instance takes ownership of
1131 void AddRequest(BatchableRequestBase* request); 1166 // |request|.
1167 void AddRequest(BatchableDelegate* request);
1132 1168
1133 // Completes building batch upload request, and starts to send the request to 1169 // Completes building batch upload request, and starts to send the request to
1134 // server. Must add at least one request before calling |Commit|. 1170 // server. Must add at least one request before calling |Commit|.
1135 void Commit(); 1171 void Commit();
1136 1172
1137 // Obtains weak pointer of this. 1173 // Obtains weak pointer of this.
1138 base::WeakPtr<BatchUploadRequest> GetWeakPtrAsBatchUploadRequest(); 1174 base::WeakPtr<BatchUploadRequest> GetWeakPtrAsBatchUploadRequest();
1139 1175
1140 // Set boundary. Only tests can use this method. 1176 // Set boundary. Only tests can use this method.
1141 void SetBoundaryForTesting(const std::string& boundary); 1177 void SetBoundaryForTesting(const std::string& boundary);
(...skipping 17 matching lines...) Expand all
1159 1195
1160 // content::UrlFetcherDelegate overrides. 1196 // content::UrlFetcherDelegate overrides.
1161 void OnURLFetchUploadProgress(const net::URLFetcher* source, 1197 void OnURLFetchUploadProgress(const net::URLFetcher* source,
1162 int64 current, 1198 int64 current,
1163 int64 total) override; 1199 int64 total) override;
1164 1200
1165 private: 1201 private:
1166 typedef void* RequestID; 1202 typedef void* RequestID;
1167 // Obtains corresponding child entry of |request_id|. Returns NULL if the 1203 // Obtains corresponding child entry of |request_id|. Returns NULL if the
1168 // entry is not found. 1204 // entry is not found.
1169 std::vector<BatchUploadChildEntry>::iterator GetChildEntry( 1205 ScopedVector<BatchUploadChildEntry>::iterator GetChildEntry(
1170 RequestID request_id); 1206 RequestID request_id);
1171 1207
1172 // Called after child requests' |Prepare| method. 1208 // Called after child requests' |Prepare| method.
1173 void OnChildRequestPrepared(RequestID request_id, DriveApiErrorCode result); 1209 void OnChildRequestPrepared(RequestID request_id, DriveApiErrorCode result);
1174 1210
1175 // Complete |Prepare| if possible. 1211 // Complete |Prepare| if possible.
1176 void MayCompletePrepare(); 1212 void MayCompletePrepare();
1177 1213
1178 // Process result for each child. 1214 // Process result for each child.
1179 void ProcessURLFetchResultsForChild(RequestID id, const std::string& body); 1215 void ProcessURLFetchResultsForChild(RequestID id, const std::string& body);
1180 1216
1181 RequestSender* const sender_; 1217 RequestSender* const sender_;
1182 const DriveApiUrlGenerator url_generator_; 1218 const DriveApiUrlGenerator url_generator_;
1183 std::vector<BatchUploadChildEntry> child_requests_; 1219 ScopedVector<BatchUploadChildEntry> child_requests_;
1184 1220
1185 PrepareCallback prepare_callback_; 1221 PrepareCallback prepare_callback_;
1186 bool committed_; 1222 bool committed_;
1187 1223
1188 // Boundary of multipart body. 1224 // Boundary of multipart body.
1189 std::string boundary_; 1225 std::string boundary_;
1190 1226
1191 // Multipart of child requests. 1227 // Multipart of child requests.
1192 ContentTypeAndData upload_content_; 1228 ContentTypeAndData upload_content_;
1193 1229
1194 // Last reported progress value. 1230 // Last reported progress value.
1195 int64 last_progress_value_; 1231 int64 last_progress_value_;
1196 1232
1197 // Note: This should remain the last member so it'll be destroyed and 1233 // Note: This should remain the last member so it'll be destroyed and
1198 // invalidate its weak pointers before any other members are destroyed. 1234 // invalidate its weak pointers before any other members are destroyed.
1199 base::WeakPtrFactory<BatchUploadRequest> weak_ptr_factory_; 1235 base::WeakPtrFactory<BatchUploadRequest> weak_ptr_factory_;
1200 1236
1201 DISALLOW_COPY_AND_ASSIGN(BatchUploadRequest); 1237 DISALLOW_COPY_AND_ASSIGN(BatchUploadRequest);
1202 }; 1238 };
1203 1239
1204 } // namespace drive 1240 } // namespace drive
1205 } // namespace google_apis 1241 } // namespace google_apis
1206 1242
1207 #endif // GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_ 1243 #endif // GOOGLE_APIS_DRIVE_DRIVE_API_REQUESTS_H_
OLDNEW
« no previous file with comments | « google_apis/drive/base_requests_unittest.cc ('k') | google_apis/drive/drive_api_requests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698