blob: dfe80c367f82101dcf22bf18b7b504f8423cf10f [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
8#include <map>
9
[email protected]4cbead42012-08-26 12:02:3910#include "chromeos/chromeos_export.h"
[email protected]64e199252012-04-06 01:54:3611#include "chromeos/dbus/cros_disks_client.h"
[email protected]4ae73292011-11-15 05:20:1812
13namespace chromeos {
14namespace disks {
15
16// Types of events DiskMountManager sends to its observers.
17enum 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.
31enum 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]4cbead42012-08-26 12:02:3939class CHROMEOS_EXPORT DiskMountManager {
[email protected]4ae73292011-11-15 05:20:1840 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]9c5620d32012-07-31 01:00:3856 const std::string& fs_uuid,
[email protected]4ae73292011-11-15 05:20:1857 const std::string& system_path_prefix,
58 DeviceType device_type,
59 uint64 total_size_in_bytes,
60 bool is_parent,
61 bool is_read_only,
62 bool has_media,
63 bool on_boot_device,
64 bool is_hidden);
65 ~Disk();
66
67 // The path of the device, used by devicekit-disks.
68 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
69 const std::string& device_path() const { return device_path_; }
70
71 // The path to the mount point of this device. Will be empty if not mounted.
72 // (e.g. /media/removable/VOLUME)
73 const std::string& mount_path() const { return mount_path_; }
74
75 // The path of the device according to the udev system.
76 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
77 const std::string& system_path() const { return system_path_; }
78
79 // The path of the device according to filesystem.
80 // (e.g. /dev/sdb)
81 const std::string& file_path() const { return file_path_; }
82
83 // Device's label.
84 const std::string& device_label() const { return device_label_; }
85
86 // If disk is a parent, then its label, else parents label.
87 // (e.g. "TransMemory")
88 const std::string& drive_label() const { return drive_label_; }
89
[email protected]9c5620d32012-07-31 01:00:3890 // Returns the file system uuid string.
91 const std::string& fs_uuid() const { return fs_uuid_; }
92
[email protected]4ae73292011-11-15 05:20:1893 // Path of the system device this device's block is a part of.
94 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
95 const std::string& system_path_prefix() const {
96 return system_path_prefix_;
97 }
98
99 // Device type.
100 DeviceType device_type() const { return device_type_; }
101
102 // Total size of the device in bytes.
103 uint64 total_size_in_bytes() const { return total_size_in_bytes_; }
104
105 // Is the device is a parent device (i.e. sdb rather than sdb1).
106 bool is_parent() const { return is_parent_; }
107
108 // Is the device read only.
109 bool is_read_only() const { return is_read_only_; }
110
111 // Does the device contains media.
112 bool has_media() const { return has_media_; }
113
114 // Is the device on the boot device.
115 bool on_boot_device() const { return on_boot_device_; }
116
117 // Shoud the device be shown in the UI, or automounted.
118 bool is_hidden() const { return is_hidden_; }
119
120 void set_mount_path(const std::string& mount_path) {
121 mount_path_ = mount_path;
122 }
123
124 void clear_mount_path() { mount_path_.clear(); }
125
126 private:
127 std::string device_path_;
128 std::string mount_path_;
129 std::string system_path_;
130 std::string file_path_;
131 std::string device_label_;
132 std::string drive_label_;
[email protected]9c5620d32012-07-31 01:00:38133 std::string fs_uuid_;
[email protected]4ae73292011-11-15 05:20:18134 std::string system_path_prefix_;
135 DeviceType device_type_;
136 uint64 total_size_in_bytes_;
137 bool is_parent_;
138 bool is_read_only_;
139 bool has_media_;
140 bool on_boot_device_;
141 bool is_hidden_;
142 };
143 typedef std::map<std::string, Disk*> DiskMap;
144
145 // A struct to store information about mount point.
146 struct MountPointInfo {
147 // Device's path.
148 std::string source_path;
149 // Mounted path.
150 std::string mount_path;
151 // Type of mount.
152 MountType mount_type;
153 // Condition of mount.
154 MountCondition mount_condition;
155
156 MountPointInfo(const std::string& source,
157 const std::string& mount,
158 const MountType type,
159 MountCondition condition)
160 : source_path(source),
161 mount_path(mount),
162 mount_type(type),
163 mount_condition(condition) {
164 }
165 };
166
167 // MountPointMap key is mount_path.
168 typedef std::map<std::string, MountPointInfo> MountPointMap;
169
170 // A callback function type which is called after UnmountDeviceRecursive
171 // finishes.
172 typedef void(*UnmountDeviceRecursiveCallbackType)(void*, bool);
173
174 // Implement this interface to be notified about disk/mount related events.
175 class Observer {
176 public:
177 virtual ~Observer() {}
178
179 // A function called when disk mount status is changed.
180 virtual void DiskChanged(DiskMountManagerEventType event,
181 const Disk* disk) = 0;
182 // A function called when device status is changed.
183 virtual void DeviceChanged(DiskMountManagerEventType event,
184 const std::string& device_path) = 0;
185 // A function called after mount is completed.
186 virtual void MountCompleted(MountEvent event_type,
187 MountError error_code,
188 const MountPointInfo& mount_info) = 0;
189 };
190
191 virtual ~DiskMountManager() {}
192
193 // Adds an observer.
194 virtual void AddObserver(Observer* observer) = 0;
195
196 // Removes an observer.
197 virtual void RemoveObserver(Observer* observer) = 0;
198
199 // Gets the list of disks found.
200 virtual const DiskMap& disks() const = 0;
201
[email protected]bcfa0072012-08-07 01:08:57202 // Returns Disk object corresponding to |source_path| or NULL on failure.
203 virtual const Disk* FindDiskBySourcePath(
204 const std::string& source_path) const = 0;
205
[email protected]4ae73292011-11-15 05:20:18206 // Gets the list of mount points.
207 virtual const MountPointMap& mount_points() const = 0;
208
209 // Requests refreshing all the information about mounted disks.
210 virtual void RequestMountInfoRefresh() = 0;
211
212 // Mounts a device.
[email protected]b9f22d12012-04-25 21:46:48213 virtual void MountPath(const std::string& source_path,
214 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33215 const std::string& mount_label,
[email protected]b9f22d12012-04-25 21:46:48216 MountType type) = 0;
[email protected]4ae73292011-11-15 05:20:18217
218 // Unmounts a mounted disk.
219 virtual void UnmountPath(const std::string& mount_path) = 0;
220
[email protected]4ae73292011-11-15 05:20:18221 // Formats device given its file path.
222 // Example: file_path: /dev/sdb1
223 virtual void FormatUnmountedDevice(const std::string& file_path) = 0;
224
225 // Formats Device given its mount path. Unmounts the device.
226 // Example: mount_path: /media/VOLUME_LABEL
227 virtual void FormatMountedDevice(const std::string& mount_path) = 0;
228
229 // Unmounts device_path and all of its known children.
230 virtual void UnmountDeviceRecursive(
231 const std::string& device_path,
232 UnmountDeviceRecursiveCallbackType callback,
233 void* user_data) = 0;
234
235 // Returns corresponding string to |type| like "device" or "file".
236 static std::string MountTypeToString(MountType type);
237
238 // The inverse function of MountTypeToString.
239 static MountType MountTypeFromString(const std::string& type_str);
240
241 // Returns corresponding string to |type| like "unknown_filesystem".
242 static std::string MountConditionToString(MountCondition type);
243
[email protected]2321d282012-01-31 23:06:59244 // Returns corresponding string to |type|, like "sd", "usb".
245 static std::string DeviceTypeToString(DeviceType type);
246
[email protected]4ae73292011-11-15 05:20:18247 // Creates the global DiskMountManager instance.
248 static void Initialize();
249
[email protected]b307bceb2011-11-17 07:49:55250 // Similar to Initialize(), but can inject an alternative
251 // DiskMountManager such as MockDiskMountManager for testing.
252 // The injected object will be owned by the internal pointer and deleted
253 // by Shutdown().
254 static void InitializeForTesting(DiskMountManager* disk_mount_manager);
255
[email protected]4ae73292011-11-15 05:20:18256 // Destroys the global DiskMountManager instance if it exists.
257 static void Shutdown();
258
259 // Returns a pointer to the global DiskMountManager instance.
260 // Initialize() should already have been called.
261 static DiskMountManager* GetInstance();
262};
263
264} // namespace disks
265} // namespace chromeos
266
[email protected]4cbead42012-08-26 12:02:39267#endif // CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_