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.
5 #ifndef CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_
6 #define CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_
10 #include "base/callback_forward.h"
11 #include "chromeos/chromeos_export.h"
12 #include "chromeos/dbus/cros_disks_client.h"
17 // Condition of mounted filesystem.
20 MOUNT_CONDITION_UNKNOWN_FILESYSTEM
,
21 MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM
,
24 // This class handles the interaction with cros-disks.
25 // Other classes can add themselves as observers.
26 class CHROMEOS_EXPORT DiskMountManager
{
28 // Event types passed to the observers.
51 // Used to house an instance of each found mount device.
54 Disk(const std::string
& device_path
,
55 const std::string
& mount_path
,
56 const std::string
& system_path
,
57 const std::string
& file_path
,
58 const std::string
& device_label
,
59 const std::string
& drive_label
,
60 const std::string
& vendor_id
,
61 const std::string
& vendor_name
,
62 const std::string
& product_id
,
63 const std::string
& product_name
,
64 const std::string
& fs_uuid
,
65 const std::string
& system_path_prefix
,
66 DeviceType device_type
,
67 uint64 total_size_in_bytes
,
75 // The path of the device, used by devicekit-disks.
76 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
77 const std::string
& device_path() const { return device_path_
; }
79 // The path to the mount point of this device. Will be empty if not mounted.
80 // (e.g. /media/removable/VOLUME)
81 const std::string
& mount_path() const { return mount_path_
; }
83 // The path of the device according to the udev system.
84 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
85 const std::string
& system_path() const { return system_path_
; }
87 // The path of the device according to filesystem.
89 const std::string
& file_path() const { return file_path_
; }
92 const std::string
& device_label() const { return device_label_
; }
94 // If disk is a parent, then its label, else parents label.
95 // (e.g. "TransMemory")
96 const std::string
& drive_label() const { return drive_label_
; }
98 // Vendor ID of the device (e.g. "18d1").
99 const std::string
& vendor_id() const { return vendor_id_
; }
101 // Vendor name of the device (e.g. "Google Inc.").
102 const std::string
& vendor_name() const { return vendor_name_
; }
104 // Product ID of the device (e.g. "4e11").
105 const std::string
& product_id() const { return product_id_
; }
107 // Product name of the device (e.g. "Nexus One").
108 const std::string
& product_name() const { return product_name_
; }
110 // Returns the file system uuid string.
111 const std::string
& fs_uuid() const { return fs_uuid_
; }
113 // Path of the system device this device's block is a part of.
114 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
115 const std::string
& system_path_prefix() const {
116 return system_path_prefix_
;
120 DeviceType
device_type() const { return device_type_
; }
122 // Total size of the device in bytes.
123 uint64
total_size_in_bytes() const { return total_size_in_bytes_
; }
125 // Is the device is a parent device (i.e. sdb rather than sdb1).
126 bool is_parent() const { return is_parent_
; }
128 // Is the device read only.
129 bool is_read_only() const { return is_read_only_
; }
131 // Does the device contains media.
132 bool has_media() const { return has_media_
; }
134 // Is the device on the boot device.
135 bool on_boot_device() const { return on_boot_device_
; }
137 // Shoud the device be shown in the UI, or automounted.
138 bool is_hidden() const { return is_hidden_
; }
140 void set_mount_path(const std::string
& mount_path
) {
141 mount_path_
= mount_path
;
144 void clear_mount_path() { mount_path_
.clear(); }
147 std::string device_path_
;
148 std::string mount_path_
;
149 std::string system_path_
;
150 std::string file_path_
;
151 std::string device_label_
;
152 std::string drive_label_
;
153 std::string vendor_id_
;
154 std::string vendor_name_
;
155 std::string product_id_
;
156 std::string product_name_
;
157 std::string fs_uuid_
;
158 std::string system_path_prefix_
;
159 DeviceType device_type_
;
160 uint64 total_size_in_bytes_
;
164 bool on_boot_device_
;
167 typedef std::map
<std::string
, Disk
*> DiskMap
;
169 // A struct to store information about mount point.
170 struct MountPointInfo
{
172 std::string source_path
;
174 std::string mount_path
;
176 MountType mount_type
;
177 // Condition of mount.
178 MountCondition mount_condition
;
180 MountPointInfo(const std::string
& source
,
181 const std::string
& mount
,
182 const MountType type
,
183 MountCondition condition
)
184 : source_path(source
),
187 mount_condition(condition
) {
191 // MountPointMap key is mount_path.
192 typedef std::map
<std::string
, MountPointInfo
> MountPointMap
;
194 // A callback function type which is called after UnmountDeviceRecursively
196 typedef base::Callback
<void(bool)> UnmountDeviceRecursivelyCallbackType
;
198 // A callback type for UnmountPath method.
199 typedef base::Callback
<void(MountError error_code
)> UnmountPathCallback
;
201 // Implement this interface to be notified about disk/mount related events.
204 virtual ~Observer() {}
206 // Called when disk mount status is changed.
207 virtual void OnDiskEvent(DiskEvent event
, const Disk
* disk
) = 0;
208 // Called when device status is changed.
209 virtual void OnDeviceEvent(DeviceEvent event
,
210 const std::string
& device_path
) = 0;
211 // Called after a mount point has been mounted or unmounted.
212 virtual void OnMountEvent(MountEvent event
,
213 MountError error_code
,
214 const MountPointInfo
& mount_info
) = 0;
215 // Called on format process events.
216 virtual void OnFormatEvent(FormatEvent event
,
217 FormatError error_code
,
218 const std::string
& device_path
) = 0;
221 virtual ~DiskMountManager() {}
224 virtual void AddObserver(Observer
* observer
) = 0;
226 // Removes an observer.
227 virtual void RemoveObserver(Observer
* observer
) = 0;
229 // Gets the list of disks found.
230 virtual const DiskMap
& disks() const = 0;
232 // Returns Disk object corresponding to |source_path| or NULL on failure.
233 virtual const Disk
* FindDiskBySourcePath(
234 const std::string
& source_path
) const = 0;
236 // Gets the list of mount points.
237 virtual const MountPointMap
& mount_points() const = 0;
239 // Requests refreshing all the information about mounted disks.
240 virtual void RequestMountInfoRefresh() = 0;
243 // Note that the mount operation may fail. To find out the result, one should
244 // observe DiskMountManager for |Observer::OnMountEvent| event, which will be
245 // raised upon the mount operation completion.
246 virtual void MountPath(const std::string
& source_path
,
247 const std::string
& source_format
,
248 const std::string
& mount_label
,
251 // Unmounts a mounted disk.
252 // |UnmountOptions| enum defined in chromeos/dbus/cros_disks_client.h.
253 // When the method is complete, |callback| will be called and observers'
254 // |OnMountEvent| will be raised.
256 // |callback| may be empty, in which case it gets ignored.
257 virtual void UnmountPath(const std::string
& mount_path
,
258 UnmountOptions options
,
259 const UnmountPathCallback
& callback
) = 0;
261 // Formats Device given its mount path. Unmounts the device.
262 // Example: mount_path: /media/VOLUME_LABEL
263 virtual void FormatMountedDevice(const std::string
& mount_path
) = 0;
265 // Unmounts device_path and all of its known children.
266 virtual void UnmountDeviceRecursively(
267 const std::string
& device_path
,
268 const UnmountDeviceRecursivelyCallbackType
& callback
) = 0;
270 // Used in tests to initialize the manager's disk and mount point sets.
271 // Default implementation does noting. It just fails.
272 virtual bool AddDiskForTest(Disk
* disk
);
273 virtual bool AddMountPointForTest(const MountPointInfo
& mount_point
);
275 // Returns corresponding string to |type| like "unknown_filesystem".
276 static std::string
MountConditionToString(MountCondition type
);
278 // Returns corresponding string to |type|, like "sd", "usb".
279 static std::string
DeviceTypeToString(DeviceType type
);
281 // Creates the global DiskMountManager instance.
282 static void Initialize();
284 // Similar to Initialize(), but can inject an alternative
285 // DiskMountManager such as MockDiskMountManager for testing.
286 // The injected object will be owned by the internal pointer and deleted
288 static void InitializeForTesting(DiskMountManager
* disk_mount_manager
);
290 // Destroys the global DiskMountManager instance if it exists.
291 static void Shutdown();
293 // Returns a pointer to the global DiskMountManager instance.
294 // Initialize() should already have been called.
295 static DiskMountManager
* GetInstance();
299 } // namespace chromeos
301 #endif // CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_