blob: 4f08a25fc4531359b2534bc34280fd44de043d28 [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]64e199252012-04-06 01:54:365#ifndef CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_
6#define CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_
[email protected]4ae73292011-11-15 05:20:187
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
[email protected]64e199252012-04-06 01:54:3613#include "chromeos/chromeos_export.h"
14#include "chromeos/dbus/dbus_client_implementation_type.h"
[email protected]4ae73292011-11-15 05:20:1815
16namespace dbus {
17class Bus;
18class Response;
19}
20
[email protected]e3c1fc92012-11-15 00:56:4621// TODO(tbarzic): We should move these enums inside CrosDisksClient,
22// to be clearer where they come from. Also, most of these are partially or
23// completely duplicated in third_party/dbus/service_constants.h. We should
24// probably use enums from service_contstants directly.
[email protected]4ae73292011-11-15 05:20:1825namespace chromeos {
26
27// Enum describing types of mount used by cros-disks.
28enum MountType {
29 MOUNT_TYPE_INVALID,
30 MOUNT_TYPE_DEVICE,
31 MOUNT_TYPE_ARCHIVE,
[email protected]9bb24222012-02-09 02:00:4332 MOUNT_TYPE_GDATA,
[email protected]4ae73292011-11-15 05:20:1833 MOUNT_TYPE_NETWORK_STORAGE,
34};
35
36// Type of device.
37enum DeviceType {
[email protected]2321d282012-01-31 23:06:5938 DEVICE_TYPE_UNKNOWN,
39 DEVICE_TYPE_USB, // USB stick.
40 DEVICE_TYPE_SD, // SD card.
[email protected]f4ae40ac2012-05-04 21:57:0041 DEVICE_TYPE_OPTICAL_DISC, // e.g. Optical disc excluding DVD.
42 DEVICE_TYPE_MOBILE, // Storage on a mobile device (e.g. Android).
43 DEVICE_TYPE_DVD, // DVD.
[email protected]4ae73292011-11-15 05:20:1844};
45
46// Mount error code used by cros-disks.
47enum MountError {
48 MOUNT_ERROR_NONE = 0,
49 MOUNT_ERROR_UNKNOWN = 1,
50 MOUNT_ERROR_INTERNAL = 2,
[email protected]e3c1fc92012-11-15 00:56:4651 MOUNT_ERROR_INVALID_ARGUMENT = 3,
52 MOUNT_ERROR_INVALID_PATH = 4,
53 MOUNT_ERROR_PATH_ALREADY_MOUNTED = 5,
54 MOUNT_ERROR_PATH_NOT_MOUNTED = 6,
55 MOUNT_ERROR_DIRECTORY_CREATION_FAILED = 7,
56 MOUNT_ERROR_INVALID_MOUNT_OPTIONS = 8,
57 MOUNT_ERROR_INVALID_UNMOUNT_OPTIONS = 9,
58 MOUNT_ERROR_INSUFFICIENT_PERMISSIONS = 10,
59 MOUNT_ERROR_MOUNT_PROGRAM_NOT_FOUND = 11,
60 MOUNT_ERROR_MOUNT_PROGRAM_FAILED = 12,
61 MOUNT_ERROR_INVALID_DEVICE_PATH = 100,
[email protected]4ae73292011-11-15 05:20:1862 MOUNT_ERROR_UNKNOWN_FILESYSTEM = 101,
[email protected]a66a23cb2012-06-19 23:15:3363 MOUNT_ERROR_UNSUPPORTED_FILESYSTEM = 102,
[email protected]4ae73292011-11-15 05:20:1864 MOUNT_ERROR_INVALID_ARCHIVE = 201,
[email protected]9bb24222012-02-09 02:00:4365 MOUNT_ERROR_NOT_AUTHENTICATED = 601,
[email protected]4ae73292011-11-15 05:20:1866 MOUNT_ERROR_PATH_UNMOUNTED = 901,
67 // TODO(tbarzic): Add more error codes as they get added to cros-disks and
68 // consider doing explicit translation from cros-disks error_types.
69};
70
[email protected]e3c1fc92012-11-15 00:56:4671// Format error reported by cros-disks.
72enum FormatError {
73 FORMAT_ERROR_NONE,
74 FORMAT_ERROR_UNKNOWN,
75 FORMAT_ERROR_INTERNAL,
76 FORMAT_ERROR_INVALID_DEVICE_PATH,
77 FORMAT_ERROR_DEVICE_BEING_FORMATTED,
78 FORMAT_ERROR_UNSUPPORTED_FILESYSTEM,
79 FORMAT_ERROR_FORMAT_PROGRAM_NOT_FOUND,
80 FORMAT_ERROR_FORMAT_PROGRAM_FAILED,
81 FORMAT_ERROR_DEVICE_NOT_ALLOWED,
82};
83
[email protected]4ae73292011-11-15 05:20:1884// Event type each corresponding to a signal sent from cros-disks.
85enum MountEventType {
[email protected]e3c1fc92012-11-15 00:56:4686 CROS_DISKS_DISK_ADDED,
87 CROS_DISKS_DISK_REMOVED,
88 CROS_DISKS_DISK_CHANGED,
89 CROS_DISKS_DEVICE_ADDED,
90 CROS_DISKS_DEVICE_REMOVED,
91 CROS_DISKS_DEVICE_SCANNED,
92 CROS_DISKS_FORMATTING_FINISHED,
[email protected]4ae73292011-11-15 05:20:1893};
94
[email protected]10795ae2012-10-10 07:33:4995// Additional unmount flags to be added to unmount request.
96enum UnmountOptions {
97 UNMOUNT_OPTIONS_NONE,
98 UNMOUNT_OPTIONS_LAZY, // Do lazy unmount.
99};
100
[email protected]4ae73292011-11-15 05:20:18101// A class to represent information about a disk sent from cros-disks.
102class DiskInfo {
103 public:
104 DiskInfo(const std::string& device_path, dbus::Response* response);
105 ~DiskInfo();
106
107 // Device path. (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27108 const std::string& device_path() const { return device_path_; }
[email protected]4ae73292011-11-15 05:20:18109
110 // Disk mount path. (e.g. /media/removable/VOLUME)
[email protected]85b95a2012012-08-07 18:57:27111 const std::string& mount_path() const { return mount_path_; }
[email protected]4ae73292011-11-15 05:20:18112
113 // Disk system path given by udev.
114 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27115 const std::string& system_path() const { return system_path_; }
[email protected]4ae73292011-11-15 05:20:18116
117 // Is a drive or not. (i.e. true with /dev/sdb, false with /dev/sdb1)
118 bool is_drive() const { return is_drive_; }
119
120 // Does the disk have media content.
121 bool has_media() const { return has_media_; }
122
123 // Is the disk on deveice we booted the machine from.
124 bool on_boot_device() const { return on_boot_device_; }
125
126 // Disk file path (e.g. /dev/sdb).
[email protected]85b95a2012012-08-07 18:57:27127 const std::string& file_path() const { return file_path_; }
[email protected]4ae73292011-11-15 05:20:18128
129 // Disk label.
[email protected]85b95a2012012-08-07 18:57:27130 const std::string& label() const { return label_; }
[email protected]4ae73292011-11-15 05:20:18131
[email protected]202e9fee2012-09-13 20:21:29132 // Vendor ID of the device (e.g. "18d1").
133 const std::string& vendor_id() const { return vendor_id_; }
134
135 // Vendor name of the device (e.g. "Google Inc.").
136 const std::string& vendor_name() const { return vendor_name_; }
137
138 // Product ID of the device (e.g. "4e11").
139 const std::string& product_id() const { return product_id_; }
140
141 // Product name of the device (e.g. "Nexus One").
142 const std::string& product_name() const { return product_name_; }
143
[email protected]4ae73292011-11-15 05:20:18144 // Disk model. (e.g. "TransMemory")
[email protected]85b95a2012012-08-07 18:57:27145 const std::string& drive_label() const { return drive_model_; }
[email protected]4ae73292011-11-15 05:20:18146
147 // Device type. Not working well, yet.
148 DeviceType device_type() const { return device_type_; }
149
150 // Total size of the disk in bytes.
151 uint64 total_size_in_bytes() const { return total_size_in_bytes_; }
152
153 // Is the device read-only.
154 bool is_read_only() const { return is_read_only_; }
155
156 // Returns true if the device should be hidden from the file browser.
157 bool is_hidden() const { return is_hidden_; }
158
[email protected]9c5620d32012-07-31 01:00:38159 // Returns file system uuid.
[email protected]85b95a2012012-08-07 18:57:27160 const std::string& uuid() const { return uuid_; }
[email protected]9c5620d32012-07-31 01:00:38161
[email protected]4ae73292011-11-15 05:20:18162 private:
163 void InitializeFromResponse(dbus::Response* response);
164
165 std::string device_path_;
166 std::string mount_path_;
167 std::string system_path_;
168 bool is_drive_;
169 bool has_media_;
170 bool on_boot_device_;
171
172 std::string file_path_;
173 std::string label_;
[email protected]202e9fee2012-09-13 20:21:29174 std::string vendor_id_;
175 std::string vendor_name_;
176 std::string product_id_;
177 std::string product_name_;
[email protected]4ae73292011-11-15 05:20:18178 std::string drive_model_;
179 DeviceType device_type_;
180 uint64 total_size_in_bytes_;
181 bool is_read_only_;
182 bool is_hidden_;
[email protected]9c5620d32012-07-31 01:00:38183 std::string uuid_;
[email protected]4ae73292011-11-15 05:20:18184};
185
186// A class to make the actual DBus calls for cros-disks service.
187// This class only makes calls, result/error handling should be done
188// by callbacks.
[email protected]64e199252012-04-06 01:54:36189class CHROMEOS_EXPORT CrosDisksClient {
[email protected]4ae73292011-11-15 05:20:18190 public:
191 // A callback to be called when DBus method call fails.
192 typedef base::Callback<void()> ErrorCallback;
193
194 // A callback to handle the result of Mount.
195 typedef base::Callback<void()> MountCallback;
196
197 // A callback to handle the result of Unmount.
198 // The argument is the device path.
[email protected]85b95a2012012-08-07 18:57:27199 typedef base::Callback<void(const std::string& device_path)> UnmountCallback;
[email protected]4ae73292011-11-15 05:20:18200
201 // A callback to handle the result of EnumerateAutoMountableDevices.
202 // The argument is the enumerated device paths.
[email protected]85b95a2012012-08-07 18:57:27203 typedef base::Callback<void(const std::vector<std::string>& device_paths)
[email protected]4ae73292011-11-15 05:20:18204 > EnumerateAutoMountableDevicesCallback;
205
206 // A callback to handle the result of FormatDevice.
207 // The first argument is the device path.
208 // The second argument is true when formatting succeeded, false otherwise.
[email protected]85b95a2012012-08-07 18:57:27209 typedef base::Callback<void(const std::string& device_path,
210 bool format_succeeded)> FormatDeviceCallback;
[email protected]4ae73292011-11-15 05:20:18211
212 // A callback to handle the result of GetDeviceProperties.
213 // The argument is the information about the specified device.
[email protected]85b95a2012012-08-07 18:57:27214 typedef base::Callback<void(const DiskInfo& disk_info)
215 > GetDevicePropertiesCallback;
[email protected]4ae73292011-11-15 05:20:18216
217 // A callback to handle MountCompleted signal.
218 // The first argument is the error code.
219 // The second argument is the source path.
220 // The third argument is the mount type.
221 // The fourth argument is the mount path.
[email protected]85b95a2012012-08-07 18:57:27222 typedef base::Callback<void(MountError error_code,
223 const std::string& source_path,
224 MountType mount_type,
225 const std::string& mount_path)
226 > MountCompletedHandler;
[email protected]4ae73292011-11-15 05:20:18227
228 // A callback to handle mount events.
229 // The first argument is the event type.
230 // The second argument is the device path.
[email protected]85b95a2012012-08-07 18:57:27231 typedef base::Callback<void(MountEventType event_type,
232 const std::string& device_path)
[email protected]4ae73292011-11-15 05:20:18233 > MountEventHandler;
234
235 virtual ~CrosDisksClient();
236
237 // Calls Mount method. |callback| is called after the method call succeeds,
238 // otherwise, |error_callback| is called.
[email protected]dcad8fc2012-04-30 23:31:33239 // When mounting an archive, caller may set two optional arguments:
240 // - The |source_format| argument passes the file extension (with the leading
241 // dot, for example ".zip"). If |source_format| is empty then the source
242 // format is auto-detected.
243 // - The |mount_label| argument passes an optional mount label to be used as
244 // the directory name of the mount point. If |mount_label| is empty, the
245 // mount label will be based on the |source_path|.
[email protected]4ae73292011-11-15 05:20:18246 virtual void Mount(const std::string& source_path,
[email protected]b9f22d12012-04-25 21:46:48247 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33248 const std::string& mount_label,
[email protected]4ae73292011-11-15 05:20:18249 MountType type,
[email protected]4a404e52012-04-11 02:25:35250 const MountCallback& callback,
251 const ErrorCallback& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18252
253 // Calls Unmount method. |callback| is called after the method call succeeds,
254 // otherwise, |error_callback| is called.
255 virtual void Unmount(const std::string& device_path,
[email protected]10795ae2012-10-10 07:33:49256 UnmountOptions options,
[email protected]4a404e52012-04-11 02:25:35257 const UnmountCallback& callback,
[email protected]10795ae2012-10-10 07:33:49258 const UnmountCallback& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18259
260 // Calls EnumerateAutoMountableDevices method. |callback| is called after the
261 // method call succeeds, otherwise, |error_callback| is called.
262 virtual void EnumerateAutoMountableDevices(
[email protected]4a404e52012-04-11 02:25:35263 const EnumerateAutoMountableDevicesCallback& callback,
264 const ErrorCallback& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18265
266 // Calls FormatDevice method. |callback| is called after the method call
267 // succeeds, otherwise, |error_callback| is called.
268 virtual void FormatDevice(const std::string& device_path,
269 const std::string& filesystem,
[email protected]4a404e52012-04-11 02:25:35270 const FormatDeviceCallback& callback,
271 const ErrorCallback& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18272
273 // Calls GetDeviceProperties method. |callback| is called after the method
274 // call succeeds, otherwise, |error_callback| is called.
275 virtual void GetDeviceProperties(const std::string& device_path,
[email protected]4a404e52012-04-11 02:25:35276 const GetDevicePropertiesCallback& callback,
277 const ErrorCallback& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18278
279 // Registers given callback for events.
280 // |mount_event_handler| is called when mount event signal is received.
281 // |mount_completed_handler| is called when MountCompleted signal is received.
282 virtual void SetUpConnections(
[email protected]4a404e52012-04-11 02:25:35283 const MountEventHandler& mount_event_handler,
284 const MountCompletedHandler& mount_completed_handler) = 0;
[email protected]4ae73292011-11-15 05:20:18285
286 // Factory function, creates a new instance and returns ownership.
287 // For normal usage, access the singleton via DBusThreadManager::Get().
[email protected]e8db03d62012-03-31 04:08:38288 static CrosDisksClient* Create(DBusClientImplementationType type,
289 dbus::Bus* bus);
[email protected]4ae73292011-11-15 05:20:18290
291 protected:
292 // Create() should be used instead.
293 CrosDisksClient();
294
295 private:
296 DISALLOW_COPY_AND_ASSIGN(CrosDisksClient);
297};
298
299} // namespace chromeos
300
[email protected]64e199252012-04-06 01:54:36301#endif // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_