[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 64e19925 | 2012-04-06 01:54:36 | [diff] [blame] | 5 | #ifndef CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_ |
| 6 | #define CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_ |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/basictypes.h" |
| 12 | #include "base/callback.h" |
[email protected] | 64e19925 | 2012-04-06 01:54:36 | [diff] [blame] | 13 | #include "chromeos/chromeos_export.h" |
| 14 | #include "chromeos/dbus/dbus_client_implementation_type.h" |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 15 | |
[email protected] | a5a8b41 | 2013-03-04 15:03:11 | [diff] [blame^] | 16 | namespace base { |
| 17 | class FilePath; |
| 18 | } |
| 19 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 20 | namespace dbus { |
| 21 | class Bus; |
| 22 | class Response; |
| 23 | } |
| 24 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 25 | // 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] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 29 | namespace chromeos { |
| 30 | |
| 31 | // Enum describing types of mount used by cros-disks. |
| 32 | enum MountType { |
| 33 | MOUNT_TYPE_INVALID, |
| 34 | MOUNT_TYPE_DEVICE, |
| 35 | MOUNT_TYPE_ARCHIVE, |
[email protected] | c9d37ea | 2012-11-28 06:33:40 | [diff] [blame] | 36 | MOUNT_TYPE_GOOGLE_DRIVE, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 37 | MOUNT_TYPE_NETWORK_STORAGE, |
| 38 | }; |
| 39 | |
| 40 | // Type of device. |
| 41 | enum DeviceType { |
[email protected] | 2321d28 | 2012-01-31 23:06:59 | [diff] [blame] | 42 | DEVICE_TYPE_UNKNOWN, |
| 43 | DEVICE_TYPE_USB, // USB stick. |
| 44 | DEVICE_TYPE_SD, // SD card. |
[email protected] | f4ae40ac | 2012-05-04 21:57:00 | [diff] [blame] | 45 | 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] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | // Mount error code used by cros-disks. |
| 51 | enum MountError { |
| 52 | MOUNT_ERROR_NONE = 0, |
| 53 | MOUNT_ERROR_UNKNOWN = 1, |
| 54 | MOUNT_ERROR_INTERNAL = 2, |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 55 | 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] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 66 | MOUNT_ERROR_UNKNOWN_FILESYSTEM = 101, |
[email protected] | a66a23cb | 2012-06-19 23:15:33 | [diff] [blame] | 67 | MOUNT_ERROR_UNSUPPORTED_FILESYSTEM = 102, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 68 | MOUNT_ERROR_INVALID_ARCHIVE = 201, |
[email protected] | 9bb2422 | 2012-02-09 02:00:43 | [diff] [blame] | 69 | MOUNT_ERROR_NOT_AUTHENTICATED = 601, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 70 | 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 | |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 75 | // Format error reported by cros-disks. |
| 76 | enum FormatError { |
| 77 | FORMAT_ERROR_NONE, |
| 78 | FORMAT_ERROR_UNKNOWN, |
| 79 | FORMAT_ERROR_INTERNAL, |
| 80 | FORMAT_ERROR_INVALID_DEVICE_PATH, |
| 81 | FORMAT_ERROR_DEVICE_BEING_FORMATTED, |
| 82 | FORMAT_ERROR_UNSUPPORTED_FILESYSTEM, |
| 83 | FORMAT_ERROR_FORMAT_PROGRAM_NOT_FOUND, |
| 84 | FORMAT_ERROR_FORMAT_PROGRAM_FAILED, |
| 85 | FORMAT_ERROR_DEVICE_NOT_ALLOWED, |
| 86 | }; |
| 87 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 88 | // Event type each corresponding to a signal sent from cros-disks. |
| 89 | enum MountEventType { |
[email protected] | e3c1fc9 | 2012-11-15 00:56:46 | [diff] [blame] | 90 | CROS_DISKS_DISK_ADDED, |
| 91 | CROS_DISKS_DISK_REMOVED, |
| 92 | CROS_DISKS_DISK_CHANGED, |
| 93 | CROS_DISKS_DEVICE_ADDED, |
| 94 | CROS_DISKS_DEVICE_REMOVED, |
| 95 | CROS_DISKS_DEVICE_SCANNED, |
| 96 | CROS_DISKS_FORMATTING_FINISHED, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 97 | }; |
| 98 | |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 99 | // Additional unmount flags to be added to unmount request. |
| 100 | enum UnmountOptions { |
| 101 | UNMOUNT_OPTIONS_NONE, |
| 102 | UNMOUNT_OPTIONS_LAZY, // Do lazy unmount. |
| 103 | }; |
| 104 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 105 | // A class to represent information about a disk sent from cros-disks. |
| 106 | class DiskInfo { |
| 107 | public: |
| 108 | DiskInfo(const std::string& device_path, dbus::Response* response); |
| 109 | ~DiskInfo(); |
| 110 | |
| 111 | // Device path. (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1) |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 112 | const std::string& device_path() const { return device_path_; } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 113 | |
| 114 | // Disk mount path. (e.g. /media/removable/VOLUME) |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 115 | const std::string& mount_path() const { return mount_path_; } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 116 | |
| 117 | // Disk system path given by udev. |
| 118 | // (e.g. /sys/devices/pci0000:00/.../8:0:0:0/block/sdb/sdb1) |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 119 | const std::string& system_path() const { return system_path_; } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 120 | |
| 121 | // Is a drive or not. (i.e. true with /dev/sdb, false with /dev/sdb1) |
| 122 | bool is_drive() const { return is_drive_; } |
| 123 | |
| 124 | // Does the disk have media content. |
| 125 | bool has_media() const { return has_media_; } |
| 126 | |
| 127 | // Is the disk on deveice we booted the machine from. |
| 128 | bool on_boot_device() const { return on_boot_device_; } |
| 129 | |
| 130 | // Disk file path (e.g. /dev/sdb). |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 131 | const std::string& file_path() const { return file_path_; } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 132 | |
| 133 | // Disk label. |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 134 | const std::string& label() const { return label_; } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 135 | |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame] | 136 | // Vendor ID of the device (e.g. "18d1"). |
| 137 | const std::string& vendor_id() const { return vendor_id_; } |
| 138 | |
| 139 | // Vendor name of the device (e.g. "Google Inc."). |
| 140 | const std::string& vendor_name() const { return vendor_name_; } |
| 141 | |
| 142 | // Product ID of the device (e.g. "4e11"). |
| 143 | const std::string& product_id() const { return product_id_; } |
| 144 | |
| 145 | // Product name of the device (e.g. "Nexus One"). |
| 146 | const std::string& product_name() const { return product_name_; } |
| 147 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 148 | // Disk model. (e.g. "TransMemory") |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 149 | const std::string& drive_label() const { return drive_model_; } |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 150 | |
| 151 | // Device type. Not working well, yet. |
| 152 | DeviceType device_type() const { return device_type_; } |
| 153 | |
| 154 | // Total size of the disk in bytes. |
| 155 | uint64 total_size_in_bytes() const { return total_size_in_bytes_; } |
| 156 | |
| 157 | // Is the device read-only. |
| 158 | bool is_read_only() const { return is_read_only_; } |
| 159 | |
| 160 | // Returns true if the device should be hidden from the file browser. |
| 161 | bool is_hidden() const { return is_hidden_; } |
| 162 | |
[email protected] | 9c5620d3 | 2012-07-31 01:00:38 | [diff] [blame] | 163 | // Returns file system uuid. |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 164 | const std::string& uuid() const { return uuid_; } |
[email protected] | 9c5620d3 | 2012-07-31 01:00:38 | [diff] [blame] | 165 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 166 | private: |
| 167 | void InitializeFromResponse(dbus::Response* response); |
| 168 | |
| 169 | std::string device_path_; |
| 170 | std::string mount_path_; |
| 171 | std::string system_path_; |
| 172 | bool is_drive_; |
| 173 | bool has_media_; |
| 174 | bool on_boot_device_; |
| 175 | |
| 176 | std::string file_path_; |
| 177 | std::string label_; |
[email protected] | 202e9fee | 2012-09-13 20:21:29 | [diff] [blame] | 178 | std::string vendor_id_; |
| 179 | std::string vendor_name_; |
| 180 | std::string product_id_; |
| 181 | std::string product_name_; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 182 | std::string drive_model_; |
| 183 | DeviceType device_type_; |
| 184 | uint64 total_size_in_bytes_; |
| 185 | bool is_read_only_; |
| 186 | bool is_hidden_; |
[email protected] | 9c5620d3 | 2012-07-31 01:00:38 | [diff] [blame] | 187 | std::string uuid_; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | // A class to make the actual DBus calls for cros-disks service. |
| 191 | // This class only makes calls, result/error handling should be done |
| 192 | // by callbacks. |
[email protected] | 64e19925 | 2012-04-06 01:54:36 | [diff] [blame] | 193 | class CHROMEOS_EXPORT CrosDisksClient { |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 194 | public: |
| 195 | // A callback to be called when DBus method call fails. |
| 196 | typedef base::Callback<void()> ErrorCallback; |
| 197 | |
| 198 | // A callback to handle the result of Mount. |
| 199 | typedef base::Callback<void()> MountCallback; |
| 200 | |
| 201 | // A callback to handle the result of Unmount. |
| 202 | // The argument is the device path. |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 203 | typedef base::Callback<void(const std::string& device_path)> UnmountCallback; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 204 | |
| 205 | // A callback to handle the result of EnumerateAutoMountableDevices. |
| 206 | // The argument is the enumerated device paths. |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 207 | typedef base::Callback<void(const std::vector<std::string>& device_paths) |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 208 | > EnumerateAutoMountableDevicesCallback; |
| 209 | |
| 210 | // A callback to handle the result of FormatDevice. |
| 211 | // The first argument is the device path. |
| 212 | // The second argument is true when formatting succeeded, false otherwise. |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 213 | typedef base::Callback<void(const std::string& device_path, |
| 214 | bool format_succeeded)> FormatDeviceCallback; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 215 | |
| 216 | // A callback to handle the result of GetDeviceProperties. |
| 217 | // The argument is the information about the specified device. |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 218 | typedef base::Callback<void(const DiskInfo& disk_info) |
| 219 | > GetDevicePropertiesCallback; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 220 | |
| 221 | // A callback to handle MountCompleted signal. |
| 222 | // The first argument is the error code. |
| 223 | // The second argument is the source path. |
| 224 | // The third argument is the mount type. |
| 225 | // The fourth argument is the mount path. |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 226 | typedef base::Callback<void(MountError error_code, |
| 227 | const std::string& source_path, |
| 228 | MountType mount_type, |
| 229 | const std::string& mount_path) |
| 230 | > MountCompletedHandler; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 231 | |
| 232 | // A callback to handle mount events. |
| 233 | // The first argument is the event type. |
| 234 | // The second argument is the device path. |
[email protected] | 85b95a201 | 2012-08-07 18:57:27 | [diff] [blame] | 235 | typedef base::Callback<void(MountEventType event_type, |
| 236 | const std::string& device_path) |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 237 | > MountEventHandler; |
| 238 | |
| 239 | virtual ~CrosDisksClient(); |
| 240 | |
| 241 | // Calls Mount method. |callback| is called after the method call succeeds, |
| 242 | // otherwise, |error_callback| is called. |
[email protected] | dcad8fc | 2012-04-30 23:31:33 | [diff] [blame] | 243 | // When mounting an archive, caller may set two optional arguments: |
| 244 | // - The |source_format| argument passes the file extension (with the leading |
| 245 | // dot, for example ".zip"). If |source_format| is empty then the source |
| 246 | // format is auto-detected. |
| 247 | // - The |mount_label| argument passes an optional mount label to be used as |
| 248 | // the directory name of the mount point. If |mount_label| is empty, the |
| 249 | // mount label will be based on the |source_path|. |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 250 | virtual void Mount(const std::string& source_path, |
[email protected] | b9f22d1 | 2012-04-25 21:46:48 | [diff] [blame] | 251 | const std::string& source_format, |
[email protected] | dcad8fc | 2012-04-30 23:31:33 | [diff] [blame] | 252 | const std::string& mount_label, |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 253 | MountType type, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 254 | const MountCallback& callback, |
| 255 | const ErrorCallback& error_callback) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 256 | |
| 257 | // Calls Unmount method. |callback| is called after the method call succeeds, |
| 258 | // otherwise, |error_callback| is called. |
| 259 | virtual void Unmount(const std::string& device_path, |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 260 | UnmountOptions options, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 261 | const UnmountCallback& callback, |
[email protected] | 10795ae | 2012-10-10 07:33:49 | [diff] [blame] | 262 | const UnmountCallback& error_callback) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 263 | |
| 264 | // Calls EnumerateAutoMountableDevices method. |callback| is called after the |
| 265 | // method call succeeds, otherwise, |error_callback| is called. |
| 266 | virtual void EnumerateAutoMountableDevices( |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 267 | const EnumerateAutoMountableDevicesCallback& callback, |
| 268 | const ErrorCallback& error_callback) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 269 | |
| 270 | // Calls FormatDevice method. |callback| is called after the method call |
| 271 | // succeeds, otherwise, |error_callback| is called. |
| 272 | virtual void FormatDevice(const std::string& device_path, |
| 273 | const std::string& filesystem, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 274 | const FormatDeviceCallback& callback, |
| 275 | const ErrorCallback& error_callback) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 276 | |
| 277 | // Calls GetDeviceProperties method. |callback| is called after the method |
| 278 | // call succeeds, otherwise, |error_callback| is called. |
| 279 | virtual void GetDeviceProperties(const std::string& device_path, |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 280 | const GetDevicePropertiesCallback& callback, |
| 281 | const ErrorCallback& error_callback) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 282 | |
| 283 | // Registers given callback for events. |
| 284 | // |mount_event_handler| is called when mount event signal is received. |
| 285 | // |mount_completed_handler| is called when MountCompleted signal is received. |
| 286 | virtual void SetUpConnections( |
[email protected] | 4a404e5 | 2012-04-11 02:25:35 | [diff] [blame] | 287 | const MountEventHandler& mount_event_handler, |
| 288 | const MountCompletedHandler& mount_completed_handler) = 0; |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 289 | |
| 290 | // Factory function, creates a new instance and returns ownership. |
| 291 | // For normal usage, access the singleton via DBusThreadManager::Get(). |
[email protected] | e8db03d6 | 2012-03-31 04:08:38 | [diff] [blame] | 292 | static CrosDisksClient* Create(DBusClientImplementationType type, |
| 293 | dbus::Bus* bus); |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 294 | |
[email protected] | a5a8b41 | 2013-03-04 15:03:11 | [diff] [blame^] | 295 | // Returns the path of the mount point for archive files. |
| 296 | static base::FilePath GetArchiveMountPoint(); |
| 297 | |
| 298 | // Returns the path of the mount point for removable disks. |
| 299 | static base::FilePath GetRemovableDiskMountPoint(); |
| 300 | |
[email protected] | 4ae7329 | 2011-11-15 05:20:18 | [diff] [blame] | 301 | protected: |
| 302 | // Create() should be used instead. |
| 303 | CrosDisksClient(); |
| 304 | |
| 305 | private: |
| 306 | DISALLOW_COPY_AND_ASSIGN(CrosDisksClient); |
| 307 | }; |
| 308 | |
| 309 | } // namespace chromeos |
| 310 | |
[email protected] | 64e19925 | 2012-04-06 01:54:36 | [diff] [blame] | 311 | #endif // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_ |