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