Chromium Code Reviews| 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" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
| 14 #include "chrome/browser/drive/drive_service_interface.h" | 14 #include "chrome/browser/drive/drive_service_interface.h" |
| 15 #include "content/public/browser/power_save_blocker.h" | 15 #include "content/public/browser/power_save_blocker.h" |
| 16 #include "google_apis/drive/drive_api_parser.h" | 16 #include "google_apis/drive/drive_api_parser.h" |
| 17 | 17 |
| 18 using google_apis::CancelCallback; | 18 using google_apis::CancelCallback; |
| 19 using google_apis::FileResource; | 19 using google_apis::FileResource; |
| 20 using google_apis::GDATA_CANCELLED; | 20 using google_apis::DRIVE_CANCELLED; |
|
mtomasz
2015/01/29 06:18:02
nit: ditto
| |
| 21 using google_apis::GDataErrorCode; | 21 using google_apis::DriveApiErrorCode; |
| 22 using google_apis::GDATA_NO_SPACE; | 22 using google_apis::DRIVE_NO_SPACE; |
| 23 using google_apis::HTTP_CONFLICT; | 23 using google_apis::HTTP_CONFLICT; |
| 24 using google_apis::HTTP_CREATED; | 24 using google_apis::HTTP_CREATED; |
| 25 using google_apis::HTTP_FORBIDDEN; | 25 using google_apis::HTTP_FORBIDDEN; |
| 26 using google_apis::HTTP_NOT_FOUND; | 26 using google_apis::HTTP_NOT_FOUND; |
| 27 using google_apis::HTTP_PRECONDITION; | 27 using google_apis::HTTP_PRECONDITION; |
| 28 using google_apis::HTTP_RESUME_INCOMPLETE; | 28 using google_apis::HTTP_RESUME_INCOMPLETE; |
| 29 using google_apis::HTTP_SUCCESS; | 29 using google_apis::HTTP_SUCCESS; |
| 30 using google_apis::ProgressCallback; | 30 using google_apis::ProgressCallback; |
| 31 using google_apis::UploadRangeResponse; | 31 using google_apis::UploadRangeResponse; |
| 32 | 32 |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 bool get_file_size_result) { | 226 bool get_file_size_result) { |
| 227 DCHECK(thread_checker_.CalledOnValidThread()); | 227 DCHECK(thread_checker_.CalledOnValidThread()); |
| 228 | 228 |
| 229 if (!get_file_size_result) { | 229 if (!get_file_size_result) { |
| 230 UploadFailed(upload_file_info.Pass(), HTTP_NOT_FOUND); | 230 UploadFailed(upload_file_info.Pass(), HTTP_NOT_FOUND); |
| 231 return; | 231 return; |
| 232 } | 232 } |
| 233 DCHECK_GE(upload_file_info->content_length, 0); | 233 DCHECK_GE(upload_file_info->content_length, 0); |
| 234 | 234 |
| 235 if (upload_file_info->cancelled) { | 235 if (upload_file_info->cancelled) { |
| 236 UploadFailed(upload_file_info.Pass(), GDATA_CANCELLED); | 236 UploadFailed(upload_file_info.Pass(), DRIVE_CANCELLED); |
| 237 return; | 237 return; |
| 238 } | 238 } |
| 239 start_initiate_upload_callback.Run(upload_file_info.Pass()); | 239 start_initiate_upload_callback.Run(upload_file_info.Pass()); |
| 240 } | 240 } |
| 241 | 241 |
| 242 void DriveUploader::CallUploadServiceAPINewFile( | 242 void DriveUploader::CallUploadServiceAPINewFile( |
| 243 const std::string& parent_resource_id, | 243 const std::string& parent_resource_id, |
| 244 const std::string& title, | 244 const std::string& title, |
| 245 const UploadNewFileOptions& options, | 245 const UploadNewFileOptions& options, |
| 246 scoped_ptr<UploadFileInfo> upload_file_info) { | 246 scoped_ptr<UploadFileInfo> upload_file_info) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 info_ptr->cancel_callback = drive_service_->InitiateUploadExistingFile( | 283 info_ptr->cancel_callback = drive_service_->InitiateUploadExistingFile( |
| 284 info_ptr->content_type, info_ptr->content_length, resource_id, options, | 284 info_ptr->content_type, info_ptr->content_length, resource_id, options, |
| 285 base::Bind(&DriveUploader::OnUploadLocationReceived, | 285 base::Bind(&DriveUploader::OnUploadLocationReceived, |
| 286 weak_ptr_factory_.GetWeakPtr(), | 286 weak_ptr_factory_.GetWeakPtr(), |
| 287 base::Passed(&upload_file_info))); | 287 base::Passed(&upload_file_info))); |
| 288 } | 288 } |
| 289 } | 289 } |
| 290 | 290 |
| 291 void DriveUploader::OnUploadLocationReceived( | 291 void DriveUploader::OnUploadLocationReceived( |
| 292 scoped_ptr<UploadFileInfo> upload_file_info, | 292 scoped_ptr<UploadFileInfo> upload_file_info, |
| 293 GDataErrorCode code, | 293 DriveApiErrorCode code, |
| 294 const GURL& upload_location) { | 294 const GURL& upload_location) { |
| 295 DCHECK(thread_checker_.CalledOnValidThread()); | 295 DCHECK(thread_checker_.CalledOnValidThread()); |
| 296 | 296 |
| 297 DVLOG(1) << "Got upload location [" << upload_location.spec() | 297 DVLOG(1) << "Got upload location [" << upload_location.spec() |
| 298 << "] for [" << upload_file_info->file_path.value() << "]"; | 298 << "] for [" << upload_file_info->file_path.value() << "]"; |
| 299 | 299 |
| 300 if (code != HTTP_SUCCESS) { | 300 if (code != HTTP_SUCCESS) { |
| 301 if (code == HTTP_PRECONDITION) | 301 if (code == HTTP_PRECONDITION) |
| 302 code = HTTP_CONFLICT; // ETag mismatch. | 302 code = HTTP_CONFLICT; // ETag mismatch. |
| 303 UploadFailed(upload_file_info.Pass(), code); | 303 UploadFailed(upload_file_info.Pass(), code); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 325 | 325 |
| 326 void DriveUploader::UploadNextChunk( | 326 void DriveUploader::UploadNextChunk( |
| 327 scoped_ptr<UploadFileInfo> upload_file_info) { | 327 scoped_ptr<UploadFileInfo> upload_file_info) { |
| 328 DCHECK(thread_checker_.CalledOnValidThread()); | 328 DCHECK(thread_checker_.CalledOnValidThread()); |
| 329 DCHECK(upload_file_info); | 329 DCHECK(upload_file_info); |
| 330 DCHECK_GE(upload_file_info->next_start_position, 0); | 330 DCHECK_GE(upload_file_info->next_start_position, 0); |
| 331 DCHECK_LE(upload_file_info->next_start_position, | 331 DCHECK_LE(upload_file_info->next_start_position, |
| 332 upload_file_info->content_length); | 332 upload_file_info->content_length); |
| 333 | 333 |
| 334 if (upload_file_info->cancelled) { | 334 if (upload_file_info->cancelled) { |
| 335 UploadFailed(upload_file_info.Pass(), GDATA_CANCELLED); | 335 UploadFailed(upload_file_info.Pass(), DRIVE_CANCELLED); |
| 336 return; | 336 return; |
| 337 } | 337 } |
| 338 | 338 |
| 339 // Limit the size of data uploaded per each request by kUploadChunkSize. | 339 // Limit the size of data uploaded per each request by kUploadChunkSize. |
| 340 const int64 end_position = std::min( | 340 const int64 end_position = std::min( |
| 341 upload_file_info->content_length, | 341 upload_file_info->content_length, |
| 342 upload_file_info->next_start_position + kUploadChunkSize); | 342 upload_file_info->next_start_position + kUploadChunkSize); |
| 343 | 343 |
| 344 UploadFileInfo* info_ptr = upload_file_info.get(); | 344 UploadFileInfo* info_ptr = upload_file_info.get(); |
| 345 info_ptr->cancel_callback = drive_service_->ResumeUpload( | 345 info_ptr->cancel_callback = drive_service_->ResumeUpload( |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 // (meaning that the data is uploaded from the beginning of the file), | 394 // (meaning that the data is uploaded from the beginning of the file), |
| 395 // proceed to upload the next chunk. | 395 // proceed to upload the next chunk. |
| 396 if (response.code != HTTP_RESUME_INCOMPLETE || | 396 if (response.code != HTTP_RESUME_INCOMPLETE || |
| 397 response.start_position_received != 0) { | 397 response.start_position_received != 0) { |
| 398 DVLOG(1) | 398 DVLOG(1) |
| 399 << "UploadNextChunk http code=" << response.code | 399 << "UploadNextChunk http code=" << response.code |
| 400 << ", start_position_received=" << response.start_position_received | 400 << ", start_position_received=" << response.start_position_received |
| 401 << ", end_position_received=" << response.end_position_received; | 401 << ", end_position_received=" << response.end_position_received; |
| 402 UploadFailed( | 402 UploadFailed( |
| 403 upload_file_info.Pass(), | 403 upload_file_info.Pass(), |
| 404 response.code == HTTP_FORBIDDEN ? GDATA_NO_SPACE : response.code); | 404 response.code == HTTP_FORBIDDEN ? DRIVE_NO_SPACE : response.code); |
| 405 return; | 405 return; |
| 406 } | 406 } |
| 407 | 407 |
| 408 DVLOG(1) << "Received range " << response.start_position_received | 408 DVLOG(1) << "Received range " << response.start_position_received |
| 409 << "-" << response.end_position_received | 409 << "-" << response.end_position_received |
| 410 << " for [" << upload_file_info->file_path.value() << "]"; | 410 << " for [" << upload_file_info->file_path.value() << "]"; |
| 411 | 411 |
| 412 upload_file_info->next_start_position = response.end_position_received; | 412 upload_file_info->next_start_position = response.end_position_received; |
| 413 UploadNextChunk(upload_file_info.Pass()); | 413 UploadNextChunk(upload_file_info.Pass()); |
| 414 } | 414 } |
| 415 | 415 |
| 416 void DriveUploader::OnUploadProgress(const ProgressCallback& callback, | 416 void DriveUploader::OnUploadProgress(const ProgressCallback& callback, |
| 417 int64 start_position, | 417 int64 start_position, |
| 418 int64 total_size, | 418 int64 total_size, |
| 419 int64 progress_of_chunk, | 419 int64 progress_of_chunk, |
| 420 int64 total_of_chunk) { | 420 int64 total_of_chunk) { |
| 421 if (!callback.is_null()) | 421 if (!callback.is_null()) |
| 422 callback.Run(start_position + progress_of_chunk, total_size); | 422 callback.Run(start_position + progress_of_chunk, total_size); |
| 423 } | 423 } |
| 424 | 424 |
| 425 void DriveUploader::UploadFailed(scoped_ptr<UploadFileInfo> upload_file_info, | 425 void DriveUploader::UploadFailed(scoped_ptr<UploadFileInfo> upload_file_info, |
| 426 GDataErrorCode error) { | 426 DriveApiErrorCode error) { |
| 427 DCHECK(thread_checker_.CalledOnValidThread()); | 427 DCHECK(thread_checker_.CalledOnValidThread()); |
| 428 | 428 |
| 429 DVLOG(1) << "Upload failed " << upload_file_info->DebugString(); | 429 DVLOG(1) << "Upload failed " << upload_file_info->DebugString(); |
| 430 | 430 |
| 431 if (upload_file_info->next_start_position < 0) { | 431 if (upload_file_info->next_start_position < 0) { |
| 432 // Discard the upload location because no request could succeed with it. | 432 // Discard the upload location because no request could succeed with it. |
| 433 // Maybe it's obsolete. | 433 // Maybe it's obsolete. |
| 434 upload_file_info->upload_location = GURL(); | 434 upload_file_info->upload_location = GURL(); |
| 435 } | 435 } |
| 436 | 436 |
| 437 upload_file_info->completion_callback.Run( | 437 upload_file_info->completion_callback.Run( |
| 438 error, upload_file_info->upload_location, scoped_ptr<FileResource>()); | 438 error, upload_file_info->upload_location, scoped_ptr<FileResource>()); |
| 439 } | 439 } |
| 440 | 440 |
| 441 void DriveUploader::OnMultipartUploadComplete( | 441 void DriveUploader::OnMultipartUploadComplete( |
| 442 scoped_ptr<UploadFileInfo> upload_file_info, | 442 scoped_ptr<UploadFileInfo> upload_file_info, |
| 443 google_apis::GDataErrorCode error, | 443 google_apis::DriveApiErrorCode error, |
| 444 scoped_ptr<FileResource> entry) { | 444 scoped_ptr<FileResource> entry) { |
| 445 DCHECK(thread_checker_.CalledOnValidThread()); | 445 DCHECK(thread_checker_.CalledOnValidThread()); |
| 446 | 446 |
| 447 if (error == HTTP_CREATED || error == HTTP_SUCCESS) { | 447 if (error == HTTP_CREATED || error == HTTP_SUCCESS) { |
| 448 DVLOG(1) << "Successfully created uploaded file=[" | 448 DVLOG(1) << "Successfully created uploaded file=[" |
| 449 << upload_file_info->file_path.value() << "]"; | 449 << upload_file_info->file_path.value() << "]"; |
| 450 // Done uploading. | 450 // Done uploading. |
| 451 upload_file_info->completion_callback.Run( | 451 upload_file_info->completion_callback.Run( |
| 452 HTTP_SUCCESS, upload_file_info->upload_location, entry.Pass()); | 452 HTTP_SUCCESS, upload_file_info->upload_location, entry.Pass()); |
| 453 } else { | 453 } else { |
| 454 DVLOG(1) << "Upload failed " << upload_file_info->DebugString(); | 454 DVLOG(1) << "Upload failed " << upload_file_info->DebugString(); |
| 455 if (error == HTTP_PRECONDITION) | 455 if (error == HTTP_PRECONDITION) |
| 456 error = HTTP_CONFLICT; // ETag mismatch. | 456 error = HTTP_CONFLICT; // ETag mismatch. |
| 457 upload_file_info->completion_callback.Run( | 457 upload_file_info->completion_callback.Run( |
| 458 error, upload_file_info->upload_location, scoped_ptr<FileResource>()); | 458 error, upload_file_info->upload_location, scoped_ptr<FileResource>()); |
| 459 } | 459 } |
| 460 } | 460 } |
| 461 | 461 |
| 462 } // namespace drive | 462 } // namespace drive |
| OLD | NEW |