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|, |location|, |vendor_name|, and |product_name|.
23 typedef void (*GetStorageInfoFunc
)(
24 const std::string
& storage_name
,
25 device::MediaTransferProtocolManager
* mtp_manager
,
28 std::string
* location
,
29 base::string16
* vendor_name
,
30 base::string16
* product_name
);
32 // Helper class to send MTP storage attachment and detachment events to
34 class MediaTransferProtocolDeviceObserverLinux
35 : public device::MediaTransferProtocolManager::Observer
{
37 MediaTransferProtocolDeviceObserverLinux(
38 StorageMonitor::Receiver
* receiver
,
39 device::MediaTransferProtocolManager
* mtp_manager
);
40 ~MediaTransferProtocolDeviceObserverLinux() override
;
42 // Finds the storage that contains |path| and populates |storage_info|.
43 // Returns false if unable to find the storage.
44 bool GetStorageInfoForPath(const base::FilePath
& path
,
45 StorageInfo
* storage_info
) const;
47 void EjectDevice(const std::string
& device_id
,
48 base::Callback
<void(StorageMonitor::EjectStatus
)> callback
);
51 // Only used in unit tests.
52 MediaTransferProtocolDeviceObserverLinux(
53 StorageMonitor::Receiver
* receiver
,
54 device::MediaTransferProtocolManager
* mtp_manager
,
55 GetStorageInfoFunc get_storage_info_func
);
57 // device::MediaTransferProtocolManager::Observer implementation.
58 // Exposed for unit tests.
59 void StorageChanged(bool is_attached
,
60 const std::string
& storage_name
) override
;
63 // Mapping of storage location and mtp storage info object.
64 typedef std::map
<std::string
, StorageInfo
> StorageLocationToInfoMap
;
66 // Enumerate existing mtp storage devices.
67 void EnumerateStorages();
69 // Find the |storage_map_| key for the record with this |device_id|. Returns
70 // true on success, false on failure.
71 bool GetLocationForDeviceId(const std::string
& device_id
,
72 std::string
* location
) const;
74 // Pointer to the MTP manager. Not owned. Client must ensure the MTP
75 // manager outlives this object.
76 device::MediaTransferProtocolManager
* mtp_manager_
;
78 // Map of all attached mtp devices.
79 StorageLocationToInfoMap storage_map_
;
81 // Function handler to get storage information. This is useful to set a mock
82 // handler for unit testing.
83 GetStorageInfoFunc get_storage_info_func_
;
85 // The notifications object to use to signal newly attached devices.
86 // Guaranteed to outlive this class.
87 StorageMonitor::Receiver
* const notifications_
;
89 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinux
);
92 } // namespace storage_monitor
94 #endif // COMPONENTS_STORAGE_MONITOR_MEDIA_TRANSFER_PROTOCOL_DEVICE_OBSERVER_LINUX_H_