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_DBUS_CROS_DISKS_CLIENT_H_
6 #define CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "chromeos/chromeos_export.h"
14 #include "chromeos/dbus/dbus_client.h"
15 #include "chromeos/dbus/dbus_client_implementation_type.h"
25 // TODO(tbarzic): We should move these enums inside CrosDisksClient,
26 // to be clearer where they come from. Also, most of these are partially or
27 // completely duplicated in third_party/dbus/service_constants.h. We should
28 // probably use enums from service_contstants directly.
31 // Enum describing types of mount used by cros-disks.
36 // TODO(hidehiko): Drive is not managed by DiskMountManager nor
37 // CrosDisksClient. Remove this after fileBrowserPrivate API is cleaned.
38 MOUNT_TYPE_GOOGLE_DRIVE
,
44 DEVICE_TYPE_USB
, // USB stick.
45 DEVICE_TYPE_SD
, // SD card.
46 DEVICE_TYPE_OPTICAL_DISC
, // e.g. Optical disc excluding DVD.
47 DEVICE_TYPE_MOBILE
, // Storage on a mobile device (e.g. Android).
48 DEVICE_TYPE_DVD
, // DVD.
51 // Mount error code used by cros-disks.
54 MOUNT_ERROR_UNKNOWN
= 1,
55 MOUNT_ERROR_INTERNAL
= 2,
56 MOUNT_ERROR_INVALID_ARGUMENT
= 3,
57 MOUNT_ERROR_INVALID_PATH
= 4,
58 MOUNT_ERROR_PATH_ALREADY_MOUNTED
= 5,
59 MOUNT_ERROR_PATH_NOT_MOUNTED
= 6,
60 MOUNT_ERROR_DIRECTORY_CREATION_FAILED
= 7,
61 MOUNT_ERROR_INVALID_MOUNT_OPTIONS
= 8,
62 MOUNT_ERROR_INVALID_UNMOUNT_OPTIONS
= 9,
63 MOUNT_ERROR_INSUFFICIENT_PERMISSIONS
= 10,
64 MOUNT_ERROR_MOUNT_PROGRAM_NOT_FOUND
= 11,
65 MOUNT_ERROR_MOUNT_PROGRAM_FAILED
= 12,
66 MOUNT_ERROR_INVALID_DEVICE_PATH
= 100,
67 MOUNT_ERROR_UNKNOWN_FILESYSTEM
= 101,
68 MOUNT_ERROR_UNSUPPORTED_FILESYSTEM
= 102,
69 MOUNT_ERROR_INVALID_ARCHIVE
= 201,
70 MOUNT_ERROR_NOT_AUTHENTICATED
= 601,
71 MOUNT_ERROR_PATH_UNMOUNTED
= 901,
72 // TODO(tbarzic): Add more error codes as they get added to cros-disks and
73 // consider doing explicit translation from cros-disks error_types.
76 // Format error reported by cros-disks.
80 FORMAT_ERROR_INTERNAL
,
81 FORMAT_ERROR_INVALID_DEVICE_PATH
,
82 FORMAT_ERROR_DEVICE_BEING_FORMATTED
,
83 FORMAT_ERROR_UNSUPPORTED_FILESYSTEM
,
84 FORMAT_ERROR_FORMAT_PROGRAM_NOT_FOUND
,
85 FORMAT_ERROR_FORMAT_PROGRAM_FAILED
,
86 FORMAT_ERROR_DEVICE_NOT_ALLOWED
,
89 // Event type each corresponding to a signal sent from cros-disks.
91 CROS_DISKS_DISK_ADDED
,
92 CROS_DISKS_DISK_REMOVED
,
93 CROS_DISKS_DISK_CHANGED
,
94 CROS_DISKS_DEVICE_ADDED
,
95 CROS_DISKS_DEVICE_REMOVED
,
96 CROS_DISKS_DEVICE_SCANNED
,
97 CROS_DISKS_FORMATTING_FINISHED
,
100 // Additional unmount flags to be added to unmount request.
101 enum UnmountOptions
{
102 UNMOUNT_OPTIONS_NONE
,
103 UNMOUNT_OPTIONS_LAZY
, // Do lazy unmount.
106 // A class to represent information about a disk sent from cros-disks.
107 class CHROMEOS_EXPORT DiskInfo
{
109 DiskInfo(const std::string
& device_path
, dbus::Response
* response
);
112 // Device path. (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
113 const std::string
& device_path() const { return device_path_
; }
115 // Disk mount path. (e.g. /media/removable/VOLUME)
116 const std::string
& mount_path() const { return mount_path_
; }
118 // Disk system path given by udev.
119 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
120 const std::string
& system_path() const { return system_path_
; }
122 // Is a drive or not. (i.e. true with /dev/sdb, false with /dev/sdb1)
123 bool is_drive() const { return is_drive_
; }
125 // Does the disk have media content.
126 bool has_media() const { return has_media_
; }
128 // Is the disk on deveice we booted the machine from.
129 bool on_boot_device() const { return on_boot_device_
; }
131 // Disk file path (e.g. /dev/sdb).
132 const std::string
& file_path() const { return file_path_
; }
135 const std::string
& label() const { return label_
; }
137 // Vendor ID of the device (e.g. "18d1").
138 const std::string
& vendor_id() const { return vendor_id_
; }
140 // Vendor name of the device (e.g. "Google Inc.").
141 const std::string
& vendor_name() const { return vendor_name_
; }
143 // Product ID of the device (e.g. "4e11").
144 const std::string
& product_id() const { return product_id_
; }
146 // Product name of the device (e.g. "Nexus One").
147 const std::string
& product_name() const { return product_name_
; }
149 // Disk model. (e.g. "TransMemory")
150 const std::string
& drive_label() const { return drive_model_
; }
152 // Device type. Not working well, yet.
153 DeviceType
device_type() const { return device_type_
; }
155 // Total size of the disk in bytes.
156 uint64
total_size_in_bytes() const { return total_size_in_bytes_
; }
158 // Is the device read-only.
159 bool is_read_only() const { return is_read_only_
; }
161 // Returns true if the device should be hidden from the file browser.
162 bool is_hidden() const { return is_hidden_
; }
164 // Returns file system uuid.
165 const std::string
& uuid() const { return uuid_
; }
168 void InitializeFromResponse(dbus::Response
* response
);
170 std::string device_path_
;
171 std::string mount_path_
;
172 std::string system_path_
;
175 bool on_boot_device_
;
177 std::string file_path_
;
179 std::string vendor_id_
;
180 std::string vendor_name_
;
181 std::string product_id_
;
182 std::string product_name_
;
183 std::string drive_model_
;
184 DeviceType device_type_
;
185 uint64 total_size_in_bytes_
;
191 // A class to make the actual DBus calls for cros-disks service.
192 // This class only makes calls, result/error handling should be done
194 class CHROMEOS_EXPORT CrosDisksClient
: public DBusClient
{
196 // A callback to handle the result of EnumerateAutoMountableDevices.
197 // The argument is the enumerated device paths.
198 typedef base::Callback
<void(const std::vector
<std::string
>& device_paths
)
199 > EnumerateAutoMountableDevicesCallback
;
201 // A callback to handle the result of FormatDevice.
202 // The argument is true when formatting succeeded.
203 typedef base::Callback
<void(bool format_succeeded
)> FormatDeviceCallback
;
205 // A callback to handle the result of GetDeviceProperties.
206 // The argument is the information about the specified device.
207 typedef base::Callback
<void(const DiskInfo
& disk_info
)
208 > GetDevicePropertiesCallback
;
210 // A callback to handle MountCompleted signal.
211 // The first argument is the error code.
212 // The second argument is the source path.
213 // The third argument is the mount type.
214 // The fourth argument is the mount path.
215 typedef base::Callback
<void(MountError error_code
,
216 const std::string
& source_path
,
217 MountType mount_type
,
218 const std::string
& mount_path
)
219 > MountCompletedHandler
;
221 // A callback to handle mount events.
222 // The first argument is the event type.
223 // The second argument is the device path.
224 typedef base::Callback
<void(MountEventType event_type
,
225 const std::string
& device_path
)
228 virtual ~CrosDisksClient();
230 // Calls Mount method. |callback| is called after the method call succeeds,
231 // otherwise, |error_callback| is called.
232 // When mounting an archive, caller may set two optional arguments:
233 // - The |source_format| argument passes the file extension (with the leading
234 // dot, for example ".zip"). If |source_format| is empty then the source
235 // format is auto-detected.
236 // - The |mount_label| argument passes an optional mount label to be used as
237 // the directory name of the mount point. If |mount_label| is empty, the
238 // mount label will be based on the |source_path|.
239 virtual void Mount(const std::string
& source_path
,
240 const std::string
& source_format
,
241 const std::string
& mount_label
,
242 const base::Closure
& callback
,
243 const base::Closure
& error_callback
) = 0;
245 // Calls Unmount method. |callback| is called after the method call succeeds,
246 // otherwise, |error_callback| is called.
247 virtual void Unmount(const std::string
& device_path
,
248 UnmountOptions options
,
249 const base::Closure
& callback
,
250 const base::Closure
& error_callback
) = 0;
252 // Calls EnumerateAutoMountableDevices method. |callback| is called after the
253 // method call succeeds, otherwise, |error_callback| is called.
254 virtual void EnumerateAutoMountableDevices(
255 const EnumerateAutoMountableDevicesCallback
& callback
,
256 const base::Closure
& error_callback
) = 0;
258 // Calls FormatDevice method. |callback| is called after the method call
259 // succeeds, otherwise, |error_callback| is called.
260 virtual void FormatDevice(const std::string
& device_path
,
261 const std::string
& filesystem
,
262 const FormatDeviceCallback
& callback
,
263 const base::Closure
& error_callback
) = 0;
265 // Calls GetDeviceProperties method. |callback| is called after the method
266 // call succeeds, otherwise, |error_callback| is called.
267 virtual void GetDeviceProperties(const std::string
& device_path
,
268 const GetDevicePropertiesCallback
& callback
,
269 const base::Closure
& error_callback
) = 0;
271 // Registers given callback for events.
272 // |mount_event_handler| is called when mount event signal is received.
273 // |mount_completed_handler| is called when MountCompleted signal is received.
274 virtual void SetUpConnections(
275 const MountEventHandler
& mount_event_handler
,
276 const MountCompletedHandler
& mount_completed_handler
) = 0;
278 // Factory function, creates a new instance and returns ownership.
279 // For normal usage, access the singleton via DBusThreadManager::Get().
280 static CrosDisksClient
* Create(DBusClientImplementationType type
);
282 // Returns the path of the mount point for archive files.
283 static base::FilePath
GetArchiveMountPoint();
285 // Returns the path of the mount point for removable disks.
286 static base::FilePath
GetRemovableDiskMountPoint();
289 // Create() should be used instead.
293 DISALLOW_COPY_AND_ASSIGN(CrosDisksClient
);
296 } // namespace chromeos
298 #endif // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_