[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 1 | // Copyright (c) 2011 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. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <map> |
| 10 | |
| 11 | #include "chrome/browser/chromeos/dbus/cros_disks_client.h" |
| 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. |
| 39 | class DiskMountManager { |
| 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, |
| 56 | const std::string& system_path_prefix, |
| 57 | DeviceType device_type, |
| 58 | uint64 total_size_in_bytes, |
| 59 | bool is_parent, |
| 60 | bool is_read_only, |
| 61 | bool has_media, |
| 62 | bool on_boot_device, |
| 63 | bool is_hidden); |
| 64 | ~Disk(); |
| 65 | |
| 66 | // The path of the device, used by devicekit-disks. |
| 67 | // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1) |
| 68 | const std::string& device_path() const { return device_path_; } |
| 69 | |
| 70 | // The path to the mount point of this device. Will be empty if not mounted. |
| 71 | // (e.g. /media/removable/VOLUME) |
| 72 | const std::string& mount_path() const { return mount_path_; } |
| 73 | |
| 74 | // The path of the device according to the udev system. |
| 75 | // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1) |
| 76 | const std::string& system_path() const { return system_path_; } |
| 77 | |
| 78 | // The path of the device according to filesystem. |
| 79 | // (e.g. /dev/sdb) |
| 80 | const std::string& file_path() const { return file_path_; } |
| 81 | |
| 82 | // Device's label. |
| 83 | const std::string& device_label() const { return device_label_; } |
| 84 | |
| 85 | // If disk is a parent, then its label, else parents label. |
| 86 | // (e.g. "TransMemory") |
| 87 | const std::string& drive_label() const { return drive_label_; } |
| 88 | |
| 89 | // Path of the system device this device's block is a part of. |
| 90 | // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/) |
| 91 | const std::string& system_path_prefix() const { |
| 92 | return system_path_prefix_; |
| 93 | } |
| 94 | |
| 95 | // Device type. |
| 96 | DeviceType device_type() const { return device_type_; } |
| 97 | |
| 98 | // Total size of the device in bytes. |
| 99 | uint64 total_size_in_bytes() const { return total_size_in_bytes_; } |
| 100 | |
| 101 | // Is the device is a parent device (i.e. sdb rather than sdb1). |
| 102 | bool is_parent() const { return is_parent_; } |
| 103 | |
| 104 | // Is the device read only. |
| 105 | bool is_read_only() const { return is_read_only_; } |
| 106 | |
| 107 | // Does the device contains media. |
| 108 | bool has_media() const { return has_media_; } |
| 109 | |
| 110 | // Is the device on the boot device. |
| 111 | bool on_boot_device() const { return on_boot_device_; } |
| 112 | |
| 113 | // Shoud the device be shown in the UI, or automounted. |
| 114 | bool is_hidden() const { return is_hidden_; } |
| 115 | |
| 116 | void set_mount_path(const std::string& mount_path) { |
| 117 | mount_path_ = mount_path; |
| 118 | } |
| 119 | |
| 120 | void clear_mount_path() { mount_path_.clear(); } |
| 121 | |
| 122 | private: |
| 123 | std::string device_path_; |
| 124 | std::string mount_path_; |
| 125 | std::string system_path_; |
| 126 | std::string file_path_; |
| 127 | std::string device_label_; |
| 128 | std::string drive_label_; |
| 129 | std::string system_path_prefix_; |
| 130 | DeviceType device_type_; |
| 131 | uint64 total_size_in_bytes_; |
| 132 | bool is_parent_; |
| 133 | bool is_read_only_; |
| 134 | bool has_media_; |
| 135 | bool on_boot_device_; |
| 136 | bool is_hidden_; |
| 137 | }; |
| 138 | typedef std::map<std::string, Disk*> DiskMap; |
| 139 | |
| 140 | // A struct to store information about mount point. |
| 141 | struct MountPointInfo { |
| 142 | // Device's path. |
| 143 | std::string source_path; |
| 144 | // Mounted path. |
| 145 | std::string mount_path; |
| 146 | // Type of mount. |
| 147 | MountType mount_type; |
| 148 | // Condition of mount. |
| 149 | MountCondition mount_condition; |
| 150 | |
| 151 | MountPointInfo(const std::string& source, |
| 152 | const std::string& mount, |
| 153 | const MountType type, |
| 154 | MountCondition condition) |
| 155 | : source_path(source), |
| 156 | mount_path(mount), |
| 157 | mount_type(type), |
| 158 | mount_condition(condition) { |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | // MountPointMap key is mount_path. |
| 163 | typedef std::map<std::string, MountPointInfo> MountPointMap; |
| 164 | |
| 165 | // A callback function type which is called after UnmountDeviceRecursive |
| 166 | // finishes. |
| 167 | typedef void(*UnmountDeviceRecursiveCallbackType)(void*, bool); |
| 168 | |
| 169 | // Implement this interface to be notified about disk/mount related events. |
| 170 | class Observer { |
| 171 | public: |
| 172 | virtual ~Observer() {} |
| 173 | |
| 174 | // A function called when disk mount status is changed. |
| 175 | virtual void DiskChanged(DiskMountManagerEventType event, |
| 176 | const Disk* disk) = 0; |
| 177 | // A function called when device status is changed. |
| 178 | virtual void DeviceChanged(DiskMountManagerEventType event, |
| 179 | const std::string& device_path) = 0; |
| 180 | // A function called after mount is completed. |
| 181 | virtual void MountCompleted(MountEvent event_type, |
| 182 | MountError error_code, |
| 183 | const MountPointInfo& mount_info) = 0; |
| 184 | }; |
| 185 | |
| 186 | virtual ~DiskMountManager() {} |
| 187 | |
| 188 | // Adds an observer. |
| 189 | virtual void AddObserver(Observer* observer) = 0; |
| 190 | |
| 191 | // Removes an observer. |
| 192 | virtual void RemoveObserver(Observer* observer) = 0; |
| 193 | |
| 194 | // Gets the list of disks found. |
| 195 | virtual const DiskMap& disks() const = 0; |
| 196 | |
| 197 | // Gets the list of mount points. |
| 198 | virtual const MountPointMap& mount_points() const = 0; |
| 199 | |
| 200 | // Requests refreshing all the information about mounted disks. |
| 201 | virtual void RequestMountInfoRefresh() = 0; |
| 202 | |
| 203 | // Mounts a device. |
| 204 | virtual void MountPath(const std::string& source_path, MountType type) = 0; |
| 205 | |
| 206 | // Unmounts a mounted disk. |
| 207 | virtual void UnmountPath(const std::string& mount_path) = 0; |
| 208 | |
| 209 | // Retrieves total and remaining available size on |mount_path|. |
| 210 | virtual void GetSizeStatsOnFileThread(const std::string& mount_path, |
| 211 | size_t* total_size_kb, |
| 212 | size_t* remaining_size_kb) = 0; |
| 213 | // Formats device given its file path. |
| 214 | // Example: file_path: /dev/sdb1 |
| 215 | virtual void FormatUnmountedDevice(const std::string& file_path) = 0; |
| 216 | |
| 217 | // Formats Device given its mount path. Unmounts the device. |
| 218 | // Example: mount_path: /media/VOLUME_LABEL |
| 219 | virtual void FormatMountedDevice(const std::string& mount_path) = 0; |
| 220 | |
| 221 | // Unmounts device_path and all of its known children. |
| 222 | virtual void UnmountDeviceRecursive( |
| 223 | const std::string& device_path, |
| 224 | UnmountDeviceRecursiveCallbackType callback, |
| 225 | void* user_data) = 0; |
| 226 | |
| 227 | // Returns corresponding string to |type| like "device" or "file". |
| 228 | static std::string MountTypeToString(MountType type); |
| 229 | |
| 230 | // The inverse function of MountTypeToString. |
| 231 | static MountType MountTypeFromString(const std::string& type_str); |
| 232 | |
| 233 | // Returns corresponding string to |type| like "unknown_filesystem". |
| 234 | static std::string MountConditionToString(MountCondition type); |
| 235 | |
| 236 | // Creates the global DiskMountManager instance. |
| 237 | static void Initialize(); |
| 238 | |
[email protected] | b307bceb | 2011-11-17 07:49:55 | [diff] [blame^] | 239 | // Similar to Initialize(), but can inject an alternative |
| 240 | // DiskMountManager such as MockDiskMountManager for testing. |
| 241 | // The injected object will be owned by the internal pointer and deleted |
| 242 | // by Shutdown(). |
| 243 | static void InitializeForTesting(DiskMountManager* disk_mount_manager); |
| 244 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 245 | // Destroys the global DiskMountManager instance if it exists. |
| 246 | static void Shutdown(); |
| 247 | |
| 248 | // Returns a pointer to the global DiskMountManager instance. |
| 249 | // Initialize() should already have been called. |
| 250 | static DiskMountManager* GetInstance(); |
| 251 | }; |
| 252 | |
| 253 | } // namespace disks |
| 254 | } // namespace chromeos |
| 255 | |
| 256 | #endif // CHROME_BROWSER_CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_ |