Commit 9d0cafb5 authored by Donna Wu's avatar Donna Wu Committed by Commit Bot

Merge MediaTransferProtocolManager to MtpDeviceManager.

This CL changes MtpDeviceManager from forwarding to
MediaTransferProtocolManagerImpl to directly having the
implementations of the latter in the methods that previously
forwarded. As MediaTransferProtocolManagerImpl was previously an
implementation detail hidden in media_transfer_protocol_manager.cc,
the relevant parts of the MediaTransferProtocolManagerImpl declaration
are moved from that cc file to mtp_device_manager.h, while the
relevant parts of the impl are inlined into the corresponding
MtpDeviceManager method implementations in mtp_device_manager.cc.

BUG=769630

Change-Id: Ia3ce06a69b8b4574b666711c2d8811d71101f579
Reviewed-on: https://chromium-review.googlesource.com/1096656
Commit-Queue: Donna Wu <donna.wu@intel.com>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#569515}
parent 1aed13d7
......@@ -235,7 +235,7 @@ void DeviceService::BindVibrationManagerRequest(
#if defined(OS_CHROMEOS)
void DeviceService::BindMtpManagerRequest(mojom::MtpManagerRequest request) {
if (!mtp_device_manager_)
mtp_device_manager_ = std::make_unique<MtpDeviceManager>();
mtp_device_manager_ = MtpDeviceManager::Initialize();
mtp_device_manager_->AddBinding(std::move(request));
}
#endif
......
......@@ -26,8 +26,6 @@ source_set("media_transfer_protocol") {
sources = [
"media_transfer_protocol_daemon_client.cc",
"media_transfer_protocol_daemon_client.h",
"media_transfer_protocol_manager.cc",
"media_transfer_protocol_manager.h",
"mtp_device_manager.cc",
"mtp_device_manager.h",
]
......
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_MANAGER_H_
#define SERVICES_DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_MANAGER_H_
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "build/build_config.h"
#include "services/device/public/mojom/mtp_file_entry.mojom.h"
#include "services/device/public/mojom/mtp_manager.mojom.h"
#include "services/device/public/mojom/mtp_storage_info.mojom.h"
#if !defined(OS_CHROMEOS)
#error "Only used on ChromeOS"
#endif
namespace device {
// This class handles the interaction with mtpd.
class MediaTransferProtocolManager {
public:
// A callback to handle the result of GetStorages().
// The argument is the returned vector of available MTP storage names.
using GetStoragesCallback =
base::OnceCallback<void(const std::vector<std::string>& storages)>;
// A callback to handle the result of OpenStorage.
// The first argument is the returned handle.
// The second argument is true if there was an error.
using OpenStorageCallback =
base::Callback<void(const std::string& handle, bool error)>;
// A callback to handle the result of CloseStorage.
// The argument is true if there was an error.
using CloseStorageCallback = base::Callback<void(bool error)>;
// A callback to handle the result of CreateDirectory.
// The first argument is true if there was an error.
using CreateDirectoryCallback = base::Callback<void(bool error)>;
// A callback to handle the result of ReadFileChunk.
// The first argument is a string containing the file data.
// The second argument is true if there was an error.
using ReadFileCallback =
base::Callback<void(const std::string& data, bool error)>;
// A callback to handle the result of RenameObject.
// The first argument is true if there was an error.
using RenameObjectCallback = base::Callback<void(bool error)>;
// A callback to handle the result of CopyFileFromLocal.
// The first argument is true if there was an error.
using CopyFileFromLocalCallback = base::Callback<void(bool error)>;
// A callback to handle the result of DeleteObject.
// The first argument is true if there was an error.
using DeleteObjectCallback = base::Callback<void(bool error)>;
virtual ~MediaTransferProtocolManager() {}
// This is a combined interface to get existing storages and set a
// client for incoming storage change events. It is designed to reduce
// async calls and eliminate a potential race condition between
// the client being set and storage updates being made.
virtual void EnumerateStoragesAndSetClient(
mojom::MtpManagerClientAssociatedPtrInfo client,
mojom::MtpManager::EnumerateStoragesAndSetClientCallback callback) = 0;
// Gets all available MTP storages and runs |callback|.
virtual void GetStorages(GetStoragesCallback callback) const = 0;
// Gets the metadata for |storage_name| and runs |callback|.
virtual void GetStorageInfo(
const std::string& storage_name,
mojom::MtpManager::GetStorageInfoCallback callback) const = 0;
// Read the metadata of |storage_name| from device and runs |callback|.
virtual void GetStorageInfoFromDevice(
const std::string& storage_name,
mojom::MtpManager::GetStorageInfoFromDeviceCallback callback) = 0;
// Opens |storage_name| in |mode| and runs |callback|.
virtual void OpenStorage(const std::string& storage_name,
const std::string& mode,
const OpenStorageCallback& callback) = 0;
// Close |storage_handle| and runs |callback|.
virtual void CloseStorage(const std::string& storage_handle,
const CloseStorageCallback& callback) = 0;
// Creates |directory_name| in |parent_id|.
virtual void CreateDirectory(const std::string& storage_handle,
uint32_t parent_id,
const std::string& directory_name,
const CreateDirectoryCallback& callback) = 0;
// Reads IDs of directory entries from |file_id| on |storage_handle| and runs
// |callback|.
virtual void ReadDirectoryEntryIds(
const std::string& storage_handle,
uint32_t file_id,
mojom::MtpManager::ReadDirectoryEntryIdsCallback callback) = 0;
// Reads file data from |file_id| on |storage_handle| and runs |callback|.
// Reads |count| bytes of data starting at |offset|.
virtual void ReadFileChunk(const std::string& storage_handle,
uint32_t file_id,
uint32_t offset,
uint32_t count,
const ReadFileCallback& callback) = 0;
// Gets the metadata for files on |storage_handle| with |file_ids|.
// Runs |callback| with the results.
// Use mojom::MtpManager::GetFileInfoCallback directly to get prepared for
// future merge.
virtual void GetFileInfo(const std::string& storage_handle,
const std::vector<uint32_t>& file_ids,
mojom::MtpManager::GetFileInfoCallback callback) = 0;
// Renames |object_id| to |new_name|.
virtual void RenameObject(const std::string& storage_handle,
uint32_t object_id,
const std::string& new_name,
const RenameObjectCallback& 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,
uint32_t parent_id,
const std::string& file_name,
const CopyFileFromLocalCallback& callback) = 0;
// Deletes |object_id|.
virtual void DeleteObject(const std::string& storage_handle,
uint32_t object_id,
const DeleteObjectCallback& callback) = 0;
// Creates and returns the global MediaTransferProtocolManager instance.
static std::unique_ptr<MediaTransferProtocolManager> Initialize();
};
} // namespace device
#endif // SERVICES_DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_MANAGER_H_
......@@ -7,16 +7,29 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/containers/queue.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/sequenced_task_runner.h"
#include "base/threading/thread_checker.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "mojo/public/cpp/bindings/interface_ptr_set.h"
#include "services/device/media_transfer_protocol/media_transfer_protocol_manager.h"
#include "services/device/media_transfer_protocol/media_transfer_protocol_daemon_client.h"
#include "services/device/public/mojom/mtp_manager.mojom.h"
#if !defined(OS_CHROMEOS)
#error "Only used on ChromeOS"
#endif
namespace dbus {
class DBus;
}
namespace device {
// This is the implementation of device::mojom::MtpManager which provides
......@@ -70,11 +83,125 @@ class MtpDeviceManager : public mojom::MtpManager {
uint32_t object_id,
DeleteObjectCallback callback) override;
// Creates and returns the global MtpDeviceManager instance.
static std::unique_ptr<MtpDeviceManager> Initialize();
private:
std::unique_ptr<MediaTransferProtocolManager>
media_transfer_protocol_manager_;
// Map of storage names to storage info.
using GetStorageInfoFromDeviceCallbackQueue =
base::queue<GetStorageInfoFromDeviceCallback>;
// Callback queues - DBus communication is in-order, thus callbacks are
// received in the same order as the requests.
using OpenStorageCallbackQueue = base::queue<OpenStorageCallback>;
// (callback, handle)
using CloseStorageCallbackQueue =
base::queue<std::pair<CloseStorageCallback, std::string>>;
using CreateDirectoryCallbackQueue = base::queue<CreateDirectoryCallback>;
using ReadDirectoryCallbackQueue = base::queue<ReadDirectoryEntryIdsCallback>;
using ReadFileCallbackQueue = base::queue<ReadFileChunkCallback>;
using GetFileInfoCallbackQueue = base::queue<GetFileInfoCallback>;
using RenameObjectCallbackQueue = base::queue<RenameObjectCallback>;
using CopyFileFromLocalCallbackQueue = base::queue<CopyFileFromLocalCallback>;
using DeleteObjectCallbackQueue = base::queue<DeleteObjectCallback>;
void OnStorageAttached(const std::string& storage_name);
void OnStorageDetached(const std::string& storage_name);
void OnStorageChanged(bool is_attach, const std::string& storage_name);
void OnEnumerateStorages(const std::vector<std::string>& storage_names);
void OnGetStorageInfo(const mojom::MtpStorageInfo& storage_info);
void OnGetStorageInfoFromDevice(const mojom::MtpStorageInfo& storage_info);
void OnGetStorageInfoFromDeviceError();
void OnOpenStorage(const std::string& handle);
void OnOpenStorageError();
void OnCloseStorage();
void OnCloseStorageError();
void OnCreateDirectory();
void OnCreateDirectoryError();
void OnReadDirectoryEntryIds(const std::vector<uint32_t>& file_ids);
void OnReadDirectoryError();
void OnReadFile(const std::string& data);
void OnReadFileError();
void OnGetFileInfo(const std::vector<mojom::MtpFileEntry>& entries);
void OnGetFileInfoError();
void OnRenameObject();
void OnRenameObjectError();
void OnCopyFileFromLocal();
void OnCopyFileFromLocalError();
void OnDeleteObject();
void OnDeleteObjectError();
// Callback to finish initialization after figuring out if the mtpd service
// has an owner, or if the service owner has changed.
// |mtpd_service_owner| contains the name of the current owner, if any.
void FinishSetupOnOriginThread(const std::string& mtpd_service_owner);
// Mtpd DBus client.
std::unique_ptr<MediaTransferProtocolDaemonClient> mtp_client_;
// And a D-Bus session for talking to mtpd. Note: In production, this is never
// a nullptr, but in tests it oftentimes is. It may be too much work for
// DBusThreadManager to provide a bus in unit tests.
scoped_refptr<dbus::Bus> const bus_;
mojo::BindingSet<mojom::MtpManager> bindings_;
// MtpManager client who keeps tuned on attachment / detachment events.
// Currently, storage_monitor::StorageMonitorCros is supposed to be the
// only client.
mojom::MtpManagerClientAssociatedPtr client_;
// Map to keep track of attached storages by name.
base::flat_map<std::string, mojom::MtpStorageInfo> storage_info_map_;
// Set of open storage handles.
base::flat_set<std::string> handles_;
std::string current_mtpd_owner_;
// Queued callbacks.
// These queues are needed becasue MediaTransferProtocolDaemonClient provides
// different callbacks for result(success_callback, error_callback) with
// MediaTransferProtocolManager, so a passed callback for a method in this
// class will be referred in both success_callback and error_callback for
// underline MediaTransferProtocolDaemonClient, and it is also the case for
// mojom interfaces, as all mojom methods are defined as OnceCallback.
GetStorageInfoFromDeviceCallbackQueue get_storage_info_from_device_callbacks_;
OpenStorageCallbackQueue open_storage_callbacks_;
CloseStorageCallbackQueue close_storage_callbacks_;
CreateDirectoryCallbackQueue create_directory_callbacks_;
ReadDirectoryCallbackQueue read_directory_callbacks_;
ReadFileCallbackQueue read_file_callbacks_;
GetFileInfoCallbackQueue get_file_info_callbacks_;
RenameObjectCallbackQueue rename_object_callbacks_;
CopyFileFromLocalCallbackQueue copy_file_from_local_callbacks_;
DeleteObjectCallbackQueue delete_object_callbacks_;
base::ThreadChecker thread_checker_;
base::WeakPtrFactory<MtpDeviceManager> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(MtpDeviceManager);
};
......
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