1// Copyright (c) 2012 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 CHROME_BROWSER_STORAGE_MONITOR_MEDIA_TRANSFER_PROTOCOL_DEVICE_OBSERVER_LINUX_H_
6#define CHROME_BROWSER_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 "chrome/browser/storage_monitor/storage_monitor.h"
13#include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
14
15namespace base {
16class FilePath;
17}
18
19namespace chrome {
20
21// Gets the mtp device information given a |storage_name|. On success,
22// fills in |id|, |name| and |location|.
23typedef void (*GetStorageInfoFunc)(
24    const std::string& storage_name,
25    device::MediaTransferProtocolManager* mtp_manager,
26    std::string* id,
27    string16* name,
28    std::string* location);
29
30// Helper class to send MTP storage attachment and detachment events to
31// StorageMonitor.
32class 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 protected:
46  // Only used in unit tests.
47  MediaTransferProtocolDeviceObserverLinux(
48      StorageMonitor::Receiver* receiver,
49      device::MediaTransferProtocolManager* mtp_manager,
50      GetStorageInfoFunc get_storage_info_func);
51
52  // device::MediaTransferProtocolManager::Observer implementation.
53  // Exposed for unit tests.
54  virtual void StorageChanged(bool is_attached,
55                              const std::string& storage_name) OVERRIDE;
56
57 private:
58  // Mapping of storage location and mtp storage info object.
59  typedef std::map<std::string, StorageInfo> StorageLocationToInfoMap;
60
61  // Enumerate existing mtp storage devices.
62  void EnumerateStorages();
63
64  // Pointer to the MTP manager. Not owned. Client must ensure the MTP
65  // manager outlives this object.
66  device::MediaTransferProtocolManager* mtp_manager_;
67
68  // Map of all attached mtp devices.
69  StorageLocationToInfoMap storage_map_;
70
71  // Function handler to get storage information. This is useful to set a mock
72  // handler for unit testing.
73  GetStorageInfoFunc get_storage_info_func_;
74
75  // The notifications object to use to signal newly attached devices.
76  // Guaranteed to outlive this class.
77  StorageMonitor::Receiver* const notifications_;
78
79  DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinux);
80};
81
82}  // namespace chrome
83
84#endif  // CHROME_BROWSER_STORAGE_MONITOR_MEDIA_TRANSFER_PROTOCOL_DEVICE_OBSERVER_LINUX_H_
85