blob: 840b29560996f131e2f56089dbe4b114dbd1531c [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]d7760592014-05-16 07:57:5222class MessageReader;
[email protected]4ae73292011-11-15 05:20:1823class Response;
24}
25
[email protected]e3c1fc92012-11-15 00:56:4626// TODO(tbarzic): We should move these enums inside CrosDisksClient,
27// to be clearer where they come from. Also, most of these are partially or
28// completely duplicated in third_party/dbus/service_constants.h. We should
29// probably use enums from service_contstants directly.
[email protected]4ae73292011-11-15 05:20:1830namespace chromeos {
31
32// Enum describing types of mount used by cros-disks.
33enum MountType {
34 MOUNT_TYPE_INVALID,
35 MOUNT_TYPE_DEVICE,
36 MOUNT_TYPE_ARCHIVE,
[email protected]4ae73292011-11-15 05:20:1837};
38
39// Type of device.
40enum DeviceType {
[email protected]2321d282012-01-31 23:06:5941 DEVICE_TYPE_UNKNOWN,
42 DEVICE_TYPE_USB, // USB stick.
43 DEVICE_TYPE_SD, // SD card.
[email protected]f4ae40ac2012-05-04 21:57:0044 DEVICE_TYPE_OPTICAL_DISC, // e.g. Optical disc excluding DVD.
45 DEVICE_TYPE_MOBILE, // Storage on a mobile device (e.g. Android).
46 DEVICE_TYPE_DVD, // DVD.
[email protected]4ae73292011-11-15 05:20:1847};
48
49// Mount error code used by cros-disks.
50enum MountError {
51 MOUNT_ERROR_NONE = 0,
52 MOUNT_ERROR_UNKNOWN = 1,
53 MOUNT_ERROR_INTERNAL = 2,
[email protected]e3c1fc92012-11-15 00:56:4654 MOUNT_ERROR_INVALID_ARGUMENT = 3,
55 MOUNT_ERROR_INVALID_PATH = 4,
56 MOUNT_ERROR_PATH_ALREADY_MOUNTED = 5,
57 MOUNT_ERROR_PATH_NOT_MOUNTED = 6,
58 MOUNT_ERROR_DIRECTORY_CREATION_FAILED = 7,
59 MOUNT_ERROR_INVALID_MOUNT_OPTIONS = 8,
60 MOUNT_ERROR_INVALID_UNMOUNT_OPTIONS = 9,
61 MOUNT_ERROR_INSUFFICIENT_PERMISSIONS = 10,
62 MOUNT_ERROR_MOUNT_PROGRAM_NOT_FOUND = 11,
63 MOUNT_ERROR_MOUNT_PROGRAM_FAILED = 12,
64 MOUNT_ERROR_INVALID_DEVICE_PATH = 100,
[email protected]4ae73292011-11-15 05:20:1865 MOUNT_ERROR_UNKNOWN_FILESYSTEM = 101,
[email protected]a66a23cb2012-06-19 23:15:3366 MOUNT_ERROR_UNSUPPORTED_FILESYSTEM = 102,
[email protected]4ae73292011-11-15 05:20:1867 MOUNT_ERROR_INVALID_ARCHIVE = 201,
[email protected]9bb24222012-02-09 02:00:4368 MOUNT_ERROR_NOT_AUTHENTICATED = 601,
[email protected]4ae73292011-11-15 05:20:1869 MOUNT_ERROR_PATH_UNMOUNTED = 901,
70 // TODO(tbarzic): Add more error codes as they get added to cros-disks and
71 // consider doing explicit translation from cros-disks error_types.
72};
73
[email protected]e3c1fc92012-11-15 00:56:4674// Format error reported by cros-disks.
75enum FormatError {
76 FORMAT_ERROR_NONE,
77 FORMAT_ERROR_UNKNOWN,
78 FORMAT_ERROR_INTERNAL,
79 FORMAT_ERROR_INVALID_DEVICE_PATH,
80 FORMAT_ERROR_DEVICE_BEING_FORMATTED,
81 FORMAT_ERROR_UNSUPPORTED_FILESYSTEM,
82 FORMAT_ERROR_FORMAT_PROGRAM_NOT_FOUND,
83 FORMAT_ERROR_FORMAT_PROGRAM_FAILED,
84 FORMAT_ERROR_DEVICE_NOT_ALLOWED,
85};
86
[email protected]4ae73292011-11-15 05:20:1887// Event type each corresponding to a signal sent from cros-disks.
88enum MountEventType {
[email protected]e3c1fc92012-11-15 00:56:4689 CROS_DISKS_DISK_ADDED,
90 CROS_DISKS_DISK_REMOVED,
91 CROS_DISKS_DISK_CHANGED,
92 CROS_DISKS_DEVICE_ADDED,
93 CROS_DISKS_DEVICE_REMOVED,
94 CROS_DISKS_DEVICE_SCANNED,
[email protected]4ae73292011-11-15 05:20:1895};
96
[email protected]10795ae2012-10-10 07:33:4997// Additional unmount flags to be added to unmount request.
98enum UnmountOptions {
99 UNMOUNT_OPTIONS_NONE,
100 UNMOUNT_OPTIONS_LAZY, // Do lazy unmount.
101};
102
[email protected]4ae73292011-11-15 05:20:18103// A class to represent information about a disk sent from cros-disks.
[email protected]daf0f6d2013-07-08 06:42:33104class CHROMEOS_EXPORT DiskInfo {
[email protected]4ae73292011-11-15 05:20:18105 public:
106 DiskInfo(const std::string& device_path, dbus::Response* response);
107 ~DiskInfo();
108
109 // Device path. (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27110 const std::string& device_path() const { return device_path_; }
[email protected]4ae73292011-11-15 05:20:18111
112 // Disk mount path. (e.g. /media/removable/VOLUME)
[email protected]85b95a2012012-08-07 18:57:27113 const std::string& mount_path() const { return mount_path_; }
[email protected]4ae73292011-11-15 05:20:18114
115 // Disk system path given by udev.
116 // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1)
[email protected]85b95a2012012-08-07 18:57:27117 const std::string& system_path() const { return system_path_; }
[email protected]4ae73292011-11-15 05:20:18118
119 // Is a drive or not. (i.e. true with /dev/sdb, false with /dev/sdb1)
120 bool is_drive() const { return is_drive_; }
121
122 // Does the disk have media content.
123 bool has_media() const { return has_media_; }
124
[email protected]d7760592014-05-16 07:57:52125 // Is the disk on device we booted the machine from.
[email protected]4ae73292011-11-15 05:20:18126 bool on_boot_device() const { return on_boot_device_; }
127
[email protected]79ed457b2014-07-22 04:07:26128 // Is the disk on a removable device.
129 bool on_removable_device() const { return on_removable_device_; }
130
[email protected]4ae73292011-11-15 05:20:18131 // Disk file path (e.g. /dev/sdb).
[email protected]85b95a2012012-08-07 18:57:27132 const std::string& file_path() const { return file_path_; }
[email protected]4ae73292011-11-15 05:20:18133
134 // Disk label.
[email protected]85b95a2012012-08-07 18:57:27135 const std::string& label() const { return label_; }
[email protected]4ae73292011-11-15 05:20:18136
[email protected]202e9fee2012-09-13 20:21:29137 // Vendor ID of the device (e.g. "18d1").
138 const std::string& vendor_id() const { return vendor_id_; }
139
140 // Vendor name of the device (e.g. "Google Inc.").
141 const std::string& vendor_name() const { return vendor_name_; }
142
143 // Product ID of the device (e.g. "4e11").
144 const std::string& product_id() const { return product_id_; }
145
146 // Product name of the device (e.g. "Nexus One").
147 const std::string& product_name() const { return product_name_; }
148
[email protected]4ae73292011-11-15 05:20:18149 // Disk model. (e.g. "TransMemory")
[email protected]85b95a2012012-08-07 18:57:27150 const std::string& drive_label() const { return drive_model_; }
[email protected]4ae73292011-11-15 05:20:18151
152 // Device type. Not working well, yet.
153 DeviceType device_type() const { return device_type_; }
154
155 // Total size of the disk in bytes.
156 uint64 total_size_in_bytes() const { return total_size_in_bytes_; }
157
158 // Is the device read-only.
159 bool is_read_only() const { return is_read_only_; }
160
161 // Returns true if the device should be hidden from the file browser.
162 bool is_hidden() const { return is_hidden_; }
163
[email protected]9c5620d32012-07-31 01:00:38164 // Returns file system uuid.
[email protected]85b95a2012012-08-07 18:57:27165 const std::string& uuid() const { return uuid_; }
[email protected]9c5620d32012-07-31 01:00:38166
[email protected]4ae73292011-11-15 05:20:18167 private:
168 void InitializeFromResponse(dbus::Response* response);
169
170 std::string device_path_;
171 std::string mount_path_;
172 std::string system_path_;
173 bool is_drive_;
174 bool has_media_;
175 bool on_boot_device_;
[email protected]79ed457b2014-07-22 04:07:26176 bool on_removable_device_;
[email protected]4ae73292011-11-15 05:20:18177
178 std::string file_path_;
179 std::string label_;
[email protected]202e9fee2012-09-13 20:21:29180 std::string vendor_id_;
181 std::string vendor_name_;
182 std::string product_id_;
183 std::string product_name_;
[email protected]4ae73292011-11-15 05:20:18184 std::string drive_model_;
185 DeviceType device_type_;
186 uint64 total_size_in_bytes_;
187 bool is_read_only_;
188 bool is_hidden_;
[email protected]9c5620d32012-07-31 01:00:38189 std::string uuid_;
[email protected]4ae73292011-11-15 05:20:18190};
191
[email protected]d7760592014-05-16 07:57:52192// A struct to represent information about a mount point sent from cros-disks.
193struct CHROMEOS_EXPORT MountEntry {
194 public:
195 MountEntry()
196 : error_code_(MOUNT_ERROR_UNKNOWN), mount_type_(MOUNT_TYPE_INVALID) {
197 }
198
199 MountEntry(MountError error_code,
200 const std::string& source_path,
201 MountType mount_type,
202 const std::string& mount_path)
203 : error_code_(error_code),
204 source_path_(source_path),
205 mount_type_(mount_type),
206 mount_path_(mount_path) {
207 }
208
209 MountError error_code() const { return error_code_; }
210 const std::string& source_path() const { return source_path_; }
211 MountType mount_type() const { return mount_type_; }
212 const std::string& mount_path() const { return mount_path_; }
213
214 private:
215 MountError error_code_;
216 std::string source_path_;
217 MountType mount_type_;
218 std::string mount_path_;
219};
220
[email protected]4ae73292011-11-15 05:20:18221// A class to make the actual DBus calls for cros-disks service.
222// This class only makes calls, result/error handling should be done
223// by callbacks.
[email protected]c5fd5362013-08-27 12:23:04224class CHROMEOS_EXPORT CrosDisksClient : public DBusClient {
[email protected]4ae73292011-11-15 05:20:18225 public:
[email protected]4ae73292011-11-15 05:20:18226 // A callback to handle the result of EnumerateAutoMountableDevices.
227 // The argument is the enumerated device paths.
[email protected]a0278d52014-05-06 03:36:15228 typedef base::Callback<void(const std::vector<std::string>& device_paths)>
229 EnumerateAutoMountableDevicesCallback;
[email protected]4ae73292011-11-15 05:20:18230
[email protected]d7760592014-05-16 07:57:52231 // A callback to handle the result of EnumerateMountEntries.
232 // The argument is the enumerated mount entries.
233 typedef base::Callback<void(const std::vector<MountEntry>& entries)>
234 EnumerateMountEntriesCallback;
235
[email protected]4ae73292011-11-15 05:20:18236 // A callback to handle the result of GetDeviceProperties.
237 // The argument is the information about the specified device.
[email protected]a0278d52014-05-06 03:36:15238 typedef base::Callback<void(const DiskInfo& disk_info)>
239 GetDevicePropertiesCallback;
[email protected]4ae73292011-11-15 05:20:18240
241 // A callback to handle MountCompleted signal.
[email protected]d7760592014-05-16 07:57:52242 typedef base::Callback<void(const MountEntry& entry)> MountCompletedHandler;
[email protected]a0278d52014-05-06 03:36:15243
244 // A callback to handle FormatCompleted signal.
245 // The first argument is the error code.
246 // The second argument is the device path.
247 typedef base::Callback<void(FormatError error_code,
248 const std::string& device_path)>
249 FormatCompletedHandler;
[email protected]4ae73292011-11-15 05:20:18250
251 // A callback to handle mount events.
252 // The first argument is the event type.
253 // The second argument is the device path.
[email protected]85b95a2012012-08-07 18:57:27254 typedef base::Callback<void(MountEventType event_type,
[email protected]a0278d52014-05-06 03:36:15255 const std::string& device_path)>
256 MountEventHandler;
[email protected]4ae73292011-11-15 05:20:18257
dcheng0280cb62015-01-16 07:37:50258 ~CrosDisksClient() override;
[email protected]4ae73292011-11-15 05:20:18259
260 // Calls Mount method. |callback| is called after the method call succeeds,
261 // otherwise, |error_callback| is called.
[email protected]dcad8fc2012-04-30 23:31:33262 // When mounting an archive, caller may set two optional arguments:
263 // - The |source_format| argument passes the file extension (with the leading
264 // dot, for example ".zip"). If |source_format| is empty then the source
265 // format is auto-detected.
266 // - The |mount_label| argument passes an optional mount label to be used as
267 // the directory name of the mount point. If |mount_label| is empty, the
268 // mount label will be based on the |source_path|.
[email protected]4ae73292011-11-15 05:20:18269 virtual void Mount(const std::string& source_path,
[email protected]b9f22d12012-04-25 21:46:48270 const std::string& source_format,
[email protected]dcad8fc2012-04-30 23:31:33271 const std::string& mount_label,
[email protected]5624a252013-07-04 03:17:53272 const base::Closure& callback,
273 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18274
275 // Calls Unmount method. |callback| is called after the method call succeeds,
276 // otherwise, |error_callback| is called.
277 virtual void Unmount(const std::string& device_path,
[email protected]10795ae2012-10-10 07:33:49278 UnmountOptions options,
[email protected]ffdcc7a9c2013-07-02 06:59:39279 const base::Closure& callback,
280 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18281
282 // Calls EnumerateAutoMountableDevices method. |callback| is called after the
283 // method call succeeds, otherwise, |error_callback| is called.
284 virtual void EnumerateAutoMountableDevices(
[email protected]4a404e52012-04-11 02:25:35285 const EnumerateAutoMountableDevicesCallback& callback,
[email protected]5624a252013-07-04 03:17:53286 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18287
[email protected]d7760592014-05-16 07:57:52288 // Calls EnumerateMountEntries. |callback| is called after the
289 // method call succeeds, otherwise, |error_callback| is called.
290 virtual void EnumerateMountEntries(
291 const EnumerateMountEntriesCallback& callback,
292 const base::Closure& error_callback) = 0;
293
[email protected]f026c0f2014-05-06 21:52:35294 // Calls Format method. |callback| is called after the method call succeeds,
295 // otherwise, |error_callback| is called.
296 virtual void Format(const std::string& device_path,
297 const std::string& filesystem,
298 const base::Closure& callback,
299 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18300
301 // Calls GetDeviceProperties method. |callback| is called after the method
302 // call succeeds, otherwise, |error_callback| is called.
303 virtual void GetDeviceProperties(const std::string& device_path,
[email protected]4a404e52012-04-11 02:25:35304 const GetDevicePropertiesCallback& callback,
[email protected]5624a252013-07-04 03:17:53305 const base::Closure& error_callback) = 0;
[email protected]4ae73292011-11-15 05:20:18306
[email protected]a0278d52014-05-06 03:36:15307 // Registers |mount_event_handler| as a callback to be invoked when a mount
308 // event signal is received.
309 virtual void SetMountEventHandler(
310 const MountEventHandler& mount_event_handler) = 0;
311
312 // Registers |mount_completed_handler| as a callback to be invoked when a
313 // MountCompleted signal is received.
314 virtual void SetMountCompletedHandler(
[email protected]4a404e52012-04-11 02:25:35315 const MountCompletedHandler& mount_completed_handler) = 0;
[email protected]4ae73292011-11-15 05:20:18316
[email protected]a0278d52014-05-06 03:36:15317 // Registers |format_completed_handler| as a callback to be invoked when a
318 // FormatCompleted signal is received.
319 virtual void SetFormatCompletedHandler(
320 const FormatCompletedHandler& format_completed_handler) = 0;
321
[email protected]4ae73292011-11-15 05:20:18322 // Factory function, creates a new instance and returns ownership.
323 // For normal usage, access the singleton via DBusThreadManager::Get().
[email protected]c5fd5362013-08-27 12:23:04324 static CrosDisksClient* Create(DBusClientImplementationType type);
[email protected]4ae73292011-11-15 05:20:18325
[email protected]a5a8b412013-03-04 15:03:11326 // Returns the path of the mount point for archive files.
327 static base::FilePath GetArchiveMountPoint();
328
329 // Returns the path of the mount point for removable disks.
330 static base::FilePath GetRemovableDiskMountPoint();
331
[email protected]4ae73292011-11-15 05:20:18332 protected:
333 // Create() should be used instead.
334 CrosDisksClient();
335
336 private:
337 DISALLOW_COPY_AND_ASSIGN(CrosDisksClient);
338};
339
340} // namespace chromeos
341
[email protected]64e199252012-04-06 01:54:36342#endif // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_