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