diff options
author | yawano <yawano@chromium.org> | 2015-03-04 19:43:15 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-05 03:43:43 +0000 |
commit | 8cd28e349ab9605da7dbf97c8c85f8c541b3b59b (patch) | |
tree | 65bc75420d1f52d538c723bd0caf274ee44022a8 /device | |
parent | 4f2eab7cd613d87c79a192bfc5183f79483d95bf (diff) | |
download | chromium_src-8cd28e349ab9605da7dbf97c8c85f8c541b3b59b.zip chromium_src-8cd28e349ab9605da7dbf97c8c85f8c541b3b59b.tar.gz chromium_src-8cd28e349ab9605da7dbf97c8c85f8c541b3b59b.tar.bz2 |
Implement CopyFileFromLocal of MTPDeviceAsyncDelegate.
CopyFileFromLocal is a method to copy a local file to the device. This method will be used in CopyInForeingFile of AsyncFileUtil.
A CL to connect CopyFileFromLocal to CopyInForeingFile will come later when all other write operations (e.g. delete) are implemented.
BUG=413541
TEST=none; manually tested with changing write_supported variable in volume_manager.cc to true.
Review URL: https://codereview.chromium.org/947943002
Cr-Commit-Position: refs/heads/master@{#319205}
Diffstat (limited to 'device')
4 files changed, 95 insertions, 2 deletions
diff --git a/device/media_transfer_protocol/media_transfer_protocol_daemon_client.cc b/device/media_transfer_protocol/media_transfer_protocol_daemon_client.cc index 0f72677..2dddbce 100644 --- a/device/media_transfer_protocol/media_transfer_protocol_daemon_client.cc +++ b/device/media_transfer_protocol/media_transfer_protocol_daemon_client.cc @@ -72,8 +72,8 @@ class MediaTransferProtocolDaemonClientImpl dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kOpenStorage); dbus::MessageWriter writer(&method_call); writer.AppendString(storage_name); - DCHECK_EQ(mtpd::kReadOnlyMode, mode); - writer.AppendString(mtpd::kReadOnlyMode); + DCHECK(mode == mtpd::kReadOnlyMode || mode == mtpd::kReadWriteMode); + writer.AppendString(mode); proxy_->CallMethod( &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, base::Bind(&MediaTransferProtocolDaemonClientImpl::OnOpenStorage, @@ -171,6 +171,28 @@ class MediaTransferProtocolDaemonClientImpl error_callback)); } + void CopyFileFromLocal(const std::string& handle, + const int source_file_descriptor, + const uint32 parent_id, + const std::string& file_name, + const CopyFileFromLocalCallback& callback, + const ErrorCallback& error_callback) override { + dbus::FileDescriptor file_descriptor(source_file_descriptor); + file_descriptor.CheckValidity(); + + dbus::MethodCall method_call(mtpd::kMtpdInterface, + mtpd::kCopyFileFromLocal); + dbus::MessageWriter writer(&method_call); + writer.AppendString(handle); + writer.AppendFileDescriptor(file_descriptor); + writer.AppendUint32(parent_id); + writer.AppendString(file_name); + proxy_->CallMethod( + &method_call, dbus::ObjectProxy::TIMEOUT_INFINITE, + base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCopyFileFromLocal, + weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); + } + // MediaTransferProtocolDaemonClient override. void ListenForChanges(const MTPStorageEventHandler& handler) override { DCHECK(!listen_for_changes_called_); @@ -349,6 +371,17 @@ class MediaTransferProtocolDaemonClientImpl callback.Run(data); } + void OnCopyFileFromLocal(const CopyFileFromLocalCallback& callback, + const ErrorCallback& error_callback, + dbus::Response* response) { + if (!response) { + error_callback.Run(); + return; + } + + callback.Run(); + } + // Handles MTPStorageAttached/Dettached signals and calls |handler|. void OnMTPStorageSignal(MTPStorageEventHandler handler, bool is_attach, diff --git a/device/media_transfer_protocol/media_transfer_protocol_daemon_client.h b/device/media_transfer_protocol/media_transfer_protocol_daemon_client.h index b051e6c..c44ba75 100644 --- a/device/media_transfer_protocol/media_transfer_protocol_daemon_client.h +++ b/device/media_transfer_protocol/media_transfer_protocol_daemon_client.h @@ -68,6 +68,9 @@ class MediaTransferProtocolDaemonClient { // The argument is a string containing the file data. typedef base::Callback<void(const std::string& data)> ReadFileCallback; + // A callback to handle the result of CopyFileFromLocal. + typedef base::Closure CopyFileFromLocalCallback; + // A callback to handle storage attach/detach events. // The first argument is true for attach, false for detach. // The second argument is the storage name. @@ -137,6 +140,18 @@ class MediaTransferProtocolDaemonClient { const ReadFileCallback& callback, const ErrorCallback& error_callback) = 0; + // Calls CopyFileFromLocal method. |callback| is called after the method call + // succeeds, otherwise, |error_callback| is called. + // |source_file_descriptor| is a file descriptor of source file. + // |parent_id| is a object id of a target directory. + // |file_name| is a file name of a target file. + virtual void CopyFileFromLocal(const std::string& handle, + const int source_file_descriptor, + const uint32 parent_id, + const std::string& file_name, + const CopyFileFromLocalCallback& callback, + const ErrorCallback& error_callback) = 0; + // Registers given callback for events. Should only be called once. // |storage_event_handler| is called when a mtp storage attach or detach // signal is received. diff --git a/device/media_transfer_protocol/media_transfer_protocol_manager.cc b/device/media_transfer_protocol/media_transfer_protocol_manager.cc index cde9e53..fb59588 100644 --- a/device/media_transfer_protocol/media_transfer_protocol_manager.cc +++ b/device/media_transfer_protocol/media_transfer_protocol_manager.cc @@ -223,6 +223,25 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { weak_ptr_factory_.GetWeakPtr())); } + void CopyFileFromLocal(const std::string& storage_handle, + const int source_file_descriptor, + const uint32 parent_id, + const std::string& file_name, + const CopyFileFromLocalCallback& callback) override { + DCHECK(thread_checker_.CalledOnValidThread()); + if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { + callback.Run(true /* error */); + return; + } + copy_file_from_local_callbacks_.push(callback); + mtp_client_->CopyFileFromLocal( + storage_handle, source_file_descriptor, parent_id, file_name, + base::Bind(&MediaTransferProtocolManagerImpl::OnCopyFileFromLocal, + weak_ptr_factory_.GetWeakPtr()), + base::Bind(&MediaTransferProtocolManagerImpl::OnCopyFileFromLocalError, + weak_ptr_factory_.GetWeakPtr())); + } + private: // Map of storage names to storage info. typedef std::map<std::string, MtpStorageInfo> StorageInfoMap; @@ -235,6 +254,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { typedef std::queue<ReadDirectoryCallback> ReadDirectoryCallbackQueue; typedef std::queue<ReadFileCallback> ReadFileCallbackQueue; typedef std::queue<GetFileInfoCallback> GetFileInfoCallbackQueue; + typedef std::queue<CopyFileFromLocalCallback> CopyFileFromLocalCallbackQueue; void OnStorageAttached(const std::string& storage_name) { DCHECK(thread_checker_.CalledOnValidThread()); @@ -451,6 +471,18 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { get_file_info_callbacks_.pop(); } + void OnCopyFileFromLocal() { + DCHECK(thread_checker_.CalledOnValidThread()); + copy_file_from_local_callbacks_.front().Run(false /* no error */); + copy_file_from_local_callbacks_.pop(); + } + + void OnCopyFileFromLocalError() { + DCHECK(thread_checker_.CalledOnValidThread()); + copy_file_from_local_callbacks_.front().Run(true /* error */); + copy_file_from_local_callbacks_.pop(); + } + // Get the Bus object used to communicate with mtpd. dbus::Bus* GetBus() { DCHECK(thread_checker_.CalledOnValidThread()); @@ -532,6 +564,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { ReadDirectoryCallbackQueue read_directory_callbacks_; ReadFileCallbackQueue read_file_callbacks_; GetFileInfoCallbackQueue get_file_info_callbacks_; + CopyFileFromLocalCallbackQueue copy_file_from_local_callbacks_; base::ThreadChecker thread_checker_; diff --git a/device/media_transfer_protocol/media_transfer_protocol_manager.h b/device/media_transfer_protocol/media_transfer_protocol_manager.h index 7ff89e9..39eea9b 100644 --- a/device/media_transfer_protocol/media_transfer_protocol_manager.h +++ b/device/media_transfer_protocol/media_transfer_protocol_manager.h @@ -59,6 +59,10 @@ class MediaTransferProtocolManager { typedef base::Callback<void(const MtpFileEntry& file_entry, bool error)> GetFileInfoCallback; + // A callback to handle the result of CopyFileFromLocal. + // The first argument is true if there was an error. + typedef base::Callback<void(bool error)> CopyFileFromLocalCallback; + // Implement this interface to be notified about MTP storage // attachment / detachment events. class Observer { @@ -115,6 +119,14 @@ class MediaTransferProtocolManager { uint32 file_id, const GetFileInfoCallback& callback) = 0; + // Copies the file from |source_file_descriptor| to |file_name| on + // |parent_id|. + virtual void CopyFileFromLocal(const std::string& storage_handle, + const int source_file_descriptor, + const uint32 parent_id, + const std::string& file_name, + const CopyFileFromLocalCallback& callback) = 0; + // Creates and returns the global MediaTransferProtocolManager instance. // On Linux, |task_runner| specifies the task runner to process asynchronous // operations. |