storage_monitor_chromeos.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // StorageMonitorCros listens for mount point changes and notifies listeners
  5. // about the addition and deletion of media devices. This class lives on the
  6. // UI thread.
  7. #ifndef COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_CHROMEOS_H_
  8. #define COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_CHROMEOS_H_
  9. #include "build/chromeos_buildflags.h"
  10. #if !BUILDFLAG(IS_CHROMEOS_ASH)
  11. #error "Should only be used on ChromeOS."
  12. #endif
  13. #include <map>
  14. #include <memory>
  15. #include <string>
  16. #include "ash/components/disks/disk_mount_manager.h"
  17. #include "base/compiler_specific.h"
  18. #include "base/memory/weak_ptr.h"
  19. #include "build/build_config.h"
  20. #include "build/chromeos_buildflags.h"
  21. #include "components/storage_monitor/storage_monitor.h"
  22. #include "mojo/public/cpp/bindings/pending_remote.h"
  23. #include "mojo/public/cpp/bindings/remote.h"
  24. #include "services/device/public/mojom/mtp_manager.mojom.h"
  25. namespace storage_monitor {
  26. class MtpManagerClientChromeOS;
  27. class StorageMonitorCros : public StorageMonitor,
  28. public ash::disks::DiskMountManager::Observer {
  29. public:
  30. // Should only be called by browser start up code.
  31. // Use StorageMonitor::GetInstance() instead.
  32. StorageMonitorCros();
  33. StorageMonitorCros(const StorageMonitorCros&) = delete;
  34. StorageMonitorCros& operator=(const StorageMonitorCros&) = delete;
  35. ~StorageMonitorCros() override;
  36. // Sets up disk listeners and issues notifications for any discovered
  37. // mount points. Sets up MTP manager and listeners.
  38. void Init() override;
  39. protected:
  40. void SetMediaTransferProtocolManagerForTest(
  41. mojo::PendingRemote<device::mojom::MtpManager> test_manager);
  42. // ash::disks::DiskMountManager::Observer implementation.
  43. void OnBootDeviceDiskEvent(ash::disks::DiskMountManager::DiskEvent event,
  44. const ash::disks::Disk& disk) override;
  45. void OnMountEvent(
  46. ash::disks::DiskMountManager::MountEvent event,
  47. ash::MountError error_code,
  48. const ash::disks::DiskMountManager::MountPoint& mount_info) override;
  49. // StorageMonitor implementation.
  50. bool GetStorageInfoForPath(const base::FilePath& path,
  51. StorageInfo* device_info) const override;
  52. void EjectDevice(const std::string& device_id,
  53. base::OnceCallback<void(EjectStatus)> callback) override;
  54. device::mojom::MtpManager* media_transfer_protocol_manager() override;
  55. private:
  56. // Mapping of mount path to removable mass storage info.
  57. typedef std::map<std::string, StorageInfo> MountMap;
  58. // Helper method that checks existing mount points to see if they are media
  59. // devices. Eventually calls AddMountedPath for all mount points.
  60. void CheckExistingMountPoints();
  61. // Adds the mount point in |mount_info| to |mount_map_| and send a media
  62. // device attach notification. |has_dcim| is true if the attached device has
  63. // a DCIM folder.
  64. void AddMountedPath(
  65. const ash::disks::DiskMountManager::MountPoint& mount_info,
  66. bool has_dcim);
  67. // Adds the mount point in |disk| to |mount_map_| and send a device
  68. // attach notification.
  69. void AddFixedStorageDisk(const ash::disks::Disk& disk);
  70. // Removes the mount point in |disk| from |mount_map_| and send a device
  71. // detach notification.
  72. void RemoveFixedStorageDisk(const ash::disks::Disk& disk);
  73. // Mapping of relevant mount points and their corresponding mount devices.
  74. MountMap mount_map_;
  75. mojo::Remote<device::mojom::MtpManager> mtp_device_manager_;
  76. std::unique_ptr<MtpManagerClientChromeOS> mtp_manager_client_;
  77. base::WeakPtrFactory<StorageMonitorCros> weak_ptr_factory_{this};
  78. };
  79. } // namespace storage_monitor
  80. #endif // COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_CHROMEOS_H_