blob: cf29e72c07bac82469a714e5bac6489bb5567f74 [file] [log] [blame]
[email protected]2321d282012-01-31 23:06:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4ae73292011-11-15 05:20:182// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4cbead42012-08-26 12:02:395#ifndef CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_
6#define CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_
[email protected]4ae73292011-11-15 05:20:187
avi6e1a22d2015-12-21 03:43:208#include <stdint.h>
9
[email protected]4ae73292011-11-15 05:20:1810#include <map>
11
[email protected]e008d4fa2013-02-21 06:38:0112#include "base/callback_forward.h"
[email protected]4cbead42012-08-26 12:02:3913#include "chromeos/chromeos_export.h"
[email protected]64e199252012-04-06 01:54:3614#include "chromeos/dbus/cros_disks_client.h"
[email protected]4ae73292011-11-15 05:20:1815
16namespace chromeos {
17namespace disks {
18
[email protected]4ae73292011-11-15 05:20:1819// Condition of mounted filesystem.
20enum MountCondition {
21 MOUNT_CONDITION_NONE,
22 MOUNT_CONDITION_UNKNOWN_FILESYSTEM,
23 MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM,
24};
25
26// This class handles the interaction with cros-disks.
27// Other classes can add themselves as observers.
[email protected]4cbead42012-08-26 12:02:3928class CHROMEOS_EXPORT DiskMountManager {
[email protected]4ae73292011-11-15 05:20:1829 public:
[email protected]e3c1fc92012-11-15 00:56:4630 // Event types passed to the observers.
31 enum DiskEvent {
32 DISK_ADDED,
33 DISK_REMOVED,
34 DISK_CHANGED,
35 };
36
37 enum DeviceEvent {
38 DEVICE_ADDED,
39 DEVICE_REMOVED,
40 DEVICE_SCANNED,
41 };
42
[email protected]4ae73292011-11-15 05:20:1843 enum MountEvent {
44 MOUNTING,
45 UNMOUNTING,
46 };
47
[email protected]e3c1fc92012-11-15 00:56:4648 enum FormatEvent {
49 FORMAT_STARTED,
50 FORMAT_COMPLETED
51 };
52
[email protected]4ae73292011-11-15 05:20:1853 // Used to house an instance of each found mount device.
54 class Disk {
55 public:
56 Disk(const std::string& device_path,
57 const std::string& mount_path,
58 const std::string& system_path,
59 const std::string& file_path,
60 const std::string& device_label,
61 const std::string& drive_label,
[email protected]202e9fee2012-09-13 20:21:2962 const std::string& vendor_id,
63 const std::string& vendor_name,
64 const std::string& product_id,
65 const std::string& product_name,
[email protected]9c5620d32012-07-31 01:00:3866 const std::string& fs_uuid,
[email protected]4ae73292011-11-15 05:20:1867 const std::string& system_path_prefix,
68 DeviceType device_type,
avi6e1a22d2015-12-21 03:43:2069 uint64_t total_size_in_bytes,
[email protected]4ae73292011-11-15 05:20:1870 bool is_parent,
71 bool is_read_only,
72 bool has_media,
73 bool on_boot_device,
[email protected]79ed457b2014-07-22 04:07:2674 bool on_removable_device,
[email protected]4ae73292011-11-15 05:20:1875 bool is_hidden);
76 ~Disk();
77
78 // The path of the device, used by devicekit-disks.
79 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
80 const std::string& device_path() const { return device_path_; }
81
82 // The path to the mount point of this device. Will be empty if not mounted.
83 // (e.g. /media/removable/VOLUME)
84 const std::string& mount_path() const { return mount_path_; }
85
86 // The path of the device according to the udev system.
87 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
88 const std::string& system_path() const { return system_path_; }
89
90 // The path of the device according to filesystem.
91 // (e.g. /dev/sdb)
92 const std::string& file_path() const { return file_path_; }
93
94 // Device's label.
95 const std::string& device_label() const { return device_label_; }
96
97 // If disk is a parent, then its label, else parents label.
98 // (e.g. "TransMemory")
99 const std::string& drive_label() const { return drive_label_; }
100
[email protected]202e9fee2012-09-13 20:21:29101 // Vendor ID of the device (e.g. "18d1").
102 const std::string& vendor_id() const { return vendor_id_; }
103
104 // Vendor name of the device (e.g. "Google Inc.").
105 const std::string& vendor_name() const { return vendor_name_; }
106
107 // Product ID of the device (e.g. "4e11").
108 const std::string& product_id() const { return product_id_; }
109
110 // Product name of the device (e.g. "Nexus One").
111 const std::string& product_name() const { return product_name_; }
112
[email protected]9c5620d32012-07-31 01:00:38113 // Returns the file system uuid string.
114 const std::string& fs_uuid() const { return fs_uuid_; }
115
[email protected]4ae73292011-11-15 05:20:18116 // Path of the system device this device's block is a part of.
117 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
118 const std::string& system_path_prefix() const {
119 return system_path_prefix_;
120 }
121
122 // Device type.
123 DeviceType device_type() const { return device_type_; }
124
125 // Total size of the device in bytes.
avi6e1a22d2015-12-21 03:43:20126 uint64_t total_size_in_bytes() const { return total_size_in_bytes_; }
[email protected]4ae73292011-11-15 05:20:18127
128 // Is the device is a parent device (i.e. sdb rather than sdb1).
129 bool is_parent() const { return is_parent_; }
130
131 // Is the device read only.
132 bool is_read_only() const { return is_read_only_; }
133
134 // Does the device contains media.
135 bool has_media() const { return has_media_; }
136
137 // Is the device on the boot device.
138 bool on_boot_device() const { return on_boot_device_; }
139
[email protected]79ed457b2014-07-22 04:07:26140 // Is the device on the removable device.
141 bool on_removable_device() const { return on_removable_device_; }
142
[email protected]4ae73292011-11-15 05:20:18143 // Shoud the device be shown in the UI, or automounted.
144 bool is_hidden() const { return is_hidden_; }
145
146 void set_mount_path(const std::string& mount_path) {
147 mount_path_ = mount_path;
148 }
149
150 void clear_mount_path() { mount_path_.clear(); }
151
152 private:
153 std::string device_path_;
154 std::string mount_path_;
155 std::string system_path_;
156 std::string file_path_;
157 std::string device_label_;
158 std::string drive_label_;
[email protected]202e9fee2012-09-13 20:21:29159 std::string vendor_id_;
160 std::string vendor_name_;
161 std::string product_id_;
162 std::string product_name_;
[email protected]9c5620d32012-07-31 01:00:38163 std::string fs_uuid_;
[email protected]4ae73292011-11-15 05:20:18164 std::string system_path_prefix_;
165 DeviceType device_type_;
avi6e1a22d2015-12-21 03:43:20166 uint64_t total_size_in_bytes_;
[email protected]4ae73292011-11-15 05:20:18167 bool is_parent_;
168 bool is_read_only_;
169 bool has_media_;
170 bool on_boot_device_;
[email protected]79ed457b2014-07-22 04:07:26171 bool on_removable_device_;
[email protected]4ae73292011-11-15 05:20:18172 bool is_hidden_;
173 };
174 typedef std::map<std::string, Disk*> DiskMap;
175
176 // A struct to store information about mount point.
177 struct MountPointInfo {
178 // Device's path.
179 std::string source_path;
180 // Mounted path.
181 std::string mount_path;
182 // Type of mount.
183 MountType mount_type;
184 // Condition of mount.
185 MountCondition mount_condition;
186
187 MountPointInfo(const std::string& source,
188 const std::string& mount,
189 const MountType type,
190 MountCondition condition)
191 : source_path(source),
192 mount_path(mount),
193 mount_type(type),
194 mount_condition(condition) {
195 }
196 };
197
198 // MountPointMap key is mount_path.
199 typedef std::map<std::string, MountPointInfo> MountPointMap;
200
[email protected]e008d4fa2013-02-21 06:38:01201 // A callback function type which is called after UnmountDeviceRecursively
[email protected]4ae73292011-11-15 05:20:18202 // finishes.
[email protected]e008d4fa2013-02-21 06:38:01203 typedef base::Callback<void(bool)> UnmountDeviceRecursivelyCallbackType;
[email protected]4ae73292011-11-15 05:20:18204
[email protected]8f919ee2013-03-14 19:53:29205 // A callback type for UnmountPath method.
206 typedef base::Callback<void(MountError error_code)> UnmountPathCallback;
207
[email protected]a2e4ee22014-07-11 05:16:35208 // A callback type for EnsureMountInfoRefreshed method.
209 typedef base::Callback<void(bool success)> EnsureMountInfoRefreshedCallback;
210
[email protected]4ae73292011-11-15 05:20:18211 // Implement this interface to be notified about disk/mount related events.
212 class Observer {
213 public:
214 virtual ~Observer() {}
215
[email protected]e3c1fc92012-11-15 00:56:46216 // Called when disk mount status is changed.
217 virtual void OnDiskEvent(DiskEvent event, const Disk* disk) = 0;
218 // Called when device status is changed.
219 virtual void OnDeviceEvent(DeviceEvent event,
[email protected]4ae73292011-11-15 05:20:18220 const std::string& device_path) = 0;
[email protected]e3c1fc92012-11-15 00:56:46221 // Called after a mount point has been mounted or unmounted.
222 virtual void OnMountEvent(MountEvent event,
223 MountError error_code,
224 const MountPointInfo& mount_info) = 0;
225 // Called on format process events.
226 virtual void OnFormatEvent(FormatEvent event,
227 FormatError error_code,
228 const std::string& device_path) = 0;
[email protected]4ae73292011-11-15 05:20:18229 };
230
231 virtual ~DiskMountManager() {}
232
233 // Adds an observer.
234 virtual void AddObserver(Observer* observer) = 0;
235
236 // Removes an observer.
237 virtual void RemoveObserver(Observer* observer) = 0;
238
239 // Gets the list of disks found.
240 virtual const DiskMap& disks() const = 0;
241
[email protected]bcfa0072012-08-07 01:08:57242 // Returns Disk object corresponding to |source_path| or NULL on failure.
243 virtual const Disk* FindDiskBySourcePath(
244 const std::string& source_path) const = 0;
245
[email protected]4ae73292011-11-15 05:20:18246 // Gets the list of mount points.
247 virtual const MountPointMap& mount_points() const = 0;
248
[email protected]a2e4ee22014-07-11 05:16:35249 // Refreshes all the information about mounting if it is not yet done and
250 // invokes |callback| when finished. If the information is already refreshed
hironoa4b675d2015-07-29 01:13:37251 // and |force| is false, it just runs |callback| immediately.
[email protected]a2e4ee22014-07-11 05:16:35252 virtual void EnsureMountInfoRefreshed(
hironoa4b675d2015-07-29 01:13:37253 const EnsureMountInfoRefreshedCallback& callback,
254 bool force) = 0;
[email protected]4ae73292011-11-15 05:20:18255
256 // Mounts a device.
[email protected]8f919ee2013-03-14 19:53:29257 // Note that the mount operation may fail. To find out the result, one should
258 // observe DiskMountManager for |Observer::OnMountEvent| event, which will be
259 // raised upon the mount operation completion.
[email protected]b9f22d12012-04-25 21:46:48260 virtual void MountPath(const std::string& source_path,
261 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33262 const std::string& mount_label,
[email protected]b9f22d12012-04-25 21:46:48263 MountType type) = 0;
[email protected]4ae73292011-11-15 05:20:18264
265 // Unmounts a mounted disk.
[email protected]10795ae2012-10-10 07:33:49266 // |UnmountOptions| enum defined in chromeos/dbus/cros_disks_client.h.
[email protected]8f919ee2013-03-14 19:53:29267 // When the method is complete, |callback| will be called and observers'
268 // |OnMountEvent| will be raised.
269 //
270 // |callback| may be empty, in which case it gets ignored.
[email protected]10795ae2012-10-10 07:33:49271 virtual void UnmountPath(const std::string& mount_path,
[email protected]8f919ee2013-03-14 19:53:29272 UnmountOptions options,
273 const UnmountPathCallback& callback) = 0;
[email protected]4ae73292011-11-15 05:20:18274
[email protected]4ae73292011-11-15 05:20:18275 // Formats Device given its mount path. Unmounts the device.
276 // Example: mount_path: /media/VOLUME_LABEL
277 virtual void FormatMountedDevice(const std::string& mount_path) = 0;
278
279 // Unmounts device_path and all of its known children.
[email protected]e008d4fa2013-02-21 06:38:01280 virtual void UnmountDeviceRecursively(
[email protected]4ae73292011-11-15 05:20:18281 const std::string& device_path,
[email protected]e008d4fa2013-02-21 06:38:01282 const UnmountDeviceRecursivelyCallbackType& callback) = 0;
[email protected]4ae73292011-11-15 05:20:18283
[email protected]e3c1fc92012-11-15 00:56:46284 // Used in tests to initialize the manager's disk and mount point sets.
285 // Default implementation does noting. It just fails.
286 virtual bool AddDiskForTest(Disk* disk);
287 virtual bool AddMountPointForTest(const MountPointInfo& mount_point);
288
[email protected]4ae73292011-11-15 05:20:18289 // Returns corresponding string to |type| like "unknown_filesystem".
290 static std::string MountConditionToString(MountCondition type);
291
[email protected]2321d282012-01-31 23:06:59292 // Returns corresponding string to |type|, like "sd", "usb".
293 static std::string DeviceTypeToString(DeviceType type);
294
[email protected]4ae73292011-11-15 05:20:18295 // Creates the global DiskMountManager instance.
296 static void Initialize();
297
[email protected]b307bceb2011-11-17 07:49:55298 // Similar to Initialize(), but can inject an alternative
299 // DiskMountManager such as MockDiskMountManager for testing.
300 // The injected object will be owned by the internal pointer and deleted
301 // by Shutdown().
302 static void InitializeForTesting(DiskMountManager* disk_mount_manager);
303
[email protected]4ae73292011-11-15 05:20:18304 // Destroys the global DiskMountManager instance if it exists.
305 static void Shutdown();
306
307 // Returns a pointer to the global DiskMountManager instance.
308 // Initialize() should already have been called.
309 static DiskMountManager* GetInstance();
310};
311
312} // namespace disks
313} // namespace chromeos
314
[email protected]4cbead42012-08-26 12:02:39315#endif // CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_