blob: 095566fca4082fbd9eeada69852a35ab5141ab8c [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"
[email protected]5624a252013-07-04 03:17:5312#include "base/callback_forward.h"
[email protected]64e199252012-04-06 01:54:3613#include "chromeos/chromeos_export.h"
[email protected]c5fd5362013-08-27 12:23:0414#include "chromeos/dbus/dbus_client.h"
[email protected]64e199252012-04-06 01:54:3615#include "chromeos/dbus/dbus_client_implementation_type.h"
[email protected]4ae73292011-11-15 05:20:1816
[email protected]a5a8b412013-03-04 15:03:1117namespace base {
18class FilePath;
19}
20
[email protected]4ae73292011-11-15 05:20:1821namespace dbus {
[email protected]4ae73292011-11-15 05:20:1822class Response;
23}
24
[email protected]e3c1fc92012-11-15 00:56:4625// 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.
[email protected]4ae73292011-11-15 05:20:1829namespace chromeos {
30
31// Enum describing types of mount used by cros-disks.
32enum MountType {
33 MOUNT_TYPE_INVALID,
34 MOUNT_TYPE_DEVICE,
35 MOUNT_TYPE_ARCHIVE,
[email protected]4ae73292011-11-15 05:20:1836};
37
38// Type of device.
39enum DeviceType {
[email protected]2321d282012-01-31 23:06:5940 DEVICE_TYPE_UNKNOWN,
41 DEVICE_TYPE_USB, // USB stick.
42 DEVICE_TYPE_SD, // SD card.
[email protected]f4ae40ac2012-05-04 21:57:0043 DEVICE_TYPE_OPTICAL_DISC, // e.g. Optical disc excluding DVD.
44 DEVICE_TYPE_MOBILE, // Storage on a mobile device (e.g. Android).
45 DEVICE_TYPE_DVD, // DVD.
[email protected]4ae73292011-11-15 05:20:1846};
47
48// Mount error code used by cros-disks.
49enum MountError {
50 MOUNT_ERROR_NONE = 0,
51 MOUNT_ERROR_UNKNOWN = 1,
52 MOUNT_ERROR_INTERNAL = 2,
[email protected]e3c1fc92012-11-15 00:56:4653 MOUNT_ERROR_INVALID_ARGUMENT = 3,
54 MOUNT_ERROR_INVALID_PATH = 4,
55 MOUNT_ERROR_PATH_ALREADY_MOUNTED = 5,
56 MOUNT_ERROR_PATH_NOT_MOUNTED = 6,
57 MOUNT_ERROR_DIRECTORY_CREATION_FAILED = 7,
58 MOUNT_ERROR_INVALID_MOUNT_OPTIONS = 8,
59 MOUNT_ERROR_INVALID_UNMOUNT_OPTIONS = 9,
60 MOUNT_ERROR_INSUFFICIENT_PERMISSIONS = 10,
61 MOUNT_ERROR_MOUNT_PROGRAM_NOT_FOUND = 11,
62 MOUNT_ERROR_MOUNT_PROGRAM_FAILED = 12,
63 MOUNT_ERROR_INVALID_DEVICE_PATH = 100,
[email protected]4ae73292011-11-15 05:20:1864 MOUNT_ERROR_UNKNOWN_FILESYSTEM = 101,
[email protected]a66a23cb2012-06-19 23:15:3365 MOUNT_ERROR_UNSUPPORTED_FILESYSTEM = 102,
[email protected]4ae73292011-11-15 05:20:1866 MOUNT_ERROR_INVALID_ARCHIVE = 201,
[email protected]9bb24222012-02-09 02:00:4367 MOUNT_ERROR_NOT_AUTHENTICATED = 601,
[email protected]4ae73292011-11-15 05:20:1868 MOUNT_ERROR_PATH_UNMOUNTED = 901,
69 // TODO(tbarzic): Add more error codes as they get added to cros-disks and
70 // consider doing explicit translation from cros-disks error_types.
71};
72
[email protected]e3c1fc92012-11-15 00:56:4673// Format error reported by cros-disks.
74enum FormatError {
75 FORMAT_ERROR_NONE,
76 FORMAT_ERROR_UNKNOWN,
77 FORMAT_ERROR_INTERNAL,
78 FORMAT_ERROR_INVALID_DEVICE_PATH,
79 FORMAT_ERROR_DEVICE_BEING_FORMATTED,
80 FORMAT_ERROR_UNSUPPORTED_FILESYSTEM,
81 FORMAT_ERROR_FORMAT_PROGRAM_NOT_FOUND,
82 FORMAT_ERROR_FORMAT_PROGRAM_FAILED,
83 FORMAT_ERROR_DEVICE_NOT_ALLOWED,
84};
85
[email protected]4ae73292011-11-15 05:20:1886// Event type each corresponding to a signal sent from cros-disks.
87enum MountEventType {
[email protected]e3c1fc92012-11-15 00:56:4688 CROS_DISKS_DISK_ADDED,
89 CROS_DISKS_DISK_REMOVED,
90 CROS_DISKS_DISK_CHANGED,
91 CROS_DISKS_DEVICE_ADDED,
92 CROS_DISKS_DEVICE_REMOVED,
93 CROS_DISKS_DEVICE_SCANNED,
[email protected]4ae73292011-11-15 05:20:1894};
95
[email protected]10795ae2012-10-10 07:33:4996// Additional unmount flags to be added to unmount request.
97enum UnmountOptions {
98 UNMOUNT_OPTIONS_NONE,
99 UNMOUNT_OPTIONS_LAZY, // Do lazy unmount.
100};
101
[email protected]4ae73292011-11-15 05:20:18102// A class to represent information about a disk sent from cros-disks.
[email protected]daf0f6d2013-07-08 06:42:33103class CHROMEOS_EXPORT DiskInfo {
[email protected]4ae73292011-11-15 05:20:18104 public:
105 DiskInfo(const std::string& device_path, dbus::Response* response);
106 ~DiskInfo();
107
108 // Device path. (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27109 const std::string& device_path() const { return device_path_; }
[email protected]4ae73292011-11-15 05:20:18110
111 // Disk mount path. (e.g. /media/removable/VOLUME)
[email protected]85b95a2012012-08-07 18:57:27112 const std::string& mount_path() const { return mount_path_; }
[email protected]4ae73292011-11-15 05:20:18113
114 // Disk system path given by udev.
115 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27116 const std::string& system_path() const { return system_path_; }
[email protected]4ae73292011-11-15 05:20:18117
118 // Is a drive or not. (i.e. true with /dev/sdb, false with /dev/sdb1)
119 bool is_drive() const { return is_drive_; }
120
121 // Does the disk have media content.
122 bool has_media() const { return has_media_; }
123
124 // Is the disk on deveice we booted the machine from.
125 bool on_boot_device() const { return on_boot_device_; }
126
127 // Disk file path (e.g. /dev/sdb).
[email protected]85b95a2012012-08-07 18:57:27128 const std::string& file_path() const { return file_path_; }
[email protected]4ae73292011-11-15 05:20:18129
130 // Disk label.
[email protected]85b95a2012012-08-07 18:57:27131 const std::string& label() const { return label_; }
[email protected]4ae73292011-11-15 05:20:18132
[email protected]202e9fee2012-09-13 20:21:29133 // Vendor ID of the device (e.g. "18d1").
134 const std::string& vendor_id() const { return vendor_id_; }
135
136 // Vendor name of the device (e.g. "Google Inc.").
137 const std::string& vendor_name() const { return vendor_name_; }
138
139 // Product ID of the device (e.g. "4e11").
140 const std::string& product_id() const { return product_id_; }
141
142 // Product name of the device (e.g. "Nexus One").
143 const std::string& product_name() const { return product_name_; }
144
[email protected]4ae73292011-11-15 05:20:18145 // Disk model. (e.g. "TransMemory")
[email protected]85b95a2012012-08-07 18:57:27146 const std::string& drive_label() const { return drive_model_; }
[email protected]4ae73292011-11-15 05:20:18147
148 // Device type. Not working well, yet.
149 DeviceType device_type() const { return device_type_; }
150
151 // Total size of the disk in bytes.
152 uint64 total_size_in_bytes() const { return total_size_in_bytes_; }
153
154 // Is the device read-only.
155 bool is_read_only() const { return is_read_only_; }
156
157 // Returns true if the device should be hidden from the file browser.
158 bool is_hidden() const { return is_hidden_; }
159
[email protected]9c5620d32012-07-31 01:00:38160 // Returns file system uuid.
[email protected]85b95a2012012-08-07 18:57:27161 const std::string& uuid() const { return uuid_; }
[email protected]9c5620d32012-07-31 01:00:38162
[email protected]4ae73292011-11-15 05:20:18163 private:
164 void InitializeFromResponse(dbus::Response* response);
165
166 std::string device_path_;
167 std::string mount_path_;
168 std::string system_path_;
169 bool is_drive_;
170 bool has_media_;
171 bool on_boot_device_;
172
173 std::string file_path_;
174 std::string label_;
[email protected]202e9fee2012-09-13 20:21:29175 std::string vendor_id_;
176 std::string vendor_name_;
177 std::string product_id_;
178 std::string product_name_;
[email protected]4ae73292011-11-15 05:20:18179 std::string drive_model_;
180 DeviceType device_type_;
181 uint64 total_size_in_bytes_;
182 bool is_read_only_;
183 bool is_hidden_;
[email protected]9c5620d32012-07-31 01:00:38184 std::string uuid_;
[email protected]4ae73292011-11-15 05:20:18185};
186
187// A class to make the actual DBus calls for cros-disks service.
188// This class only makes calls, result/error handling should be done
189// by callbacks.
[email protected]c5fd5362013-08-27 12:23:04190class CHROMEOS_EXPORT CrosDisksClient : public DBusClient {
[email protected]4ae73292011-11-15 05:20:18191 public:
[email protected]4ae73292011-11-15 05:20:18192 // A callback to handle the result of EnumerateAutoMountableDevices.
193 // The argument is the enumerated device paths.
[email protected]a0278d52014-05-06 03:36:15194 typedef base::Callback<void(const std::vector<std::string>& device_paths)>
195 EnumerateAutoMountableDevicesCallback;
[email protected]4ae73292011-11-15 05:20:18196
[email protected]4ae73292011-11-15 05:20:18197 // A callback to handle the result of GetDeviceProperties.
198 // The argument is the information about the specified device.
[email protected]a0278d52014-05-06 03:36:15199 typedef base::Callback<void(const DiskInfo& disk_info)>
200 GetDevicePropertiesCallback;
[email protected]4ae73292011-11-15 05:20:18201
202 // A callback to handle MountCompleted signal.
203 // The first argument is the error code.
204 // The second argument is the source path.
205 // The third argument is the mount type.
206 // The fourth argument is the mount path.
[email protected]85b95a2012012-08-07 18:57:27207 typedef base::Callback<void(MountError error_code,
208 const std::string& source_path,
209 MountType mount_type,
[email protected]a0278d52014-05-06 03:36:15210 const std::string& mount_path)>
211 MountCompletedHandler;
212
213 // A callback to handle FormatCompleted signal.
214 // The first argument is the error code.
215 // The second argument is the device path.
216 typedef base::Callback<void(FormatError error_code,
217 const std::string& device_path)>
218 FormatCompletedHandler;
[email protected]4ae73292011-11-15 05:20:18219
220 // A callback to handle mount events.
221 // The first argument is the event type.
222 // The second argument is the device path.
[email protected]85b95a2012012-08-07 18:57:27223 typedef base::Callback<void(MountEventType event_type,
[email protected]a0278d52014-05-06 03:36:15224 const std::string& device_path)>
225 MountEventHandler;
[email protected]4ae73292011-11-15 05:20:18226
227 virtual ~CrosDisksClient();
228
229 // Calls Mount method. |callback| is called after the method call succeeds,
230 // otherwise, |error_callback| is called.
[email protected]dcad8fc2012-04-30 23:31:33231 // When mounting an archive, caller may set two optional arguments:
232 // - The |source_format| argument passes the file extension (with the leading
233 // dot, for example ".zip"). If |source_format| is empty then the source
234 // format is auto-detected.
235 // - The |mount_label| argument passes an optional mount label to be used as
236 // the directory name of the mount point. If |mount_label| is empty, the
237 // mount label will be based on the |source_path|.
[email protected]4ae73292011-11-15 05:20:18238 virtual void Mount(const std::string& source_path,
[email protected]b9f22d12012-04-25 21:46:48239 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33240 const std::string& mount_label,
[email protected]5624a252013-07-04 03:17:53241 const base::Closure& callback,
242 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18243
244 // Calls Unmount method. |callback| is called after the method call succeeds,
245 // otherwise, |error_callback| is called.
246 virtual void Unmount(const std::string& device_path,
[email protected]10795ae2012-10-10 07:33:49247 UnmountOptions options,
[email protected]ffdcc7a9c2013-07-02 06:59:39248 const base::Closure& callback,
249 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18250
251 // Calls EnumerateAutoMountableDevices method. |callback| is called after the
252 // method call succeeds, otherwise, |error_callback| is called.
253 virtual void EnumerateAutoMountableDevices(
[email protected]4a404e52012-04-11 02:25:35254 const EnumerateAutoMountableDevicesCallback& callback,
[email protected]5624a252013-07-04 03:17:53255 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18256
[email protected]f026c0f2014-05-06 21:52:35257 // Calls Format method. |callback| is called after the method call succeeds,
258 // otherwise, |error_callback| is called.
259 virtual void Format(const std::string& device_path,
260 const std::string& filesystem,
261 const base::Closure& callback,
262 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18263
264 // Calls GetDeviceProperties method. |callback| is called after the method
265 // call succeeds, otherwise, |error_callback| is called.
266 virtual void GetDeviceProperties(const std::string& device_path,
[email protected]4a404e52012-04-11 02:25:35267 const GetDevicePropertiesCallback& callback,
[email protected]5624a252013-07-04 03:17:53268 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18269
[email protected]a0278d52014-05-06 03:36:15270 // Registers |mount_event_handler| as a callback to be invoked when a mount
271 // event signal is received.
272 virtual void SetMountEventHandler(
273 const MountEventHandler& mount_event_handler) = 0;
274
275 // Registers |mount_completed_handler| as a callback to be invoked when a
276 // MountCompleted signal is received.
277 virtual void SetMountCompletedHandler(
[email protected]4a404e52012-04-11 02:25:35278 const MountCompletedHandler& mount_completed_handler) = 0;
[email protected]4ae73292011-11-15 05:20:18279
[email protected]a0278d52014-05-06 03:36:15280 // Registers |format_completed_handler| as a callback to be invoked when a
281 // FormatCompleted signal is received.
282 virtual void SetFormatCompletedHandler(
283 const FormatCompletedHandler& format_completed_handler) = 0;
284
[email protected]4ae73292011-11-15 05:20:18285 // Factory function, creates a new instance and returns ownership.
286 // For normal usage, access the singleton via DBusThreadManager::Get().
[email protected]c5fd5362013-08-27 12:23:04287 static CrosDisksClient* Create(DBusClientImplementationType type);
[email protected]4ae73292011-11-15 05:20:18288
[email protected]a5a8b412013-03-04 15:03:11289 // Returns the path of the mount point for archive files.
290 static base::FilePath GetArchiveMountPoint();
291
292 // Returns the path of the mount point for removable disks.
293 static base::FilePath GetRemovableDiskMountPoint();
294
[email protected]4ae73292011-11-15 05:20:18295 protected:
296 // Create() should be used instead.
297 CrosDisksClient();
298
299 private:
300 DISALLOW_COPY_AND_ASSIGN(CrosDisksClient);
301};
302
303} // namespace chromeos
304
[email protected]64e199252012-04-06 01:54:36305#endif // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_