blob: c77a8d6d2f46ed1f533385e920062eb5cba16f54 [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"
Hidehiko Abec293cdcf2018-02-08 02:10:4518#include "chromeos/dbus/dbus_method_call_status.h"
[email protected]4ae73292011-11-15 05:20:1819
[email protected]a5a8b412013-03-04 15:03:1120namespace base {
21class FilePath;
22}
23
[email protected]4ae73292011-11-15 05:20:1824namespace dbus {
[email protected]4ae73292011-11-15 05:20:1825class Response;
26}
27
[email protected]e3c1fc92012-11-15 00:56:4628// TODO(tbarzic): We should move these enums inside CrosDisksClient,
29// to be clearer where they come from. Also, most of these are partially or
30// completely duplicated in third_party/dbus/service_constants.h. We should
31// probably use enums from service_contstants directly.
[email protected]4ae73292011-11-15 05:20:1832namespace chromeos {
33
34// Enum describing types of mount used by cros-disks.
35enum MountType {
36 MOUNT_TYPE_INVALID,
37 MOUNT_TYPE_DEVICE,
38 MOUNT_TYPE_ARCHIVE,
Sam McNally785315a2018-05-14 00:12:3939 MOUNT_TYPE_NETWORK_STORAGE,
[email protected]4ae73292011-11-15 05:20:1840};
41
42// Type of device.
43enum DeviceType {
[email protected]2321d282012-01-31 23:06:5944 DEVICE_TYPE_UNKNOWN,
45 DEVICE_TYPE_USB, // USB stick.
46 DEVICE_TYPE_SD, // SD card.
[email protected]f4ae40ac2012-05-04 21:57:0047 DEVICE_TYPE_OPTICAL_DISC, // e.g. Optical disc excluding DVD.
48 DEVICE_TYPE_MOBILE, // Storage on a mobile device (e.g. Android).
49 DEVICE_TYPE_DVD, // DVD.
[email protected]4ae73292011-11-15 05:20:1850};
51
52// Mount error code used by cros-disks.
53enum MountError {
54 MOUNT_ERROR_NONE = 0,
55 MOUNT_ERROR_UNKNOWN = 1,
56 MOUNT_ERROR_INTERNAL = 2,
[email protected]e3c1fc92012-11-15 00:56:4657 MOUNT_ERROR_INVALID_ARGUMENT = 3,
58 MOUNT_ERROR_INVALID_PATH = 4,
59 MOUNT_ERROR_PATH_ALREADY_MOUNTED = 5,
60 MOUNT_ERROR_PATH_NOT_MOUNTED = 6,
61 MOUNT_ERROR_DIRECTORY_CREATION_FAILED = 7,
62 MOUNT_ERROR_INVALID_MOUNT_OPTIONS = 8,
63 MOUNT_ERROR_INVALID_UNMOUNT_OPTIONS = 9,
64 MOUNT_ERROR_INSUFFICIENT_PERMISSIONS = 10,
65 MOUNT_ERROR_MOUNT_PROGRAM_NOT_FOUND = 11,
66 MOUNT_ERROR_MOUNT_PROGRAM_FAILED = 12,
67 MOUNT_ERROR_INVALID_DEVICE_PATH = 100,
[email protected]4ae73292011-11-15 05:20:1868 MOUNT_ERROR_UNKNOWN_FILESYSTEM = 101,
[email protected]a66a23cb2012-06-19 23:15:3369 MOUNT_ERROR_UNSUPPORTED_FILESYSTEM = 102,
[email protected]4ae73292011-11-15 05:20:1870 MOUNT_ERROR_INVALID_ARCHIVE = 201,
[email protected]9bb24222012-02-09 02:00:4371 MOUNT_ERROR_NOT_AUTHENTICATED = 601,
[email protected]4ae73292011-11-15 05:20:1872 MOUNT_ERROR_PATH_UNMOUNTED = 901,
73 // TODO(tbarzic): Add more error codes as they get added to cros-disks and
74 // consider doing explicit translation from cros-disks error_types.
75};
76
Klemen Kozjekbf5610f2017-08-25 20:20:0977// Rename error reported by cros-disks.
78enum RenameError {
79 RENAME_ERROR_NONE,
80 RENAME_ERROR_UNKNOWN,
81 RENAME_ERROR_INTERNAL,
82 RENAME_ERROR_INVALID_DEVICE_PATH,
83 RENAME_ERROR_DEVICE_BEING_RENAMED,
84 RENAME_ERROR_UNSUPPORTED_FILESYSTEM,
85 RENAME_ERROR_RENAME_PROGRAM_NOT_FOUND,
86 RENAME_ERROR_RENAME_PROGRAM_FAILED,
87 RENAME_ERROR_DEVICE_NOT_ALLOWED,
88 RENAME_ERROR_LONG_NAME,
89 RENAME_ERROR_INVALID_CHARACTER,
90};
91
[email protected]e3c1fc92012-11-15 00:56:4692// Format error reported by cros-disks.
93enum FormatError {
94 FORMAT_ERROR_NONE,
95 FORMAT_ERROR_UNKNOWN,
96 FORMAT_ERROR_INTERNAL,
97 FORMAT_ERROR_INVALID_DEVICE_PATH,
98 FORMAT_ERROR_DEVICE_BEING_FORMATTED,
99 FORMAT_ERROR_UNSUPPORTED_FILESYSTEM,
100 FORMAT_ERROR_FORMAT_PROGRAM_NOT_FOUND,
101 FORMAT_ERROR_FORMAT_PROGRAM_FAILED,
102 FORMAT_ERROR_DEVICE_NOT_ALLOWED,
103};
104
[email protected]4ae73292011-11-15 05:20:18105// Event type each corresponding to a signal sent from cros-disks.
106enum MountEventType {
[email protected]e3c1fc92012-11-15 00:56:46107 CROS_DISKS_DISK_ADDED,
108 CROS_DISKS_DISK_REMOVED,
109 CROS_DISKS_DISK_CHANGED,
110 CROS_DISKS_DEVICE_ADDED,
111 CROS_DISKS_DEVICE_REMOVED,
112 CROS_DISKS_DEVICE_SCANNED,
[email protected]4ae73292011-11-15 05:20:18113};
114
[email protected]10795ae2012-10-10 07:33:49115// Additional unmount flags to be added to unmount request.
116enum UnmountOptions {
117 UNMOUNT_OPTIONS_NONE,
118 UNMOUNT_OPTIONS_LAZY, // Do lazy unmount.
119};
120
yamaguchi585d5402016-08-02 09:27:36121// Mount option to control write permission to a device.
122enum MountAccessMode {
123 MOUNT_ACCESS_MODE_READ_WRITE,
124 MOUNT_ACCESS_MODE_READ_ONLY,
125};
126
yamaguchifa8efc72016-10-21 15:05:51127// Whether to mount to a new path or remount a device already mounted.
128enum RemountOption {
129 // Mount a new device. If the device is already mounted, the mount status is
130 // unchanged and the callback for MountCompleted will receive
131 // MOUNT_ERROR_PATH_ALREADY_MOUNTED error code.
132 REMOUNT_OPTION_MOUNT_NEW_DEVICE,
133 // Remount a device that is already mounted. If the device is not mounted
134 // yet, it will do nothing and the callback for MountCompleted will receive
135 // MOUNT_ERROR_PATH_NOT_MOUNTED error code.
136 REMOUNT_OPTION_REMOUNT_EXISTING_DEVICE,
137};
138
[email protected]4ae73292011-11-15 05:20:18139// A class to represent information about a disk sent from cros-disks.
[email protected]daf0f6d2013-07-08 06:42:33140class CHROMEOS_EXPORT DiskInfo {
[email protected]4ae73292011-11-15 05:20:18141 public:
142 DiskInfo(const std::string& device_path, dbus::Response* response);
143 ~DiskInfo();
144
145 // Device path. (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27146 const std::string& device_path() const { return device_path_; }
[email protected]4ae73292011-11-15 05:20:18147
148 // Disk mount path. (e.g. /media/removable/VOLUME)
[email protected]85b95a2012012-08-07 18:57:27149 const std::string& mount_path() const { return mount_path_; }
[email protected]4ae73292011-11-15 05:20:18150
151 // Disk system path given by udev.
152 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27153 const std::string& system_path() const { return system_path_; }
[email protected]4ae73292011-11-15 05:20:18154
155 // Is a drive or not. (i.e. true with /dev/sdb, false with /dev/sdb1)
156 bool is_drive() const { return is_drive_; }
157
158 // Does the disk have media content.
159 bool has_media() const { return has_media_; }
160
[email protected]d7760592014-05-16 07:57:52161 // Is the disk on device we booted the machine from.
[email protected]4ae73292011-11-15 05:20:18162 bool on_boot_device() const { return on_boot_device_; }
163
[email protected]79ed457b2014-07-22 04:07:26164 // Is the disk on a removable device.
165 bool on_removable_device() const { return on_removable_device_; }
166
Aga Wronskaddc1a752017-12-01 19:44:02167 // Is the device read-only.
168 bool is_read_only() const { return is_read_only_; }
169
170 // Returns true if the device should be hidden from the file browser.
171 bool is_hidden() const { return is_hidden_; }
172
173 // Is the disk virtual.
174 bool is_virtual() const { return is_virtual_; }
175
[email protected]4ae73292011-11-15 05:20:18176 // Disk file path (e.g. /dev/sdb).
[email protected]85b95a2012012-08-07 18:57:27177 const std::string& file_path() const { return file_path_; }
[email protected]4ae73292011-11-15 05:20:18178
179 // Disk label.
[email protected]85b95a2012012-08-07 18:57:27180 const std::string& label() const { return label_; }
[email protected]4ae73292011-11-15 05:20:18181
[email protected]202e9fee2012-09-13 20:21:29182 // Vendor ID of the device (e.g. "18d1").
183 const std::string& vendor_id() const { return vendor_id_; }
184
185 // Vendor name of the device (e.g. "Google Inc.").
186 const std::string& vendor_name() const { return vendor_name_; }
187
188 // Product ID of the device (e.g. "4e11").
189 const std::string& product_id() const { return product_id_; }
190
191 // Product name of the device (e.g. "Nexus One").
192 const std::string& product_name() const { return product_name_; }
193
[email protected]4ae73292011-11-15 05:20:18194 // Disk model. (e.g. "TransMemory")
[email protected]85b95a2012012-08-07 18:57:27195 const std::string& drive_label() const { return drive_model_; }
[email protected]4ae73292011-11-15 05:20:18196
197 // Device type. Not working well, yet.
198 DeviceType device_type() const { return device_type_; }
199
200 // Total size of the disk in bytes.
avi6e1a22d2015-12-21 03:43:20201 uint64_t total_size_in_bytes() const { return total_size_in_bytes_; }
[email protected]4ae73292011-11-15 05:20:18202
[email protected]9c5620d32012-07-31 01:00:38203 // Returns file system uuid.
[email protected]85b95a2012012-08-07 18:57:27204 const std::string& uuid() const { return uuid_; }
[email protected]9c5620d32012-07-31 01:00:38205
Klemen Kozjek46f1c6192017-08-25 19:20:54206 // Returns file system type identifier.
207 const std::string& file_system_type() const { return file_system_type_; }
208
[email protected]4ae73292011-11-15 05:20:18209 private:
210 void InitializeFromResponse(dbus::Response* response);
211
212 std::string device_path_;
213 std::string mount_path_;
214 std::string system_path_;
215 bool is_drive_;
216 bool has_media_;
217 bool on_boot_device_;
[email protected]79ed457b2014-07-22 04:07:26218 bool on_removable_device_;
Aga Wronskaddc1a752017-12-01 19:44:02219 bool is_read_only_;
220 bool is_hidden_;
221 bool is_virtual_;
[email protected]4ae73292011-11-15 05:20:18222
223 std::string file_path_;
224 std::string label_;
[email protected]202e9fee2012-09-13 20:21:29225 std::string vendor_id_;
226 std::string vendor_name_;
227 std::string product_id_;
228 std::string product_name_;
[email protected]4ae73292011-11-15 05:20:18229 std::string drive_model_;
230 DeviceType device_type_;
avi6e1a22d2015-12-21 03:43:20231 uint64_t total_size_in_bytes_;
[email protected]9c5620d32012-07-31 01:00:38232 std::string uuid_;
Klemen Kozjek46f1c6192017-08-25 19:20:54233 std::string file_system_type_;
[email protected]4ae73292011-11-15 05:20:18234};
235
[email protected]d7760592014-05-16 07:57:52236// A struct to represent information about a mount point sent from cros-disks.
237struct CHROMEOS_EXPORT MountEntry {
238 public:
239 MountEntry()
240 : error_code_(MOUNT_ERROR_UNKNOWN), mount_type_(MOUNT_TYPE_INVALID) {
241 }
242
243 MountEntry(MountError error_code,
244 const std::string& source_path,
245 MountType mount_type,
246 const std::string& mount_path)
247 : error_code_(error_code),
248 source_path_(source_path),
249 mount_type_(mount_type),
250 mount_path_(mount_path) {
251 }
252
253 MountError error_code() const { return error_code_; }
254 const std::string& source_path() const { return source_path_; }
255 MountType mount_type() const { return mount_type_; }
256 const std::string& mount_path() const { return mount_path_; }
257
258 private:
259 MountError error_code_;
260 std::string source_path_;
261 MountType mount_type_;
262 std::string mount_path_;
263};
264
[email protected]4ae73292011-11-15 05:20:18265// A class to make the actual DBus calls for cros-disks service.
266// This class only makes calls, result/error handling should be done
267// by callbacks.
[email protected]c5fd5362013-08-27 12:23:04268class CHROMEOS_EXPORT CrosDisksClient : public DBusClient {
[email protected]4ae73292011-11-15 05:20:18269 public:
Anand K. Mistry24fdc5b2018-07-27 01:08:31270 // A callback to handle the result of EnumerateDevices.
271 // The argument is the enumerated device paths.
[email protected]a0278d52014-05-06 03:36:15272 typedef base::Callback<void(const std::vector<std::string>& device_paths)>
Aga Wronskaddc1a752017-12-01 19:44:02273 EnumerateDevicesCallback;
[email protected]4ae73292011-11-15 05:20:18274
[email protected]d7760592014-05-16 07:57:52275 // A callback to handle the result of EnumerateMountEntries.
276 // The argument is the enumerated mount entries.
277 typedef base::Callback<void(const std::vector<MountEntry>& entries)>
278 EnumerateMountEntriesCallback;
279
[email protected]4ae73292011-11-15 05:20:18280 // A callback to handle the result of GetDeviceProperties.
281 // The argument is the information about the specified device.
[email protected]a0278d52014-05-06 03:36:15282 typedef base::Callback<void(const DiskInfo& disk_info)>
283 GetDevicePropertiesCallback;
[email protected]4ae73292011-11-15 05:20:18284
Hidehiko Abe06ce6dc2017-12-08 19:32:03285 class Observer {
286 public:
287 // Called when a mount event signal is received.
288 virtual void OnMountEvent(MountEventType event_type,
289 const std::string& device_path) = 0;
[email protected]a0278d52014-05-06 03:36:15290
Hidehiko Abe06ce6dc2017-12-08 19:32:03291 // Called when a MountCompleted signal is received.
292 virtual void OnMountCompleted(const MountEntry& entry) = 0;
[email protected]4ae73292011-11-15 05:20:18293
Hidehiko Abe06ce6dc2017-12-08 19:32:03294 // Called when a FormatCompleted signal is received.
295 virtual void OnFormatCompleted(FormatError error_code,
296 const std::string& device_path) = 0;
Klemen Kozjekbf5610f2017-08-25 20:20:09297
Hidehiko Abe06ce6dc2017-12-08 19:32:03298 // Called when a RenameCompleted signal is received.
299 virtual void OnRenameCompleted(RenameError error_code,
300 const std::string& device_path) = 0;
301
302 protected:
303 virtual ~Observer() = default;
304 };
[email protected]4ae73292011-11-15 05:20:18305
dcheng0280cb62015-01-16 07:37:50306 ~CrosDisksClient() override;
[email protected]4ae73292011-11-15 05:20:18307
Hidehiko Abe06ce6dc2017-12-08 19:32:03308 // Registers the given |observer| to listen D-Bus signals.
309 virtual void AddObserver(Observer* observer) = 0;
310
311 // Unregisters the |observer| from this instance.
312 virtual void RemoveObserver(Observer* observer) = 0;
313
Hidehiko Abec293cdcf2018-02-08 02:10:45314 // Calls Mount method. On method call completion, |callback| is called with
315 // |true| on success, or with |false| otherwise.
[email protected]dcad8fc2012-04-30 23:31:33316 // When mounting an archive, caller may set two optional arguments:
317 // - The |source_format| argument passes the file extension (with the leading
318 // dot, for example ".zip"). If |source_format| is empty then the source
319 // format is auto-detected.
320 // - The |mount_label| argument passes an optional mount label to be used as
321 // the directory name of the mount point. If |mount_label| is empty, the
322 // mount label will be based on the |source_path|.
[email protected]4ae73292011-11-15 05:20:18323 virtual void Mount(const std::string& source_path,
[email protected]b9f22d12012-04-25 21:46:48324 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33325 const std::string& mount_label,
Sergei Datsenkod19248182018-05-11 01:52:56326 const std::vector<std::string>& mount_options,
yamaguchi585d5402016-08-02 09:27:36327 MountAccessMode access_mode,
yamaguchifa8efc72016-10-21 15:05:51328 RemountOption remount,
Hidehiko Abec293cdcf2018-02-08 02:10:45329 VoidDBusMethodCallback callback) = 0;
[email protected]4ae73292011-11-15 05:20:18330
Hidehiko Abec293cdcf2018-02-08 02:10:45331 // Calls Unmount method. On method call completion, |callback| is called
332 // with |true| on success, or with |false| otherwise.
[email protected]4ae73292011-11-15 05:20:18333 virtual void Unmount(const std::string& device_path,
[email protected]10795ae2012-10-10 07:33:49334 UnmountOptions options,
Hidehiko Abec293cdcf2018-02-08 02:10:45335 VoidDBusMethodCallback callback) = 0;
[email protected]4ae73292011-11-15 05:20:18336
Aga Wronskaddc1a752017-12-01 19:44:02337 // Calls EnumerateDevices method. |callback| is called after the
338 // method call succeeds, otherwise, |error_callback| is called.
339 virtual void EnumerateDevices(const EnumerateDevicesCallback& callback,
340 const base::Closure& error_callback) = 0;
341
[email protected]d7760592014-05-16 07:57:52342 // Calls EnumerateMountEntries. |callback| is called after the
343 // method call succeeds, otherwise, |error_callback| is called.
344 virtual void EnumerateMountEntries(
345 const EnumerateMountEntriesCallback& callback,
346 const base::Closure& error_callback) = 0;
347
Hidehiko Abec293cdcf2018-02-08 02:10:45348 // Calls Format method. On completion, |callback| is called, with |true| on
349 // success, or with |false| otherwise.
[email protected]f026c0f2014-05-06 21:52:35350 virtual void Format(const std::string& device_path,
351 const std::string& filesystem,
Hidehiko Abec293cdcf2018-02-08 02:10:45352 VoidDBusMethodCallback callback) = 0;
[email protected]4ae73292011-11-15 05:20:18353
Hidehiko Abec293cdcf2018-02-08 02:10:45354 // Calls Rename method. On completion, |callback| is called, with |true| on
355 // success, or with |false| otherwise.
Klemen Kozjekbf5610f2017-08-25 20:20:09356 virtual void Rename(const std::string& device_path,
357 const std::string& volume_name,
Hidehiko Abec293cdcf2018-02-08 02:10:45358 VoidDBusMethodCallback callback) = 0;
Klemen Kozjekbf5610f2017-08-25 20:20:09359
[email protected]4ae73292011-11-15 05:20:18360 // Calls GetDeviceProperties method. |callback| is called after the method
361 // call succeeds, otherwise, |error_callback| is called.
362 virtual void GetDeviceProperties(const std::string& device_path,
[email protected]4a404e52012-04-11 02:25:35363 const GetDevicePropertiesCallback& callback,
[email protected]5624a252013-07-04 03:17:53364 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18365
[email protected]4ae73292011-11-15 05:20:18366 // Factory function, creates a new instance and returns ownership.
367 // For normal usage, access the singleton via DBusThreadManager::Get().
[email protected]c5fd5362013-08-27 12:23:04368 static CrosDisksClient* Create(DBusClientImplementationType type);
[email protected]4ae73292011-11-15 05:20:18369
[email protected]a5a8b412013-03-04 15:03:11370 // Returns the path of the mount point for archive files.
371 static base::FilePath GetArchiveMountPoint();
372
373 // Returns the path of the mount point for removable disks.
374 static base::FilePath GetRemovableDiskMountPoint();
375
yamaguchi585d5402016-08-02 09:27:36376 // Composes a list of mount options.
377 static std::vector<std::string> ComposeMountOptions(
Sergei Datsenkod19248182018-05-11 01:52:56378 const std::vector<std::string>& options,
yamaguchi585d5402016-08-02 09:27:36379 const std::string& mount_label,
yamaguchifa8efc72016-10-21 15:05:51380 MountAccessMode access_mode,
381 RemountOption remount);
yamaguchi585d5402016-08-02 09:27:36382
[email protected]4ae73292011-11-15 05:20:18383 protected:
384 // Create() should be used instead.
385 CrosDisksClient();
386
387 private:
388 DISALLOW_COPY_AND_ASSIGN(CrosDisksClient);
389};
390
391} // namespace chromeos
392
[email protected]64e199252012-04-06 01:54:36393#endif // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_