OLD | NEW |
(Empty) | |
| 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 // Client code to talk to the MTP daemon. The MTP daemon is responsible for |
| 6 // communicating with PTP / MTP capable devices like cameras and smartphones. |
| 7 |
| 8 #ifndef CHROMEOS_DBUS_MTPD_CLIENT_H_ |
| 9 #define CHROMEOS_DBUS_MTPD_CLIENT_H_ |
| 10 |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/basictypes.h" |
| 15 #include "base/callback.h" |
| 16 #include "base/time.h" |
| 17 #include "chromeos/chromeos_export.h" |
| 18 #include "chromeos/dbus/dbus_client_implementation_type.h" |
| 19 |
| 20 namespace dbus { |
| 21 class Bus; |
| 22 class Response; |
| 23 } |
| 24 |
| 25 namespace chromeos { |
| 26 |
| 27 // Mode to open a storage in. |
| 28 enum OpenStorageMode { |
| 29 OPEN_STORAGE_MODE_READ_ONLY, |
| 30 }; |
| 31 |
| 32 // Values match libmtp values unless noted below. |
| 33 // TODO(thestig) See if we can do better than this. |
| 34 enum FileType { |
| 35 FILE_TYPE_FOLDER = 0, |
| 36 FILE_TYPE_JPEG = 14, |
| 37 FILE_TYPE_JFIF = 15, |
| 38 FILE_TYPE_TIFF = 16, |
| 39 FILE_TYPE_BMP = 17, |
| 40 FILE_TYPE_GIF = 18, |
| 41 FILE_TYPE_PICT = 19, |
| 42 FILE_TYPE_PNG = 20, |
| 43 FILE_TYPE_WINDOWSIMAGEFORMAT = 25, |
| 44 FILE_TYPE_JP2 = 40, |
| 45 FILE_TYPE_JPX = 41, |
| 46 // Truly unknown file type. |
| 47 FILE_TYPE_UNKNOWN = 44, |
| 48 // There's more file types to map to, but right now they are not interesting. |
| 49 // Just assign a dummy value for now. |
| 50 FILE_TYPE_OTHER = 9999 |
| 51 }; |
| 52 |
| 53 // A class to represent information about a storage sent from mtpd. |
| 54 class StorageInfo { |
| 55 public: |
| 56 StorageInfo(const std::string& storage_name, dbus::Response* response); |
| 57 ~StorageInfo(); |
| 58 |
| 59 // Storage name. (e.g. usb:1,5:65537) |
| 60 const std::string& storage_name() const { return storage_name_; } |
| 61 |
| 62 // Vendor. (e.g. Kodak) |
| 63 const std::string& vendor() const { return vendor_; } |
| 64 |
| 65 // Vendor ID. (e.g. 0x040a) |
| 66 uint16 vendor_id() const { return vendor_id_; } |
| 67 |
| 68 // Product. (e.g. DC4800) |
| 69 const std::string& product() const { return product_; } |
| 70 |
| 71 // Vendor ID. (e.g. 0x0160) |
| 72 uint16 product_id() const { return product_id_; } |
| 73 |
| 74 // Device flags as defined by libmtp. |
| 75 uint32 device_flags() const { return device_flags_; } |
| 76 |
| 77 // Storage type as defined in libmtp. (e.g. PTP_ST_FixedROM) |
| 78 uint16 storage_type() const { return storage_type_; } |
| 79 |
| 80 // File system type as defined in libmtp. (e.g. PTP_FST_DCF) |
| 81 uint16 filesystem_type() const { return filesystem_type_; } |
| 82 |
| 83 // Access capability as defined in libmtp. (e.g. PTP_AC_ReadWrite) |
| 84 uint16 access_capability() const { return access_capability_; } |
| 85 |
| 86 // Max capacity in bytes. |
| 87 uint64 max_capacity() const { return max_capacity_; } |
| 88 |
| 89 // Free space in byte. |
| 90 uint64 free_space_in_bytes() const { return free_space_in_bytes_; } |
| 91 |
| 92 // Free space in number of objects. |
| 93 uint64 free_space_in_objects() const { return free_space_in_objects_; } |
| 94 |
| 95 // Storage description. (e.g. internal memory) |
| 96 const std::string& storage_description() const { |
| 97 return storage_description_; |
| 98 } |
| 99 |
| 100 // Volume identifier. (e.g. the serial number, should be unique) |
| 101 const std::string& volume_identifier() const { return volume_identifier_; } |
| 102 |
| 103 private: |
| 104 void InitializeFromResponse(dbus::Response* response); |
| 105 |
| 106 // Device info. (A device can have multiple storages) |
| 107 std::string vendor_; |
| 108 uint16 vendor_id_; |
| 109 std::string product_; |
| 110 uint16 product_id_; |
| 111 uint32 device_flags_; |
| 112 |
| 113 // Storage info. |
| 114 std::string storage_name_; |
| 115 uint16 storage_type_; |
| 116 uint16 filesystem_type_; |
| 117 uint16 access_capability_; |
| 118 uint64 max_capacity_; |
| 119 uint64 free_space_in_bytes_; |
| 120 uint64 free_space_in_objects_; |
| 121 std::string storage_description_; |
| 122 std::string volume_identifier_; |
| 123 }; |
| 124 |
| 125 // A class to represent information about a file entry sent from mtpd. |
| 126 class FileEntry { |
| 127 public: |
| 128 explicit FileEntry(dbus::Response* response); |
| 129 ~FileEntry(); |
| 130 |
| 131 // ID for the file. |
| 132 uint32 item_id() const { return item_id_; } |
| 133 |
| 134 // ID for the file's parent. |
| 135 uint32 parent_id() const { return parent_id_; } |
| 136 |
| 137 // Name of the file. |
| 138 const std::string& file_name() const { return file_name_; } |
| 139 |
| 140 // Size of the file. |
| 141 uint64 file_size() const { return file_size_; } |
| 142 |
| 143 // Modification time of the file. |
| 144 base::Time modification_date() const { return modification_date_; } |
| 145 |
| 146 // File type. |
| 147 FileType file_type() const { return file_type_; } |
| 148 |
| 149 private: |
| 150 void InitializeFromResponse(dbus::Response* response); |
| 151 |
| 152 // Storage info. |
| 153 uint32 item_id_; |
| 154 uint32 parent_id_; |
| 155 std::string file_name_; |
| 156 uint64 file_size_; |
| 157 base::Time modification_date_; |
| 158 FileType file_type_; |
| 159 }; |
| 160 |
| 161 // A class to make the actual DBus calls for mtpd service. |
| 162 // This class only makes calls, result/error handling should be done |
| 163 // by callbacks. |
| 164 class CHROMEOS_EXPORT MTPDClient { |
| 165 public: |
| 166 // A callback to be called when DBus method call fails. |
| 167 typedef base::Callback<void()> ErrorCallback; |
| 168 |
| 169 // A callback to handle the result of EnumerateAutoMountableDevices. |
| 170 // The argument is the enumerated storage names. |
| 171 typedef base::Callback<void(const std::vector<std::string>& storage_names) |
| 172 > EnumerateStorageCallback; |
| 173 |
| 174 // A callback to handle the result of GetStorageInfo. |
| 175 // The argument is the information about the specified storage. |
| 176 typedef base::Callback<void(const StorageInfo& storage_info) |
| 177 > GetStorageInfoCallback; |
| 178 |
| 179 // A callback to handle the result of OpenStorage. |
| 180 // The argument is the returned handle. |
| 181 typedef base::Callback<void(const std::string& handle)> OpenStorageCallback; |
| 182 |
| 183 // A callback to handle the result of CloseStorage. |
| 184 typedef base::Callback<void()> CloseStorageCallback; |
| 185 |
| 186 // A callback to handle the result of ReadDirectoryByPath/Id. |
| 187 // The argument is a vector of file entries. |
| 188 typedef base::Callback<void(const std::vector<FileEntry>& file_entries) |
| 189 > ReadDirectoryCallback; |
| 190 |
| 191 // A callback to handle the result of ReadFileByPath/Id. |
| 192 // The argument is a string containing the file data. |
| 193 // TODO(thestig) Consider using a file descriptor instead of the data. |
| 194 typedef base::Callback<void(const std::string& data)> ReadFileCallback; |
| 195 |
| 196 // A callback to handle storage attach/detach events. |
| 197 // The first argument is true for attach, false for detach. |
| 198 // The second argument is the storage name. |
| 199 typedef base::Callback<void(bool is_attach, |
| 200 const std::string& storage_name) |
| 201 > MTPStorageEventHandler; |
| 202 |
| 203 virtual ~MTPDClient(); |
| 204 |
| 205 // Calls EnumerateStorage method. |callback| is called after the |
| 206 // method call succeeds, otherwise, |error_callback| is called. |
| 207 virtual void EnumerateStorage( |
| 208 const EnumerateStorageCallback& callback, |
| 209 const ErrorCallback& error_callback) = 0; |
| 210 |
| 211 // Calls GetStorageInfo method. |callback| is called after the method call |
| 212 // succeeds, otherwise, |error_callback| is called. |
| 213 virtual void GetStorageInfo(const std::string& storage_name, |
| 214 const GetStorageInfoCallback& callback, |
| 215 const ErrorCallback& error_callback) = 0; |
| 216 |
| 217 // Calls OpenStorage method. |callback| is called after the method call |
| 218 // succeeds, otherwise, |error_callback| is called. |
| 219 // OpenStorage returns a handle in |callback|. |
| 220 virtual void OpenStorage(const std::string& storage_name, |
| 221 OpenStorageMode mode, |
| 222 const OpenStorageCallback& callback, |
| 223 const ErrorCallback& error_callback) = 0; |
| 224 |
| 225 // Calls CloseStorage method. |callback| is called after the method call |
| 226 // succeeds, otherwise, |error_callback| is called. |
| 227 // |handle| comes from a OpenStorageCallback. |
| 228 virtual void CloseStorage(const std::string& handle, |
| 229 const CloseStorageCallback& callback, |
| 230 const ErrorCallback& error_callback) = 0; |
| 231 |
| 232 // Calls ReadDirectoryByPath method. |callback| is called after the method |
| 233 // call succeeds, otherwise, |error_callback| is called. |
| 234 virtual void ReadDirectoryByPath(const std::string& handle, |
| 235 const std::string& path, |
| 236 const ReadDirectoryCallback& callback, |
| 237 const ErrorCallback& error_callback) = 0; |
| 238 |
| 239 // Calls ReadDirectoryById method. |callback| is called after the method |
| 240 // call succeeds, otherwise, |error_callback| is called. |
| 241 // |file_id| is a MTP-device specific id for a file. |
| 242 virtual void ReadDirectoryById(const std::string& handle, |
| 243 uint32 file_id, |
| 244 const ReadDirectoryCallback& callback, |
| 245 const ErrorCallback& error_callback) = 0; |
| 246 |
| 247 // Calls ReadFileByPath method. |callback| is called after the method call |
| 248 // succeeds, otherwise, |error_callback| is called. |
| 249 virtual void ReadFileByPath(const std::string& handle, |
| 250 const std::string& path, |
| 251 const ReadFileCallback& callback, |
| 252 const ErrorCallback& error_callback) = 0; |
| 253 |
| 254 // Calls ReadFileById method. |callback| is called after the method call |
| 255 // succeeds, otherwise, |error_callback| is called. |
| 256 // |file_id| is a MTP-device specific id for a file. |
| 257 virtual void ReadFileById(const std::string& handle, |
| 258 uint32 file_id, |
| 259 const ReadFileCallback& callback, |
| 260 const ErrorCallback& error_callback) = 0; |
| 261 |
| 262 // Registers given callback for events. |
| 263 // |storage_event_handler| is called when a mtp storage attach or detach |
| 264 // signal is received. |
| 265 virtual void SetUpConnections(const MTPStorageEventHandler& handler) = 0; |
| 266 |
| 267 // Factory function, creates a new instance and returns ownership. |
| 268 // For normal usage, access the singleton via DBusThreadManager::Get(). |
| 269 static MTPDClient* Create(DBusClientImplementationType type, dbus::Bus* bus); |
| 270 |
| 271 protected: |
| 272 // Create() should be used instead. |
| 273 MTPDClient(); |
| 274 |
| 275 private: |
| 276 DISALLOW_COPY_AND_ASSIGN(MTPDClient); |
| 277 }; |
| 278 |
| 279 } // namespace chromeos |
| 280 |
| 281 #endif // CHROMEOS_DBUS_MTPD_CLIENT_H_ |
OLD | NEW |