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   // Is the disk on a removable device.
    129   bool on_removable_device() const { return on_removable_device_; }
    130 
    131   // Disk file path (e.g. /dev/sdb).
    132   const std::string& file_path() const { return file_path_; }
    133 
    134   // Disk label.
    135   const std::string& label() const { return label_; }
    136 
    137   // Vendor ID of the device (e.g. "18d1").
    138   const std::string& vendor_id() const { return vendor_id_; }
    139 
    140   // Vendor name of the device (e.g. "Google Inc.").
    141   const std::string& vendor_name() const { return vendor_name_; }
    142 
    143   // Product ID of the device (e.g. "4e11").
    144   const std::string& product_id() const { return product_id_; }
    145 
    146   // Product name of the device (e.g. "Nexus One").
    147   const std::string& product_name() const { return product_name_; }
    148 
    149   // Disk model. (e.g. "TransMemory")
    150   const std::string& drive_label() const { return drive_model_; }
    151 
    152   // Device type. Not working well, yet.
    153   DeviceType device_type() const { return device_type_; }
    154 
    155   // Total size of the disk in bytes.
    156   uint64 total_size_in_bytes() const { return total_size_in_bytes_; }
    157 
    158   // Is the device read-only.
    159   bool is_read_only() const { return is_read_only_; }
    160 
    161   // Returns true if the device should be hidden from the file browser.
    162   bool is_hidden() const { return is_hidden_; }
    163 
    164   // Returns file system uuid.
    165   const std::string& uuid() const { return uuid_; }
    166 
    167  private:
    168   void InitializeFromResponse(dbus::Response* response);
    169 
    170   std::string device_path_;
    171   std::string mount_path_;
    172   std::string system_path_;
    173   bool is_drive_;
    174   bool has_media_;
    175   bool on_boot_device_;
    176   bool on_removable_device_;
    177 
    178   std::string file_path_;
    179   std::string label_;
    180   std::string vendor_id_;
    181   std::string vendor_name_;
    182   std::string product_id_;
    183   std::string product_name_;
    184   std::string drive_model_;
    185   DeviceType device_type_;
    186   uint64 total_size_in_bytes_;
    187   bool is_read_only_;
    188   bool is_hidden_;
    189   std::string uuid_;
    190 };
    191 
    192 // A struct to represent information about a mount point sent from cros-disks.
    193 struct CHROMEOS_EXPORT MountEntry {
    194  public:
    195   MountEntry()
    196       : error_code_(MOUNT_ERROR_UNKNOWN), mount_type_(MOUNT_TYPE_INVALID) {
    197   }
    198 
    199   MountEntry(MountError error_code,
    200              const std::string& source_path,
    201              MountType mount_type,
    202              const std::string& mount_path)
    203       : error_code_(error_code),
    204         source_path_(source_path),
    205         mount_type_(mount_type),
    206         mount_path_(mount_path) {
    207   }
    208 
    209   MountError error_code() const { return error_code_; }
    210   const std::string& source_path() const { return source_path_; }
    211   MountType mount_type() const { return mount_type_; }
    212   const std::string& mount_path() const { return mount_path_; }
    213 
    214  private:
    215   MountError error_code_;
    216   std::string source_path_;
    217   MountType mount_type_;
    218   std::string mount_path_;
    219 };
    220 
    221 // A class to make the actual DBus calls for cros-disks service.
    222 // This class only makes calls, result/error handling should be done
    223 // by callbacks.
    224 class CHROMEOS_EXPORT CrosDisksClient : public DBusClient {
    225  public:
    226   // A callback to handle the result of EnumerateAutoMountableDevices.
    227   // The argument is the enumerated device paths.
    228   typedef base::Callback<void(const std::vector<std::string>& device_paths)>
    229       EnumerateAutoMountableDevicesCallback;
    230 
    231   // A callback to handle the result of EnumerateMountEntries.
    232   // The argument is the enumerated mount entries.
    233   typedef base::Callback<void(const std::vector<MountEntry>& entries)>
    234       EnumerateMountEntriesCallback;
    235 
    236   // A callback to handle the result of GetDeviceProperties.
    237   // The argument is the information about the specified device.
    238   typedef base::Callback<void(const DiskInfo& disk_info)>
    239       GetDevicePropertiesCallback;
    240 
    241   // A callback to handle MountCompleted signal.
    242   typedef base::Callback<void(const MountEntry& entry)> MountCompletedHandler;
    243 
    244   // A callback to handle FormatCompleted signal.
    245   // The first argument is the error code.
    246   // The second argument is the device path.
    247   typedef base::Callback<void(FormatError error_code,
    248                               const std::string& device_path)>
    249       FormatCompletedHandler;
    250 
    251   // A callback to handle mount events.
    252   // The first argument is the event type.
    253   // The second argument is the device path.
    254   typedef base::Callback<void(MountEventType event_type,
    255                               const std::string& device_path)>
    256       MountEventHandler;
    257 
    258   virtual ~CrosDisksClient();
    259 
    260   // Calls Mount method.  |callback| is called after the method call succeeds,
    261   // otherwise, |error_callback| is called.
    262   // When mounting an archive, caller may set two optional arguments:
    263   // - The |source_format| argument passes the file extension (with the leading
    264   //   dot, for example ".zip"). If |source_format| is empty then the source
    265   //   format is auto-detected.
    266   // - The |mount_label| argument passes an optional mount label to be used as
    267   //   the directory name of the mount point. If |mount_label| is empty, the
    268   //   mount label will be based on the |source_path|.
    269   virtual void Mount(const std::string& source_path,
    270                      const std::string& source_format,
    271                      const std::string& mount_label,
    272                      const base::Closure& callback,
    273                      const base::Closure& error_callback) = 0;
    274 
    275   // Calls Unmount method.  |callback| is called after the method call succeeds,
    276   // otherwise, |error_callback| is called.
    277   virtual void Unmount(const std::string& device_path,
    278                        UnmountOptions options,
    279                        const base::Closure& callback,
    280                        const base::Closure& error_callback) = 0;
    281 
    282   // Calls EnumerateAutoMountableDevices method.  |callback| is called after the
    283   // method call succeeds, otherwise, |error_callback| is called.
    284   virtual void EnumerateAutoMountableDevices(
    285       const EnumerateAutoMountableDevicesCallback& callback,
    286       const base::Closure& error_callback) = 0;
    287 
    288   // Calls EnumerateMountEntries.  |callback| is called after the
    289   // method call succeeds, otherwise, |error_callback| is called.
    290   virtual void EnumerateMountEntries(
    291       const EnumerateMountEntriesCallback& callback,
    292       const base::Closure& error_callback) = 0;
    293 
    294   // Calls Format method.  |callback| is called after the method call succeeds,
    295   // otherwise, |error_callback| is called.
    296   virtual void Format(const std::string& device_path,
    297                       const std::string& filesystem,
    298                       const base::Closure& callback,
    299                       const base::Closure& error_callback) = 0;
    300 
    301   // Calls GetDeviceProperties method.  |callback| is called after the method
    302   // call succeeds, otherwise, |error_callback| is called.
    303   virtual void GetDeviceProperties(const std::string& device_path,
    304                                    const GetDevicePropertiesCallback& callback,
    305                                    const base::Closure& error_callback) = 0;
    306 
    307   // Registers |mount_event_handler| as a callback to be invoked when a mount
    308   // event signal is received.
    309   virtual void SetMountEventHandler(
    310       const MountEventHandler& mount_event_handler) = 0;
    311 
    312   // Registers |mount_completed_handler| as a callback to be invoked when a
    313   // MountCompleted signal is received.
    314   virtual void SetMountCompletedHandler(
    315       const MountCompletedHandler& mount_completed_handler) = 0;
    316 
    317   // Registers |format_completed_handler| as a callback to be invoked when a
    318   // FormatCompleted signal is received.
    319   virtual void SetFormatCompletedHandler(
    320       const FormatCompletedHandler& format_completed_handler) = 0;
    321 
    322   // Factory function, creates a new instance and returns ownership.
    323   // For normal usage, access the singleton via DBusThreadManager::Get().
    324   static CrosDisksClient* Create(DBusClientImplementationType type);
    325 
    326   // Returns the path of the mount point for archive files.
    327   static base::FilePath GetArchiveMountPoint();
    328 
    329   // Returns the path of the mount point for removable disks.
    330   static base::FilePath GetRemovableDiskMountPoint();
    331 
    332  protected:
    333   // Create() should be used instead.
    334   CrosDisksClient();
    335 
    336  private:
    337   DISALLOW_COPY_AND_ASSIGN(CrosDisksClient);
    338 };
    339 
    340 }  // namespace chromeos
    341 
    342 #endif  // CHROMEOS_DBUS_CROS_DISKS_CLIENT_H_
    343