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