OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/callback.h" | |
14 #include "chrome/browser/chromeos/dbus/dbus_client_implementation_type.h" | |
15 | |
16 namespace dbus { | |
17 class Bus; | |
18 class Response; | |
19 } | |
20 | |
21 namespace chromeos { | |
22 | |
23 // Enum describing types of mount used by cros-disks. | |
24 enum MountType { | |
25 MOUNT_TYPE_INVALID, | |
26 MOUNT_TYPE_DEVICE, | |
27 MOUNT_TYPE_ARCHIVE, | |
28 MOUNT_TYPE_GDATA, | |
29 MOUNT_TYPE_NETWORK_STORAGE, | |
30 }; | |
31 | |
32 // Type of device. | |
33 enum DeviceType { | |
34 DEVICE_TYPE_UNKNOWN, | |
35 DEVICE_TYPE_USB, // USB stick. | |
36 DEVICE_TYPE_SD, // SD card. | |
37 DEVICE_TYPE_OPTICAL_DISC, // e.g. DVD. | |
38 DEVICE_TYPE_MOBILE // Storage on a mobile device (e.g. Android). | |
39 }; | |
40 | |
41 // Mount error code used by cros-disks. | |
42 enum MountError { | |
43 MOUNT_ERROR_NONE = 0, | |
44 MOUNT_ERROR_UNKNOWN = 1, | |
45 MOUNT_ERROR_INTERNAL = 2, | |
46 MOUNT_ERROR_UNKNOWN_FILESYSTEM = 101, | |
47 MOUNT_ERROR_UNSUPORTED_FILESYSTEM = 102, | |
48 MOUNT_ERROR_INVALID_ARCHIVE = 201, | |
49 MOUNT_ERROR_LIBRARY_NOT_LOADED = 501, | |
50 MOUNT_ERROR_NOT_AUTHENTICATED = 601, | |
51 MOUNT_ERROR_NETWORK_ERROR = 602, | |
52 MOUNT_ERROR_PATH_UNMOUNTED = 901, | |
53 // TODO(tbarzic): Add more error codes as they get added to cros-disks and | |
54 // consider doing explicit translation from cros-disks error_types. | |
55 }; | |
56 | |
57 // Event type each corresponding to a signal sent from cros-disks. | |
58 enum MountEventType { | |
59 DISK_ADDED, | |
60 DISK_REMOVED, | |
61 DISK_CHANGED, | |
62 DEVICE_ADDED, | |
63 DEVICE_REMOVED, | |
64 DEVICE_SCANNED, | |
65 FORMATTING_FINISHED, | |
66 }; | |
67 | |
68 // A class to represent information about a disk sent from cros-disks. | |
69 class DiskInfo { | |
70 public: | |
71 DiskInfo(const std::string& device_path, dbus::Response* response); | |
72 ~DiskInfo(); | |
73 | |
74 // Device path. (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1) | |
75 std::string device_path() const { return device_path_; } | |
76 | |
77 // Disk mount path. (e.g. /media/removable/VOLUME) | |
78 std::string mount_path() const { return mount_path_; } | |
79 | |
80 // Disk system path given by udev. | |
81 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1) | |
82 std::string system_path() const { return system_path_; } | |
83 | |
84 // Is a drive or not. (i.e. true with /dev/sdb, false with /dev/sdb1) | |
85 bool is_drive() const { return is_drive_; } | |
86 | |
87 // Does the disk have media content. | |
88 bool has_media() const { return has_media_; } | |
89 | |
90 // Is the disk on deveice we booted the machine from. | |
91 bool on_boot_device() const { return on_boot_device_; } | |
92 | |
93 // Disk file path (e.g. /dev/sdb). | |
94 std::string file_path() const { return file_path_; } | |
95 | |
96 // Disk label. | |
97 std::string label() const { return label_; } | |
98 | |
99 // Disk model. (e.g. "TransMemory") | |
100 std::string drive_label() const { return drive_model_; } | |
101 | |
102 // Device type. Not working well, yet. | |
103 DeviceType device_type() const { return device_type_; } | |
104 | |
105 // Total size of the disk in bytes. | |
106 uint64 total_size_in_bytes() const { return total_size_in_bytes_; } | |
107 | |
108 // Is the device read-only. | |
109 bool is_read_only() const { return is_read_only_; } | |
110 | |
111 // Returns true if the device should be hidden from the file browser. | |
112 bool is_hidden() const { return is_hidden_; } | |
113 | |
114 private: | |
115 void InitializeFromResponse(dbus::Response* response); | |
116 | |
117 std::string device_path_; | |
118 std::string mount_path_; | |
119 std::string system_path_; | |
120 bool is_drive_; | |
121 bool has_media_; | |
122 bool on_boot_device_; | |
123 | |
124 std::string file_path_; | |
125 std::string label_; | |
126 std::string drive_model_; | |
127 DeviceType device_type_; | |
128 uint64 total_size_in_bytes_; | |
129 bool is_read_only_; | |
130 bool is_hidden_; | |
131 }; | |
132 | |
133 // A class to make the actual DBus calls for cros-disks service. | |
134 // This class only makes calls, result/error handling should be done | |
135 // by callbacks. | |
136 class CrosDisksClient { | |
137 public: | |
138 // A callback to be called when DBus method call fails. | |
139 typedef base::Callback<void()> ErrorCallback; | |
140 | |
141 // A callback to handle the result of Mount. | |
142 typedef base::Callback<void()> MountCallback; | |
143 | |
144 // A callback to handle the result of Unmount. | |
145 // The argument is the device path. | |
146 typedef base::Callback<void(const std::string&)> UnmountCallback; | |
147 | |
148 // A callback to handle the result of EnumerateAutoMountableDevices. | |
149 // The argument is the enumerated device paths. | |
150 typedef base::Callback<void(const std::vector<std::string>&) | |
151 > EnumerateAutoMountableDevicesCallback; | |
152 | |
153 // A callback to handle the result of FormatDevice. | |
154 // The first argument is the device path. | |
155 // The second argument is true when formatting succeeded, false otherwise. | |
156 typedef base::Callback<void(const std::string&, bool)> FormatDeviceCallback; | |
157 | |
158 // A callback to handle the result of GetDeviceProperties. | |
159 // The argument is the information about the specified device. | |
160 typedef base::Callback<void(const DiskInfo&)> GetDevicePropertiesCallback; | |
161 | |
162 // A callback to handle MountCompleted signal. | |
163 // The first argument is the error code. | |
164 // The second argument is the source path. | |
165 // The third argument is the mount type. | |
166 // The fourth argument is the mount path. | |
167 typedef base::Callback<void(MountError, const std::string&, MountType, | |
168 const std::string&)> MountCompletedHandler; | |
169 | |
170 // A callback to handle mount events. | |
171 // The first argument is the event type. | |
172 // The second argument is the device path. | |
173 typedef base::Callback<void(MountEventType, const std::string&) | |
174 > MountEventHandler; | |
175 | |
176 virtual ~CrosDisksClient(); | |
177 | |
178 // Calls Mount method. |callback| is called after the method call succeeds, | |
179 // otherwise, |error_callback| is called. | |
180 virtual void Mount(const std::string& source_path, | |
181 MountType type, | |
182 MountCallback callback, | |
183 ErrorCallback error_callback) = 0; | |
184 | |
185 // Calls Unmount method. |callback| is called after the method call succeeds, | |
186 // otherwise, |error_callback| is called. | |
187 virtual void Unmount(const std::string& device_path, | |
188 UnmountCallback callback, | |
189 ErrorCallback error_callback) = 0; | |
190 | |
191 // Calls EnumerateAutoMountableDevices method. |callback| is called after the | |
192 // method call succeeds, otherwise, |error_callback| is called. | |
193 virtual void EnumerateAutoMountableDevices( | |
194 EnumerateAutoMountableDevicesCallback callback, | |
195 ErrorCallback error_callback) = 0; | |
196 | |
197 // Calls FormatDevice method. |callback| is called after the method call | |
198 // succeeds, otherwise, |error_callback| is called. | |
199 virtual void FormatDevice(const std::string& device_path, | |
200 const std::string& filesystem, | |
201 FormatDeviceCallback callback, | |
202 ErrorCallback error_callback) = 0; | |
203 | |
204 // Calls GetDeviceProperties method. |callback| is called after the method | |
205 // call succeeds, otherwise, |error_callback| is called. | |
206 virtual void GetDeviceProperties(const std::string& device_path, | |
207 GetDevicePropertiesCallback callback, | |
208 ErrorCallback error_callback) = 0; | |
209 | |
210 // Registers given callback for events. | |
211 // |mount_event_handler| is called when mount event signal is received. | |
212 // |mount_completed_handler| is called when MountCompleted signal is received. | |
213 virtual void SetUpConnections( | |
214 MountEventHandler mount_event_handler, | |
215 MountCompletedHandler mount_completed_handler) = 0; | |
216 | |
217 // Factory function, creates a new instance and returns ownership. | |
218 // For normal usage, access the singleton via DBusThreadManager::Get(). | |
219 static CrosDisksClient* Create(DBusClientImplementationType type, | |
220 dbus::Bus* bus); | |
221 | |
222 protected: | |
223 // Create() should be used instead. | |
224 CrosDisksClient(); | |
225 | |
226 private: | |
227 DISALLOW_COPY_AND_ASSIGN(CrosDisksClient); | |
228 }; | |
229 | |
230 } // namespace chromeos | |
231 | |
232 #endif // CHROME_BROWSER_CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_ | |
OLD | NEW |