mtp_manager_client_chromeos.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2018 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. #ifndef COMPONENTS_STORAGE_MONITOR_MTP_MANAGER_CLIENT_CHROMEOS_H_
  5. #define COMPONENTS_STORAGE_MONITOR_MTP_MANAGER_CLIENT_CHROMEOS_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/weak_ptr.h"
  10. #include "components/storage_monitor/storage_monitor.h"
  11. #include "mojo/public/cpp/bindings/associated_receiver.h"
  12. #include "services/device/public/mojom/mtp_manager.mojom.h"
  13. namespace base {
  14. class FilePath;
  15. }
  16. namespace storage_monitor {
  17. // This client listens for MTP storage attachment and detachment events
  18. // from MtpManager and forwards them to StorageMonitor.
  19. class MtpManagerClientChromeOS : public device::mojom::MtpManagerClient {
  20. public:
  21. MtpManagerClientChromeOS(StorageMonitor::Receiver* receiver,
  22. device::mojom::MtpManager* mtp_manager);
  23. MtpManagerClientChromeOS(const MtpManagerClientChromeOS&) = delete;
  24. MtpManagerClientChromeOS& operator=(const MtpManagerClientChromeOS&) = delete;
  25. ~MtpManagerClientChromeOS() override;
  26. // Finds the storage that contains |path| and populates |storage_info|.
  27. // Returns false if unable to find the storage.
  28. bool GetStorageInfoForPath(const base::FilePath& path,
  29. StorageInfo* storage_info) const;
  30. void EjectDevice(
  31. const std::string& device_id,
  32. base::OnceCallback<void(StorageMonitor::EjectStatus)> callback);
  33. protected:
  34. // device::mojom::MtpManagerClient implementation.
  35. // Exposed for unit tests.
  36. void StorageAttached(device::mojom::MtpStorageInfoPtr storage_info) override;
  37. void StorageDetached(const std::string& storage_name) override;
  38. private:
  39. // Mapping of storage location and MTP storage info object.
  40. using StorageLocationToInfoMap = std::map<std::string, StorageInfo>;
  41. // Enumerate existing MTP storage devices.
  42. void OnReceivedStorages(
  43. std::vector<device::mojom::MtpStorageInfoPtr> storage_info_list);
  44. // Find the |storage_map_| key for the record with this |device_id|. Returns
  45. // true on success, false on failure.
  46. bool GetLocationForDeviceId(const std::string& device_id,
  47. std::string* location) const;
  48. // Map of all attached MTP devices.
  49. StorageLocationToInfoMap storage_map_;
  50. // Pointer to the MTP manager. Not owned. Client must ensure the MTP
  51. // manager outlives this object.
  52. device::mojom::MtpManager* const mtp_manager_;
  53. mojo::AssociatedReceiver<device::mojom::MtpManagerClient> receiver_{this};
  54. // The notifications object to use to signal newly attached devices.
  55. // Guaranteed to outlive this class.
  56. StorageMonitor::Receiver* const notifications_;
  57. base::WeakPtrFactory<MtpManagerClientChromeOS> weak_ptr_factory_{this};
  58. };
  59. } // namespace storage_monitor
  60. #endif // COMPONENTS_STORAGE_MONITOR_MTP_MANAGER_CLIENT_CHROMEOS_H_