Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / components / storage_monitor / media_transfer_protocol_device_observer_linux.h
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| and |location|.
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
30 // Helper class to send MTP storage attachment and detachment events to
31 // StorageMonitor.
32 class MediaTransferProtocolDeviceObserverLinux
33     : public device::MediaTransferProtocolManager::Observer {
34  public:
35   MediaTransferProtocolDeviceObserverLinux(
36       StorageMonitor::Receiver* receiver,
37       device::MediaTransferProtocolManager* mtp_manager);
38   virtual ~MediaTransferProtocolDeviceObserverLinux();
39
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;
44
45   void EjectDevice(const std::string& device_id,
46                    base::Callback<void(StorageMonitor::EjectStatus)> callback);
47
48  protected:
49   // Only used in unit tests.
50   MediaTransferProtocolDeviceObserverLinux(
51       StorageMonitor::Receiver* receiver,
52       device::MediaTransferProtocolManager* mtp_manager,
53       GetStorageInfoFunc get_storage_info_func);
54
55   // device::MediaTransferProtocolManager::Observer implementation.
56   // Exposed for unit tests.
57   virtual void StorageChanged(bool is_attached,
58                               const std::string& storage_name) OVERRIDE;
59
60  private:
61   // Mapping of storage location and mtp storage info object.
62   typedef std::map<std::string, StorageInfo> StorageLocationToInfoMap;
63
64   // Enumerate existing mtp storage devices.
65   void EnumerateStorages();
66
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;
71
72   // Pointer to the MTP manager. Not owned. Client must ensure the MTP
73   // manager outlives this object.
74   device::MediaTransferProtocolManager* mtp_manager_;
75
76   // Map of all attached mtp devices.
77   StorageLocationToInfoMap storage_map_;
78
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_;
82
83   // The notifications object to use to signal newly attached devices.
84   // Guaranteed to outlive this class.
85   StorageMonitor::Receiver* const notifications_;
86
87   DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinux);
88 };
89
90 }  // namespace storage_monitor
91
92 #endif  // COMPONENTS_STORAGE_MONITOR_MEDIA_TRANSFER_PROTOCOL_DEVICE_OBSERVER_LINUX_H_