1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_STORAGE_MONITOR_MTP_MANAGER_CLIENT_CHROMEOS_H_
6 #define COMPONENTS_STORAGE_MONITOR_MTP_MANAGER_CLIENT_CHROMEOS_H_
12 #include "base/memory/raw_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/storage_monitor/storage_monitor.h"
15 #include "mojo/public/cpp/bindings/associated_receiver.h"
16 #include "services/device/public/mojom/mtp_manager.mojom.h"
22 namespace storage_monitor {
24 // This client listens for MTP storage attachment and detachment events
25 // from MtpManager and forwards them to StorageMonitor.
26 class MtpManagerClientChromeOS : public device::mojom::MtpManagerClient {
28 MtpManagerClientChromeOS(StorageMonitor::Receiver* receiver,
29 device::mojom::MtpManager* mtp_manager);
31 MtpManagerClientChromeOS(const MtpManagerClientChromeOS&) = delete;
32 MtpManagerClientChromeOS& operator=(const MtpManagerClientChromeOS&) = delete;
34 ~MtpManagerClientChromeOS() override;
36 // Finds the storage that contains |path| and populates |storage_info|.
37 // Returns false if unable to find the storage.
38 bool GetStorageInfoForPath(const base::FilePath& path,
39 StorageInfo* storage_info) const;
42 const std::string& device_id,
43 base::OnceCallback<void(StorageMonitor::EjectStatus)> callback);
46 // device::mojom::MtpManagerClient implementation.
47 // Exposed for unit tests.
48 void StorageAttached(device::mojom::MtpStorageInfoPtr storage_info) override;
49 void StorageDetached(const std::string& storage_name) override;
52 // Mapping of storage location and MTP storage info object.
53 using StorageLocationToInfoMap = std::map<std::string, StorageInfo>;
55 // Enumerate existing MTP storage devices.
56 void OnReceivedStorages(
57 std::vector<device::mojom::MtpStorageInfoPtr> storage_info_list);
59 // Find the |storage_map_| key for the record with this |device_id|. Returns
60 // true on success, false on failure.
61 bool GetLocationForDeviceId(const std::string& device_id,
62 std::string* location) const;
64 // Map of all attached MTP devices.
65 StorageLocationToInfoMap storage_map_;
67 // Pointer to the MTP manager. Not owned. Client must ensure the MTP
68 // manager outlives this object.
69 const raw_ptr<device::mojom::MtpManager, ExperimentalAsh> mtp_manager_;
71 mojo::AssociatedReceiver<device::mojom::MtpManagerClient> receiver_{this};
73 // The notifications object to use to signal newly attached devices.
74 // Guaranteed to outlive this class.
75 const raw_ptr<StorageMonitor::Receiver, ExperimentalAsh> notifications_;
77 base::WeakPtrFactory<MtpManagerClientChromeOS> weak_ptr_factory_{this};
80 } // namespace storage_monitor
82 #endif // COMPONENTS_STORAGE_MONITOR_MTP_MANAGER_CLIENT_CHROMEOS_H_