| 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 "components/drive/drive_uploader.h" | 5 #include "components/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" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/macros.h" |
| 12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
| 15 #include "components/drive/service/drive_service_interface.h" | 16 #include "components/drive/service/drive_service_interface.h" |
| 16 #include "content/public/browser/power_save_blocker.h" | 17 #include "content/public/browser/power_save_blocker.h" |
| 17 #include "google_apis/drive/drive_api_parser.h" | 18 #include "google_apis/drive/drive_api_parser.h" |
| 18 | 19 |
| 19 using google_apis::CancelCallback; | 20 using google_apis::CancelCallback; |
| 20 using google_apis::FileResource; | 21 using google_apis::FileResource; |
| 21 using google_apis::DRIVE_CANCELLED; | 22 using google_apis::DRIVE_CANCELLED; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 32 using google_apis::UploadRangeResponse; | 33 using google_apis::UploadRangeResponse; |
| 33 | 34 |
| 34 namespace drive { | 35 namespace drive { |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 // Upload data is split to multiple HTTP request each conveying kUploadChunkSize | 38 // Upload data is split to multiple HTTP request each conveying kUploadChunkSize |
| 38 // bytes (except the request for uploading the last chunk of data). | 39 // bytes (except the request for uploading the last chunk of data). |
| 39 // The value must be a multiple of 512KB according to the spec of GData WAPI and | 40 // The value must be a multiple of 512KB according to the spec of GData WAPI and |
| 40 // Drive API v2. It is set to a smaller value than 2^31 for working around | 41 // Drive API v2. It is set to a smaller value than 2^31 for working around |
| 41 // server side error (crbug.com/264089). | 42 // server side error (crbug.com/264089). |
| 42 const int64 kUploadChunkSize = (1LL << 30); // 1GB | 43 const int64_t kUploadChunkSize = (1LL << 30); // 1GB |
| 43 // Maximum file size to be uploaded by multipart requests. The file that is | 44 // Maximum file size to be uploaded by multipart requests. The file that is |
| 44 // larger than the size is processed by resumable upload. | 45 // larger than the size is processed by resumable upload. |
| 45 const int64 kMaxMultipartUploadSize = (1LL << 20); // 1MB | 46 const int64_t kMaxMultipartUploadSize = (1LL << 20); // 1MB |
| 46 | 47 |
| 47 // Drive upload protocol. This is used to back a histogram. Sync this with UMA | 48 // Drive upload protocol. This is used to back a histogram. Sync this with UMA |
| 48 // enum "DriveUploadProtocol" and treat this as append-only. | 49 // enum "DriveUploadProtocol" and treat this as append-only. |
| 49 enum DriveUploadProtocol { | 50 enum DriveUploadProtocol { |
| 50 UPLOAD_METHOD_RESUMABLE, | 51 UPLOAD_METHOD_RESUMABLE, |
| 51 UPLOAD_METHOD_MULTIPART, | 52 UPLOAD_METHOD_MULTIPART, |
| 52 UPLOAD_METHOD_BATCH, | 53 UPLOAD_METHOD_BATCH, |
| 53 UPLOAD_METHOD_MAX_VALUE | 54 UPLOAD_METHOD_MAX_VALUE |
| 54 }; | 55 }; |
| 55 | 56 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 const UploadCompletionCallback completion_callback; | 130 const UploadCompletionCallback completion_callback; |
| 130 | 131 |
| 131 // Callback to periodically notify the upload progress. | 132 // Callback to periodically notify the upload progress. |
| 132 const ProgressCallback progress_callback; | 133 const ProgressCallback progress_callback; |
| 133 | 134 |
| 134 // Location URL where file is to be uploaded to, returned from | 135 // Location URL where file is to be uploaded to, returned from |
| 135 // InitiateUpload. Used for the subsequent ResumeUpload requests. | 136 // InitiateUpload. Used for the subsequent ResumeUpload requests. |
| 136 GURL upload_location; | 137 GURL upload_location; |
| 137 | 138 |
| 138 // Header content-Length. | 139 // Header content-Length. |
| 139 int64 content_length; | 140 int64_t content_length; |
| 140 | 141 |
| 141 int64 next_start_position; | 142 int64_t next_start_position; |
| 142 | 143 |
| 143 // Blocks system suspend while upload is in progress. | 144 // Blocks system suspend while upload is in progress. |
| 144 scoped_ptr<content::PowerSaveBlocker> power_save_blocker; | 145 scoped_ptr<content::PowerSaveBlocker> power_save_blocker; |
| 145 | 146 |
| 146 // Fields for implementing cancellation. |cancel_callback| is non-null if | 147 // Fields for implementing cancellation. |cancel_callback| is non-null if |
| 147 // there is an in-flight HTTP request. In that case, |cancell_callback| will | 148 // there is an in-flight HTTP request. In that case, |cancell_callback| will |
| 148 // cancel the operation. |cancelled| is initially false and turns to true | 149 // cancel the operation. |cancelled| is initially false and turns to true |
| 149 // once Cancel() is called. DriveUploader will check this field before after | 150 // once Cancel() is called. DriveUploader will check this field before after |
| 150 // an async task other than HTTP requests and cancels the subsequent requests | 151 // an async task other than HTTP requests and cancels the subsequent requests |
| 151 // if this is flagged to true. | 152 // if this is flagged to true. |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 DCHECK_GE(upload_file_info->next_start_position, 0); | 402 DCHECK_GE(upload_file_info->next_start_position, 0); |
| 402 DCHECK_LE(upload_file_info->next_start_position, | 403 DCHECK_LE(upload_file_info->next_start_position, |
| 403 upload_file_info->content_length); | 404 upload_file_info->content_length); |
| 404 | 405 |
| 405 if (upload_file_info->cancelled) { | 406 if (upload_file_info->cancelled) { |
| 406 UploadFailed(upload_file_info.Pass(), DRIVE_CANCELLED); | 407 UploadFailed(upload_file_info.Pass(), DRIVE_CANCELLED); |
| 407 return; | 408 return; |
| 408 } | 409 } |
| 409 | 410 |
| 410 // Limit the size of data uploaded per each request by kUploadChunkSize. | 411 // Limit the size of data uploaded per each request by kUploadChunkSize. |
| 411 const int64 end_position = std::min( | 412 const int64_t end_position = |
| 412 upload_file_info->content_length, | 413 std::min(upload_file_info->content_length, |
| 413 upload_file_info->next_start_position + kUploadChunkSize); | 414 upload_file_info->next_start_position + kUploadChunkSize); |
| 414 | 415 |
| 415 UploadFileInfo* info_ptr = upload_file_info.get(); | 416 UploadFileInfo* info_ptr = upload_file_info.get(); |
| 416 info_ptr->cancel_callback = drive_service_->ResumeUpload( | 417 info_ptr->cancel_callback = drive_service_->ResumeUpload( |
| 417 info_ptr->upload_location, | 418 info_ptr->upload_location, |
| 418 info_ptr->next_start_position, | 419 info_ptr->next_start_position, |
| 419 end_position, | 420 end_position, |
| 420 info_ptr->content_length, | 421 info_ptr->content_length, |
| 421 info_ptr->content_type, | 422 info_ptr->content_type, |
| 422 info_ptr->file_path, | 423 info_ptr->file_path, |
| 423 base::Bind(&DriveUploader::OnUploadRangeResponseReceived, | 424 base::Bind(&DriveUploader::OnUploadRangeResponseReceived, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 | 479 |
| 479 DVLOG(1) << "Received range " << response.start_position_received | 480 DVLOG(1) << "Received range " << response.start_position_received |
| 480 << "-" << response.end_position_received | 481 << "-" << response.end_position_received |
| 481 << " for [" << upload_file_info->file_path.value() << "]"; | 482 << " for [" << upload_file_info->file_path.value() << "]"; |
| 482 | 483 |
| 483 upload_file_info->next_start_position = response.end_position_received; | 484 upload_file_info->next_start_position = response.end_position_received; |
| 484 UploadNextChunk(upload_file_info.Pass()); | 485 UploadNextChunk(upload_file_info.Pass()); |
| 485 } | 486 } |
| 486 | 487 |
| 487 void DriveUploader::OnUploadProgress(const ProgressCallback& callback, | 488 void DriveUploader::OnUploadProgress(const ProgressCallback& callback, |
| 488 int64 start_position, | 489 int64_t start_position, |
| 489 int64 total_size, | 490 int64_t total_size, |
| 490 int64 progress_of_chunk, | 491 int64_t progress_of_chunk, |
| 491 int64 total_of_chunk) { | 492 int64_t total_of_chunk) { |
| 492 if (!callback.is_null()) | 493 if (!callback.is_null()) |
| 493 callback.Run(start_position + progress_of_chunk, total_size); | 494 callback.Run(start_position + progress_of_chunk, total_size); |
| 494 } | 495 } |
| 495 | 496 |
| 496 void DriveUploader::UploadFailed(scoped_ptr<UploadFileInfo> upload_file_info, | 497 void DriveUploader::UploadFailed(scoped_ptr<UploadFileInfo> upload_file_info, |
| 497 DriveApiErrorCode error) { | 498 DriveApiErrorCode error) { |
| 498 DCHECK(thread_checker_.CalledOnValidThread()); | 499 DCHECK(thread_checker_.CalledOnValidThread()); |
| 499 | 500 |
| 500 DVLOG(1) << "Upload failed " << upload_file_info->DebugString(); | 501 DVLOG(1) << "Upload failed " << upload_file_info->DebugString(); |
| 501 | 502 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 524 } else { | 525 } else { |
| 525 DVLOG(1) << "Upload failed " << upload_file_info->DebugString(); | 526 DVLOG(1) << "Upload failed " << upload_file_info->DebugString(); |
| 526 if (error == HTTP_PRECONDITION) | 527 if (error == HTTP_PRECONDITION) |
| 527 error = HTTP_CONFLICT; // ETag mismatch. | 528 error = HTTP_CONFLICT; // ETag mismatch. |
| 528 upload_file_info->completion_callback.Run( | 529 upload_file_info->completion_callback.Run( |
| 529 error, upload_file_info->upload_location, scoped_ptr<FileResource>()); | 530 error, upload_file_info->upload_location, scoped_ptr<FileResource>()); |
| 530 } | 531 } |
| 531 } | 532 } |
| 532 | 533 |
| 533 } // namespace drive | 534 } // namespace drive |
| OLD | NEW |