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/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 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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& path, |
| 356 uint32 total_size, | 360 uint32 read_offset, |
| 361 uint32 bytes_to_read, | |
| 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 path_(path), |
| 362 total_bytes_(total_size), | 367 read_offset_(read_offset), |
| 363 error_occurred_(false), | 368 bytes_to_read_(bytes_to_read), |
| 364 media_task_runner_(task_runner), | 369 media_task_runner_(task_runner), |
| 365 on_task_completed_event_(task_completed_event), | 370 on_task_completed_event_(task_completed_event), |
| 366 on_shutdown_event_(shutdown_event) { | 371 on_shutdown_event_(shutdown_event) { |
| 367 DCHECK(on_task_completed_event_); | 372 DCHECK(on_task_completed_event_); |
| 368 DCHECK(on_shutdown_event_); | 373 DCHECK(on_shutdown_event_); |
| 369 } | 374 } |
| 370 | 375 |
| 371 // This function is invoked on |media_task_runner_| to post the task on UI | 376 // 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. | 377 // thread. This blocks the |media_task_runner_| until the task is complete. |
| 373 void Run() { | 378 void Run() { |
| 374 if (on_shutdown_event_->IsSignaled()) { | 379 if (on_shutdown_event_->IsSignaled()) { |
| 375 // Process is in shutdown mode. | 380 // Process is in shutdown mode. |
| 376 // Do not post any task on |media_task_runner_|. | 381 // Do not post any task on |media_task_runner_|. |
| 377 return; | 382 return; |
| 378 } | 383 } |
| 379 | 384 |
| 380 while (!error_occurred_ && (data_.size() < total_bytes_) && | 385 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 381 !cancel_tasks_flag_.IsSet()) { | 386 Bind(&ReadFileWorker::DoWorkOnUIThread, this)); |
| 382 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 387 on_task_completed_event_->Wait(); |
| 383 Bind(&ReadFileWorker::DoWorkOnUIThread, this)); | 388 |
| 384 on_task_completed_event_->Wait(); | 389 if (on_shutdown_event_->IsSignaled()) |
| 385 if (on_shutdown_event_->IsSignaled()) | 390 cancel_tasks_flag_.Set(); |
|
kmadhusu
2012/11/29 18:08:17
Instead of moving this logic from ReadFileWorker =
Lei Zhang
2012/11/30 01:01:34
Done.
| |
| 386 cancel_tasks_flag_.Set(); | |
| 387 } | |
| 388 } | 391 } |
| 389 | 392 |
| 390 // Returns the media file contents received from mtpd. | 393 // Returns the media file contents received from mtpd. |
| 391 const std::string& data() const { return data_; } | 394 const std::string& data() const { return data_; } |
| 392 | 395 |
| 393 // Returns the |media_task_runner_| associated with this worker object. | 396 // Returns the |media_task_runner_| associated with this worker object. |
| 394 // This function is exposed for WorkerDeleter struct to access the | 397 // This function is exposed for WorkerDeleter struct to access the |
| 395 // |media_task_runner_|. | 398 // |media_task_runner_|. |
| 396 SequencedTaskRunner* media_task_runner() const { | 399 SequencedTaskRunner* media_task_runner() const { |
| 397 return media_task_runner_.get(); | 400 return media_task_runner_.get(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 408 } | 411 } |
| 409 | 412 |
| 410 // Dispatches a request to MediaTransferProtocolManager to get the media file | 413 // Dispatches a request to MediaTransferProtocolManager to get the media file |
| 411 // contents. | 414 // contents. |
| 412 void DoWorkOnUIThread() { | 415 void DoWorkOnUIThread() { |
| 413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 414 if (cancel_tasks_flag_.IsSet()) | 417 if (cancel_tasks_flag_.IsSet()) |
| 415 return; | 418 return; |
| 416 | 419 |
| 417 GetMediaTransferProtocolManager()->ReadFileChunkByPath( | 420 GetMediaTransferProtocolManager()->ReadFileChunkByPath( |
| 418 device_handle_, path_, data_.size(), BytesToRead(), | 421 device_handle_, path_, read_offset_, bytes_to_read_, |
| 419 Bind(&ReadFileWorker::OnDidWorkOnUIThread, this)); | 422 Bind(&ReadFileWorker::OnDidWorkOnUIThread, this)); |
| 420 } | 423 } |
| 421 | 424 |
| 422 // Query callback for DoWorkOnUIThread(). On success, |data| has the media | 425 // Query callback for DoWorkOnUIThread(). On success, |data| has the media |
| 423 // file contents. On failure, |error| is set to true. This function signals | 426 // file contents. On failure, |error| is set to true. This function signals |
| 424 // to unblock |media_task_runner_|. | 427 // to unblock |media_task_runner_|. |
| 425 void OnDidWorkOnUIThread(const std::string& data, bool error) { | 428 void OnDidWorkOnUIThread(const std::string& data, bool error) { |
| 426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 427 if (cancel_tasks_flag_.IsSet()) | 430 if (cancel_tasks_flag_.IsSet()) |
| 428 return; | 431 return; |
| 429 | 432 |
| 430 error_occurred_ = error; | |
| 431 if (!error) { | 433 if (!error) { |
| 432 if ((BytesToRead() == data.size())) { | 434 DCHECK_EQ(bytes_to_read_, data.size()); |
| 433 // TODO(kmadhusu): Data could be really huge. Consider passing data by | 435 data_ = data; |
| 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 } | 436 } |
| 441 on_task_completed_event_->Signal(); | 437 on_task_completed_event_->Signal(); |
| 442 } | 438 } |
| 443 | 439 |
| 444 uint32 BytesToRead() const { | |
| 445 // Read data in 1 MB chunks. | |
| 446 static const uint32 kReadChunkSize = 1024 * 1024; | |
| 447 return std::min(kReadChunkSize, | |
| 448 total_bytes_ - static_cast<uint32>(data_.size())); | |
| 449 } | |
| 450 | |
| 451 // The device unique identifier to query the device. | 440 // The device unique identifier to query the device. |
| 452 const std::string device_handle_; | 441 const std::string device_handle_; |
| 453 | 442 |
| 454 // The media device file path. | 443 // The media device file path. |
| 455 const std::string path_; | 444 const std::string path_; |
| 456 | 445 |
| 446 const uint32 read_offset_; | |
| 447 const uint32 bytes_to_read_; | |
| 448 | |
| 457 // The data from mtpd. | 449 // The data from mtpd. |
| 458 std::string data_; | 450 std::string data_; |
| 459 | 451 |
| 460 // Number of bytes to read. | |
| 461 const uint32 total_bytes_; | |
| 462 | |
| 463 // Whether an error occurred during file transfer. | |
| 464 bool error_occurred_; | |
| 465 | |
| 466 // A reference to |media_task_runner_| to destruct this object on the correct | 452 // A reference to |media_task_runner_| to destruct this object on the correct |
| 467 // thread. | 453 // thread. |
| 468 scoped_refptr<SequencedTaskRunner> media_task_runner_; | 454 scoped_refptr<SequencedTaskRunner> media_task_runner_; |
| 469 | 455 |
| 470 // |media_task_runner_| can wait on this event until the required operation | 456 // |media_task_runner_| can wait on this event until the required operation |
| 471 // is complete. | 457 // is complete. |
| 472 // TODO(kmadhusu): Remove this WaitableEvent after modifying the | 458 // TODO(kmadhusu): Remove this WaitableEvent after modifying the |
| 473 // DeviceMediaFileUtil functions as asynchronous functions. | 459 // DeviceMediaFileUtil functions as asynchronous functions. |
| 474 WaitableEvent* on_task_completed_event_; | 460 WaitableEvent* on_task_completed_event_; |
| 475 | 461 |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 850 if (!LazyInit()) | 836 if (!LazyInit()) |
| 851 return base::PLATFORM_FILE_ERROR_FAILED; | 837 return base::PLATFORM_FILE_ERROR_FAILED; |
| 852 | 838 |
| 853 PlatformFileError error = GetFileInfo(device_file_path, file_info); | 839 PlatformFileError error = GetFileInfo(device_file_path, file_info); |
| 854 if (error != base::PLATFORM_FILE_OK) | 840 if (error != base::PLATFORM_FILE_OK) |
| 855 return error; | 841 return error; |
| 856 | 842 |
| 857 if (file_info->size <= 0 || file_info->size > kuint32max) | 843 if (file_info->size <= 0 || file_info->size > kuint32max) |
| 858 return base::PLATFORM_FILE_ERROR_FAILED; | 844 return base::PLATFORM_FILE_ERROR_FAILED; |
| 859 | 845 |
| 860 scoped_refptr<ReadFileWorker> worker(new ReadFileWorker( | 846 // Read data in 1 MB chunks. |
| 861 device_handle_, | 847 static const uint32 kReadChunkSize = 1024 * 1024; |
| 862 GetDeviceRelativePath(device_path_, device_file_path.value()), | 848 uint32 bytes_remaining = file_info->size; |
| 863 file_info->size, | 849 int fd = open(local_path.value().c_str(), O_WRONLY); |
| 864 media_task_runner_, &on_task_completed_event_, &on_shutdown_event_)); | 850 if (fd < 0) |
| 865 worker->Run(); | 851 return base::PLATFORM_FILE_ERROR_FAILED; |
| 852 file_util::ScopedFD fd_scoper(&fd); | |
| 866 | 853 |
| 867 const std::string& file_data = worker->data(); | 854 while (bytes_remaining > 0) { |
| 868 int data_size = static_cast<int>(file_data.length()); | 855 uint32 offset = file_info->size - bytes_remaining; |
| 869 if (file_data.empty() || | 856 uint32 bytes_to_read = std::min(kReadChunkSize, bytes_remaining); |
| 870 file_util::WriteFile(local_path, file_data.c_str(), | 857 |
| 871 data_size) != data_size) { | 858 scoped_refptr<ReadFileWorker> worker(new ReadFileWorker( |
| 872 return base::PLATFORM_FILE_ERROR_FAILED; | 859 device_handle_, |
| 860 GetDeviceRelativePath(device_path_, device_file_path.value()), | |
| 861 offset, bytes_to_read, | |
| 862 media_task_runner_, &on_task_completed_event_, &on_shutdown_event_)); | |
| 863 worker->Run(); | |
| 864 | |
| 865 const std::string& file_data = worker->data(); | |
| 866 int data_size = static_cast<int>(file_data.length()); | |
| 867 if (bytes_to_read != file_data.length() || | |
| 868 file_util::WriteFileDescriptor(fd, file_data.c_str(), data_size) < 0) { | |
| 869 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 870 } | |
| 871 | |
| 872 bytes_remaining -= data_size; | |
| 873 } | 873 } |
| 874 | 874 |
| 875 // Modify the last modified time to null. This prevents the time stamp | 875 // Modify the last modified time to null. This prevents the time stamp |
| 876 // verfication in LocalFileStreamReader. | 876 // verfication in LocalFileStreamReader. |
| 877 file_info->last_modified = base::Time(); | 877 file_info->last_modified = base::Time(); |
| 878 return error; | 878 return base::PLATFORM_FILE_OK; |
| 879 } | 879 } |
| 880 | 880 |
| 881 SequencedTaskRunner* MTPDeviceDelegateImplLinux::GetMediaTaskRunner() { | 881 SequencedTaskRunner* MTPDeviceDelegateImplLinux::GetMediaTaskRunner() { |
| 882 return media_task_runner_.get(); | 882 return media_task_runner_.get(); |
| 883 } | 883 } |
| 884 | 884 |
| 885 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { | 885 void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { |
| 886 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 886 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 887 // Caution: This function is called on the IO thread. Access only the thread | 887 // 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 | 888 // 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 Loading... | |
| 929 } | 929 } |
| 930 | 930 |
| 931 void MTPDeviceDelegateImplLinux::DeleteDelegateOnTaskRunner() { | 931 void MTPDeviceDelegateImplLinux::DeleteDelegateOnTaskRunner() { |
| 932 DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); | 932 DCHECK(media_task_runner_->RunsTasksOnCurrentThread()); |
| 933 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 933 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 934 Bind(&CloseStorageOnUIThread, device_handle_)); | 934 Bind(&CloseStorageOnUIThread, device_handle_)); |
| 935 delete this; | 935 delete this; |
| 936 } | 936 } |
| 937 | 937 |
| 938 } // namespace chrome | 938 } // namespace chrome |
| OLD | NEW |