blob: 8b80ec65f350427d6b2bc06c05a0cdfaa529fd8c [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
avi6e1a22d2015-12-21 03:43:208#include <stdint.h>
9
[email protected]4ae73292011-11-15 05:20:1810#include <string>
11#include <vector>
12
[email protected]5624a252013-07-04 03:17:5313#include "base/callback_forward.h"
avi6e1a22d2015-12-21 03:43:2014#include "base/macros.h"
[email protected]64e199252012-04-06 01:54:3615#include "chromeos/chromeos_export.h"
[email protected]c5fd5362013-08-27 12:23:0416#include "chromeos/dbus/dbus_client.h"
jamescook9be05a42016-09-19 19:09:4917#include "chromeos/dbus/dbus_client_implementation_type.h"
[email protected]4ae73292011-11-15 05:20:1818
[email protected]a5a8b412013-03-04 15:03:1119namespace base {
20class FilePath;
21}
22
[email protected]4ae73292011-11-15 05:20:1823namespace dbus {
[email protected]4ae73292011-11-15 05:20:1824class Response;
25}
26
[email protected]e3c1fc92012-11-15 00:56:4627// TODO(tbarzic): We should move these enums inside CrosDisksClient,
28// to be clearer where they come from. Also, most of these are partially or
29// completely duplicated in third_party/dbus/service_constants.h. We should
30// probably use enums from service_contstants directly.
[email protected]4ae73292011-11-15 05:20:1831namespace chromeos {
32
33// Enum describing types of mount used by cros-disks.
34enum MountType {
35 MOUNT_TYPE_INVALID,
36 MOUNT_TYPE_DEVICE,
37 MOUNT_TYPE_ARCHIVE,
[email protected]4ae73292011-11-15 05:20:1838};
39
40// Type of device.
41enum DeviceType {
[email protected]2321d282012-01-31 23:06:5942 DEVICE_TYPE_UNKNOWN,
43 DEVICE_TYPE_USB, // USB stick.
44 DEVICE_TYPE_SD, // SD card.
[email protected]f4ae40ac2012-05-04 21:57:0045 DEVICE_TYPE_OPTICAL_DISC, // e.g. Optical disc excluding DVD.
46 DEVICE_TYPE_MOBILE, // Storage on a mobile device (e.g. Android).
47 DEVICE_TYPE_DVD, // DVD.
[email protected]4ae73292011-11-15 05:20:1848};
49
50// Mount error code used by cros-disks.
51enum MountError {
52 MOUNT_ERROR_NONE = 0,
53 MOUNT_ERROR_UNKNOWN = 1,
54 MOUNT_ERROR_INTERNAL = 2,
[email protected]e3c1fc92012-11-15 00:56:4655 MOUNT_ERROR_INVALID_ARGUMENT = 3,
56 MOUNT_ERROR_INVALID_PATH = 4,
57 MOUNT_ERROR_PATH_ALREADY_MOUNTED = 5,
58 MOUNT_ERROR_PATH_NOT_MOUNTED = 6,
59 MOUNT_ERROR_DIRECTORY_CREATION_FAILED = 7,
60 MOUNT_ERROR_INVALID_MOUNT_OPTIONS = 8,
61 MOUNT_ERROR_INVALID_UNMOUNT_OPTIONS = 9,
62 MOUNT_ERROR_INSUFFICIENT_PERMISSIONS = 10,
63 MOUNT_ERROR_MOUNT_PROGRAM_NOT_FOUND = 11,
64 MOUNT_ERROR_MOUNT_PROGRAM_FAILED = 12,
65 MOUNT_ERROR_INVALID_DEVICE_PATH = 100,
[email protected]4ae73292011-11-15 05:20:1866 MOUNT_ERROR_UNKNOWN_FILESYSTEM = 101,
[email protected]a66a23cb2012-06-19 23:15:3367 MOUNT_ERROR_UNSUPPORTED_FILESYSTEM = 102,
[email protected]4ae73292011-11-15 05:20:1868 MOUNT_ERROR_INVALID_ARCHIVE = 201,
[email protected]9bb24222012-02-09 02:00:4369 MOUNT_ERROR_NOT_AUTHENTICATED = 601,
[email protected]4ae73292011-11-15 05:20:1870 MOUNT_ERROR_PATH_UNMOUNTED = 901,
71 // TODO(tbarzic): Add more error codes as they get added to cros-disks and
72 // consider doing explicit translation from cros-disks error_types.
73};
74
Klemen Kozjekbf5610f2017-08-25 20:20:0975// Rename error reported by cros-disks.
76enum RenameError {
77 RENAME_ERROR_NONE,
78 RENAME_ERROR_UNKNOWN,
79 RENAME_ERROR_INTERNAL,
80 RENAME_ERROR_INVALID_DEVICE_PATH,
81 RENAME_ERROR_DEVICE_BEING_RENAMED,
82 RENAME_ERROR_UNSUPPORTED_FILESYSTEM,
83 RENAME_ERROR_RENAME_PROGRAM_NOT_FOUND,
84 RENAME_ERROR_RENAME_PROGRAM_FAILED,
85 RENAME_ERROR_DEVICE_NOT_ALLOWED,
86 RENAME_ERROR_LONG_NAME,
87 RENAME_ERROR_INVALID_CHARACTER,
88};
89
[email protected]e3c1fc92012-11-15 00:56:4690// Format error reported by cros-disks.
91enum FormatError {
92 FORMAT_ERROR_NONE,
93 FORMAT_ERROR_UNKNOWN,
94 FORMAT_ERROR_INTERNAL,
95 FORMAT_ERROR_INVALID_DEVICE_PATH,
96 FORMAT_ERROR_DEVICE_BEING_FORMATTED,
97 FORMAT_ERROR_UNSUPPORTED_FILESYSTEM,
98 FORMAT_ERROR_FORMAT_PROGRAM_NOT_FOUND,
99 FORMAT_ERROR_FORMAT_PROGRAM_FAILED,
100 FORMAT_ERROR_DEVICE_NOT_ALLOWED,
101};
102
[email protected]4ae73292011-11-15 05:20:18103// Event type each corresponding to a signal sent from cros-disks.
104enum MountEventType {
[email protected]e3c1fc92012-11-15 00:56:46105 CROS_DISKS_DISK_ADDED,
106 CROS_DISKS_DISK_REMOVED,
107 CROS_DISKS_DISK_CHANGED,
108 CROS_DISKS_DEVICE_ADDED,
109 CROS_DISKS_DEVICE_REMOVED,
110 CROS_DISKS_DEVICE_SCANNED,
[email protected]4ae73292011-11-15 05:20:18111};
112
[email protected]10795ae2012-10-10 07:33:49113// Additional unmount flags to be added to unmount request.
114enum UnmountOptions {
115 UNMOUNT_OPTIONS_NONE,
116 UNMOUNT_OPTIONS_LAZY, // Do lazy unmount.
117};
118
yamaguchi585d5402016-08-02 09:27:36119// Mount option to control write permission to a device.
120enum MountAccessMode {
121 MOUNT_ACCESS_MODE_READ_WRITE,
122 MOUNT_ACCESS_MODE_READ_ONLY,
123};
124
yamaguchifa8efc72016-10-21 15:05:51125// Whether to mount to a new path or remount a device already mounted.
126enum RemountOption {
127 // Mount a new device. If the device is already mounted, the mount status is
128 // unchanged and the callback for MountCompleted will receive
129 // MOUNT_ERROR_PATH_ALREADY_MOUNTED error code.
130 REMOUNT_OPTION_MOUNT_NEW_DEVICE,
131 // Remount a device that is already mounted. If the device is not mounted
132 // yet, it will do nothing and the callback for MountCompleted will receive
133 // MOUNT_ERROR_PATH_NOT_MOUNTED error code.
134 REMOUNT_OPTION_REMOUNT_EXISTING_DEVICE,
135};
136
[email protected]4ae73292011-11-15 05:20:18137// A class to represent information about a disk sent from cros-disks.
[email protected]daf0f6d2013-07-08 06:42:33138class CHROMEOS_EXPORT DiskInfo {
[email protected]4ae73292011-11-15 05:20:18139 public:
140 DiskInfo(const std::string& device_path, dbus::Response* response);
141 ~DiskInfo();
142
143 // Device path. (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27144 const std::string& device_path() const { return device_path_; }
[email protected]4ae73292011-11-15 05:20:18145
146 // Disk mount path. (e.g. /media/removable/VOLUME)
[email protected]85b95a2012012-08-07 18:57:27147 const std::string& mount_path() const { return mount_path_; }
[email protected]4ae73292011-11-15 05:20:18148
149 // Disk system path given by udev.
150 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27151 const std::string& system_path() const { return system_path_; }
[email protected]4ae73292011-11-15 05:20:18152
153 // Is a drive or not. (i.e. true with /dev/sdb, false with /dev/sdb1)
154 bool is_drive() const { return is_drive_; }
155
156 // Does the disk have media content.
157 bool has_media() const { return has_media_; }
158
[email protected]d7760592014-05-16 07:57:52159 // Is the disk on device we booted the machine from.
[email protected]4ae73292011-11-15 05:20:18160 bool on_boot_device() const { return on_boot_device_; }
161
[email protected]79ed457b2014-07-22 04:07:26162 // Is the disk on a removable device.
163 bool on_removable_device() const { return on_removable_device_; }
164
[email protected]4ae73292011-11-15 05:20:18165 // Disk file path (e.g. /dev/sdb).
[email protected]85b95a2012012-08-07 18:57:27166 const std::string& file_path() const { return file_path_; }
[email protected]4ae73292011-11-15 05:20:18167
168 // Disk label.
[email protected]85b95a2012012-08-07 18:57:27169 const std::string& label() const { return label_; }
[email protected]4ae73292011-11-15 05:20:18170
[email protected]202e9fee2012-09-13 20:21:29171 // Vendor ID of the device (e.g. "18d1").
172 const std::string& vendor_id() const { return vendor_id_; }
173
174 // Vendor name of the device (e.g. "Google Inc.").
175 const std::string& vendor_name() const { return vendor_name_; }
176
177 // Product ID of the device (e.g. "4e11").
178 const std::string& product_id() const { return product_id_; }
179
180 // Product name of the device (e.g. "Nexus One").
181 const std::string& product_name() const { return product_name_; }
182
[email protected]4ae73292011-11-15 05:20:18183 // Disk model. (e.g. "TransMemory")
[email protected]85b95a2012012-08-07 18:57:27184 const std::string& drive_label() const { return drive_model_; }
[email protected]4ae73292011-11-15 05:20:18185
186 // Device type. Not working well, yet.
187 DeviceType device_type() const { return device_type_; }
188
189 // Total size of the disk in bytes.
avi6e1a22d2015-12-21 03:43:20190 uint64_t total_size_in_bytes() const { return total_size_in_bytes_; }
[email protected]4ae73292011-11-15 05:20:18191
192 // Is the device read-only.
193 bool is_read_only() const { return is_read_only_; }
194
195 // Returns true if the device should be hidden from the file browser.
196 bool is_hidden() const { return is_hidden_; }
197
[email protected]9c5620d32012-07-31 01:00:38198 // Returns file system uuid.
[email protected]85b95a2012012-08-07 18:57:27199 const std::string& uuid() const { return uuid_; }
[email protected]9c5620d32012-07-31 01:00:38200
Klemen Kozjek46f1c6192017-08-25 19:20:54201 // Returns file system type identifier.
202 const std::string& file_system_type() const { return file_system_type_; }
203
[email protected]4ae73292011-11-15 05:20:18204 private:
205 void InitializeFromResponse(dbus::Response* response);
206
207 std::string device_path_;
208 std::string mount_path_;
209 std::string system_path_;
210 bool is_drive_;
211 bool has_media_;
212 bool on_boot_device_;
[email protected]79ed457b2014-07-22 04:07:26213 bool on_removable_device_;
[email protected]4ae73292011-11-15 05:20:18214
215 std::string file_path_;
216 std::string label_;
[email protected]202e9fee2012-09-13 20:21:29217 std::string vendor_id_;
218 std::string vendor_name_;
219 std::string product_id_;
220 std::string product_name_;
[email protected]4ae73292011-11-15 05:20:18221 std::string drive_model_;
222 DeviceType device_type_;
avi6e1a22d2015-12-21 03:43:20223 uint64_t total_size_in_bytes_;
[email protected]4ae73292011-11-15 05:20:18224 bool is_read_only_;
225 bool is_hidden_;
[email protected]9c5620d32012-07-31 01:00:38226 std::string uuid_;
Klemen Kozjek46f1c6192017-08-25 19:20:54227 std::string file_system_type_;
[email protected]4ae73292011-11-15 05:20:18228};
229
[email protected]d7760592014-05-16 07:57:52230// A struct to represent information about a mount point sent from cros-disks.
231struct CHROMEOS_EXPORT MountEntry {
232 public:
233 MountEntry()
234 : error_code_(MOUNT_ERROR_UNKNOWN), mount_type_(MOUNT_TYPE_INVALID) {
235 }
236
237 MountEntry(MountError error_code,
238 const std::string& source_path,
239 MountType mount_type,
240 const std::string& mount_path)
241 : error_code_(error_code),
242 source_path_(source_path),
243 mount_type_(mount_type),
244 mount_path_(mount_path) {
245 }
246
247 MountError error_code() const { return error_code_; }
248 const std::string& source_path() const { return source_path_; }
249 MountType mount_type() const { return mount_type_; }
250 const std::string& mount_path() const { return mount_path_; }
251
252 private:
253 MountError error_code_;
254 std::string source_path_;
255 MountType mount_type_;
256 std::string mount_path_;
257};
258
[email protected]4ae73292011-11-15 05:20:18259// A class to make the actual DBus calls for cros-disks service.
260// This class only makes calls, result/error handling should be done
261// by callbacks.
[email protected]c5fd5362013-08-27 12:23:04262class CHROMEOS_EXPORT CrosDisksClient : public DBusClient {
[email protected]4ae73292011-11-15 05:20:18263 public:
[email protected]4ae73292011-11-15 05:20:18264 // A callback to handle the result of EnumerateAutoMountableDevices.
265 // The argument is the enumerated device paths.
[email protected]a0278d52014-05-06 03:36:15266 typedef base::Callback<void(const std::vector<std::string>& device_paths)>
267 EnumerateAutoMountableDevicesCallback;
[email protected]4ae73292011-11-15 05:20:18268
[email protected]d7760592014-05-16 07:57:52269 // A callback to handle the result of EnumerateMountEntries.
270 // The argument is the enumerated mount entries.
271 typedef base::Callback<void(const std::vector<MountEntry>& entries)>
272 EnumerateMountEntriesCallback;
273
[email protected]4ae73292011-11-15 05:20:18274 // A callback to handle the result of GetDeviceProperties.
275 // The argument is the information about the specified device.
[email protected]a0278d52014-05-06 03:36:15276 typedef base::Callback<void(const DiskInfo& disk_info)>
277 GetDevicePropertiesCallback;
[email protected]4ae73292011-11-15 05:20:18278
279 // A callback to handle MountCompleted signal.
[email protected]d7760592014-05-16 07:57:52280 typedef base::Callback<void(const MountEntry& entry)> MountCompletedHandler;
[email protected]a0278d52014-05-06 03:36:15281
282 // A callback to handle FormatCompleted signal.
283 // The first argument is the error code.
284 // The second argument is the device path.
285 typedef base::Callback<void(FormatError error_code,
286 const std::string& device_path)>
287 FormatCompletedHandler;
[email protected]4ae73292011-11-15 05:20:18288
Klemen Kozjekbf5610f2017-08-25 20:20:09289 // A callback to handle RenameCompleted signal.
290 // The first argument is the error code.
291 // The second argument is the device path.
292 typedef base::Callback<void(RenameError error_code,
293 const std::string& device_path)>
294 RenameCompletedHandler;
295
[email protected]4ae73292011-11-15 05:20:18296 // A callback to handle mount events.
297 // The first argument is the event type.
298 // The second argument is the device path.
[email protected]85b95a2012012-08-07 18:57:27299 typedef base::Callback<void(MountEventType event_type,
[email protected]a0278d52014-05-06 03:36:15300 const std::string& device_path)>
301 MountEventHandler;
[email protected]4ae73292011-11-15 05:20:18302
dcheng0280cb62015-01-16 07:37:50303 ~CrosDisksClient() override;
[email protected]4ae73292011-11-15 05:20:18304
305 // Calls Mount method. |callback| is called after the method call succeeds,
306 // otherwise, |error_callback| is called.
[email protected]dcad8fc2012-04-30 23:31:33307 // When mounting an archive, caller may set two optional arguments:
308 // - The |source_format| argument passes the file extension (with the leading
309 // dot, for example ".zip"). If |source_format| is empty then the source
310 // format is auto-detected.
311 // - The |mount_label| argument passes an optional mount label to be used as
312 // the directory name of the mount point. If |mount_label| is empty, the
313 // mount label will be based on the |source_path|.
[email protected]4ae73292011-11-15 05:20:18314 virtual void Mount(const std::string& source_path,
[email protected]b9f22d12012-04-25 21:46:48315 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33316 const std::string& mount_label,
yamaguchi585d5402016-08-02 09:27:36317 MountAccessMode access_mode,
yamaguchifa8efc72016-10-21 15:05:51318 RemountOption remount,
[email protected]5624a252013-07-04 03:17:53319 const base::Closure& callback,
320 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18321
322 // Calls Unmount method. |callback| is called after the method call succeeds,
323 // otherwise, |error_callback| is called.
324 virtual void Unmount(const std::string& device_path,
[email protected]10795ae2012-10-10 07:33:49325 UnmountOptions options,
[email protected]ffdcc7a9c2013-07-02 06:59:39326 const base::Closure& callback,
327 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18328
329 // Calls EnumerateAutoMountableDevices method. |callback| is called after the
330 // method call succeeds, otherwise, |error_callback| is called.
331 virtual void EnumerateAutoMountableDevices(
[email protected]4a404e52012-04-11 02:25:35332 const EnumerateAutoMountableDevicesCallback& callback,
[email protected]5624a252013-07-04 03:17:53333 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18334
[email protected]d7760592014-05-16 07:57:52335 // Calls EnumerateMountEntries. |callback| is called after the
336 // method call succeeds, otherwise, |error_callback| is called.
337 virtual void EnumerateMountEntries(
338 const EnumerateMountEntriesCallback& callback,
339 const base::Closure& error_callback) = 0;
340
[email protected]f026c0f2014-05-06 21:52:35341 // Calls Format method. |callback| is called after the method call succeeds,
342 // otherwise, |error_callback| is called.
343 virtual void Format(const std::string& device_path,
344 const std::string& filesystem,
345 const base::Closure& callback,
346 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18347
Klemen Kozjekbf5610f2017-08-25 20:20:09348 // Calls Rename method. |callback| is called after the method call succeeds,
349 // otherwise, |error_callback| is called.
350 virtual void Rename(const std::string& device_path,
351 const std::string& volume_name,
352 const base::Closure& callback,
353 const base::Closure& error_callback) = 0;
354
[email protected]4ae73292011-11-15 05:20:18355 // Calls GetDeviceProperties method. |callback| is called after the method
356 // call succeeds, otherwise, |error_callback| is called.
357 virtual void GetDeviceProperties(const std::string& device_path,
[email protected]4a404e52012-04-11 02:25:35358 const GetDevicePropertiesCallback& callback,
[email protected]5624a252013-07-04 03:17:53359 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18360
[email protected]a0278d52014-05-06 03:36:15361 // Registers |mount_event_handler| as a callback to be invoked when a mount
362 // event signal is received.
363 virtual void SetMountEventHandler(
364 const MountEventHandler& mount_event_handler) = 0;
365
366 // Registers |mount_completed_handler| as a callback to be invoked when a
367 // MountCompleted signal is received.
368 virtual void SetMountCompletedHandler(
[email protected]4a404e52012-04-11 02:25:35369 const MountCompletedHandler& mount_completed_handler) = 0;
[email protected]4ae73292011-11-15 05:20:18370
[email protected]a0278d52014-05-06 03:36:15371 // Registers |format_completed_handler| as a callback to be invoked when a
372 // FormatCompleted signal is received.
373 virtual void SetFormatCompletedHandler(
374 const FormatCompletedHandler& format_completed_handler) = 0;
375
Klemen Kozjekbf5610f2017-08-25 20:20:09376 // Registers |rename_completed_handler| as a callback to be invoked when a
377 // RenameCompleted signal is received.
378 virtual void SetRenameCompletedHandler(
379 const RenameCompletedHandler& rename_completed_handler) = 0;
380
[email protected]4ae73292011-11-15 05:20:18381 // Factory function, creates a new instance and returns ownership.
382 // For normal usage, access the singleton via DBusThreadManager::Get().
[email protected]c5fd5362013-08-27 12:23:04383 static CrosDisksClient* Create(DBusClientImplementationType type);
[email protected]4ae73292011-11-15 05:20:18384
[email protected]a5a8b412013-03-04 15:03:11385 // Returns the path of the mount point for archive files.
386 static base::FilePath GetArchiveMountPoint();
387
388 // Returns the path of the mount point for removable disks.
389 static base::FilePath GetRemovableDiskMountPoint();
390
yamaguchi585d5402016-08-02 09:27:36391 // Composes a list of mount options.
392 static std::vector<std::string> ComposeMountOptions(
393 const std::string& mount_label,
yamaguchifa8efc72016-10-21 15:05:51394 MountAccessMode access_mode,
395 RemountOption remount);
yamaguchi585d5402016-08-02 09:27:36396
[email protected]4ae73292011-11-15 05:20:18397 protected:
398 // Create() should be used instead.
399 CrosDisksClient();
400
401 private:
402 DISALLOW_COPY_AND_ASSIGN(CrosDisksClient);
403};
404
405} // namespace chromeos
406
[email protected]64e199252012-04-06 01:54:36407#endif // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_