OLD | NEW |
| (Empty) |
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_CROS_MOUNT_LIBRARY_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_CROS_MOUNT_LIBRARY_H_ | |
7 #pragma once | |
8 | |
9 #include <map> | |
10 #include <string> | |
11 | |
12 #include "base/memory/singleton.h" | |
13 #include "base/observer_list.h" | |
14 #include "base/time.h" | |
15 #include "third_party/cros/chromeos_mount.h" | |
16 | |
17 namespace chromeos { | |
18 | |
19 typedef enum MountLibraryEventType { | |
20 MOUNT_DISK_ADDED, | |
21 MOUNT_DISK_REMOVED, | |
22 MOUNT_DISK_CHANGED, | |
23 MOUNT_DISK_MOUNTED, | |
24 MOUNT_DISK_UNMOUNTED, | |
25 MOUNT_DEVICE_ADDED, | |
26 MOUNT_DEVICE_REMOVED, | |
27 MOUNT_DEVICE_SCANNED, | |
28 MOUNT_FORMATTING_STARTED, | |
29 MOUNT_FORMATTING_FINISHED | |
30 } MountLibraryEventType; | |
31 | |
32 typedef enum MountCondition { | |
33 MOUNT_CONDITION_NONE, | |
34 MOUNT_CONDITION_UNKNOWN_FILESYSTEM, | |
35 MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM | |
36 } MountCondition; | |
37 | |
38 class MountLibcrosProxy { | |
39 public: | |
40 virtual ~MountLibcrosProxy() {} | |
41 virtual void CallMountPath(const char* source_path, | |
42 MountType type, | |
43 const MountPathOptions& options, | |
44 MountCompletedMonitor callback, | |
45 void* object) = 0; | |
46 virtual void CallUnmountPath(const char* path, | |
47 UnmountRequestCallback callback, | |
48 void* object) = 0; | |
49 virtual void CallRequestMountInfo(RequestMountInfoCallback callback, | |
50 void* object) = 0; | |
51 virtual void CallFormatDevice(const char* device_path, | |
52 const char* filesystem, | |
53 FormatRequestCallback callback, | |
54 void* object) = 0; | |
55 virtual void CallGetDiskProperties(const char* device_path, | |
56 GetDiskPropertiesCallback callback, | |
57 void* object) = 0; | |
58 virtual MountEventConnection MonitorCrosDisks(MountEventMonitor monitor, | |
59 MountCompletedMonitor mount_complete_monitor, | |
60 void* object) = 0; | |
61 virtual void DisconnectCrosDisksMonitorIfSet(MountEventConnection connection) | |
62 = 0; | |
63 }; | |
64 | |
65 // This class handles the interaction with the ChromeOS mount library APIs. | |
66 // Classes can add themselves as observers. Users can get an instance of this | |
67 // library class like this: chromeos::CrosLibrary::Get()->GetMountLibrary() | |
68 class MountLibrary { | |
69 public: | |
70 enum MountEvent { | |
71 MOUNTING, | |
72 UNMOUNTING | |
73 }; | |
74 // Used to house an instance of each found mount device. | |
75 class Disk { | |
76 public: | |
77 Disk(const std::string& device_path, | |
78 const std::string& mount_path, | |
79 const std::string& system_path, | |
80 const std::string& file_path, | |
81 const std::string& device_label, | |
82 const std::string& drive_label, | |
83 const std::string& parent_path, | |
84 const std::string& system_path_prefix, | |
85 DeviceType device_type, | |
86 uint64 total_size, | |
87 bool is_parent, | |
88 bool is_read_only, | |
89 bool has_media, | |
90 bool on_boot_device, | |
91 bool is_hidden); | |
92 ~Disk(); | |
93 | |
94 // The path of the device, used by devicekit-disks. | |
95 const std::string& device_path() const { return device_path_; } | |
96 // The path to the mount point of this device. Will be empty if not mounted. | |
97 const std::string& mount_path() const { return mount_path_; } | |
98 // The path of the device according to the udev system. | |
99 const std::string& system_path() const { return system_path_; } | |
100 // The path of the device according to filesystem. | |
101 const std::string& file_path() const { return file_path_; } | |
102 // Device's label. | |
103 const std::string& device_label() const { return device_label_; } | |
104 // If disk is a parent, then its label, else parents label. | |
105 const std::string& drive_label() const { return drive_label_; } | |
106 // Parents device path. If device has no parent, then empty string. | |
107 const std::string& parent_path() const { return parent_path_; } | |
108 // Path of the system device this device's block is a part of. | |
109 const std::string& system_path_prefix() const { | |
110 return system_path_prefix_; | |
111 } | |
112 // Device type. | |
113 DeviceType device_type() const { return device_type_; } | |
114 // Total size of the device. | |
115 uint64 total_size() const { return total_size_; } | |
116 // Is the device is a parent device (i.e. sdb rather than sdb1). | |
117 bool is_parent() const { return is_parent_; } | |
118 // Is the device read only. | |
119 bool is_read_only() const { return is_read_only_; } | |
120 // Does the device contains media. | |
121 bool has_media() const { return has_media_; } | |
122 // Is the device on the boot device. | |
123 bool on_boot_device() const { return on_boot_device_; } | |
124 // Shoud the device be shown in the UI, or automounted. | |
125 bool is_hidden() const { return is_hidden_; } | |
126 | |
127 void set_mount_path(const char* mount_path) { mount_path_ = mount_path; } | |
128 void clear_mount_path() { mount_path_.clear(); } | |
129 | |
130 private: | |
131 std::string device_path_; | |
132 std::string mount_path_; | |
133 std::string system_path_; | |
134 std::string file_path_; | |
135 std::string device_label_; | |
136 std::string drive_label_; | |
137 std::string parent_path_; | |
138 std::string system_path_prefix_; | |
139 DeviceType device_type_; | |
140 uint64 total_size_; | |
141 bool is_parent_; | |
142 bool is_read_only_; | |
143 bool has_media_; | |
144 bool on_boot_device_; | |
145 bool is_hidden_; | |
146 }; | |
147 typedef std::map<std::string, Disk*> DiskMap; | |
148 typedef std::map<std::string, std::string> PathMap; | |
149 | |
150 struct MountPointInfo { | |
151 std::string source_path; | |
152 std::string mount_path; | |
153 MountType mount_type; | |
154 MountCondition mount_condition; | |
155 | |
156 MountPointInfo(const char* source, const char* mount, const MountType type, | |
157 MountCondition condition) | |
158 : source_path(source ? source : ""), | |
159 mount_path(mount ? mount : ""), | |
160 mount_type(type), | |
161 mount_condition(condition) { | |
162 } | |
163 }; | |
164 | |
165 // MountPointMap key is mount_path. | |
166 typedef std::map<std::string, MountPointInfo> MountPointMap; | |
167 | |
168 typedef void(*UnmountDeviceRecursiveCallbackType)(void*, bool); | |
169 | |
170 class Observer { | |
171 public: | |
172 virtual ~Observer() {} | |
173 // Async API events. | |
174 virtual void DiskChanged(MountLibraryEventType event, | |
175 const Disk* disk) = 0; | |
176 virtual void DeviceChanged(MountLibraryEventType event, | |
177 const std::string& device_path) = 0; | |
178 virtual void MountCompleted(MountEvent event_type, | |
179 MountError error_code, | |
180 const MountPointInfo& mount_info) = 0; | |
181 }; | |
182 | |
183 virtual ~MountLibrary() {} | |
184 virtual void Init() = 0; | |
185 virtual void AddObserver(Observer* observer) = 0; | |
186 virtual void RemoveObserver(Observer* observer) = 0; | |
187 virtual const DiskMap& disks() const = 0; | |
188 virtual const MountPointMap& mount_points() const = 0; | |
189 | |
190 virtual void RequestMountInfoRefresh() = 0; | |
191 virtual void MountPath(const char* source_path, | |
192 MountType type, | |
193 const MountPathOptions& options) = 0; | |
194 // |path| is device's mount path. | |
195 virtual void UnmountPath(const char* path) = 0; | |
196 | |
197 // Retrieves total and remaining available size on |mount_path|. | |
198 virtual void GetSizeStatsOnFileThread(const char* mount_path, | |
199 size_t* total_size_kb, | |
200 size_t* remaining_size_kb) = 0; | |
201 | |
202 // Formats device given its file path. | |
203 // Example: file_path: /dev/sdb1 | |
204 virtual void FormatUnmountedDevice(const char* file_path) = 0; | |
205 | |
206 // Formats Device given its mount path. Unmount's the device | |
207 // Example: mount_path: /media/VOLUME_LABEL | |
208 virtual void FormatMountedDevice(const char* mount_path) = 0; | |
209 | |
210 // Unmounts device_poath and all of its known children. | |
211 virtual void UnmountDeviceRecursive(const char* device_path, | |
212 UnmountDeviceRecursiveCallbackType callback, void* user_data) = 0; | |
213 | |
214 // Helper functions for parameter conversions. | |
215 static std::string MountTypeToString(MountType type); | |
216 static MountType MountTypeFromString(const std::string& type_str); | |
217 static std::string MountConditionToString(MountCondition type); | |
218 | |
219 // Used in testing. Enables mocking libcros. | |
220 virtual void SetLibcrosProxy(MountLibcrosProxy* proxy) {} | |
221 | |
222 // Factory function, creates a new instance and returns ownership. | |
223 // For normal usage, access the singleton via CrosLibrary::Get(). | |
224 static MountLibrary* GetImpl(bool stub); | |
225 }; | |
226 | |
227 } // namespace chromeos | |
228 | |
229 #endif // CHROME_BROWSER_CHROMEOS_CROS_MOUNT_LIBRARY_H_ | |
OLD | NEW |