Less strict CHECK for DeviceCapabilities results.
[chromium-blink-merge.git] / chromeos / disks / disk_mount_manager.h
blob4edc475256855453962a80c55fd70e6db823c5f5
1 // Copyright (c) 2012 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.
5 #ifndef CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_
6 #define CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_
8 #include <map>
10 #include "base/callback_forward.h"
11 #include "chromeos/chromeos_export.h"
12 #include "chromeos/dbus/cros_disks_client.h"
14 namespace chromeos {
15 namespace disks {
17 // Condition of mounted filesystem.
18 enum MountCondition {
19 MOUNT_CONDITION_NONE,
20 MOUNT_CONDITION_UNKNOWN_FILESYSTEM,
21 MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM,
24 // This class handles the interaction with cros-disks.
25 // Other classes can add themselves as observers.
26 class CHROMEOS_EXPORT DiskMountManager {
27 public:
28 // Event types passed to the observers.
29 enum DiskEvent {
30 DISK_ADDED,
31 DISK_REMOVED,
32 DISK_CHANGED,
35 enum DeviceEvent {
36 DEVICE_ADDED,
37 DEVICE_REMOVED,
38 DEVICE_SCANNED,
41 enum MountEvent {
42 MOUNTING,
43 UNMOUNTING,
46 enum FormatEvent {
47 FORMAT_STARTED,
48 FORMAT_COMPLETED
51 // Used to house an instance of each found mount device.
52 class Disk {
53 public:
54 Disk(const std::string& device_path,
55 const std::string& mount_path,
56 const std::string& system_path,
57 const std::string& file_path,
58 const std::string& device_label,
59 const std::string& drive_label,
60 const std::string& vendor_id,
61 const std::string& vendor_name,
62 const std::string& product_id,
63 const std::string& product_name,
64 const std::string& fs_uuid,
65 const std::string& system_path_prefix,
66 DeviceType device_type,
67 uint64 total_size_in_bytes,
68 bool is_parent,
69 bool is_read_only,
70 bool has_media,
71 bool on_boot_device,
72 bool is_hidden);
73 ~Disk();
75 // The path of the device, used by devicekit-disks.
76 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
77 const std::string& device_path() const { return device_path_; }
79 // The path to the mount point of this device. Will be empty if not mounted.
80 // (e.g. /media/removable/VOLUME)
81 const std::string& mount_path() const { return mount_path_; }
83 // The path of the device according to the udev system.
84 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
85 const std::string& system_path() const { return system_path_; }
87 // The path of the device according to filesystem.
88 // (e.g. /dev/sdb)
89 const std::string& file_path() const { return file_path_; }
91 // Device's label.
92 const std::string& device_label() const { return device_label_; }
94 // If disk is a parent, then its label, else parents label.
95 // (e.g. "TransMemory")
96 const std::string& drive_label() const { return drive_label_; }
98 // Vendor ID of the device (e.g. "18d1").
99 const std::string& vendor_id() const { return vendor_id_; }
101 // Vendor name of the device (e.g. "Google Inc.").
102 const std::string& vendor_name() const { return vendor_name_; }
104 // Product ID of the device (e.g. "4e11").
105 const std::string& product_id() const { return product_id_; }
107 // Product name of the device (e.g. "Nexus One").
108 const std::string& product_name() const { return product_name_; }
110 // Returns the file system uuid string.
111 const std::string& fs_uuid() const { return fs_uuid_; }
113 // Path of the system device this device's block is a part of.
114 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/)
115 const std::string& system_path_prefix() const {
116 return system_path_prefix_;
119 // Device type.
120 DeviceType device_type() const { return device_type_; }
122 // Total size of the device in bytes.
123 uint64 total_size_in_bytes() const { return total_size_in_bytes_; }
125 // Is the device is a parent device (i.e. sdb rather than sdb1).
126 bool is_parent() const { return is_parent_; }
128 // Is the device read only.
129 bool is_read_only() const { return is_read_only_; }
131 // Does the device contains media.
132 bool has_media() const { return has_media_; }
134 // Is the device on the boot device.
135 bool on_boot_device() const { return on_boot_device_; }
137 // Shoud the device be shown in the UI, or automounted.
138 bool is_hidden() const { return is_hidden_; }
140 void set_mount_path(const std::string& mount_path) {
141 mount_path_ = mount_path;
144 void clear_mount_path() { mount_path_.clear(); }
146 private:
147 std::string device_path_;
148 std::string mount_path_;
149 std::string system_path_;
150 std::string file_path_;
151 std::string device_label_;
152 std::string drive_label_;
153 std::string vendor_id_;
154 std::string vendor_name_;
155 std::string product_id_;
156 std::string product_name_;
157 std::string fs_uuid_;
158 std::string system_path_prefix_;
159 DeviceType device_type_;
160 uint64 total_size_in_bytes_;
161 bool is_parent_;
162 bool is_read_only_;
163 bool has_media_;
164 bool on_boot_device_;
165 bool is_hidden_;
167 typedef std::map<std::string, Disk*> DiskMap;
169 // A struct to store information about mount point.
170 struct MountPointInfo {
171 // Device's path.
172 std::string source_path;
173 // Mounted path.
174 std::string mount_path;
175 // Type of mount.
176 MountType mount_type;
177 // Condition of mount.
178 MountCondition mount_condition;
180 MountPointInfo(const std::string& source,
181 const std::string& mount,
182 const MountType type,
183 MountCondition condition)
184 : source_path(source),
185 mount_path(mount),
186 mount_type(type),
187 mount_condition(condition) {
191 // MountPointMap key is mount_path.
192 typedef std::map<std::string, MountPointInfo> MountPointMap;
194 // A callback function type which is called after UnmountDeviceRecursively
195 // finishes.
196 typedef base::Callback<void(bool)> UnmountDeviceRecursivelyCallbackType;
198 // A callback type for UnmountPath method.
199 typedef base::Callback<void(MountError error_code)> UnmountPathCallback;
201 // Implement this interface to be notified about disk/mount related events.
202 class Observer {
203 public:
204 virtual ~Observer() {}
206 // Called when disk mount status is changed.
207 virtual void OnDiskEvent(DiskEvent event, const Disk* disk) = 0;
208 // Called when device status is changed.
209 virtual void OnDeviceEvent(DeviceEvent event,
210 const std::string& device_path) = 0;
211 // Called after a mount point has been mounted or unmounted.
212 virtual void OnMountEvent(MountEvent event,
213 MountError error_code,
214 const MountPointInfo& mount_info) = 0;
215 // Called on format process events.
216 virtual void OnFormatEvent(FormatEvent event,
217 FormatError error_code,
218 const std::string& device_path) = 0;
221 virtual ~DiskMountManager() {}
223 // Adds an observer.
224 virtual void AddObserver(Observer* observer) = 0;
226 // Removes an observer.
227 virtual void RemoveObserver(Observer* observer) = 0;
229 // Gets the list of disks found.
230 virtual const DiskMap& disks() const = 0;
232 // Returns Disk object corresponding to |source_path| or NULL on failure.
233 virtual const Disk* FindDiskBySourcePath(
234 const std::string& source_path) const = 0;
236 // Gets the list of mount points.
237 virtual const MountPointMap& mount_points() const = 0;
239 // Requests refreshing all the information about mounted disks.
240 virtual void RequestMountInfoRefresh() = 0;
242 // Mounts a device.
243 // Note that the mount operation may fail. To find out the result, one should
244 // observe DiskMountManager for |Observer::OnMountEvent| event, which will be
245 // raised upon the mount operation completion.
246 virtual void MountPath(const std::string& source_path,
247 const std::string& source_format,
248 const std::string& mount_label,
249 MountType type) = 0;
251 // Unmounts a mounted disk.
252 // |UnmountOptions| enum defined in chromeos/dbus/cros_disks_client.h.
253 // When the method is complete, |callback| will be called and observers'
254 // |OnMountEvent| will be raised.
256 // |callback| may be empty, in which case it gets ignored.
257 virtual void UnmountPath(const std::string& mount_path,
258 UnmountOptions options,
259 const UnmountPathCallback& callback) = 0;
261 // Formats Device given its mount path. Unmounts the device.
262 // Example: mount_path: /media/VOLUME_LABEL
263 virtual void FormatMountedDevice(const std::string& mount_path) = 0;
265 // Unmounts device_path and all of its known children.
266 virtual void UnmountDeviceRecursively(
267 const std::string& device_path,
268 const UnmountDeviceRecursivelyCallbackType& callback) = 0;
270 // Used in tests to initialize the manager's disk and mount point sets.
271 // Default implementation does noting. It just fails.
272 virtual bool AddDiskForTest(Disk* disk);
273 virtual bool AddMountPointForTest(const MountPointInfo& mount_point);
275 // Returns corresponding string to |type| like "unknown_filesystem".
276 static std::string MountConditionToString(MountCondition type);
278 // Returns corresponding string to |type|, like "sd", "usb".
279 static std::string DeviceTypeToString(DeviceType type);
281 // Creates the global DiskMountManager instance.
282 static void Initialize();
284 // Similar to Initialize(), but can inject an alternative
285 // DiskMountManager such as MockDiskMountManager for testing.
286 // The injected object will be owned by the internal pointer and deleted
287 // by Shutdown().
288 static void InitializeForTesting(DiskMountManager* disk_mount_manager);
290 // Destroys the global DiskMountManager instance if it exists.
291 static void Shutdown();
293 // Returns a pointer to the global DiskMountManager instance.
294 // Initialize() should already have been called.
295 static DiskMountManager* GetInstance();
298 } // namespace disks
299 } // namespace chromeos
301 #endif // CHROMEOS_DISKS_DISK_MOUNT_MANAGER_H_