diff options
author | yawano <yawano@chromium.org> | 2015-03-10 02:21:45 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-10 09:22:18 +0000 |
commit | e27c27c27edf0b7f0c898f97a966e77a98a41a6d (patch) | |
tree | d9d182379201e0a1b4b83cdb37990f513d6cb98f /device | |
parent | 7566e012b24b2a8492eb34cafb247142aee00fae (diff) | |
download | chromium_src-e27c27c27edf0b7f0c898f97a966e77a98a41a6d.zip chromium_src-e27c27c27edf0b7f0c898f97a966e77a98a41a6d.tar.gz chromium_src-e27c27c27edf0b7f0c898f97a966e77a98a41a6d.tar.bz2 |
Implement DeleteFile and DeleteDirectory.
BUG=413541
TEST=manually tested by chaning write_supported in volume_manager.cc to true.
Review URL: https://codereview.chromium.org/982283002
Cr-Commit-Position: refs/heads/master@{#319853}
Diffstat (limited to 'device')
4 files changed, 111 insertions, 36 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 2dddbce..832d598 100644 --- a/device/media_transfer_protocol/media_transfer_protocol_daemon_client.cc +++ b/device/media_transfer_protocol/media_transfer_protocol_daemon_client.cc @@ -193,6 +193,20 @@ class MediaTransferProtocolDaemonClientImpl weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); } + void DeleteObject(const std::string& handle, + const uint32 object_id, + const DeleteObjectCallback& callback, + const ErrorCallback& error_callback) override { + dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kDeleteObject); + dbus::MessageWriter writer(&method_call); + writer.AppendString(handle); + writer.AppendUint32(object_id); + proxy_->CallMethod( + &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, + base::Bind(&MediaTransferProtocolDaemonClientImpl::OnDeleteObject, + weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); + } + // MediaTransferProtocolDaemonClient override. void ListenForChanges(const MTPStorageEventHandler& handler) override { DCHECK(!listen_for_changes_called_); @@ -382,6 +396,17 @@ class MediaTransferProtocolDaemonClientImpl callback.Run(); } + void OnDeleteObject(const DeleteObjectCallback& 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 c44ba75..ae5e8c0 100644 --- a/device/media_transfer_protocol/media_transfer_protocol_daemon_client.h +++ b/device/media_transfer_protocol/media_transfer_protocol_daemon_client.h @@ -71,6 +71,9 @@ class MediaTransferProtocolDaemonClient { // A callback to handle the result of CopyFileFromLocal. typedef base::Closure CopyFileFromLocalCallback; + // A callback to handle the result of DeleteObject. + typedef base::Closure DeleteObjectCallback; + // 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. @@ -152,6 +155,14 @@ class MediaTransferProtocolDaemonClient { const CopyFileFromLocalCallback& callback, const ErrorCallback& error_callback) = 0; + // Calls DeleteObject method. |callback| is called after the method call + // succeeds, otherwise, |error_callback| is called. + // |object_id| is an object id of a file or directory which is deleted. + virtual void DeleteObject(const std::string& handle, + const uint32 object_id, + const DeleteObjectCallback& 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 fb59588..2a7f478 100644 --- a/device/media_transfer_protocol/media_transfer_protocol_manager.cc +++ b/device/media_transfer_protocol/media_transfer_protocol_manager.cc @@ -161,7 +161,8 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { // MediaTransferProtocolManager override. void ReadDirectory(const std::string& storage_handle, - uint32 file_id, + const uint32 file_id, + const size_t max_size, const ReadDirectoryCallback& callback) override { DCHECK(thread_checker_.CalledOnValidThread()); if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { @@ -172,11 +173,10 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { } read_directory_callbacks_.push(callback); mtp_client_->ReadDirectoryEntryIds( - storage_handle, - file_id, - base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryEntryIds, - weak_ptr_factory_.GetWeakPtr(), - storage_handle), + storage_handle, file_id, + base::Bind(&MediaTransferProtocolManagerImpl:: + OnReadDirectoryEntryIdsToReadDirectory, + weak_ptr_factory_.GetWeakPtr(), storage_handle, max_size), base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, weak_ptr_factory_.GetWeakPtr())); } @@ -242,6 +242,23 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { weak_ptr_factory_.GetWeakPtr())); } + void DeleteObject(const std::string& storage_handle, + const uint32 object_id, + const DeleteObjectCallback& callback) override { + DCHECK(thread_checker_.CalledOnValidThread()); + if (!ContainsKey(handles_, storage_handle) || !mtp_client_) { + callback.Run(true /* error */); + return; + } + delete_object_callbacks_.push(callback); + mtp_client_->DeleteObject( + storage_handle, object_id, + base::Bind(&MediaTransferProtocolManagerImpl::OnDeleteObject, + weak_ptr_factory_.GetWeakPtr()), + base::Bind(&MediaTransferProtocolManagerImpl::OnDeleteObjectError, + weak_ptr_factory_.GetWeakPtr())); + } + private: // Map of storage names to storage info. typedef std::map<std::string, MtpStorageInfo> StorageInfoMap; @@ -255,6 +272,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { typedef std::queue<ReadFileCallback> ReadFileCallbackQueue; typedef std::queue<GetFileInfoCallback> GetFileInfoCallbackQueue; typedef std::queue<CopyFileFromLocalCallback> CopyFileFromLocalCallbackQueue; + typedef std::queue<DeleteObjectCallback> DeleteObjectCallbackQueue; void OnStorageAttached(const std::string& storage_name) { DCHECK(thread_checker_.CalledOnValidThread()); @@ -357,40 +375,38 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { close_storage_callbacks_.pop(); } - void OnReadDirectoryEntryIds(const std::string& storage_handle, - const std::vector<uint32>& file_ids) { + void OnReadDirectoryEntryIdsToReadDirectory( + const std::string& storage_handle, + const size_t max_size, + const std::vector<uint32>& file_ids) { DCHECK(thread_checker_.CalledOnValidThread()); if (file_ids.empty()) { - OnGotDirectoryEntries(storage_handle, - file_ids, - kInitialOffset, - file_ids, - std::vector<MtpFileEntry>()); + OnGotDirectoryEntries(storage_handle, file_ids, kInitialOffset, max_size, + file_ids, std::vector<MtpFileEntry>()); return; } std::vector<uint32> sorted_file_ids = file_ids; std::sort(sorted_file_ids.begin(), sorted_file_ids.end()); + const size_t chunk_size = + max_size == 0 ? kFileInfoToFetchChunkSize + : std::min(max_size, kFileInfoToFetchChunkSize); + mtp_client_->GetFileInfo( - storage_handle, - file_ids, - kInitialOffset, - kFileInfoToFetchChunkSize, + storage_handle, file_ids, kInitialOffset, chunk_size, base::Bind(&MediaTransferProtocolManagerImpl::OnGotDirectoryEntries, - weak_ptr_factory_.GetWeakPtr(), - storage_handle, - file_ids, - kInitialOffset, - sorted_file_ids), + weak_ptr_factory_.GetWeakPtr(), storage_handle, file_ids, + kInitialOffset, max_size, sorted_file_ids), base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, weak_ptr_factory_.GetWeakPtr())); } void OnGotDirectoryEntries(const std::string& storage_handle, const std::vector<uint32>& file_ids, - size_t offset, + const size_t offset, + const size_t max_size, const std::vector<uint32>& sorted_file_ids, const std::vector<MtpFileEntry>& file_entries) { DCHECK(thread_checker_.CalledOnValidThread()); @@ -409,25 +425,25 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { } } - size_t next_offset = file_ids.size(); + const size_t directory_size = + max_size == 0 ? file_ids.size() : std::min(file_ids.size(), max_size); + size_t next_offset = directory_size; if (offset < SIZE_MAX - kFileInfoToFetchChunkSize) next_offset = std::min(next_offset, offset + kFileInfoToFetchChunkSize); - bool has_more = next_offset < file_ids.size(); + bool has_more = next_offset < directory_size; read_directory_callbacks_.front().Run(file_entries, has_more, false /* no error */); + if (has_more) { + const size_t chunk_size = + std::min(directory_size - next_offset, kFileInfoToFetchChunkSize); + mtp_client_->GetFileInfo( - storage_handle, - file_ids, - next_offset, - kFileInfoToFetchChunkSize, + storage_handle, file_ids, next_offset, chunk_size, base::Bind(&MediaTransferProtocolManagerImpl::OnGotDirectoryEntries, - weak_ptr_factory_.GetWeakPtr(), - storage_handle, - file_ids, - next_offset, - sorted_file_ids), + weak_ptr_factory_.GetWeakPtr(), storage_handle, file_ids, + next_offset, max_size, sorted_file_ids), base::Bind(&MediaTransferProtocolManagerImpl::OnReadDirectoryError, weak_ptr_factory_.GetWeakPtr())); return; @@ -483,6 +499,18 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { copy_file_from_local_callbacks_.pop(); } + void OnDeleteObject() { + DCHECK(thread_checker_.CalledOnValidThread()); + delete_object_callbacks_.front().Run(false /* no error */); + delete_object_callbacks_.pop(); + } + + void OnDeleteObjectError() { + DCHECK(thread_checker_.CalledOnValidThread()); + delete_object_callbacks_.front().Run(true /* error */); + delete_object_callbacks_.pop(); + } + // Get the Bus object used to communicate with mtpd. dbus::Bus* GetBus() { DCHECK(thread_checker_.CalledOnValidThread()); @@ -565,6 +593,7 @@ class MediaTransferProtocolManagerImpl : public MediaTransferProtocolManager { ReadFileCallbackQueue read_file_callbacks_; GetFileInfoCallbackQueue get_file_info_callbacks_; CopyFileFromLocalCallbackQueue copy_file_from_local_callbacks_; + DeleteObjectCallbackQueue delete_object_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 39eea9b..1915ce2 100644 --- a/device/media_transfer_protocol/media_transfer_protocol_manager.h +++ b/device/media_transfer_protocol/media_transfer_protocol_manager.h @@ -63,6 +63,10 @@ class MediaTransferProtocolManager { // The first argument is true if there was an error. typedef base::Callback<void(bool error)> CopyFileFromLocalCallback; + // A callback to handle the result of DeleteObject. + // The first argument is true if there was an error. + typedef base::Callback<void(bool error)> DeleteObjectCallback; + // Implement this interface to be notified about MTP storage // attachment / detachment events. class Observer { @@ -100,9 +104,10 @@ class MediaTransferProtocolManager { const CloseStorageCallback& callback) = 0; // Reads directory entries from |file_id| on |storage_handle| and runs - // |callback|. + // |callback|. |max_size| is a maximum number of files to be read. virtual void ReadDirectory(const std::string& storage_handle, - uint32 file_id, + const uint32 file_id, + const size_t max_size, const ReadDirectoryCallback& callback) = 0; // Reads file data from |file_id| on |storage_handle| and runs |callback|. @@ -127,6 +132,11 @@ class MediaTransferProtocolManager { const std::string& file_name, const CopyFileFromLocalCallback& callback) = 0; + // Deletes |object_id|. + virtual void DeleteObject(const std::string& storage_handle, + const uint32 object_id, + const DeleteObjectCallback& callback) = 0; + // Creates and returns the global MediaTransferProtocolManager instance. // On Linux, |task_runner| specifies the task runner to process asynchronous // operations. |