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