1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_STORAGE_MONITOR_MEDIA_TRANSFER_PROTOCOL_DEVICE_OBSERVER_LINUX_H_
6 #define COMPONENTS_STORAGE_MONITOR_MEDIA_TRANSFER_PROTOCOL_DEVICE_OBSERVER_LINUX_H_
11 #include "base/strings/string16.h"
12 #include "components/storage_monitor/storage_monitor.h"
13 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
19 namespace storage_monitor {
21 // Gets the mtp device information given a |storage_name|. On success,
22 // fills in |id|, |name| and |location|.
23 typedef void (*GetStorageInfoFunc)(
24 const std::string& storage_name,
25 device::MediaTransferProtocolManager* mtp_manager,
28 std::string* location);
30 // Helper class to send MTP storage attachment and detachment events to
32 class MediaTransferProtocolDeviceObserverLinux
33 : public device::MediaTransferProtocolManager::Observer {
35 MediaTransferProtocolDeviceObserverLinux(
36 StorageMonitor::Receiver* receiver,
37 device::MediaTransferProtocolManager* mtp_manager);
38 virtual ~MediaTransferProtocolDeviceObserverLinux();
40 // Finds the storage that contains |path| and populates |storage_info|.
41 // Returns false if unable to find the storage.
42 bool GetStorageInfoForPath(const base::FilePath& path,
43 StorageInfo* storage_info) const;
45 void EjectDevice(const std::string& device_id,
46 base::Callback<void(StorageMonitor::EjectStatus)> callback);
49 // Only used in unit tests.
50 MediaTransferProtocolDeviceObserverLinux(
51 StorageMonitor::Receiver* receiver,
52 device::MediaTransferProtocolManager* mtp_manager,
53 GetStorageInfoFunc get_storage_info_func);
55 // device::MediaTransferProtocolManager::Observer implementation.
56 // Exposed for unit tests.
57 virtual void StorageChanged(bool is_attached,
58 const std::string& storage_name) OVERRIDE;
61 // Mapping of storage location and mtp storage info object.
62 typedef std::map<std::string, StorageInfo> StorageLocationToInfoMap;
64 // Enumerate existing mtp storage devices.
65 void EnumerateStorages();
67 // Find the |storage_map_| key for the record with this |device_id|. Returns
68 // true on success, false on failure.
69 bool GetLocationForDeviceId(const std::string& device_id,
70 std::string* location) const;
72 // Pointer to the MTP manager. Not owned. Client must ensure the MTP
73 // manager outlives this object.
74 device::MediaTransferProtocolManager* mtp_manager_;
76 // Map of all attached mtp devices.
77 StorageLocationToInfoMap storage_map_;
79 // Function handler to get storage information. This is useful to set a mock
80 // handler for unit testing.
81 GetStorageInfoFunc get_storage_info_func_;
83 // The notifications object to use to signal newly attached devices.
84 // Guaranteed to outlive this class.
85 StorageMonitor::Receiver* const notifications_;
87 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinux);
90 } // namespace storage_monitor
92 #endif // COMPONENTS_STORAGE_MONITOR_MEDIA_TRANSFER_PROTOCOL_DEVICE_OBSERVER_LINUX_H_