blob: 82506b96e91ca140e0a3e2ff5b975ff59d7ecb8a [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
avi6e1a22d2015-12-21 03:43:208#include <stdint.h>
9
[email protected]4ae73292011-11-15 05:20:1810#include <map>
11
[email protected]e008d4fa2013-02-21 06:38:0112#include "base/callback_forward.h"
[email protected]4cbead42012-08-26 12:02:3913#include "chromeos/chromeos_export.h"
[email protected]64e199252012-04-06 01:54:3614#include "chromeos/dbus/cros_disks_client.h"
[email protected]4ae73292011-11-15 05:20:1815
16namespace chromeos {
17namespace disks {
18
[email protected]4ae73292011-11-15 05:20:1819// Condition of mounted filesystem.
20enum MountCondition {
21 MOUNT_CONDITION_NONE,
22 MOUNT_CONDITION_UNKNOWN_FILESYSTEM,
23 MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM,
24};
25
26// This class handles the interaction with cros-disks.
27// Other classes can add themselves as observers.
[email protected]4cbead42012-08-26 12:02:3928class CHROMEOS_EXPORT DiskMountManager {
[email protected]4ae73292011-11-15 05:20:1829 public:
[email protected]e3c1fc92012-11-15 00:56:4630 // Event types passed to the observers.
31 enum DiskEvent {
32 DISK_ADDED,
33 DISK_REMOVED,
34 DISK_CHANGED,
35 };
36
37 enum DeviceEvent {
38 DEVICE_ADDED,
39 DEVICE_REMOVED,
40 DEVICE_SCANNED,
41 };
42
[email protected]4ae73292011-11-15 05:20:1843 enum MountEvent {
44 MOUNTING,
45 UNMOUNTING,
46 };
47
[email protected]e3c1fc92012-11-15 00:56:4648 enum FormatEvent {
49 FORMAT_STARTED,
50 FORMAT_COMPLETED
51 };
52
[email protected]4ae73292011-11-15 05:20:1853 // Used to house an instance of each found mount device.
54 class Disk {
55 public:
56 Disk(const std::string& device_path,
57 const std::string& mount_path,
58 const std::string& system_path,
59 const std::string& file_path,
60 const std::string& device_label,
61 const std::string& drive_label,
[email protected]202e9fee2012-09-13 20:21:2962 const std::string& vendor_id,
63 const std::string& vendor_name,
64 const std::string& product_id,
65 const std::string& product_name,
[email protected]9c5620d32012-07-31 01:00:3866 const std::string& fs_uuid,
[email protected]4ae73292011-11-15 05:20:1867 const std::string& system_path_prefix,
68 DeviceType device_type,
avi6e1a22d2015-12-21 03:43:2069 uint64_t total_size_in_bytes,
[email protected]4ae73292011-11-15 05:20:1870 bool is_parent,
71 bool is_read_only,
72 bool has_media,
73 bool on_boot_device,
[email protected]79ed457b2014-07-22 04:07:2674 bool on_removable_device,
[email protected]4ae73292011-11-15 05:20:1875 bool is_hidden);
vmpstr2a0c10402016-04-08 21:28:0776 Disk(const Disk& other);
[email protected]4ae73292011-11-15 05:20:1877 ~Disk();
78
79 // The path of the device, used by devicekit-disks.
80 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
81 const std::string& device_path() const { return device_path_; }
82
83 // The path to the mount point of this device. Will be empty if not mounted.
84 // (e.g. /media/removable/VOLUME)
85 const std::string& mount_path() const { return mount_path_; }
86
87 // The path of the device according to the udev system.
88 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
89 const std::string& system_path() const { return system_path_; }
90
91 // The path of the device according to filesystem.
92 // (e.g. /dev/sdb)
93 const std::string& file_path() const { return file_path_; }
94
95 // Device's label.
96 const std::string& device_label() const { return device_label_; }
97
98 // If disk is a parent, then its label, else parents label.
99 // (e.g. "TransMemory")
100 const std::string& drive_label() const { return drive_label_; }
101
[email protected]202e9fee2012-09-13 20:21:29102 // Vendor ID of the device (e.g. "18d1").
103 const std::string& vendor_id() const { return vendor_id_; }
104
105 // Vendor name of the device (e.g. "Google Inc.").
106 const std::string& vendor_name() const { return vendor_name_; }
107
108 // Product ID of the device (e.g. "4e11").
109 const std::string& product_id() const { return product_id_; }
110
111 // Product name of the device (e.g. "Nexus One").
112 const std::string& product_name() const { return product_name_; }
113
[email protected]9c5620d32012-07-31 01:00:38114 // Returns the file system uuid string.
115 const std::string& fs_uuid() const { return fs_uuid_; }
116
[email protected]4ae73292011-11-15 05:20:18117 // Path of the system device this device's block is a part of.
118 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
119 const std::string& system_path_prefix() const {
120 return system_path_prefix_;
121 }
122
123 // Device type.
124 DeviceType device_type() const { return device_type_; }
125
126 // Total size of the device in bytes.
avi6e1a22d2015-12-21 03:43:20127 uint64_t total_size_in_bytes() const { return total_size_in_bytes_; }
[email protected]4ae73292011-11-15 05:20:18128
129 // Is the device is a parent device (i.e. sdb rather than sdb1).
130 bool is_parent() const { return is_parent_; }
131
132 // Is the device read only.
133 bool is_read_only() const { return is_read_only_; }
134
135 // Does the device contains media.
136 bool has_media() const { return has_media_; }
137
138 // Is the device on the boot device.
139 bool on_boot_device() const { return on_boot_device_; }
140
[email protected]79ed457b2014-07-22 04:07:26141 // Is the device on the removable device.
142 bool on_removable_device() const { return on_removable_device_; }
143
[email protected]4ae73292011-11-15 05:20:18144 // Shoud the device be shown in the UI, or automounted.
145 bool is_hidden() const { return is_hidden_; }
146
147 void set_mount_path(const std::string& mount_path) {
148 mount_path_ = mount_path;
149 }
150
151 void clear_mount_path() { mount_path_.clear(); }
152
153 private:
154 std::string device_path_;
155 std::string mount_path_;
156 std::string system_path_;
157 std::string file_path_;
158 std::string device_label_;
159 std::string drive_label_;
[email protected]202e9fee2012-09-13 20:21:29160 std::string vendor_id_;
161 std::string vendor_name_;
162 std::string product_id_;
163 std::string product_name_;
[email protected]9c5620d32012-07-31 01:00:38164 std::string fs_uuid_;
[email protected]4ae73292011-11-15 05:20:18165 std::string system_path_prefix_;
166 DeviceType device_type_;
avi6e1a22d2015-12-21 03:43:20167 uint64_t total_size_in_bytes_;
[email protected]4ae73292011-11-15 05:20:18168 bool is_parent_;
169 bool is_read_only_;
170 bool has_media_;
171 bool on_boot_device_;
[email protected]79ed457b2014-07-22 04:07:26172 bool on_removable_device_;
[email protected]4ae73292011-11-15 05:20:18173 bool is_hidden_;
174 };
175 typedef std::map<std::string, Disk*> DiskMap;
176
177 // A struct to store information about mount point.
178 struct MountPointInfo {
179 // Device's path.
180 std::string source_path;
181 // Mounted path.
182 std::string mount_path;
183 // Type of mount.
184 MountType mount_type;
185 // Condition of mount.
186 MountCondition mount_condition;
187
188 MountPointInfo(const std::string& source,
189 const std::string& mount,
190 const MountType type,
191 MountCondition condition)
192 : source_path(source),
193 mount_path(mount),
194 mount_type(type),
195 mount_condition(condition) {
196 }
197 };
198
199 // MountPointMap key is mount_path.
200 typedef std::map<std::string, MountPointInfo> MountPointMap;
201
[email protected]e008d4fa2013-02-21 06:38:01202 // A callback function type which is called after UnmountDeviceRecursively
[email protected]4ae73292011-11-15 05:20:18203 // finishes.
[email protected]e008d4fa2013-02-21 06:38:01204 typedef base::Callback<void(bool)> UnmountDeviceRecursivelyCallbackType;
[email protected]4ae73292011-11-15 05:20:18205
[email protected]8f919ee2013-03-14 19:53:29206 // A callback type for UnmountPath method.
207 typedef base::Callback<void(MountError error_code)> UnmountPathCallback;
208
[email protected]a2e4ee22014-07-11 05:16:35209 // A callback type for EnsureMountInfoRefreshed method.
210 typedef base::Callback<void(bool success)> EnsureMountInfoRefreshedCallback;
211
[email protected]4ae73292011-11-15 05:20:18212 // Implement this interface to be notified about disk/mount related events.
213 class Observer {
214 public:
215 virtual ~Observer() {}
216
[email protected]e3c1fc92012-11-15 00:56:46217 // Called when disk mount status is changed.
218 virtual void OnDiskEvent(DiskEvent event, const Disk* disk) = 0;
219 // Called when device status is changed.
220 virtual void OnDeviceEvent(DeviceEvent event,
[email protected]4ae73292011-11-15 05:20:18221 const std::string& device_path) = 0;
[email protected]e3c1fc92012-11-15 00:56:46222 // Called after a mount point has been mounted or unmounted.
223 virtual void OnMountEvent(MountEvent event,
224 MountError error_code,
225 const MountPointInfo& mount_info) = 0;
226 // Called on format process events.
227 virtual void OnFormatEvent(FormatEvent event,
228 FormatError error_code,
229 const std::string& device_path) = 0;
[email protected]4ae73292011-11-15 05:20:18230 };
231
232 virtual ~DiskMountManager() {}
233
234 // Adds an observer.
235 virtual void AddObserver(Observer* observer) = 0;
236
237 // Removes an observer.
238 virtual void RemoveObserver(Observer* observer) = 0;
239
240 // Gets the list of disks found.
241 virtual const DiskMap& disks() const = 0;
242
[email protected]bcfa0072012-08-07 01:08:57243 // Returns Disk object corresponding to |source_path| or NULL on failure.
244 virtual const Disk* FindDiskBySourcePath(
245 const std::string& source_path) const = 0;
246
[email protected]4ae73292011-11-15 05:20:18247 // Gets the list of mount points.
248 virtual const MountPointMap& mount_points() const = 0;
249
[email protected]a2e4ee22014-07-11 05:16:35250 // Refreshes all the information about mounting if it is not yet done and
251 // invokes |callback| when finished. If the information is already refreshed
hironoa4b675d2015-07-29 01:13:37252 // and |force| is false, it just runs |callback| immediately.
[email protected]a2e4ee22014-07-11 05:16:35253 virtual void EnsureMountInfoRefreshed(
hironoa4b675d2015-07-29 01:13:37254 const EnsureMountInfoRefreshedCallback& callback,
255 bool force) = 0;
[email protected]4ae73292011-11-15 05:20:18256
257 // Mounts a device.
[email protected]8f919ee2013-03-14 19:53:29258 // Note that the mount operation may fail. To find out the result, one should
259 // observe DiskMountManager for |Observer::OnMountEvent| event, which will be
260 // raised upon the mount operation completion.
[email protected]b9f22d12012-04-25 21:46:48261 virtual void MountPath(const std::string& source_path,
262 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33263 const std::string& mount_label,
[email protected]b9f22d12012-04-25 21:46:48264 MountType type) = 0;
[email protected]4ae73292011-11-15 05:20:18265
266 // Unmounts a mounted disk.
[email protected]10795ae2012-10-10 07:33:49267 // |UnmountOptions| enum defined in chromeos/dbus/cros_disks_client.h.
[email protected]8f919ee2013-03-14 19:53:29268 // When the method is complete, |callback| will be called and observers'
269 // |OnMountEvent| will be raised.
270 //
271 // |callback| may be empty, in which case it gets ignored.
[email protected]10795ae2012-10-10 07:33:49272 virtual void UnmountPath(const std::string& mount_path,
[email protected]8f919ee2013-03-14 19:53:29273 UnmountOptions options,
274 const UnmountPathCallback& callback) = 0;
[email protected]4ae73292011-11-15 05:20:18275
[email protected]4ae73292011-11-15 05:20:18276 // Formats Device given its mount path. Unmounts the device.
277 // Example: mount_path: /media/VOLUME_LABEL
278 virtual void FormatMountedDevice(const std::string& mount_path) = 0;
279
280 // Unmounts device_path and all of its known children.
[email protected]e008d4fa2013-02-21 06:38:01281 virtual void UnmountDeviceRecursively(
[email protected]4ae73292011-11-15 05:20:18282 const std::string& device_path,
[email protected]e008d4fa2013-02-21 06:38:01283 const UnmountDeviceRecursivelyCallbackType& callback) = 0;
[email protected]4ae73292011-11-15 05:20:18284
[email protected]e3c1fc92012-11-15 00:56:46285 // Used in tests to initialize the manager's disk and mount point sets.
286 // Default implementation does noting. It just fails.
287 virtual bool AddDiskForTest(Disk* disk);
288 virtual bool AddMountPointForTest(const MountPointInfo& mount_point);
289
[email protected]4ae73292011-11-15 05:20:18290 // Returns corresponding string to |type| like "unknown_filesystem".
291 static std::string MountConditionToString(MountCondition type);
292
[email protected]2321d282012-01-31 23:06:59293 // Returns corresponding string to |type|, like "sd", "usb".
294 static std::string DeviceTypeToString(DeviceType type);
295
[email protected]4ae73292011-11-15 05:20:18296 // Creates the global DiskMountManager instance.
297 static void Initialize();
298
[email protected]b307bceb2011-11-17 07:49:55299 // Similar to Initialize(), but can inject an alternative
300 // DiskMountManager such as MockDiskMountManager for testing.
301 // The injected object will be owned by the internal pointer and deleted
302 // by Shutdown().
303 static void InitializeForTesting(DiskMountManager* disk_mount_manager);
304
[email protected]4ae73292011-11-15 05:20:18305 // Destroys the global DiskMountManager instance if it exists.
306 static void Shutdown();
307
308 // Returns a pointer to the global DiskMountManager instance.
309 // Initialize() should already have been called.
310 static DiskMountManager* GetInstance();
311};
312
313} // namespace disks
314} // namespace chromeos
315
[email protected]4cbead42012-08-26 12:02:39316#endif // CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_