[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4cbead4 | 2012-08-26 12:02:39 | [diff] [blame] | 5 | #ifndef CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_ |
| 6 | #define CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_ |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 7 | |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 10 | #include <map> |
| 11 | |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 12 | #include "base/callback_forward.h" |
[email protected] | 4cbead4 | 2012-08-26 12:02:39 | [diff] [blame] | 13 | #include "chromeos/chromeos_export.h" |
[email protected] | 64e19925 | 2012-04-06 01:54:36 | [diff] [blame] | 14 | #include "chromeos/dbus/cros_disks_client.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 15 | |
| 16 | namespace chromeos { |
| 17 | namespace disks { |
| 18 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 19 | // Condition of mounted filesystem. |
| 20 | enum 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] | 4cbead4 | 2012-08-26 12:02:39 | [diff] [blame] | 28 | class CHROMEOS_EXPORT DiskMountManager { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 29 | public: |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 30 | // 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] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 43 | enum MountEvent { |
| 44 | MOUNTING, |
| 45 | UNMOUNTING, |
| 46 | }; |
| 47 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 48 | enum FormatEvent { |
| 49 | FORMAT_STARTED, |
| 50 | FORMAT_COMPLETED |
| 51 | }; |
| 52 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 53 | // 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] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame] | 62 | 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] | 9c5620d3 | 2012-07-31 01:00:38 | [diff] [blame] | 66 | const std::string& fs_uuid, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 67 | const std::string& system_path_prefix, |
| 68 | DeviceType device_type, |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 69 | uint64_t total_size_in_bytes, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 70 | bool is_parent, |
| 71 | bool is_read_only, |
| 72 | bool has_media, |
| 73 | bool on_boot_device, |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 74 | bool on_removable_device, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 75 | bool is_hidden); |
vmpstr | 2a0c1040 | 2016-04-08 21:28:07 | [diff] [blame] | 76 | Disk(const Disk& other); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 77 | ~Disk(); |
| 78 | |
| 79 | // The path of the device, used by devicekit-disks. |
| 80 | // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1) |
| 81 | const std::string& device_path() const { return device_path_; } |
| 82 | |
| 83 | // The path to the mount point of this device. Will be empty if not mounted. |
| 84 | // (e.g. /media/removable/VOLUME) |
| 85 | const std::string& mount_path() const { return mount_path_; } |
| 86 | |
| 87 | // The path of the device according to the udev system. |
| 88 | // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1) |
| 89 | const std::string& system_path() const { return system_path_; } |
| 90 | |
| 91 | // The path of the device according to filesystem. |
| 92 | // (e.g. /dev/sdb) |
| 93 | const std::string& file_path() const { return file_path_; } |
| 94 | |
| 95 | // Device's label. |
| 96 | const std::string& device_label() const { return device_label_; } |
| 97 | |
| 98 | // If disk is a parent, then its label, else parents label. |
| 99 | // (e.g. "TransMemory") |
| 100 | const std::string& drive_label() const { return drive_label_; } |
| 101 | |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame] | 102 | // Vendor ID of the device (e.g. "18d1"). |
| 103 | const std::string& vendor_id() const { return vendor_id_; } |
| 104 | |
| 105 | // Vendor name of the device (e.g. "Google Inc."). |
| 106 | const std::string& vendor_name() const { return vendor_name_; } |
| 107 | |
| 108 | // Product ID of the device (e.g. "4e11"). |
| 109 | const std::string& product_id() const { return product_id_; } |
| 110 | |
| 111 | // Product name of the device (e.g. "Nexus One"). |
| 112 | const std::string& product_name() const { return product_name_; } |
| 113 | |
[email protected] | 9c5620d3 | 2012-07-31 01:00:38 | [diff] [blame] | 114 | // Returns the file system uuid string. |
| 115 | const std::string& fs_uuid() const { return fs_uuid_; } |
| 116 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 117 | // Path of the system device this device's block is a part of. |
| 118 | // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/) |
| 119 | const std::string& system_path_prefix() const { |
| 120 | return system_path_prefix_; |
| 121 | } |
| 122 | |
| 123 | // Device type. |
| 124 | DeviceType device_type() const { return device_type_; } |
| 125 | |
| 126 | // Total size of the device in bytes. |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 127 | uint64_t total_size_in_bytes() const { return total_size_in_bytes_; } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 128 | |
| 129 | // Is the device is a parent device (i.e. sdb rather than sdb1). |
| 130 | bool is_parent() const { return is_parent_; } |
| 131 | |
| 132 | // Is the device read only. |
| 133 | bool is_read_only() const { return is_read_only_; } |
| 134 | |
| 135 | // Does the device contains media. |
| 136 | bool has_media() const { return has_media_; } |
| 137 | |
| 138 | // Is the device on the boot device. |
| 139 | bool on_boot_device() const { return on_boot_device_; } |
| 140 | |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 141 | // Is the device on the removable device. |
| 142 | bool on_removable_device() const { return on_removable_device_; } |
| 143 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 144 | // Shoud the device be shown in the UI, or automounted. |
| 145 | bool is_hidden() const { return is_hidden_; } |
| 146 | |
| 147 | void set_mount_path(const std::string& mount_path) { |
| 148 | mount_path_ = mount_path; |
| 149 | } |
| 150 | |
| 151 | void clear_mount_path() { mount_path_.clear(); } |
| 152 | |
| 153 | private: |
| 154 | std::string device_path_; |
| 155 | std::string mount_path_; |
| 156 | std::string system_path_; |
| 157 | std::string file_path_; |
| 158 | std::string device_label_; |
| 159 | std::string drive_label_; |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame] | 160 | std::string vendor_id_; |
| 161 | std::string vendor_name_; |
| 162 | std::string product_id_; |
| 163 | std::string product_name_; |
[email protected] | 9c5620d3 | 2012-07-31 01:00:38 | [diff] [blame] | 164 | std::string fs_uuid_; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 165 | std::string system_path_prefix_; |
| 166 | DeviceType device_type_; |
avi | 6e1a22d | 2015-12-21 03:43:20 | [diff] [blame] | 167 | uint64_t total_size_in_bytes_; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 168 | bool is_parent_; |
| 169 | bool is_read_only_; |
| 170 | bool has_media_; |
| 171 | bool on_boot_device_; |
[email protected] | 79ed457b | 2014-07-22 04:07:26 | [diff] [blame] | 172 | bool on_removable_device_; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 173 | bool is_hidden_; |
| 174 | }; |
| 175 | typedef std::map<std::string, Disk*> DiskMap; |
| 176 | |
| 177 | // A struct to store information about mount point. |
| 178 | struct MountPointInfo { |
| 179 | // Device's path. |
| 180 | std::string source_path; |
| 181 | // Mounted path. |
| 182 | std::string mount_path; |
| 183 | // Type of mount. |
| 184 | MountType mount_type; |
| 185 | // Condition of mount. |
| 186 | MountCondition mount_condition; |
| 187 | |
| 188 | MountPointInfo(const std::string& source, |
| 189 | const std::string& mount, |
| 190 | const MountType type, |
| 191 | MountCondition condition) |
| 192 | : source_path(source), |
| 193 | mount_path(mount), |
| 194 | mount_type(type), |
| 195 | mount_condition(condition) { |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | // MountPointMap key is mount_path. |
| 200 | typedef std::map<std::string, MountPointInfo> MountPointMap; |
| 201 | |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 202 | // A callback function type which is called after UnmountDeviceRecursively |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 203 | // finishes. |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 204 | typedef base::Callback<void(bool)> UnmountDeviceRecursivelyCallbackType; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 205 | |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 206 | // A callback type for UnmountPath method. |
| 207 | typedef base::Callback<void(MountError error_code)> UnmountPathCallback; |
| 208 | |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 209 | // A callback type for EnsureMountInfoRefreshed method. |
| 210 | typedef base::Callback<void(bool success)> EnsureMountInfoRefreshedCallback; |
| 211 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 212 | // Implement this interface to be notified about disk/mount related events. |
| 213 | class Observer { |
| 214 | public: |
| 215 | virtual ~Observer() {} |
| 216 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 217 | // Called when disk mount status is changed. |
| 218 | virtual void OnDiskEvent(DiskEvent event, const Disk* disk) = 0; |
| 219 | // Called when device status is changed. |
| 220 | virtual void OnDeviceEvent(DeviceEvent event, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 221 | const std::string& device_path) = 0; |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 222 | // Called after a mount point has been mounted or unmounted. |
| 223 | virtual void OnMountEvent(MountEvent event, |
| 224 | MountError error_code, |
| 225 | const MountPointInfo& mount_info) = 0; |
| 226 | // Called on format process events. |
| 227 | virtual void OnFormatEvent(FormatEvent event, |
| 228 | FormatError error_code, |
| 229 | const std::string& device_path) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | virtual ~DiskMountManager() {} |
| 233 | |
| 234 | // Adds an observer. |
| 235 | virtual void AddObserver(Observer* observer) = 0; |
| 236 | |
| 237 | // Removes an observer. |
| 238 | virtual void RemoveObserver(Observer* observer) = 0; |
| 239 | |
| 240 | // Gets the list of disks found. |
| 241 | virtual const DiskMap& disks() const = 0; |
| 242 | |
[email protected] | bcfa007 | 2012-08-07 01:08:57 | [diff] [blame] | 243 | // Returns Disk object corresponding to |source_path| or NULL on failure. |
| 244 | virtual const Disk* FindDiskBySourcePath( |
| 245 | const std::string& source_path) const = 0; |
| 246 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 247 | // Gets the list of mount points. |
| 248 | virtual const MountPointMap& mount_points() const = 0; |
| 249 | |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 250 | // Refreshes all the information about mounting if it is not yet done and |
| 251 | // invokes |callback| when finished. If the information is already refreshed |
hirono | a4b675d | 2015-07-29 01:13:37 | [diff] [blame] | 252 | // and |force| is false, it just runs |callback| immediately. |
[email protected] | a2e4ee2 | 2014-07-11 05:16:35 | [diff] [blame] | 253 | virtual void EnsureMountInfoRefreshed( |
hirono | a4b675d | 2015-07-29 01:13:37 | [diff] [blame] | 254 | const EnsureMountInfoRefreshedCallback& callback, |
| 255 | bool force) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 256 | |
| 257 | // Mounts a device. |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 258 | // Note that the mount operation may fail. To find out the result, one should |
| 259 | // observe DiskMountManager for |Observer::OnMountEvent| event, which will be |
| 260 | // raised upon the mount operation completion. |
[email protected] | b9f22d1 | 2012-04-25 21:46:48 | [diff] [blame] | 261 | virtual void MountPath(const std::string& source_path, |
| 262 | const std::string& source_format, |
[email protected] | dcad8fc | 2012-04-30 23:31:33 | [diff] [blame] | 263 | const std::string& mount_label, |
[email protected] | b9f22d1 | 2012-04-25 21:46:48 | [diff] [blame] | 264 | MountType type) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 265 | |
| 266 | // Unmounts a mounted disk. |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 267 | // |UnmountOptions| enum defined in chromeos/dbus/cros_disks_client.h. |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 268 | // When the method is complete, |callback| will be called and observers' |
| 269 | // |OnMountEvent| will be raised. |
| 270 | // |
| 271 | // |callback| may be empty, in which case it gets ignored. |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 272 | virtual void UnmountPath(const std::string& mount_path, |
[email protected] | 8f919ee | 2013-03-14 19:53:29 | [diff] [blame] | 273 | UnmountOptions options, |
| 274 | const UnmountPathCallback& callback) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 275 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 276 | // Formats Device given its mount path. Unmounts the device. |
| 277 | // Example: mount_path: /media/VOLUME_LABEL |
| 278 | virtual void FormatMountedDevice(const std::string& mount_path) = 0; |
| 279 | |
| 280 | // Unmounts device_path and all of its known children. |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 281 | virtual void UnmountDeviceRecursively( |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 282 | const std::string& device_path, |
[email protected] | e008d4fa | 2013-02-21 06:38:01 | [diff] [blame] | 283 | const UnmountDeviceRecursivelyCallbackType& callback) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 284 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 285 | // Used in tests to initialize the manager's disk and mount point sets. |
| 286 | // Default implementation does noting. It just fails. |
| 287 | virtual bool AddDiskForTest(Disk* disk); |
| 288 | virtual bool AddMountPointForTest(const MountPointInfo& mount_point); |
| 289 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 290 | // Returns corresponding string to |type| like "unknown_filesystem". |
| 291 | static std::string MountConditionToString(MountCondition type); |
| 292 | |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 293 | // Returns corresponding string to |type|, like "sd", "usb". |
| 294 | static std::string DeviceTypeToString(DeviceType type); |
| 295 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 296 | // Creates the global DiskMountManager instance. |
| 297 | static void Initialize(); |
| 298 | |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame] | 299 | // Similar to Initialize(), but can inject an alternative |
| 300 | // DiskMountManager such as MockDiskMountManager for testing. |
| 301 | // The injected object will be owned by the internal pointer and deleted |
| 302 | // by Shutdown(). |
| 303 | static void InitializeForTesting(DiskMountManager* disk_mount_manager); |
| 304 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 305 | // Destroys the global DiskMountManager instance if it exists. |
| 306 | static void Shutdown(); |
| 307 | |
| 308 | // Returns a pointer to the global DiskMountManager instance. |
| 309 | // Initialize() should already have been called. |
| 310 | static DiskMountManager* GetInstance(); |
| 311 | }; |
| 312 | |
| 313 | } // namespace disks |
| 314 | } // namespace chromeos |
| 315 | |
[email protected] | 4cbead4 | 2012-08-26 12:02:39 | [diff] [blame] | 316 | #endif // CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_ |