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