Chromium Code Reviews| Index: chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.cc |
| diff --git a/chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.cc b/chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.cc |
| index b1cd91c59e85b22ebcf1fb10cdbc323495c7d5be..a2edab40738a59a0470527ef72d798fd83968051 100644 |
| --- a/chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.cc |
| +++ b/chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.cc |
| @@ -4,11 +4,13 @@ |
| #include "chrome/browser/media_galleries/linux/mtp_device_delegate_impl_linux.h" |
| +#include <fcntl.h> |
| #include <algorithm> |
| #include <vector> |
| #include "base/bind.h" |
| #include "base/numerics/safe_conversions.h" |
| +#include "base/posix/eintr_wrapper.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_split.h" |
| #include "base/strings/string_util.h" |
| @@ -70,6 +72,7 @@ MTPDeviceTaskHelper* GetDeviceTaskHelperForStorage( |
| // |reply_callback| runs on the IO thread. |
| void OpenStorageOnUIThread( |
| const std::string& storage_name, |
| + const bool read_only, |
| const MTPDeviceTaskHelper::OpenStorageCallback& reply_callback) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| MTPDeviceTaskHelper* task_helper = |
| @@ -79,7 +82,7 @@ void OpenStorageOnUIThread( |
| MTPDeviceTaskHelperMapService::GetInstance()->CreateDeviceTaskHelper( |
| storage_name); |
| } |
| - task_helper->OpenStorage(storage_name, reply_callback); |
| + task_helper->OpenStorage(storage_name, read_only, reply_callback); |
| } |
| // Enumerates the |dir_id| directory file entries. |
| @@ -169,6 +172,24 @@ void ReadBytesOnUIThread( |
| task_helper->ReadBytes(request); |
| } |
| +void CopyFileFromLocalOnUIThread( |
| + const std::string& storage_name, |
| + const int source_file_descriptor, |
| + const uint32 parent_id, |
| + const std::string& file_name, |
| + const MTPDeviceTaskHelper::CopyFileFromLocalSuccessCallback& |
| + success_callback, |
| + const MTPDeviceTaskHelper::ErrorCallback& error_callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + MTPDeviceTaskHelper* task_helper = |
| + GetDeviceTaskHelperForStorage(storage_name); |
| + if (!task_helper) |
| + return; |
| + task_helper->CopyFileFromLocal(storage_name, source_file_descriptor, |
| + parent_id, file_name, success_callback, |
| + error_callback); |
| +} |
| + |
| // Closes the device storage specified by the |storage_name| and destroys the |
| // MTPDeviceTaskHelper object associated with the device storage. |
| // |
| @@ -310,10 +331,12 @@ bool MTPDeviceDelegateImplLinux::MTPFileNode::DeleteChild(uint32 file_id) { |
| } |
| MTPDeviceDelegateImplLinux::MTPDeviceDelegateImplLinux( |
| - const std::string& device_location) |
| + const std::string& device_location, |
| + const bool read_only) |
| : init_state_(UNINITIALIZED), |
| task_in_progress_(false), |
| device_path_(device_location), |
| + read_only_(read_only), |
| root_node_(new MTPFileNode(mtpd::kRootFileId, |
| "", // Root node has no name. |
| NULL, // And no parent node. |
| @@ -432,6 +455,27 @@ void MTPDeviceDelegateImplLinux::ReadBytes( |
| closure)); |
| } |
| +bool MTPDeviceDelegateImplLinux::IsReadOnly() { |
| + return read_only_; |
| +} |
| + |
| +void MTPDeviceDelegateImplLinux::CopyFileFromLocal( |
| + const base::FilePath& source_file_path, |
| + const base::FilePath& device_file_path, |
| + const CopyFileFromLocalSuccessCallback& success_callback, |
| + const ErrorCallback& error_callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + DCHECK(!source_file_path.empty()); |
| + DCHECK(!device_file_path.empty()); |
| + base::Closure closure = |
| + base::Bind(&MTPDeviceDelegateImplLinux::CopyFileFromLocalInternal, |
| + weak_ptr_factory_.GetWeakPtr(), source_file_path, |
| + device_file_path, success_callback, error_callback); |
| + EnsureInitAndRunTask(PendingTaskInfo(device_file_path.DirName(), |
| + content::BrowserThread::IO, FROM_HERE, |
| + closure)); |
| +} |
| + |
| void MTPDeviceDelegateImplLinux::CancelPendingTasksAndDeleteDelegate() { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| // To cancel all the pending tasks, destroy the MTPDeviceTaskHelper object. |
| @@ -569,7 +613,7 @@ void MTPDeviceDelegateImplLinux::ReadBytesInternal( |
| file_id)); |
| base::Closure closure = |
| - base::Bind(base::Bind(&ReadBytesOnUIThread, storage_name_, request)); |
| + base::Bind(&ReadBytesOnUIThread, storage_name_, request); |
| EnsureInitAndRunTask(PendingTaskInfo(base::FilePath(), |
| content::BrowserThread::UI, |
| FROM_HERE, |
| @@ -580,6 +624,41 @@ void MTPDeviceDelegateImplLinux::ReadBytesInternal( |
| PendingRequestDone(); |
| } |
| +void MTPDeviceDelegateImplLinux::CopyFileFromLocalInternal( |
| + const base::FilePath& source_file_path, |
| + const base::FilePath& device_file_path, |
| + const CopyFileFromLocalSuccessCallback& success_callback, |
| + const ErrorCallback& error_callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + |
| + const int source_file_descriptor = |
| + open(source_file_path.value().c_str(), O_RDONLY); |
|
Lei Zhang
2015/03/02 23:41:39
Actually, we should open() and close() this on a F
yawano
2015/03/03 09:16:54
Done.
|
| + |
| + uint32 parent_id; |
| + if (source_file_descriptor != -1 && |
| + CachedPathToId(device_file_path.DirName(), &parent_id)) { |
| + CopyFileFromLocalSuccessCallback success_callback_wrapper = |
| + base::Bind(&MTPDeviceDelegateImplLinux::OnDidCopyFileFromLocal, |
| + weak_ptr_factory_.GetWeakPtr(), success_callback, |
| + source_file_descriptor); |
| + |
| + ErrorCallback error_callback_wrapper = base::Bind( |
| + &MTPDeviceDelegateImplLinux::HandleCopyFileFromLocalError, |
| + weak_ptr_factory_.GetWeakPtr(), error_callback, source_file_descriptor); |
| + |
| + base::Closure closure = base::Bind( |
| + &CopyFileFromLocalOnUIThread, storage_name_, source_file_descriptor, |
| + parent_id, device_file_path.BaseName().value(), |
| + success_callback_wrapper, error_callback_wrapper); |
| + EnsureInitAndRunTask(PendingTaskInfo( |
| + base::FilePath(), content::BrowserThread::UI, FROM_HERE, closure)); |
| + } else { |
| + error_callback.Run(base::File::FILE_ERROR_INVALID_OPERATION); |
| + } |
| + |
| + PendingRequestDone(); |
| +} |
| + |
| void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask( |
| const PendingTaskInfo& task_info) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| @@ -599,10 +678,8 @@ void MTPDeviceDelegateImplLinux::EnsureInitAndRunTask( |
| init_state_ = PENDING_INIT; |
| task_in_progress_ = true; |
| content::BrowserThread::PostTask( |
| - content::BrowserThread::UI, |
| - FROM_HERE, |
| - base::Bind(&OpenStorageOnUIThread, |
| - storage_name_, |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&OpenStorageOnUIThread, storage_name_, read_only_, |
| base::Bind(&MTPDeviceDelegateImplLinux::OnInitCompleted, |
| weak_ptr_factory_.GetWeakPtr()))); |
| } |
| @@ -854,6 +931,27 @@ void MTPDeviceDelegateImplLinux::OnFillFileCacheFailed( |
| pending_tasks_.front().path.clear(); |
| } |
| +void MTPDeviceDelegateImplLinux::OnDidCopyFileFromLocal( |
| + const CopyFileFromLocalSuccessCallback& success_callback, |
| + const uint32 source_file_descriptor) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + |
| + IGNORE_EINTR(close(source_file_descriptor)); |
| + success_callback.Run(); |
| + PendingRequestDone(); |
| +} |
| + |
| +void MTPDeviceDelegateImplLinux::HandleCopyFileFromLocalError( |
| + const ErrorCallback& error_callback, |
| + const uint32 source_file_descriptor, |
| + base::File::Error error) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + |
| + close(source_file_descriptor); |
| + error_callback.Run(error); |
| + PendingRequestDone(); |
| +} |
| + |
| void MTPDeviceDelegateImplLinux::HandleDeviceFileError( |
| const ErrorCallback& error_callback, |
| uint32 file_id, |
| @@ -943,7 +1041,8 @@ bool MTPDeviceDelegateImplLinux::CachedPathToId(const base::FilePath& path, |
| void CreateMTPDeviceAsyncDelegate( |
| const std::string& device_location, |
| + const bool read_only, |
| const CreateMTPDeviceAsyncDelegateCallback& callback) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| - callback.Run(new MTPDeviceDelegateImplLinux(device_location)); |
| + callback.Run(new MTPDeviceDelegateImplLinux(device_location, read_only)); |
| } |