blob: 895c6070bb8d32e75b0350c89b17cc670b95924a [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
[email protected]4ae73292011-11-15 05:20:1816// Condition of mounted filesystem.
17enum 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]4cbead42012-08-26 12:02:3925class CHROMEOS_EXPORT DiskMountManager {
[email protected]4ae73292011-11-15 05:20:1826 public:
[email protected]e3c1fc92012-11-15 00:56:4627 // 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]4ae73292011-11-15 05:20:1840 enum MountEvent {
41 MOUNTING,
42 UNMOUNTING,
43 };
44
[email protected]e3c1fc92012-11-15 00:56:4645 enum FormatEvent {
46 FORMAT_STARTED,
47 FORMAT_COMPLETED
48 };
49
[email protected]4ae73292011-11-15 05:20:1850 // 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]202e9fee2012-09-13 20:21:2959 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]9c5620d32012-07-31 01:00:3863 const std::string& fs_uuid,
[email protected]4ae73292011-11-15 05:20:1864 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]202e9fee2012-09-13 20:21:2997 // 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]9c5620d32012-07-31 01:00:38109 // Returns the file system uuid string.
110 const std::string& fs_uuid() const { return fs_uuid_; }
111
[email protected]4ae73292011-11-15 05:20:18112 // 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]202e9fee2012-09-13 20:21:29152 std::string vendor_id_;
153 std::string vendor_name_;
154 std::string product_id_;
155 std::string product_name_;
[email protected]9c5620d32012-07-31 01:00:38156 std::string fs_uuid_;
[email protected]4ae73292011-11-15 05:20:18157 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]e3c1fc92012-11-15 00:56:46202 // 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]4ae73292011-11-15 05:20:18206 const std::string& device_path) = 0;
[email protected]e3c1fc92012-11-15 00:56:46207 // 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]4ae73292011-11-15 05:20:18215 };
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]bcfa0072012-08-07 01:08:57228 // 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]4ae73292011-11-15 05:20:18232 // 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]b9f22d12012-04-25 21:46:48239 virtual void MountPath(const std::string& source_path,
240 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33241 const std::string& mount_label,
[email protected]b9f22d12012-04-25 21:46:48242 MountType type) = 0;
[email protected]4ae73292011-11-15 05:20:18243
244 // Unmounts a mounted disk.
[email protected]10795ae2012-10-10 07:33:49245 // |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]4ae73292011-11-15 05:20:18248
[email protected]4ae73292011-11-15 05:20:18249 // 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]e3c1fc92012-11-15 00:56:46259 // 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]4ae73292011-11-15 05:20:18264 // 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]2321d282012-01-31 23:06:59273 // Returns corresponding string to |type|, like "sd", "usb".
274 static std::string DeviceTypeToString(DeviceType type);
275
[email protected]4ae73292011-11-15 05:20:18276 // Creates the global DiskMountManager instance.
277 static void Initialize();
278
[email protected]b307bceb2011-11-17 07:49:55279 // 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]4ae73292011-11-15 05:20:18285 // 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]4cbead42012-08-26 12:02:39296#endif // CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_