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 "chrome/browser/drive/drive_uploader.h" | 5 #include "chrome/browser/drive/drive_uploader.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 // bytes (except the request for uploading the last chunk of data). | 37 // bytes (except the request for uploading the last chunk of data). |
38 // The value must be a multiple of 512KB according to the spec of GData WAPI and | 38 // The value must be a multiple of 512KB according to the spec of GData WAPI and |
39 // Drive API v2. It is set to a smaller value than 2^31 for working around | 39 // Drive API v2. It is set to a smaller value than 2^31 for working around |
40 // server side error (crbug.com/264089). | 40 // server side error (crbug.com/264089). |
41 const int64 kUploadChunkSize = (1LL << 30); // 1GB | 41 const int64 kUploadChunkSize = (1LL << 30); // 1GB |
42 // Maximum file size to be uploaded by multipart requests. The file that is | 42 // Maximum file size to be uploaded by multipart requests. The file that is |
43 // larger than the size is processed by resumable upload. | 43 // larger than the size is processed by resumable upload. |
44 const int64 kMaxMultipartUploadSize = (1LL << 20); // 1MB | 44 const int64 kMaxMultipartUploadSize = (1LL << 20); // 1MB |
45 } // namespace | 45 } // namespace |
46 | 46 |
| 47 // Refcounted helper class to manage batch request. DriveUploader uses the class |
| 48 // for keeping the BatchRequestConfigurator instance while it prepares upload |
| 49 // file information asynchronously. DriveUploader discard the reference after |
| 50 // getting file information and the instance will be destroyed after all |
| 51 // preparations complete. At that time, the helper instance commits owned batch |
| 52 // request at the destrutor. |
| 53 class DriveUploader::RefCountedBatchRequest |
| 54 : public base::RefCounted<RefCountedBatchRequest> { |
| 55 public: |
| 56 RefCountedBatchRequest( |
| 57 scoped_ptr<BatchRequestConfiguratorInterface> configurator) |
| 58 : configurator_(configurator.Pass()) {} |
| 59 |
| 60 // Gets pointer of BatchRequestConfiguratorInterface owned by the instance. |
| 61 BatchRequestConfiguratorInterface* configurator() const { |
| 62 return configurator_.get(); |
| 63 } |
| 64 |
| 65 private: |
| 66 friend class base::RefCounted<RefCountedBatchRequest>; |
| 67 ~RefCountedBatchRequest() { configurator_->Commit(); } |
| 68 scoped_ptr<BatchRequestConfiguratorInterface> configurator_; |
| 69 }; |
| 70 |
47 // Structure containing current upload information of file, passed between | 71 // Structure containing current upload information of file, passed between |
48 // DriveServiceInterface methods and callbacks. | 72 // DriveServiceInterface methods and callbacks. |
49 struct DriveUploader::UploadFileInfo { | 73 struct DriveUploader::UploadFileInfo { |
50 UploadFileInfo(const base::FilePath& local_path, | 74 UploadFileInfo(const base::FilePath& local_path, |
51 const std::string& content_type, | 75 const std::string& content_type, |
52 const UploadCompletionCallback& callback, | 76 const UploadCompletionCallback& callback, |
53 const ProgressCallback& progress_callback) | 77 const ProgressCallback& progress_callback) |
54 : file_path(local_path), | 78 : file_path(local_path), |
55 content_type(content_type), | 79 content_type(content_type), |
56 completion_callback(callback), | 80 completion_callback(callback), |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 DCHECK(!local_file_path.empty()); | 172 DCHECK(!local_file_path.empty()); |
149 DCHECK(!title.empty()); | 173 DCHECK(!title.empty()); |
150 DCHECK(!content_type.empty()); | 174 DCHECK(!content_type.empty()); |
151 DCHECK(!callback.is_null()); | 175 DCHECK(!callback.is_null()); |
152 | 176 |
153 return StartUploadFile( | 177 return StartUploadFile( |
154 scoped_ptr<UploadFileInfo>(new UploadFileInfo( | 178 scoped_ptr<UploadFileInfo>(new UploadFileInfo( |
155 local_file_path, content_type, callback, progress_callback)), | 179 local_file_path, content_type, callback, progress_callback)), |
156 base::Bind(&DriveUploader::CallUploadServiceAPINewFile, | 180 base::Bind(&DriveUploader::CallUploadServiceAPINewFile, |
157 weak_ptr_factory_.GetWeakPtr(), parent_resource_id, title, | 181 weak_ptr_factory_.GetWeakPtr(), parent_resource_id, title, |
158 options)); | 182 options, current_batch_request_)); |
| 183 } |
| 184 |
| 185 void DriveUploader::StartBatchProcessing() { |
| 186 DCHECK(current_batch_request_ == nullptr); |
| 187 current_batch_request_ = |
| 188 new RefCountedBatchRequest(drive_service_->StartBatchRequest().Pass()); |
| 189 } |
| 190 |
| 191 void DriveUploader::StopBatchProcessing() { |
| 192 current_batch_request_ = nullptr; |
159 } | 193 } |
160 | 194 |
161 CancelCallback DriveUploader::UploadExistingFile( | 195 CancelCallback DriveUploader::UploadExistingFile( |
162 const std::string& resource_id, | 196 const std::string& resource_id, |
163 const base::FilePath& local_file_path, | 197 const base::FilePath& local_file_path, |
164 const std::string& content_type, | 198 const std::string& content_type, |
165 const UploadExistingFileOptions& options, | 199 const UploadExistingFileOptions& options, |
166 const UploadCompletionCallback& callback, | 200 const UploadCompletionCallback& callback, |
167 const ProgressCallback& progress_callback) { | 201 const ProgressCallback& progress_callback) { |
168 DCHECK(thread_checker_.CalledOnValidThread()); | 202 DCHECK(thread_checker_.CalledOnValidThread()); |
169 DCHECK(!resource_id.empty()); | 203 DCHECK(!resource_id.empty()); |
170 DCHECK(!local_file_path.empty()); | 204 DCHECK(!local_file_path.empty()); |
171 DCHECK(!content_type.empty()); | 205 DCHECK(!content_type.empty()); |
172 DCHECK(!callback.is_null()); | 206 DCHECK(!callback.is_null()); |
173 | 207 |
174 return StartUploadFile( | 208 return StartUploadFile( |
175 scoped_ptr<UploadFileInfo>(new UploadFileInfo( | 209 scoped_ptr<UploadFileInfo>(new UploadFileInfo( |
176 local_file_path, content_type, callback, progress_callback)), | 210 local_file_path, content_type, callback, progress_callback)), |
177 base::Bind(&DriveUploader::CallUploadServiceAPIExistingFile, | 211 base::Bind(&DriveUploader::CallUploadServiceAPIExistingFile, |
178 weak_ptr_factory_.GetWeakPtr(), resource_id, options)); | 212 weak_ptr_factory_.GetWeakPtr(), resource_id, options, |
| 213 current_batch_request_)); |
179 } | 214 } |
180 | 215 |
181 CancelCallback DriveUploader::ResumeUploadFile( | 216 CancelCallback DriveUploader::ResumeUploadFile( |
182 const GURL& upload_location, | 217 const GURL& upload_location, |
183 const base::FilePath& local_file_path, | 218 const base::FilePath& local_file_path, |
184 const std::string& content_type, | 219 const std::string& content_type, |
185 const UploadCompletionCallback& callback, | 220 const UploadCompletionCallback& callback, |
186 const ProgressCallback& progress_callback) { | 221 const ProgressCallback& progress_callback) { |
187 DCHECK(thread_checker_.CalledOnValidThread()); | 222 DCHECK(thread_checker_.CalledOnValidThread()); |
188 DCHECK(!local_file_path.empty()); | 223 DCHECK(!local_file_path.empty()); |
189 DCHECK(!content_type.empty()); | 224 DCHECK(!content_type.empty()); |
190 DCHECK(!callback.is_null()); | 225 DCHECK(!callback.is_null()); |
191 | 226 |
192 scoped_ptr<UploadFileInfo> upload_file_info(new UploadFileInfo( | 227 scoped_ptr<UploadFileInfo> upload_file_info(new UploadFileInfo( |
193 local_file_path, content_type, | 228 local_file_path, content_type, callback, progress_callback)); |
194 callback, progress_callback)); | |
195 upload_file_info->upload_location = upload_location; | 229 upload_file_info->upload_location = upload_location; |
196 | 230 |
197 return StartUploadFile( | 231 return StartUploadFile( |
198 upload_file_info.Pass(), | 232 upload_file_info.Pass(), |
199 base::Bind(&DriveUploader::StartGetUploadStatus, | 233 base::Bind(&DriveUploader::StartGetUploadStatus, |
200 weak_ptr_factory_.GetWeakPtr())); | 234 weak_ptr_factory_.GetWeakPtr())); |
201 } | 235 } |
202 | 236 |
203 CancelCallback DriveUploader::StartUploadFile( | 237 CancelCallback DriveUploader::StartUploadFile( |
204 scoped_ptr<UploadFileInfo> upload_file_info, | 238 scoped_ptr<UploadFileInfo> upload_file_info, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 UploadFailed(upload_file_info.Pass(), DRIVE_CANCELLED); | 270 UploadFailed(upload_file_info.Pass(), DRIVE_CANCELLED); |
237 return; | 271 return; |
238 } | 272 } |
239 start_initiate_upload_callback.Run(upload_file_info.Pass()); | 273 start_initiate_upload_callback.Run(upload_file_info.Pass()); |
240 } | 274 } |
241 | 275 |
242 void DriveUploader::CallUploadServiceAPINewFile( | 276 void DriveUploader::CallUploadServiceAPINewFile( |
243 const std::string& parent_resource_id, | 277 const std::string& parent_resource_id, |
244 const std::string& title, | 278 const std::string& title, |
245 const UploadNewFileOptions& options, | 279 const UploadNewFileOptions& options, |
| 280 const scoped_refptr<RefCountedBatchRequest>& batch_request, |
246 scoped_ptr<UploadFileInfo> upload_file_info) { | 281 scoped_ptr<UploadFileInfo> upload_file_info) { |
247 DCHECK(thread_checker_.CalledOnValidThread()); | 282 DCHECK(thread_checker_.CalledOnValidThread()); |
248 | 283 |
249 UploadFileInfo* const info_ptr = upload_file_info.get(); | 284 UploadFileInfo* const info_ptr = upload_file_info.get(); |
250 if (info_ptr->content_length <= kMaxMultipartUploadSize) { | 285 if (info_ptr->content_length <= kMaxMultipartUploadSize) { |
251 info_ptr->cancel_callback = drive_service_->MultipartUploadNewFile( | 286 DriveServiceBatchOperationsInterface* service = drive_service_; |
| 287 // If this is a batched request, calls the API on the request instead. |
| 288 if (batch_request.get()) |
| 289 service = batch_request->configurator(); |
| 290 info_ptr->cancel_callback = service->MultipartUploadNewFile( |
252 info_ptr->content_type, info_ptr->content_length, parent_resource_id, | 291 info_ptr->content_type, info_ptr->content_length, parent_resource_id, |
253 title, info_ptr->file_path, options, | 292 title, info_ptr->file_path, options, |
254 base::Bind(&DriveUploader::OnMultipartUploadComplete, | 293 base::Bind(&DriveUploader::OnMultipartUploadComplete, |
255 weak_ptr_factory_.GetWeakPtr(), | 294 weak_ptr_factory_.GetWeakPtr(), |
256 base::Passed(&upload_file_info)), | 295 base::Passed(&upload_file_info)), |
257 info_ptr->progress_callback); | 296 info_ptr->progress_callback); |
258 } else { | 297 } else { |
259 info_ptr->cancel_callback = drive_service_->InitiateUploadNewFile( | 298 info_ptr->cancel_callback = drive_service_->InitiateUploadNewFile( |
260 info_ptr->content_type, info_ptr->content_length, parent_resource_id, | 299 info_ptr->content_type, info_ptr->content_length, parent_resource_id, |
261 title, options, base::Bind(&DriveUploader::OnUploadLocationReceived, | 300 title, options, base::Bind(&DriveUploader::OnUploadLocationReceived, |
262 weak_ptr_factory_.GetWeakPtr(), | 301 weak_ptr_factory_.GetWeakPtr(), |
263 base::Passed(&upload_file_info))); | 302 base::Passed(&upload_file_info))); |
264 } | 303 } |
265 } | 304 } |
266 | 305 |
267 void DriveUploader::CallUploadServiceAPIExistingFile( | 306 void DriveUploader::CallUploadServiceAPIExistingFile( |
268 const std::string& resource_id, | 307 const std::string& resource_id, |
269 const UploadExistingFileOptions& options, | 308 const UploadExistingFileOptions& options, |
| 309 const scoped_refptr<RefCountedBatchRequest>& batch_request, |
270 scoped_ptr<UploadFileInfo> upload_file_info) { | 310 scoped_ptr<UploadFileInfo> upload_file_info) { |
271 DCHECK(thread_checker_.CalledOnValidThread()); | 311 DCHECK(thread_checker_.CalledOnValidThread()); |
272 | 312 |
273 UploadFileInfo* const info_ptr = upload_file_info.get(); | 313 UploadFileInfo* const info_ptr = upload_file_info.get(); |
274 if (info_ptr->content_length <= kMaxMultipartUploadSize) { | 314 if (info_ptr->content_length <= kMaxMultipartUploadSize) { |
275 info_ptr->cancel_callback = drive_service_->MultipartUploadExistingFile( | 315 DriveServiceBatchOperationsInterface* service = drive_service_; |
| 316 // If this is a batched request, calls the API on the request instead. |
| 317 if (batch_request.get()) |
| 318 service = batch_request->configurator(); |
| 319 info_ptr->cancel_callback = service->MultipartUploadExistingFile( |
276 info_ptr->content_type, info_ptr->content_length, resource_id, | 320 info_ptr->content_type, info_ptr->content_length, resource_id, |
277 info_ptr->file_path, options, | 321 info_ptr->file_path, options, |
278 base::Bind(&DriveUploader::OnMultipartUploadComplete, | 322 base::Bind(&DriveUploader::OnMultipartUploadComplete, |
279 weak_ptr_factory_.GetWeakPtr(), | 323 weak_ptr_factory_.GetWeakPtr(), |
280 base::Passed(&upload_file_info)), | 324 base::Passed(&upload_file_info)), |
281 info_ptr->progress_callback); | 325 info_ptr->progress_callback); |
282 } else { | 326 } else { |
283 info_ptr->cancel_callback = drive_service_->InitiateUploadExistingFile( | 327 info_ptr->cancel_callback = drive_service_->InitiateUploadExistingFile( |
284 info_ptr->content_type, info_ptr->content_length, resource_id, options, | 328 info_ptr->content_type, info_ptr->content_length, resource_id, options, |
285 base::Bind(&DriveUploader::OnUploadLocationReceived, | 329 base::Bind(&DriveUploader::OnUploadLocationReceived, |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 } else { | 497 } else { |
454 DVLOG(1) << "Upload failed " << upload_file_info->DebugString(); | 498 DVLOG(1) << "Upload failed " << upload_file_info->DebugString(); |
455 if (error == HTTP_PRECONDITION) | 499 if (error == HTTP_PRECONDITION) |
456 error = HTTP_CONFLICT; // ETag mismatch. | 500 error = HTTP_CONFLICT; // ETag mismatch. |
457 upload_file_info->completion_callback.Run( | 501 upload_file_info->completion_callback.Run( |
458 error, upload_file_info->upload_location, scoped_ptr<FileResource>()); | 502 error, upload_file_info->upload_location, scoped_ptr<FileResource>()); |
459 } | 503 } |
460 } | 504 } |
461 | 505 |
462 } // namespace drive | 506 } // namespace drive |
OLD | NEW |