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