Commit 963756a3 authored by Donna Wu's avatar Donna Wu Committed by Commit Bot

Migrate MtpDeviceManager to OnceCallback.

This CL migrated to as many OnceCallback(s) as possible in
services/device/media_transfer_protocol directory. Due to some
unchanged DBus interfaces, there are still several base::Bind()
left in the implementation.

BUG=714018

Change-Id: Iceb2558f45a54e43503fc67fe40380cc9baef248
Reviewed-on: https://chromium-review.googlesource.com/1111693Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Commit-Queue: Donna Wu <donna.wu@intel.com>
Cr-Commit-Position: refs/heads/master@{#570676}
parent 033617cc
......@@ -38,50 +38,51 @@ namespace device {
class MediaTransferProtocolDaemonClient {
public:
// A callback to be called when DBus method call fails.
using ErrorCallback = base::Closure;
using ErrorCallback = base::OnceClosure;
// A callback to handle the result of EnumerateAutoMountableDevices.
// The argument is the enumerated storage names.
using EnumerateStoragesCallback =
base::Callback<void(const std::vector<std::string>& storage_names)>;
base::OnceCallback<void(const std::vector<std::string>& storage_names)>;
// A callback to handle the result of GetStorageInfo.
// The argument is the information about the specified storage.
using GetStorageInfoCallback =
base::Callback<void(const mojom::MtpStorageInfo& storage_info)>;
base::OnceCallback<void(const mojom::MtpStorageInfo& storage_info)>;
// A callback to handle the result of OpenStorage.
// The argument is the returned handle.
using OpenStorageCallback = base::Callback<void(const std::string& handle)>;
using OpenStorageCallback =
base::OnceCallback<void(const std::string& handle)>;
// A callback to handle the result of CloseStorage.
using CloseStorageCallback = base::Closure;
using CloseStorageCallback = base::OnceClosure;
// A callback to handle the result of CreateDirectory.
using CreateDirectoryCallback = base::Closure;
using CreateDirectoryCallback = base::OnceClosure;
// A callback to handle the result of ReadDirectoryEntryIds.
// The argument is a vector of file ids.
using ReadDirectoryEntryIdsCallback =
base::Callback<void(const std::vector<uint32_t>& file_ids)>;
base::OnceCallback<void(const std::vector<uint32_t>& file_ids)>;
// A callback to handle the result of GetFileInfo.
// The argument is a vector of file entries.
using GetFileInfoCallback = base::Callback<void(
using GetFileInfoCallback = base::OnceCallback<void(
const std::vector<mojom::MtpFileEntry>& file_entries)>;
// A callback to handle the result of ReadFileChunkById.
// The argument is a string containing the file data.
using ReadFileCallback = base::Callback<void(const std::string& data)>;
using ReadFileCallback = base::OnceCallback<void(const std::string& data)>;
// A callback to handle the result of RenameObject.
using RenameObjectCallback = base::Closure;
using RenameObjectCallback = base::OnceClosure;
// A callback to handle the result of CopyFileFromLocal.
using CopyFileFromLocalCallback = base::Closure;
using CopyFileFromLocalCallback = base::OnceClosure;
// A callback to handle the result of DeleteObject.
using DeleteObjectCallback = base::Closure;
using DeleteObjectCallback = base::OnceClosure;
// A callback to handle storage attach/detach events.
// The first argument is true for attach, false for detach.
......@@ -93,37 +94,35 @@ class MediaTransferProtocolDaemonClient {
// Calls EnumerateStorages method. |callback| is called after the
// method call succeeds, otherwise, |error_callback| is called.
virtual void EnumerateStorages(
const EnumerateStoragesCallback& callback,
const ErrorCallback& error_callback) = 0;
virtual void EnumerateStorages(EnumerateStoragesCallback callback,
ErrorCallback error_callback) = 0;
// Calls GetStorageInfo method. |callback| is called after the method call
// succeeds, otherwise, |error_callback| is called.
virtual void GetStorageInfo(const std::string& storage_name,
const GetStorageInfoCallback& callback,
const ErrorCallback& error_callback) = 0;
GetStorageInfoCallback callback,
ErrorCallback error_callback) = 0;
// Calls GetStorageInfoFromDevice method. |callback| is called after the
// method call succeeds, otherwise, |error_callback| is called.
virtual void GetStorageInfoFromDevice(
const std::string& storage_name,
const GetStorageInfoCallback& callback,
const ErrorCallback& error_callback) = 0;
virtual void GetStorageInfoFromDevice(const std::string& storage_name,
GetStorageInfoCallback callback,
ErrorCallback error_callback) = 0;
// Calls OpenStorage method. |callback| is called after the method call
// succeeds, otherwise, |error_callback| is called.
// OpenStorage returns a handle in |callback|.
virtual void OpenStorage(const std::string& storage_name,
const std::string& mode,
const OpenStorageCallback& callback,
const ErrorCallback& error_callback) = 0;
OpenStorageCallback callback,
ErrorCallback error_callback) = 0;
// Calls CloseStorage method. |callback| is called after the method call
// succeeds, otherwise, |error_callback| is called.
// |handle| comes from a OpenStorageCallback.
virtual void CloseStorage(const std::string& handle,
const CloseStorageCallback& callback,
const ErrorCallback& error_callback) = 0;
CloseStorageCallback callback,
ErrorCallback error_callback) = 0;
// Calls CreateDirectory method. |callback| is called after the method call
// succeeds, otherwise, |error_callback| is called.
......@@ -132,25 +131,24 @@ class MediaTransferProtocolDaemonClient {
virtual void CreateDirectory(const std::string& handle,
const uint32_t parent_id,
const std::string& directory_name,
const CreateDirectoryCallback& callback,
const ErrorCallback& error_callback) = 0;
CreateDirectoryCallback callback,
ErrorCallback error_callback) = 0;
// Calls ReadDirectoryEntryIds method. |callback| is called after the method
// call succeeds, otherwise, |error_callback| is called.
// |file_id| is a MTP-device specific id for a file.
virtual void ReadDirectoryEntryIds(
const std::string& handle,
virtual void ReadDirectoryEntryIds(const std::string& handle,
uint32_t file_id,
const ReadDirectoryEntryIdsCallback& callback,
const ErrorCallback& error_callback) = 0;
ReadDirectoryEntryIdsCallback callback,
ErrorCallback error_callback) = 0;
// Calls GetFileInfo method. |callback| is called after the method
// call succeeds, otherwise, |error_callback| is called.
// |file_ids| is a list of MTP-device specific file ids.
virtual void GetFileInfo(const std::string& handle,
const std::vector<uint32_t>& file_ids,
const GetFileInfoCallback& callback,
const ErrorCallback& error_callback) = 0;
GetFileInfoCallback callback,
ErrorCallback error_callback) = 0;
// Calls ReadFileChunk method. |callback| is called after the method call
// succeeds, otherwise, |error_callback| is called.
......@@ -161,8 +159,8 @@ class MediaTransferProtocolDaemonClient {
uint32_t file_id,
uint32_t offset,
uint32_t bytes_to_read,
const ReadFileCallback& callback,
const ErrorCallback& error_callback) = 0;
ReadFileCallback callback,
ErrorCallback error_callback) = 0;
// Calls RenameObject method. |callback| is called after the method call
// succeeds, otherwise, |error_callback| is called.
......@@ -171,8 +169,8 @@ class MediaTransferProtocolDaemonClient {
virtual void RenameObject(const std::string& handle,
const uint32_t object_id,
const std::string& new_name,
const RenameObjectCallback& callback,
const ErrorCallback& error_callback) = 0;
RenameObjectCallback callback,
ErrorCallback error_callback) = 0;
// Calls CopyFileFromLocal method. |callback| is called after the method call
// succeeds, otherwise, |error_callback| is called.
......@@ -183,16 +181,16 @@ class MediaTransferProtocolDaemonClient {
const int source_file_descriptor,
const uint32_t parent_id,
const std::string& file_name,
const CopyFileFromLocalCallback& callback,
const ErrorCallback& error_callback) = 0;
CopyFileFromLocalCallback callback,
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_t object_id,
const DeleteObjectCallback& callback,
const ErrorCallback& error_callback) = 0;
DeleteObjectCallback callback,
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
......
......@@ -92,9 +92,9 @@ void MtpDeviceManager::GetStorageInfoFromDevice(
get_storage_info_from_device_callbacks_.push(std::move(callback));
mtp_client_->GetStorageInfoFromDevice(
storage_name,
base::Bind(&MtpDeviceManager::OnGetStorageInfoFromDevice,
base::BindOnce(&MtpDeviceManager::OnGetStorageInfoFromDevice,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&MtpDeviceManager::OnGetStorageInfoFromDeviceError,
base::BindOnce(&MtpDeviceManager::OnGetStorageInfoFromDeviceError,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -108,9 +108,9 @@ void MtpDeviceManager::OpenStorage(const std::string& storage_name,
}
open_storage_callbacks_.push(std::move(callback));
mtp_client_->OpenStorage(storage_name, mode,
base::Bind(&MtpDeviceManager::OnOpenStorage,
base::BindOnce(&MtpDeviceManager::OnOpenStorage,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&MtpDeviceManager::OnOpenStorageError,
base::BindOnce(&MtpDeviceManager::OnOpenStorageError,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -123,10 +123,11 @@ void MtpDeviceManager::CloseStorage(const std::string& storage_handle,
}
close_storage_callbacks_.push(
std::make_pair(std::move(callback), storage_handle));
mtp_client_->CloseStorage(storage_handle,
base::Bind(&MtpDeviceManager::OnCloseStorage,
mtp_client_->CloseStorage(
storage_handle,
base::BindOnce(&MtpDeviceManager::OnCloseStorage,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&MtpDeviceManager::OnCloseStorageError,
base::BindOnce(&MtpDeviceManager::OnCloseStorageError,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -142,9 +143,9 @@ void MtpDeviceManager::CreateDirectory(const std::string& storage_handle,
create_directory_callbacks_.push(std::move(callback));
mtp_client_->CreateDirectory(
storage_handle, parent_id, directory_name,
base::Bind(&MtpDeviceManager::OnCreateDirectory,
base::BindOnce(&MtpDeviceManager::OnCreateDirectory,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&MtpDeviceManager::OnCreateDirectoryError,
base::BindOnce(&MtpDeviceManager::OnCreateDirectoryError,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -160,9 +161,9 @@ void MtpDeviceManager::ReadDirectoryEntryIds(
read_directory_callbacks_.push(std::move(callback));
mtp_client_->ReadDirectoryEntryIds(
storage_handle, file_id,
base::Bind(&MtpDeviceManager::OnReadDirectoryEntryIds,
base::BindOnce(&MtpDeviceManager::OnReadDirectoryEntryIds,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&MtpDeviceManager::OnReadDirectoryError,
base::BindOnce(&MtpDeviceManager::OnReadDirectoryError,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -177,10 +178,10 @@ void MtpDeviceManager::ReadFileChunk(const std::string& storage_handle,
return;
}
read_file_callbacks_.push(std::move(callback));
mtp_client_->ReadFileChunk(
storage_handle, file_id, offset, count,
base::Bind(&MtpDeviceManager::OnReadFile, weak_ptr_factory_.GetWeakPtr()),
base::Bind(&MtpDeviceManager::OnReadFileError,
mtp_client_->ReadFileChunk(storage_handle, file_id, offset, count,
base::BindOnce(&MtpDeviceManager::OnReadFile,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&MtpDeviceManager::OnReadFileError,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -195,9 +196,9 @@ void MtpDeviceManager::GetFileInfo(const std::string& storage_handle,
}
get_file_info_callbacks_.push(std::move(callback));
mtp_client_->GetFileInfo(storage_handle, file_ids,
base::Bind(&MtpDeviceManager::OnGetFileInfo,
base::BindOnce(&MtpDeviceManager::OnGetFileInfo,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&MtpDeviceManager::OnGetFileInfoError,
base::BindOnce(&MtpDeviceManager::OnGetFileInfoError,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -211,10 +212,11 @@ void MtpDeviceManager::RenameObject(const std::string& storage_handle,
return;
}
rename_object_callbacks_.push(std::move(callback));
mtp_client_->RenameObject(storage_handle, object_id, new_name,
base::Bind(&MtpDeviceManager::OnRenameObject,
mtp_client_->RenameObject(
storage_handle, object_id, new_name,
base::BindOnce(&MtpDeviceManager::OnRenameObject,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&MtpDeviceManager::OnRenameObjectError,
base::BindOnce(&MtpDeviceManager::OnRenameObjectError,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -231,9 +233,9 @@ void MtpDeviceManager::CopyFileFromLocal(const std::string& storage_handle,
copy_file_from_local_callbacks_.push(std::move(callback));
mtp_client_->CopyFileFromLocal(
storage_handle, source_file_descriptor, parent_id, file_name,
base::Bind(&MtpDeviceManager::OnCopyFileFromLocal,
base::BindOnce(&MtpDeviceManager::OnCopyFileFromLocal,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&MtpDeviceManager::OnCopyFileFromLocalError,
base::BindOnce(&MtpDeviceManager::OnCopyFileFromLocalError,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -246,20 +248,22 @@ void MtpDeviceManager::DeleteObject(const std::string& storage_handle,
return;
}
delete_object_callbacks_.push(std::move(callback));
mtp_client_->DeleteObject(storage_handle, object_id,
base::Bind(&MtpDeviceManager::OnDeleteObject,
mtp_client_->DeleteObject(
storage_handle, object_id,
base::BindOnce(&MtpDeviceManager::OnDeleteObject,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&MtpDeviceManager::OnDeleteObjectError,
base::BindOnce(&MtpDeviceManager::OnDeleteObjectError,
weak_ptr_factory_.GetWeakPtr()));
}
// private methods
void MtpDeviceManager::OnStorageAttached(const std::string& storage_name) {
DCHECK(thread_checker_.CalledOnValidThread());
mtp_client_->GetStorageInfo(storage_name,
base::Bind(&MtpDeviceManager::OnGetStorageInfo,
mtp_client_->GetStorageInfo(
storage_name,
base::BindOnce(&MtpDeviceManager::OnGetStorageInfo,
weak_ptr_factory_.GetWeakPtr()),
base::DoNothing());
base::DoNothing::Once());
}
void MtpDeviceManager::OnStorageDetached(const std::string& storage_name) {
......@@ -504,9 +508,9 @@ void MtpDeviceManager::FinishSetupOnOriginThread(
mtp_client_->ListenForChanges(base::Bind(&MtpDeviceManager::OnStorageChanged,
weak_ptr_factory_.GetWeakPtr()));
mtp_client_->EnumerateStorages(
base::Bind(&MtpDeviceManager::OnEnumerateStorages,
base::BindOnce(&MtpDeviceManager::OnEnumerateStorages,
weak_ptr_factory_.GetWeakPtr()),
base::DoNothing());
base::DoNothing::Once());
}
// static
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment