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

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_uploader.cc

Issue 10540132: gdata: Convert GDataFileSystem::AddUploadFile() to asynchronous. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Get rid of the lock as suggested by Satoru. Created 8 years, 6 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 | Annotate | Revision Log
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 "chrome/browser/chromeos/gdata/gdata_uploader.h" 5 #include "chrome/browser/chromeos/gdata/gdata_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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 // Create buffer to hold upload data. 66 // Create buffer to hold upload data.
67 info->buf_len = std::min(info->file_size, kUploadChunkSize); 67 info->buf_len = std::min(info->file_size, kUploadChunkSize);
68 info->buf = new net::IOBuffer(info->buf_len); 68 info->buf = new net::IOBuffer(info->buf_len);
69 69
70 OpenFile(info); 70 OpenFile(info);
71 return upload_id; 71 return upload_id;
72 } 72 }
73 73
74 void GDataUploader::UpdateUpload(int upload_id, 74 void GDataUploader::UpdateUpload(int upload_id,
75 content::DownloadItem* download) { 75 content::DownloadItem* download) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
77
76 UploadFileInfo* upload_file_info = GetUploadFileInfo(upload_id); 78 UploadFileInfo* upload_file_info = GetUploadFileInfo(upload_id);
77 if (!upload_file_info) 79 if (!upload_file_info)
78 return; 80 return;
79 81
80 const int64 file_size = download->GetReceivedBytes(); 82 const int64 file_size = download->GetReceivedBytes();
81 83
82 // Update file_size and all_bytes_present. 84 // Update file_size and all_bytes_present.
83 DVLOG(1) << "Updating file size from " << upload_file_info->file_size 85 DVLOG(1) << "Updating file size from " << upload_file_info->file_size
84 << " to " << file_size 86 << " to " << file_size
85 << (download->AllDataSaved() ? " (AllDataSaved)" : " (In-progress)"); 87 << (download->AllDataSaved() ? " (AllDataSaved)" : " (In-progress)");
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 if (response.code == HTTP_CREATED) { 314 if (response.code == HTTP_CREATED) {
313 DVLOG(1) << "Successfully created uploaded file=[" 315 DVLOG(1) << "Successfully created uploaded file=["
314 << upload_file_info->title; 316 << upload_file_info->title;
315 317
316 // Done uploading. 318 // Done uploading.
317 upload_file_info->entry = entry.Pass(); 319 upload_file_info->entry = entry.Pass();
318 if (!upload_file_info->completion_callback.is_null()) { 320 if (!upload_file_info->completion_callback.is_null()) {
319 upload_file_info->completion_callback.Run(base::PLATFORM_FILE_OK, 321 upload_file_info->completion_callback.Run(base::PLATFORM_FILE_OK,
320 upload_file_info); 322 upload_file_info);
321 } 323 }
322 // TODO(achuith): DeleteUpload() here and let clients call 324
323 // GDataFileSystem::AddUploadedFile. 325 // Remove |upload_id| from the UploadFileInfoMap. The UploadFileInfo object
326 // will be deleted later upon transfer completion.
327 RemoveUpload(upload_id);
324 return; 328 return;
325 } 329 }
326 330
327 // If code is 308 (RESUME_INCOMPLETE) and range_received is what has been 331 // If code is 308 (RESUME_INCOMPLETE) and range_received is what has been
328 // previously uploaded (i.e. = upload_file_info->end_range), proceed to 332 // previously uploaded (i.e. = upload_file_info->end_range), proceed to
329 // upload the next chunk. 333 // upload the next chunk.
330 if (response.code != HTTP_RESUME_INCOMPLETE || 334 if (response.code != HTTP_RESUME_INCOMPLETE ||
331 response.start_range_received != 0 || 335 response.start_range_received != 0 ||
332 response.end_range_received != upload_file_info->end_range) { 336 response.end_range_received != upload_file_info->end_range) {
333 // TODO(achuith): Handle error cases, e.g. 337 // TODO(achuith): Handle error cases, e.g.
(...skipping 16 matching lines...) Expand all
350 354
351 // Continue uploading. 355 // Continue uploading.
352 UploadNextChunk(upload_file_info); 356 UploadNextChunk(upload_file_info);
353 } 357 }
354 358
355 void GDataUploader::MoveFileToCache(UploadFileInfo* upload_file_info) { 359 void GDataUploader::MoveFileToCache(UploadFileInfo* upload_file_info) {
356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 360 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
357 if (upload_file_info->entry == NULL) 361 if (upload_file_info->entry == NULL)
358 return; 362 return;
359 363
364 int upload_id = upload_file_info->upload_id;
achuithb 2012/06/14 22:27:18 nit: could we make this const?
hshi1 2012/06/15 00:16:28 Done.
360 DVLOG(1) << "MoveFileToCache " << upload_file_info->file_path.value(); 365 DVLOG(1) << "MoveFileToCache " << upload_file_info->file_path.value();
361 file_system_->AddUploadedFile( 366 file_system_->AddUploadedFile(
362 upload_file_info->gdata_path.DirName(), 367 upload_file_info->gdata_path.DirName(),
363 upload_file_info->entry.get(), 368 upload_file_info->entry.get(),
364 upload_file_info->file_path, 369 upload_file_info->file_path,
365 GDataCache::FILE_OPERATION_MOVE); 370 GDataCache::FILE_OPERATION_MOVE,
366 DeleteUpload(upload_file_info); 371 base::Bind(&GDataUploader::OnAddUploadFileComplete,
372 uploader_factory_.GetWeakPtr(),
373 upload_file_info));
374
375 // Remove |upload_id| from the UploadFileInfoMap. The UploadFileInfo object
376 // will be deleted in OnAddUploadFileComplete.
377 RemoveUpload(upload_id);
378 }
379
380 void GDataUploader::OnAddUploadFileComplete(UploadFileInfo* upload_file_info) {
381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
382 delete upload_file_info;
367 } 383 }
368 384
369 void GDataUploader::UploadFailed(UploadFileInfo* upload_file_info, 385 void GDataUploader::UploadFailed(UploadFileInfo* upload_file_info,
370 base::PlatformFileError error) { 386 base::PlatformFileError error) {
371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 387 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
388
389 int upload_id = upload_file_info->upload_id;
achuithb 2012/06/14 22:27:18 nit: make this const
hshi1 2012/06/15 00:16:28 Done.
372 LOG(ERROR) << "Upload failed " << upload_file_info->DebugString(); 390 LOG(ERROR) << "Upload failed " << upload_file_info->DebugString();
373 if (!upload_file_info->completion_callback.is_null()) { 391 if (!upload_file_info->completion_callback.is_null()) {
374 upload_file_info->completion_callback.Run(error, 392 upload_file_info->completion_callback.Run(error,
375 upload_file_info); 393 upload_file_info);
376 } 394 }
377 DeleteUpload(upload_file_info);
378 }
379 395
380 void GDataUploader::DeleteUpload(UploadFileInfo* upload_file_info) { 396 RemoveUpload(upload_id);
381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
382
383 DVLOG(1) << "Deleting upload " << upload_file_info->gdata_path.value();
384 pending_uploads_.erase(upload_file_info->upload_id);
385
386 // The file stream is closed by the destructor asynchronously.
387 delete upload_file_info->file_stream;
388 delete upload_file_info; 397 delete upload_file_info;
389 } 398 }
390 399
400 void GDataUploader::RemoveUpload(int upload_id) {
401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
402 pending_uploads_.erase(upload_id);
403 }
404
391 } // namespace gdata 405 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698