blob: dc3f76cb74ba31527f9c81745d269f7d7b11bd02 [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.
Anand K. Mistry25b15982018-08-16 01:19:1353// These values are not the same as cros_disks::MountErrorType.
[email protected]4ae73292011-11-15 05:20:1854enum MountError {
Anand K. Mistry25b15982018-08-16 01:19:1355 MOUNT_ERROR_NONE,
56 MOUNT_ERROR_UNKNOWN,
57 MOUNT_ERROR_INTERNAL,
58 MOUNT_ERROR_INVALID_ARGUMENT,
59 MOUNT_ERROR_INVALID_PATH,
60 MOUNT_ERROR_PATH_ALREADY_MOUNTED,
61 MOUNT_ERROR_PATH_NOT_MOUNTED,
62 MOUNT_ERROR_DIRECTORY_CREATION_FAILED,
63 MOUNT_ERROR_INVALID_MOUNT_OPTIONS,
64 MOUNT_ERROR_INVALID_UNMOUNT_OPTIONS,
65 MOUNT_ERROR_INSUFFICIENT_PERMISSIONS,
66 MOUNT_ERROR_MOUNT_PROGRAM_NOT_FOUND,
67 MOUNT_ERROR_MOUNT_PROGRAM_FAILED,
68 MOUNT_ERROR_INVALID_DEVICE_PATH,
69 MOUNT_ERROR_UNKNOWN_FILESYSTEM,
70 MOUNT_ERROR_UNSUPPORTED_FILESYSTEM,
71 MOUNT_ERROR_INVALID_ARCHIVE,
[email protected]4ae73292011-11-15 05:20:1872};
73
Klemen Kozjekbf5610f2017-08-25 20:20:0974// Rename error reported by cros-disks.
75enum RenameError {
76 RENAME_ERROR_NONE,
77 RENAME_ERROR_UNKNOWN,
78 RENAME_ERROR_INTERNAL,
79 RENAME_ERROR_INVALID_DEVICE_PATH,
80 RENAME_ERROR_DEVICE_BEING_RENAMED,
81 RENAME_ERROR_UNSUPPORTED_FILESYSTEM,
82 RENAME_ERROR_RENAME_PROGRAM_NOT_FOUND,
83 RENAME_ERROR_RENAME_PROGRAM_FAILED,
84 RENAME_ERROR_DEVICE_NOT_ALLOWED,
85 RENAME_ERROR_LONG_NAME,
86 RENAME_ERROR_INVALID_CHARACTER,
87};
88
[email protected]e3c1fc92012-11-15 00:56:4689// Format error reported by cros-disks.
90enum FormatError {
91 FORMAT_ERROR_NONE,
92 FORMAT_ERROR_UNKNOWN,
93 FORMAT_ERROR_INTERNAL,
94 FORMAT_ERROR_INVALID_DEVICE_PATH,
95 FORMAT_ERROR_DEVICE_BEING_FORMATTED,
96 FORMAT_ERROR_UNSUPPORTED_FILESYSTEM,
97 FORMAT_ERROR_FORMAT_PROGRAM_NOT_FOUND,
98 FORMAT_ERROR_FORMAT_PROGRAM_FAILED,
99 FORMAT_ERROR_DEVICE_NOT_ALLOWED,
100};
101
[email protected]4ae73292011-11-15 05:20:18102// Event type each corresponding to a signal sent from cros-disks.
103enum MountEventType {
[email protected]e3c1fc92012-11-15 00:56:46104 CROS_DISKS_DISK_ADDED,
105 CROS_DISKS_DISK_REMOVED,
106 CROS_DISKS_DISK_CHANGED,
107 CROS_DISKS_DEVICE_ADDED,
108 CROS_DISKS_DEVICE_REMOVED,
109 CROS_DISKS_DEVICE_SCANNED,
[email protected]4ae73292011-11-15 05:20:18110};
111
[email protected]10795ae2012-10-10 07:33:49112// Additional unmount flags to be added to unmount request.
113enum UnmountOptions {
114 UNMOUNT_OPTIONS_NONE,
115 UNMOUNT_OPTIONS_LAZY, // Do lazy unmount.
116};
117
yamaguchi585d5402016-08-02 09:27:36118// Mount option to control write permission to a device.
119enum MountAccessMode {
120 MOUNT_ACCESS_MODE_READ_WRITE,
121 MOUNT_ACCESS_MODE_READ_ONLY,
122};
123
yamaguchifa8efc72016-10-21 15:05:51124// Whether to mount to a new path or remount a device already mounted.
125enum RemountOption {
126 // Mount a new device. If the device is already mounted, the mount status is
127 // unchanged and the callback for MountCompleted will receive
128 // MOUNT_ERROR_PATH_ALREADY_MOUNTED error code.
129 REMOUNT_OPTION_MOUNT_NEW_DEVICE,
130 // Remount a device that is already mounted. If the device is not mounted
131 // yet, it will do nothing and the callback for MountCompleted will receive
132 // MOUNT_ERROR_PATH_NOT_MOUNTED error code.
133 REMOUNT_OPTION_REMOUNT_EXISTING_DEVICE,
134};
135
[email protected]4ae73292011-11-15 05:20:18136// A class to represent information about a disk sent from cros-disks.
[email protected]daf0f6d2013-07-08 06:42:33137class CHROMEOS_EXPORT DiskInfo {
[email protected]4ae73292011-11-15 05:20:18138 public:
139 DiskInfo(const std::string& device_path, dbus::Response* response);
140 ~DiskInfo();
141
142 // Device path. (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27143 const std::string& device_path() const { return device_path_; }
[email protected]4ae73292011-11-15 05:20:18144
145 // Disk mount path. (e.g. /media/removable/VOLUME)
[email protected]85b95a2012012-08-07 18:57:27146 const std::string& mount_path() const { return mount_path_; }
[email protected]4ae73292011-11-15 05:20:18147
148 // Disk system path given by udev.
149 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27150 const std::string& system_path() const { return system_path_; }
[email protected]4ae73292011-11-15 05:20:18151
152 // Is a drive or not. (i.e. true with /dev/sdb, false with /dev/sdb1)
153 bool is_drive() const { return is_drive_; }
154
155 // Does the disk have media content.
156 bool has_media() const { return has_media_; }
157
[email protected]d7760592014-05-16 07:57:52158 // Is the disk on device we booted the machine from.
[email protected]4ae73292011-11-15 05:20:18159 bool on_boot_device() const { return on_boot_device_; }
160
[email protected]79ed457b2014-07-22 04:07:26161 // Is the disk on a removable device.
162 bool on_removable_device() const { return on_removable_device_; }
163
Aga Wronskaddc1a752017-12-01 19:44:02164 // Is the device read-only.
165 bool is_read_only() const { return is_read_only_; }
166
167 // Returns true if the device should be hidden from the file browser.
168 bool is_hidden() const { return is_hidden_; }
169
170 // Is the disk virtual.
171 bool is_virtual() const { return is_virtual_; }
172
Anand K. Mistry2a5ed862018-08-08 00:53:31173 // Is the disk auto-mountable.
174 bool is_auto_mountable() const { return is_auto_mountable_; }
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_;
Anand K. Mistry2a5ed862018-08-08 00:53:31222 bool is_auto_mountable_;
[email protected]4ae73292011-11-15 05:20:18223
224 std::string file_path_;
225 std::string label_;
[email protected]202e9fee2012-09-13 20:21:29226 std::string vendor_id_;
227 std::string vendor_name_;
228 std::string product_id_;
229 std::string product_name_;
[email protected]4ae73292011-11-15 05:20:18230 std::string drive_model_;
231 DeviceType device_type_;
avi6e1a22d2015-12-21 03:43:20232 uint64_t total_size_in_bytes_;
[email protected]9c5620d32012-07-31 01:00:38233 std::string uuid_;
Klemen Kozjek46f1c6192017-08-25 19:20:54234 std::string file_system_type_;
[email protected]4ae73292011-11-15 05:20:18235};
236
[email protected]d7760592014-05-16 07:57:52237// A struct to represent information about a mount point sent from cros-disks.
238struct CHROMEOS_EXPORT MountEntry {
239 public:
240 MountEntry()
241 : error_code_(MOUNT_ERROR_UNKNOWN), mount_type_(MOUNT_TYPE_INVALID) {
242 }
243
244 MountEntry(MountError error_code,
245 const std::string& source_path,
246 MountType mount_type,
247 const std::string& mount_path)
248 : error_code_(error_code),
249 source_path_(source_path),
250 mount_type_(mount_type),
251 mount_path_(mount_path) {
252 }
253
254 MountError error_code() const { return error_code_; }
255 const std::string& source_path() const { return source_path_; }
256 MountType mount_type() const { return mount_type_; }
257 const std::string& mount_path() const { return mount_path_; }
258
259 private:
260 MountError error_code_;
261 std::string source_path_;
262 MountType mount_type_;
263 std::string mount_path_;
264};
265
[email protected]4ae73292011-11-15 05:20:18266// A class to make the actual DBus calls for cros-disks service.
267// This class only makes calls, result/error handling should be done
268// by callbacks.
[email protected]c5fd5362013-08-27 12:23:04269class CHROMEOS_EXPORT CrosDisksClient : public DBusClient {
[email protected]4ae73292011-11-15 05:20:18270 public:
Anand K. Mistry24fdc5b2018-07-27 01:08:31271 // A callback to handle the result of EnumerateDevices.
272 // The argument is the enumerated device paths.
[email protected]a0278d52014-05-06 03:36:15273 typedef base::Callback<void(const std::vector<std::string>& device_paths)>
Aga Wronskaddc1a752017-12-01 19:44:02274 EnumerateDevicesCallback;
[email protected]4ae73292011-11-15 05:20:18275
[email protected]d7760592014-05-16 07:57:52276 // A callback to handle the result of EnumerateMountEntries.
277 // The argument is the enumerated mount entries.
278 typedef base::Callback<void(const std::vector<MountEntry>& entries)>
279 EnumerateMountEntriesCallback;
280
[email protected]4ae73292011-11-15 05:20:18281 // A callback to handle the result of GetDeviceProperties.
282 // The argument is the information about the specified device.
[email protected]a0278d52014-05-06 03:36:15283 typedef base::Callback<void(const DiskInfo& disk_info)>
284 GetDevicePropertiesCallback;
[email protected]4ae73292011-11-15 05:20:18285
Hidehiko Abe06ce6dc2017-12-08 19:32:03286 class Observer {
287 public:
288 // Called when a mount event signal is received.
289 virtual void OnMountEvent(MountEventType event_type,
290 const std::string& device_path) = 0;
[email protected]a0278d52014-05-06 03:36:15291
Hidehiko Abe06ce6dc2017-12-08 19:32:03292 // Called when a MountCompleted signal is received.
293 virtual void OnMountCompleted(const MountEntry& entry) = 0;
[email protected]4ae73292011-11-15 05:20:18294
Hidehiko Abe06ce6dc2017-12-08 19:32:03295 // Called when a FormatCompleted signal is received.
296 virtual void OnFormatCompleted(FormatError error_code,
297 const std::string& device_path) = 0;
Klemen Kozjekbf5610f2017-08-25 20:20:09298
Hidehiko Abe06ce6dc2017-12-08 19:32:03299 // Called when a RenameCompleted signal is received.
300 virtual void OnRenameCompleted(RenameError error_code,
301 const std::string& device_path) = 0;
302
303 protected:
304 virtual ~Observer() = default;
305 };
[email protected]4ae73292011-11-15 05:20:18306
dcheng0280cb62015-01-16 07:37:50307 ~CrosDisksClient() override;
[email protected]4ae73292011-11-15 05:20:18308
Hidehiko Abe06ce6dc2017-12-08 19:32:03309 // Registers the given |observer| to listen D-Bus signals.
310 virtual void AddObserver(Observer* observer) = 0;
311
312 // Unregisters the |observer| from this instance.
313 virtual void RemoveObserver(Observer* observer) = 0;
314
Hidehiko Abec293cdcf2018-02-08 02:10:45315 // Calls Mount method. On method call completion, |callback| is called with
316 // |true| on success, or with |false| otherwise.
[email protected]dcad8fc2012-04-30 23:31:33317 // When mounting an archive, caller may set two optional arguments:
318 // - The |source_format| argument passes the file extension (with the leading
319 // dot, for example ".zip"). If |source_format| is empty then the source
320 // format is auto-detected.
321 // - The |mount_label| argument passes an optional mount label to be used as
322 // the directory name of the mount point. If |mount_label| is empty, the
323 // mount label will be based on the |source_path|.
[email protected]4ae73292011-11-15 05:20:18324 virtual void Mount(const std::string& source_path,
[email protected]b9f22d12012-04-25 21:46:48325 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33326 const std::string& mount_label,
Sergei Datsenkod19248182018-05-11 01:52:56327 const std::vector<std::string>& mount_options,
yamaguchi585d5402016-08-02 09:27:36328 MountAccessMode access_mode,
yamaguchifa8efc72016-10-21 15:05:51329 RemountOption remount,
Hidehiko Abec293cdcf2018-02-08 02:10:45330 VoidDBusMethodCallback callback) = 0;
[email protected]4ae73292011-11-15 05:20:18331
Hidehiko Abec293cdcf2018-02-08 02:10:45332 // Calls Unmount method. On method call completion, |callback| is called
333 // with |true| on success, or with |false| otherwise.
[email protected]4ae73292011-11-15 05:20:18334 virtual void Unmount(const std::string& device_path,
[email protected]10795ae2012-10-10 07:33:49335 UnmountOptions options,
Hidehiko Abec293cdcf2018-02-08 02:10:45336 VoidDBusMethodCallback callback) = 0;
[email protected]4ae73292011-11-15 05:20:18337
Aga Wronskaddc1a752017-12-01 19:44:02338 // Calls EnumerateDevices method. |callback| is called after the
339 // method call succeeds, otherwise, |error_callback| is called.
340 virtual void EnumerateDevices(const EnumerateDevicesCallback& callback,
341 const base::Closure& error_callback) = 0;
342
[email protected]d7760592014-05-16 07:57:52343 // Calls EnumerateMountEntries. |callback| is called after the
344 // method call succeeds, otherwise, |error_callback| is called.
345 virtual void EnumerateMountEntries(
346 const EnumerateMountEntriesCallback& callback,
347 const base::Closure& error_callback) = 0;
348
Hidehiko Abec293cdcf2018-02-08 02:10:45349 // Calls Format method. On completion, |callback| is called, with |true| on
350 // success, or with |false| otherwise.
[email protected]f026c0f2014-05-06 21:52:35351 virtual void Format(const std::string& device_path,
352 const std::string& filesystem,
Hidehiko Abec293cdcf2018-02-08 02:10:45353 VoidDBusMethodCallback callback) = 0;
[email protected]4ae73292011-11-15 05:20:18354
Hidehiko Abec293cdcf2018-02-08 02:10:45355 // Calls Rename method. On completion, |callback| is called, with |true| on
356 // success, or with |false| otherwise.
Klemen Kozjekbf5610f2017-08-25 20:20:09357 virtual void Rename(const std::string& device_path,
358 const std::string& volume_name,
Hidehiko Abec293cdcf2018-02-08 02:10:45359 VoidDBusMethodCallback callback) = 0;
Klemen Kozjekbf5610f2017-08-25 20:20:09360
[email protected]4ae73292011-11-15 05:20:18361 // Calls GetDeviceProperties method. |callback| is called after the method
362 // call succeeds, otherwise, |error_callback| is called.
363 virtual void GetDeviceProperties(const std::string& device_path,
[email protected]4a404e52012-04-11 02:25:35364 const GetDevicePropertiesCallback& callback,
[email protected]5624a252013-07-04 03:17:53365 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18366
[email protected]4ae73292011-11-15 05:20:18367 // Factory function, creates a new instance and returns ownership.
368 // For normal usage, access the singleton via DBusThreadManager::Get().
[email protected]c5fd5362013-08-27 12:23:04369 static CrosDisksClient* Create(DBusClientImplementationType type);
[email protected]4ae73292011-11-15 05:20:18370
[email protected]a5a8b412013-03-04 15:03:11371 // Returns the path of the mount point for archive files.
372 static base::FilePath GetArchiveMountPoint();
373
374 // Returns the path of the mount point for removable disks.
375 static base::FilePath GetRemovableDiskMountPoint();
376
yamaguchi585d5402016-08-02 09:27:36377 // Composes a list of mount options.
378 static std::vector<std::string> ComposeMountOptions(
Sergei Datsenkod19248182018-05-11 01:52:56379 const std::vector<std::string>& options,
yamaguchi585d5402016-08-02 09:27:36380 const std::string& mount_label,
yamaguchifa8efc72016-10-21 15:05:51381 MountAccessMode access_mode,
382 RemountOption remount);
yamaguchi585d5402016-08-02 09:27:36383
[email protected]4ae73292011-11-15 05:20:18384 protected:
385 // Create() should be used instead.
386 CrosDisksClient();
387
388 private:
389 DISALLOW_COPY_AND_ASSIGN(CrosDisksClient);
390};
391
392} // namespace chromeos
393
[email protected]64e199252012-04-06 01:54:36394#endif // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_