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