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