Commit 6547fc81 authored by Donna Wu's avatar Donna Wu Committed by Commit Bot

Implement MTPManagerClientChromeOS to replace existing MTP observer.

Implementation of MTPManagerClientChromeOS is mostly copied from
media_transfer_protocol_device_observer_chromeos.h/cc files which
will be replaced in subsequent CLs.

This is a preparation CL for the conversion of MTP mojo interfaces'
usage in the client side without any functional changes for the
notifications to upper level clients have been blocked.

BUG=769630

Change-Id: I28076cc4a4e30783426c6ccb5af1805ef913d7ae
Reviewed-on: https://chromium-review.googlesource.com/1046376
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@{#557827}
parent 1974ab0e
...@@ -6,11 +6,17 @@ ...@@ -6,11 +6,17 @@
#include <utility> #include <utility>
#include "base/strings/utf_string_conversions.h"
#include "components/storage_monitor/storage_info.h"
#include "components/storage_monitor/storage_info_utils.h"
namespace storage_monitor { namespace storage_monitor {
MtpManagerClientChromeOS::MtpManagerClientChromeOS( MtpManagerClientChromeOS::MtpManagerClientChromeOS(
device::mojom::MtpManager* mtp_manager) device::mojom::MtpManager* mtp_manager)
: mtp_manager_(mtp_manager), binding_(this), weak_ptr_factory_(this) { : mtp_manager_(mtp_manager),
binding_(this),
weak_ptr_factory_(this) {
device::mojom::MtpManagerClientAssociatedPtrInfo client; device::mojom::MtpManagerClientAssociatedPtrInfo client;
binding_.Bind(mojo::MakeRequest(&client)); binding_.Bind(mojo::MakeRequest(&client));
mtp_manager_->EnumerateStoragesAndSetClient( mtp_manager_->EnumerateStoragesAndSetClient(
...@@ -21,20 +27,89 @@ MtpManagerClientChromeOS::MtpManagerClientChromeOS( ...@@ -21,20 +27,89 @@ MtpManagerClientChromeOS::MtpManagerClientChromeOS(
MtpManagerClientChromeOS::~MtpManagerClientChromeOS() {} MtpManagerClientChromeOS::~MtpManagerClientChromeOS() {}
bool MtpManagerClientChromeOS::GetStorageInfoForPath(
const base::FilePath& path,
StorageInfo* storage_info) const {
DCHECK(storage_info);
if (!path.IsAbsolute())
return false;
std::vector<base::FilePath::StringType> path_components;
path.GetComponents(&path_components);
if (path_components.size() < 2)
return false;
// First and second component of the path specifies the device location.
// E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the
// device location.
const auto info_it =
storage_map_.find(GetDeviceLocationFromStorageName(path_components[1]));
if (info_it == storage_map_.end())
return false;
*storage_info = info_it->second;
return true;
}
void MtpManagerClientChromeOS::EjectDevice(
const std::string& device_id,
base::Callback<void(StorageMonitor::EjectStatus)> callback) {
std::string location;
if (!GetLocationForDeviceId(device_id, &location)) {
callback.Run(StorageMonitor::EJECT_NO_SUCH_DEVICE);
return;
}
// TODO(thestig): Change this to tell the MTP manager to eject the device.
StorageDetached(location);
callback.Run(StorageMonitor::EJECT_OK);
}
// device::mojom::MtpManagerClient override. // device::mojom::MtpManagerClient override.
void MtpManagerClientChromeOS::StorageAttached( void MtpManagerClientChromeOS::StorageAttached(
device::mojom::MtpStorageInfoPtr mtp_storage_info) { device::mojom::MtpStorageInfoPtr mtp_storage_info) {
// TODO(donna.wu@intel.com) implement this when replacing mojo methods. if (!mtp_storage_info)
// 1. adjust local storage_info map return;
// 2. notify higher StorageMonitor observers about the event.
// Create StorageMonitor format StorageInfo and update the local map.
std::string device_id = GetDeviceIdFromStorageInfo(*mtp_storage_info);
base::string16 storage_label =
GetDeviceLabelFromStorageInfo(*mtp_storage_info);
std::string location =
GetDeviceLocationFromStorageName(mtp_storage_info->storage_name);
base::string16 vendor_name = base::UTF8ToUTF16(mtp_storage_info->vendor);
base::string16 product_name = base::UTF8ToUTF16(mtp_storage_info->product);
if (device_id.empty() || storage_label.empty())
return;
DCHECK(!base::ContainsKey(storage_map_, location));
StorageInfo storage_info(device_id, location, storage_label, vendor_name,
product_name, 0);
storage_map_[location] = storage_info;
// TODO(donna.wu@intel.com): Notify StorageMonitor observers about the event
// atomically with porting clients away from
// MediaTransferProtocolDeviceObserverChromeOS to this class.
} }
// device::mojom::MtpManagerClient override. // device::mojom::MtpManagerClient override.
void MtpManagerClientChromeOS::StorageDetached( void MtpManagerClientChromeOS::StorageDetached(
const std::string& storage_name) { const std::string& storage_name) {
// TODO(donna.wu@intel.com) implement this when replacing mojo methods. DCHECK(!storage_name.empty());
// 1. adjust local storage_info map
// 2. notify higher StorageMonitor observers about the event. StorageLocationToInfoMap::iterator it =
storage_map_.find(GetDeviceLocationFromStorageName(storage_name));
if (it == storage_map_.end())
return;
// TODO(donna.wu@intel.com): Notify StorageMonitor observers about the event
// atomically with porting clients away from
// MediaTransferProtocolDeviceObserverChromeOS to this class.
storage_map_.erase(it);
} }
void MtpManagerClientChromeOS::OnReceivedStorages( void MtpManagerClientChromeOS::OnReceivedStorages(
...@@ -43,4 +118,16 @@ void MtpManagerClientChromeOS::OnReceivedStorages( ...@@ -43,4 +118,16 @@ void MtpManagerClientChromeOS::OnReceivedStorages(
StorageAttached(std::move(storage_info)); StorageAttached(std::move(storage_info));
} }
bool MtpManagerClientChromeOS::GetLocationForDeviceId(
const std::string& device_id,
std::string* location) const {
for (const auto& it : storage_map_) {
if (it.second.device_id() == device_id) {
*location = it.first;
return true;
}
}
return false;
}
} // namespace storage_monitor } // namespace storage_monitor
...@@ -5,16 +5,21 @@ ...@@ -5,16 +5,21 @@
#ifndef COMPONENTS_STORAGE_MONITOR_MTP_MANAGER_CLIENT_CHROMEOS_H_ #ifndef COMPONENTS_STORAGE_MONITOR_MTP_MANAGER_CLIENT_CHROMEOS_H_
#define COMPONENTS_STORAGE_MONITOR_MTP_MANAGER_CLIENT_CHROMEOS_H_ #define COMPONENTS_STORAGE_MONITOR_MTP_MANAGER_CLIENT_CHROMEOS_H_
#include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "components/storage_monitor/storage_monitor.h"
#include "mojo/public/cpp/bindings/associated_binding.h" #include "mojo/public/cpp/bindings/associated_binding.h"
#include "services/device/public/mojom/mtp_manager.mojom.h" #include "services/device/public/mojom/mtp_manager.mojom.h"
namespace base {
class FilePath;
}
namespace storage_monitor { namespace storage_monitor {
// This class will replace 'MediaTransferProtocolDeviceObserverChromeOS'.
// This client listens for MTP storage attachment and detachment events // This client listens for MTP storage attachment and detachment events
// from MtpManager and forwards them to StorageMonitor. // from MtpManager and forwards them to StorageMonitor.
class MtpManagerClientChromeOS : public device::mojom::MtpManagerClient { class MtpManagerClientChromeOS : public device::mojom::MtpManagerClient {
...@@ -22,6 +27,14 @@ class MtpManagerClientChromeOS : public device::mojom::MtpManagerClient { ...@@ -22,6 +27,14 @@ class MtpManagerClientChromeOS : public device::mojom::MtpManagerClient {
explicit MtpManagerClientChromeOS(device::mojom::MtpManager* mtp_manager); explicit MtpManagerClientChromeOS(device::mojom::MtpManager* mtp_manager);
~MtpManagerClientChromeOS() override; ~MtpManagerClientChromeOS() override;
// Finds the storage that contains |path| and populates |storage_info|.
// Returns false if unable to find the storage.
bool GetStorageInfoForPath(const base::FilePath& path,
StorageInfo* storage_info) const;
void EjectDevice(const std::string& device_id,
base::Callback<void(StorageMonitor::EjectStatus)> callback);
protected: protected:
// device::mojom::MtpManagerClient implementation. // device::mojom::MtpManagerClient implementation.
// Exposed for unit tests. // Exposed for unit tests.
...@@ -29,9 +42,21 @@ class MtpManagerClientChromeOS : public device::mojom::MtpManagerClient { ...@@ -29,9 +42,21 @@ class MtpManagerClientChromeOS : public device::mojom::MtpManagerClient {
void StorageDetached(const std::string& storage_name) override; void StorageDetached(const std::string& storage_name) override;
private: private:
// Mapping of storage location and MTP storage info object.
using StorageLocationToInfoMap = std::map<std::string, StorageInfo>;
// Enumerate existing MTP storage devices.
void OnReceivedStorages( void OnReceivedStorages(
std::vector<device::mojom::MtpStorageInfoPtr> storage_info_list); std::vector<device::mojom::MtpStorageInfoPtr> storage_info_list);
// Find the |storage_map_| key for the record with this |device_id|. Returns
// true on success, false on failure.
bool GetLocationForDeviceId(const std::string& device_id,
std::string* location) const;
// Map of all attached MTP devices.
StorageLocationToInfoMap storage_map_;
// Pointer to the MTP manager. Not owned. Client must ensure the MTP // Pointer to the MTP manager. Not owned. Client must ensure the MTP
// manager outlives this object. // manager outlives this object.
device::mojom::MtpManager* const mtp_manager_; device::mojom::MtpManager* const mtp_manager_;
......
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