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 MessageReader;
     23 class Response;
     24 }
     25 
     26 // 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.
     30 namespace chromeos {
     31 
     32 // Enum describing types of mount used by cros-disks.
     33 enum MountType {
     34   MOUNT_TYPE_INVALID,
     35   MOUNT_TYPE_DEVICE,
     36   MOUNT_TYPE_ARCHIVE,
     37 };
     38 
     39 // Type of device.
     40 enum DeviceType {
     41   DEVICE_TYPE_UNKNOWN,
     42   DEVICE_TYPE_USB,  // USB stick.
     43   DEVICE_TYPE_SD,  // SD card.
     44   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.
     47 };
     48 
     49 // Mount error code used by cros-disks.
     50 enum MountError {
     51   MOUNT_ERROR_NONE = 0,
     52   MOUNT_ERROR_UNKNOWN = 1,
     53   MOUNT_ERROR_INTERNAL = 2,
     54   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,
     65   MOUNT_ERROR_UNKNOWN_FILESYSTEM = 101,
     66   MOUNT_ERROR_UNSUPPORTED_FILESYSTEM = 102,
     67   MOUNT_ERROR_INVALID_ARCHIVE = 201,
     68   MOUNT_ERROR_NOT_AUTHENTICATED = 601,
     69   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 
     74 // Format error reported by cros-disks.
     75 enum 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 
     87 // Event type each corresponding to a signal sent from cros-disks.
     88 enum MountEventType {
     89   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,
     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 device 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 struct to represent information about a mount point sent from cros-disks.
    189 struct CHROMEOS_EXPORT MountEntry {
    190  public:
    191   MountEntry()
    192       : error_code_(MOUNT_ERROR_UNKNOWN), mount_type_(MOUNT_TYPE_INVALID) {
    193   }
    194 
    195   MountEntry(MountError error_code,
    196              const std::string& source_path,
    197              MountType mount_type,
    198              const std::string& mount_path)
    199       : error_code_(error_code),
    200         source_path_(source_path),
    201         mount_type_(mount_type),
    202         mount_path_(mount_path) {
    203   }
    204 
    205   MountError error_code() const { return error_code_; }
    206   const std::string& source_path() const { return source_path_; }
    207   MountType mount_type() const { return mount_type_; }
    208   const std::string& mount_path() const { return mount_path_; }
    209 
    210  private:
    211   MountError error_code_;
    212   std::string source_path_;
    213   MountType mount_type_;
    214   std::string mount_path_;
    215 };
    216 
    217 // A class to make the actual DBus calls for cros-disks service.
    218 // This class only makes calls, result/error handling should be done
    219 // by callbacks.
    220 class CHROMEOS_EXPORT CrosDisksClient : public DBusClient {
    221  public:
    222   // A callback to handle the result of EnumerateAutoMountableDevices.
    223   // The argument is the enumerated device paths.
    224   typedef base::Callback<void(const std::vector<std::string>& device_paths)>
    225       EnumerateAutoMountableDevicesCallback;
    226 
    227   // A callback to handle the result of EnumerateMountEntries.
    228   // The argument is the enumerated mount entries.
    229   typedef base::Callback<void(const std::vector<MountEntry>& entries)>
    230       EnumerateMountEntriesCallback;
    231 
    232   // A callback to handle the result of GetDeviceProperties.
    233   // The argument is the information about the specified device.
    234   typedef base::Callback<void(const DiskInfo& disk_info)>
    235       GetDevicePropertiesCallback;
    236 
    237   // A callback to handle MountCompleted signal.
    238   typedef base::Callback<void(const MountEntry& entry)> MountCompletedHandler;
    239 
    240   // A callback to handle FormatCompleted signal.
    241   // The first argument is the error code.
    242   // The second argument is the device path.
    243   typedef base::Callback<void(FormatError error_code,
    244                               const std::string& device_path)>
    245       FormatCompletedHandler;
    246 
    247   // A callback to handle mount events.
    248   // The first argument is the event type.
    249   // The second argument is the device path.
    250   typedef base::Callback<void(MountEventType event_type,
    251                               const std::string& device_path)>
    252       MountEventHandler;
    253 
    254   virtual ~CrosDisksClient();
    255 
    256   // Calls Mount method.  |callback| is called after the method call succeeds,
    257   // otherwise, |error_callback| is called.
    258   // When mounting an archive, caller may set two optional arguments:
    259   // - The |source_format| argument passes the file extension (with the leading
    260   //   dot, for example ".zip"). If |source_format| is empty then the source
    261   //   format is auto-detected.
    262   // - The |mount_label| argument passes an optional mount label to be used as
    263   //   the directory name of the mount point. If |mount_label| is empty, the
    264   //   mount label will be based on the |source_path|.
    265   virtual void Mount(const std::string& source_path,
    266                      const std::string& source_format,
    267                      const std::string& mount_label,
    268                      const base::Closure& callback,
    269                      const base::Closure& error_callback) = 0;
    270 
    271   // Calls Unmount method.  |callback| is called after the method call succeeds,
    272   // otherwise, |error_callback| is called.
    273   virtual void Unmount(const std::string& device_path,
    274                        UnmountOptions options,
    275                        const base::Closure& callback,
    276                        const base::Closure& error_callback) = 0;
    277 
    278   // Calls EnumerateAutoMountableDevices method.  |callback| is called after the
    279   // method call succeeds, otherwise, |error_callback| is called.
    280   virtual void EnumerateAutoMountableDevices(
    281       const EnumerateAutoMountableDevicesCallback& callback,
    282       const base::Closure& error_callback) = 0;
    283 
    284   // Calls EnumerateMountEntries.  |callback| is called after the
    285   // method call succeeds, otherwise, |error_callback| is called.
    286   virtual void EnumerateMountEntries(
    287       const EnumerateMountEntriesCallback& callback,
    288       const base::Closure& error_callback) = 0;
    289 
    290   // Calls Format method.  |callback| is called after the method call succeeds,
    291   // otherwise, |error_callback| is called.
    292   virtual void Format(const std::string& device_path,
    293                       const std::string& filesystem,
    294                       const base::Closure& callback,
    295                       const base::Closure& error_callback) = 0;
    296 
    297   // Calls GetDeviceProperties method.  |callback| is called after the method
    298   // call succeeds, otherwise, |error_callback| is called.
    299   virtual void GetDeviceProperties(const std::string& device_path,
    300                                    const GetDevicePropertiesCallback& callback,
    301                                    const base::Closure& error_callback) = 0;
    302 
    303   // Registers |mount_event_handler| as a callback to be invoked when a mount
    304   // event signal is received.
    305   virtual void SetMountEventHandler(
    306       const MountEventHandler& mount_event_handler) = 0;
    307 
    308   // Registers |mount_completed_handler| as a callback to be invoked when a
    309   // MountCompleted signal is received.
    310   virtual void SetMountCompletedHandler(
    311       const MountCompletedHandler& mount_completed_handler) = 0;
    312 
    313   // Registers |format_completed_handler| as a callback to be invoked when a
    314   // FormatCompleted signal is received.
    315   virtual void SetFormatCompletedHandler(
    316       const FormatCompletedHandler& format_completed_handler) = 0;
    317 
    318   // Factory function, creates a new instance and returns ownership.
    319   // For normal usage, access the singleton via DBusThreadManager::Get().
    320   static CrosDisksClient* Create(DBusClientImplementationType type);
    321 
    322   // Returns the path of the mount point for archive files.
    323   static base::FilePath GetArchiveMountPoint();
    324 
    325   // Returns the path of the mount point for removable disks.
    326   static base::FilePath GetRemovableDiskMountPoint();
    327 
    328  protected:
    329   // Create() should be used instead.
    330   CrosDisksClient();
    331 
    332  private:
    333   DISALLOW_COPY_AND_ASSIGN(CrosDisksClient);
    334 };
    335 
    336 }  // namespace chromeos
    337 
    338 #endif  // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_
    339