• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef COMPONENTS_STORAGE_MONITOR_MEDIA_TRANSFER_PROTOCOL_DEVICE_OBSERVER_LINUX_H_
6 #define COMPONENTS_STORAGE_MONITOR_MEDIA_TRANSFER_PROTOCOL_DEVICE_OBSERVER_LINUX_H_
7 
8 #include <map>
9 #include <string>
10 
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"
14 
15 namespace base {
16 class FilePath;
17 }
18 
19 namespace storage_monitor {
20 
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,
26     std::string* id,
27     base::string16* name,
28     std::string* location,
29     base::string16* vendor_name,
30     base::string16* product_name);
31 
32 // Helper class to send MTP storage attachment and detachment events to
33 // StorageMonitor.
34 class MediaTransferProtocolDeviceObserverLinux
35     : public device::MediaTransferProtocolManager::Observer {
36  public:
37   MediaTransferProtocolDeviceObserverLinux(
38       StorageMonitor::Receiver* receiver,
39       device::MediaTransferProtocolManager* mtp_manager);
40   virtual ~MediaTransferProtocolDeviceObserverLinux();
41 
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;
46 
47   void EjectDevice(const std::string& device_id,
48                    base::Callback<void(StorageMonitor::EjectStatus)> callback);
49 
50  protected:
51   // Only used in unit tests.
52   MediaTransferProtocolDeviceObserverLinux(
53       StorageMonitor::Receiver* receiver,
54       device::MediaTransferProtocolManager* mtp_manager,
55       GetStorageInfoFunc get_storage_info_func);
56 
57   // device::MediaTransferProtocolManager::Observer implementation.
58   // Exposed for unit tests.
59   virtual void StorageChanged(bool is_attached,
60                               const std::string& storage_name) OVERRIDE;
61 
62  private:
63   // Mapping of storage location and mtp storage info object.
64   typedef std::map<std::string, StorageInfo> StorageLocationToInfoMap;
65 
66   // Enumerate existing mtp storage devices.
67   void EnumerateStorages();
68 
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;
73 
74   // Pointer to the MTP manager. Not owned. Client must ensure the MTP
75   // manager outlives this object.
76   device::MediaTransferProtocolManager* mtp_manager_;
77 
78   // Map of all attached mtp devices.
79   StorageLocationToInfoMap storage_map_;
80 
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_;
84 
85   // The notifications object to use to signal newly attached devices.
86   // Guaranteed to outlive this class.
87   StorageMonitor::Receiver* const notifications_;
88 
89   DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinux);
90 };
91 
92 }  // namespace storage_monitor
93 
94 #endif  // COMPONENTS_STORAGE_MONITOR_MEDIA_TRANSFER_PROTOCOL_DEVICE_OBSERVER_LINUX_H_
95