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

Side by Side Diff: chrome/browser/media_gallery/mtp_device_delegate_impl_linux.cc

Issue 11414221: Media Galleries: On Linux, write data to snapshots in chunks rather than at once. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years 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
« no previous file with comments | « no previous file | no next file » | 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 #include "chrome/browser/media_gallery/mtp_device_delegate_impl_linux.h" 5 #include "chrome/browser/media_gallery/mtp_device_delegate_impl_linux.h"
6 6
7 #include <fcntl.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10
7 #include "base/bind.h" 11 #include "base/bind.h"
8 #include "base/file_path.h" 12 #include "base/file_path.h"
9 #include "base/file_util.h" 13 #include "base/file_util.h"
10 #include "base/sequenced_task_runner.h" 14 #include "base/sequenced_task_runner.h"
11 #include "base/sequenced_task_runner_helpers.h" 15 #include "base/sequenced_task_runner_helpers.h"
12 #include "base/string_util.h" 16 #include "base/string_util.h"
13 #include "base/synchronization/cancellation_flag.h" 17 #include "base/synchronization/cancellation_flag.h"
14 #include "base/threading/sequenced_worker_pool.h" 18 #include "base/threading/sequenced_worker_pool.h"
15 #include "chrome/browser/media_transfer_protocol/media_transfer_protocol_manager .h" 19 #include "chrome/browser/media_transfer_protocol/media_transfer_protocol_manager .h"
16 #include "chrome/browser/media_transfer_protocol/mtp_file_entry.pb.h" 20 #include "chrome/browser/media_transfer_protocol/mtp_file_entry.pb.h"
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 349
346 DISALLOW_COPY_AND_ASSIGN(GetFileInfoWorker); 350 DISALLOW_COPY_AND_ASSIGN(GetFileInfoWorker);
347 }; 351 };
348 352
349 // Worker class to read media device file data given a file |path|. 353 // Worker class to read media device file data given a file |path|.
350 class ReadFileWorker 354 class ReadFileWorker
351 : public RefCountedThreadSafe<ReadFileWorker, ReadFileWorkerDeleter> { 355 : public RefCountedThreadSafe<ReadFileWorker, ReadFileWorkerDeleter> {
352 public: 356 public:
353 // Constructed on |media_task_runner_| thread. 357 // Constructed on |media_task_runner_| thread.
354 ReadFileWorker(const std::string& handle, 358 ReadFileWorker(const std::string& handle,
355 const std::string& path, 359 const std::string& src_path,
356 uint32 total_size, 360 uint32 total_size,
361 const FilePath& dest_path,
357 SequencedTaskRunner* task_runner, 362 SequencedTaskRunner* task_runner,
358 WaitableEvent* task_completed_event, 363 WaitableEvent* task_completed_event,
359 WaitableEvent* shutdown_event) 364 WaitableEvent* shutdown_event)
360 : device_handle_(handle), 365 : device_handle_(handle),
361 path_(path), 366 src_path_(src_path),
362 total_bytes_(total_size), 367 total_bytes_(total_size),
368 dest_path_(dest_path),
369 bytes_read_(0),
363 error_occurred_(false), 370 error_occurred_(false),
364 media_task_runner_(task_runner), 371 media_task_runner_(task_runner),
365 on_task_completed_event_(task_completed_event), 372 on_task_completed_event_(task_completed_event),
366 on_shutdown_event_(shutdown_event) { 373 on_shutdown_event_(shutdown_event) {
367 DCHECK(on_task_completed_event_); 374 DCHECK(on_task_completed_event_);
368 DCHECK(on_shutdown_event_); 375 DCHECK(on_shutdown_event_);
369 } 376 }
370 377
371 // This function is invoked on |media_task_runner_| to post the task on UI 378 // This function is invoked on |media_task_runner_| to post the task on UI
372 // thread. This blocks the |media_task_runner_| until the task is complete. 379 // thread. This blocks the |media_task_runner_| until the task is complete.
373 void Run() { 380 void Run() {
374 if (on_shutdown_event_->IsSignaled()) { 381 if (on_shutdown_event_->IsSignaled()) {
375 // Process is in shutdown mode. 382 // Process is in shutdown mode.
376 // Do not post any task on |media_task_runner_|. 383 // Do not post any task on |media_task_runner_|.
377 return; 384 return;
378 } 385 }
379 386
380 while (!error_occurred_ && (data_.size() < total_bytes_) && 387 int dest_fd = open(dest_path_.value().c_str(), O_WRONLY);
381 !cancel_tasks_flag_.IsSet()) { 388 if (dest_fd < 0)
389 return;
390 file_util::ScopedFD dest_fd_scoper(&dest_fd);
391
392 while (bytes_read_ < total_bytes_) {
382 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 393 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
383 Bind(&ReadFileWorker::DoWorkOnUIThread, this)); 394 Bind(&ReadFileWorker::DoWorkOnUIThread, this));
384 on_task_completed_event_->Wait(); 395 on_task_completed_event_->Wait();
385 if (on_shutdown_event_->IsSignaled()) 396 if (error_occurred_)
397 break;
398 if (on_shutdown_event_->IsSignaled()) {
386 cancel_tasks_flag_.Set(); 399 cancel_tasks_flag_.Set();
400 break;
401 }
402
403 int bytes_written =
404 file_util::WriteFileDescriptor(dest_fd, data_.data(), data_.size());
405 if (static_cast<int>(data_.size()) != bytes_written)
406 break;
407
408 bytes_read_ += data_.size();
387 } 409 }
388 } 410 }
389 411
390 // Returns the media file contents received from mtpd. 412 bool Succeeded() const {
391 const std::string& data() const { return data_; } 413 return !error_occurred_ && (bytes_read_ == total_bytes_);
414 }
392 415
393 // Returns the |media_task_runner_| associated with this worker object. 416 // Returns the |media_task_runner_| associated with this worker object.
394 // This function is exposed for WorkerDeleter struct to access the 417 // This function is exposed for WorkerDeleter struct to access the
395 // |media_task_runner_|. 418 // |media_task_runner_|.
396 SequencedTaskRunner* media_task_runner() const { 419 SequencedTaskRunner* media_task_runner() const {
397 return media_task_runner_.get(); 420 return media_task_runner_.get();
398 } 421 }
399 422
400 private: 423 private:
401 friend struct WorkerDeleter<ReadFileWorker>; 424 friend struct WorkerDeleter<ReadFileWorker>;
402 friend class DeleteHelper<ReadFileWorker>; 425 friend class DeleteHelper<ReadFileWorker>;
403 friend class RefCountedThreadSafe<ReadFileWorker, ReadFileWorkerDeleter>; 426 friend class RefCountedThreadSafe<ReadFileWorker, ReadFileWorkerDeleter>;
404 427
405 // Destructed via ReadFileWorkerDeleter. 428 // Destructed via ReadFileWorkerDeleter.
406 virtual ~ReadFileWorker() { 429 virtual ~ReadFileWorker() {
407 // This object must be destructed on |media_task_runner_|. 430 // This object must be destructed on |media_task_runner_|.
408 } 431 }
409 432
410 // Dispatches a request to MediaTransferProtocolManager to get the media file 433 // Dispatches a request to MediaTransferProtocolManager to get the media file
411 // contents. 434 // contents.
412 void DoWorkOnUIThread() { 435 void DoWorkOnUIThread() {
413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 436 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
414 if (cancel_tasks_flag_.IsSet()) 437 if (cancel_tasks_flag_.IsSet())
415 return; 438 return;
416 439
417 GetMediaTransferProtocolManager()->ReadFileChunkByPath( 440 GetMediaTransferProtocolManager()->ReadFileChunkByPath(
418 device_handle_, path_, data_.size(), BytesToRead(), 441 device_handle_, src_path_, bytes_read_, BytesToRead(),
419 Bind(&ReadFileWorker::OnDidWorkOnUIThread, this)); 442 Bind(&ReadFileWorker::OnDidWorkOnUIThread, this));
420 } 443 }
421 444
422 // Query callback for DoWorkOnUIThread(). On success, |data| has the media 445 // Query callback for DoWorkOnUIThread(). On success, |data| has the media
423 // file contents. On failure, |error| is set to true. This function signals 446 // file contents. On failure, |error| is set to true. This function signals
424 // to unblock |media_task_runner_|. 447 // to unblock |media_task_runner_|.
425 void OnDidWorkOnUIThread(const std::string& data, bool error) { 448 void OnDidWorkOnUIThread(const std::string& data, bool error) {
426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 449 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
427 if (cancel_tasks_flag_.IsSet()) 450 if (cancel_tasks_flag_.IsSet())
428 return; 451 return;
429 452
430 error_occurred_ = error; 453 error_occurred_ = error || (data.size() != BytesToRead());
431 if (!error) { 454 if (!error_occurred_)
432 if ((BytesToRead() == data.size())) { 455 data_ = data;
433 // TODO(kmadhusu): Data could be really huge. Consider passing data by
434 // pointer/ref rather than by value here to avoid an extra data copy.
435 data_.append(data);
436 } else {
437 NOTREACHED();
438 error_occurred_ = true;
439 }
440 }
441 on_task_completed_event_->Signal(); 456 on_task_completed_event_->Signal();
442 } 457 }
443 458
444 uint32 BytesToRead() const { 459 uint32 BytesToRead() const {
445 // Read data in 1 MB chunks. 460 // Read data in 1 MB chunks.
446 static const uint32 kReadChunkSize = 1024 * 1024; 461 static const uint32 kReadChunkSize = 1024 * 1024;
447 return std::min(kReadChunkSize, 462 return std::min(kReadChunkSize, total_bytes_ - bytes_read_);
448 total_bytes_ - static_cast<uint32>(data_.size()));
449 } 463 }
450 464
451 // The device unique identifier to query the device. 465 // The device unique identifier to query the device.
452 const std::string device_handle_; 466 const std::string device_handle_;
453 467
454 // The media device file path. 468 // The media device file path.
455 const std::string path_; 469 const std::string src_path_;
456
457 // The data from mtpd.
458 std::string data_;
459 470
460 // Number of bytes to read. 471 // Number of bytes to read.
461 const uint32 total_bytes_; 472 const uint32 total_bytes_;
462 473
474 // Where to write the data read from the device.
475 const FilePath dest_path_;
476
477 /*****************************************************************************
478 * The variables below are accessed on both |media_task_runner_| and the UI
479 * thread. However, there's no concurrent access because the UI thread is in a
480 * blocked state when access occurs on |media_task_runner_|.
481 */
482
483 // Number of bytes read from the device.
484 uint32 bytes_read_;
485
486 // Temporary data storage.
487 std::string data_;
488
463 // Whether an error occurred during file transfer. 489 // Whether an error occurred during file transfer.
464 bool error_occurred_; 490 bool error_occurred_;
465 491
492 /****************************************************************************/
493
466 // A reference to |media_task_runner_| to destruct this object on the correct 494 // A reference to |media_task_runner_| to destruct this object on the correct
467 // thread. 495 // thread.
468 scoped_refptr<SequencedTaskRunner> media_task_runner_; 496 scoped_refptr<SequencedTaskRunner> media_task_runner_;
469 497
470 // |media_task_runner_| can wait on this event until the required operation 498 // |media_task_runner_| can wait on this event until the required operation
471 // is complete. 499 // is complete.
472 // TODO(kmadhusu): Remove this WaitableEvent after modifying the 500 // TODO(kmadhusu): Remove this WaitableEvent after modifying the
473 // DeviceMediaFileUtil functions as asynchronous functions. 501 // DeviceMediaFileUtil functions as asynchronous functions.
474 WaitableEvent* on_task_completed_event_; 502 WaitableEvent* on_task_completed_event_;
475 503
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 PlatformFileError error = GetFileInfo(device_file_path, file_info); 881 PlatformFileError error = GetFileInfo(device_file_path, file_info);
854 if (error != base::PLATFORM_FILE_OK) 882 if (error != base::PLATFORM_FILE_OK)
855 return error; 883 return error;
856 884
857 if (file_info->size <= 0 || file_info->size > kuint32max) 885 if (file_info->size <= 0 || file_info->size > kuint32max)
858 return base::PLATFORM_FILE_ERROR_FAILED; 886 return base::PLATFORM_FILE_ERROR_FAILED;
859 887
860 scoped_refptr<ReadFileWorker> worker(new ReadFileWorker( 888 scoped_refptr<ReadFileWorker> worker(new ReadFileWorker(
861 device_handle_, 889 device_handle_,
862 GetDeviceRelativePath(device_path_, device_file_path.value()), 890 GetDeviceRelativePath(device_path_, device_file_path.value()),
863 file_info->size, 891 file_info->size, local_path, media_task_runner_,
864 media_task_runner_, &on_task_completed_event_, &on_shutdown_event_)); 892 &on_task_completed_event_, &on_shutdown_event_));
865 worker->Run(); 893 worker->Run();
866 894
867 const std::string& file_data = worker->data(); 895 if (!worker->Succeeded())
868 int data_size = static_cast<int>(file_data.length());
869 if (file_data.empty() ||
870 file_util::WriteFile(local_path, file_data.c_str(),
871 data_size) != data_size) {
872 return base::PLATFORM_FILE_ERROR_FAILED; 896 return base::PLATFORM_FILE_ERROR_FAILED;
873 }
874 897
875 // Modify the last modified time to null. This prevents the time stamp 898 // Modify the last modified time to null. This prevents the time stamp
876 // verfication in LocalFileStreamReader. 899 // verfication in LocalFileStreamReader.
877 file_info->last_modified = base::Time(); 900 file_info->last_modified = base::Time();
878 return error; 901 return base::PLATFORM_FILE_OK;
879 } 902 }
880 903
881 SequencedTaskRunner* MTPDeviceDelegateImplLinux::GetMediaTaskRunner() { 904 SequencedTaskRunner* MTPDeviceDelegateImplLinux::GetMediaTaskRunner() {
882 return media_task_runner_.get(); 905 return media_task_runner_.get();
883 } 906 }
884 907
885 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { 908 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() {
886 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 909 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
887 // Caution: This function is called on the IO thread. Access only the thread 910 // Caution: This function is called on the IO thread. Access only the thread
888 // safe member variables in this function. Do all the clean up operations in 911 // safe member variables in this function. Do all the clean up operations in
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 } 952 }
930 953
931 void MTPDeviceDelegateImplLinux::DeleteDelegateOnTaskRunner() { 954 void MTPDeviceDelegateImplLinux::DeleteDelegateOnTaskRunner() {
932 DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); 955 DCHECK(media_task_runner_->RunsTasksOnCurrentThread());
933 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 956 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
934 Bind(&CloseStorageOnUIThread, device_handle_)); 957 Bind(&CloseStorageOnUIThread, device_handle_));
935 delete this; 958 delete this;
936 } 959 }
937 960
938 } // namespace chrome 961 } // namespace chrome
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698