summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/media_transfer_protocol_daemon_client.h
blob: a5779377ce7153faf1bd69b2bb10f2ed0698f7e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Client code to talk to the Media Transfer Protocol daemon. The MTP daemon is
// responsible for communicating with PTP / MTP capable devices like cameras
// and smartphones.

#ifndef CHROMEOS_DBUS_MEDIA_TRANSFER_PROTOCOL_DAEMON_CLIENT_H_
#define CHROMEOS_DBUS_MEDIA_TRANSFER_PROTOCOL_DAEMON_CLIENT_H_

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/time.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_client_implementation_type.h"

namespace dbus {
class Bus;
class Response;
}

namespace chromeos {

// Mode to open a storage in.
enum OpenStorageMode {
  OPEN_STORAGE_MODE_READ_ONLY,
};

// Values match libmtp values unless noted below.
// TODO(thestig) See if we can do better than this.
enum FileType {
  FILE_TYPE_FOLDER = 0,
  FILE_TYPE_JPEG = 14,
  FILE_TYPE_JFIF = 15,
  FILE_TYPE_TIFF = 16,
  FILE_TYPE_BMP = 17,
  FILE_TYPE_GIF = 18,
  FILE_TYPE_PICT = 19,
  FILE_TYPE_PNG = 20,
  FILE_TYPE_WINDOWSIMAGEFORMAT = 25,
  FILE_TYPE_JP2 = 40,
  FILE_TYPE_JPX = 41,
  // Truly unknown file type.
  FILE_TYPE_UNKNOWN = 44,
  // There's more file types to map to, but right now they are not interesting.
  // Just assign a dummy value for now.
  FILE_TYPE_OTHER = 9999
};

// A class to represent information about a storage sent from mtpd.
class CHROMEOS_EXPORT StorageInfo {
 public:
  StorageInfo();
  StorageInfo(const std::string& storage_name, dbus::Response* response);
  ~StorageInfo();

  // Storage name. (e.g. usb:1,5:65537)
  const std::string& storage_name() const { return storage_name_; }

  // Vendor. (e.g. Kodak)
  const std::string& vendor() const { return vendor_; }

  // Vendor ID. (e.g. 0x040a)
  uint16 vendor_id() const { return vendor_id_; }

  // Product. (e.g. DC4800)
  const std::string& product() const { return product_; }

  // Vendor ID. (e.g. 0x0160)
  uint16 product_id() const { return product_id_; }

  // Device flags as defined by libmtp.
  uint32 device_flags() const { return device_flags_; }

  // Storage type as defined in libmtp. (e.g. PTP_ST_FixedROM)
  uint16 storage_type() const { return storage_type_; }

  // File system type as defined in libmtp. (e.g. PTP_FST_DCF)
  uint16 filesystem_type() const { return filesystem_type_; }

  // Access capability as defined in libmtp. (e.g. PTP_AC_ReadWrite)
  uint16 access_capability() const { return access_capability_; }

  // Max capacity in bytes.
  uint64 max_capacity() const { return max_capacity_; }

  // Free space in byte.
  uint64 free_space_in_bytes() const { return free_space_in_bytes_; }

  // Free space in number of objects.
  uint64 free_space_in_objects() const { return free_space_in_objects_; }

  // Storage description. (e.g. internal memory)
  const std::string& storage_description() const {
    return storage_description_;
  }

  // Volume identifier. (e.g. the serial number, should be unique)
  const std::string& volume_identifier() const { return volume_identifier_; }

 private:
  void InitializeFromResponse(dbus::Response* response);

  // Device info. (A device can have multiple storages)
  std::string vendor_;
  uint16 vendor_id_;
  std::string product_;
  uint16 product_id_;
  uint32 device_flags_;

  // Storage info.
  std::string storage_name_;
  uint16 storage_type_;
  uint16 filesystem_type_;
  uint16 access_capability_;
  uint64 max_capacity_;
  uint64 free_space_in_bytes_;
  uint64 free_space_in_objects_;
  std::string storage_description_;
  std::string volume_identifier_;
};

// A class to represent information about a file entry sent from mtpd.
class CHROMEOS_EXPORT FileEntry {
 public:
  FileEntry();
  explicit FileEntry(dbus::Response* response);
  ~FileEntry();

  // ID for the file.
  uint32 item_id() const { return item_id_; }

  // ID for the file's parent.
  uint32 parent_id() const { return parent_id_; }

  // Name of the file.
  const std::string& file_name() const { return file_name_; }

  // Size of the file.
  uint64 file_size() const { return file_size_; }

  // Modification time of the file.
  base::Time modification_date() const { return modification_date_; }

  // File type.
  FileType file_type() const { return file_type_; }

 private:
  void InitializeFromResponse(dbus::Response* response);

  // Storage info.
  uint32 item_id_;
  uint32 parent_id_;
  std::string file_name_;
  uint64 file_size_;
  base::Time modification_date_;
  FileType file_type_;
};

// A class to make the actual DBus calls for mtpd service.
// This class only makes calls, result/error handling should be done
// by callbacks.
class CHROMEOS_EXPORT MediaTransferProtocolDaemonClient {
 public:
  // A callback to be called when DBus method call fails.
  typedef base::Callback<void()> ErrorCallback;

  // A callback to handle the result of EnumerateAutoMountableDevices.
  // The argument is the enumerated storage names.
  typedef base::Callback<void(const std::vector<std::string>& storage_names)
                         > EnumerateStorageCallback;

  // A callback to handle the result of GetStorageInfo.
  // The argument is the information about the specified storage.
  typedef base::Callback<void(const StorageInfo& storage_info)
                         > GetStorageInfoCallback;

  // A callback to handle the result of OpenStorage.
  // The argument is the returned handle.
  typedef base::Callback<void(const std::string& handle)> OpenStorageCallback;

  // A callback to handle the result of CloseStorage.
  typedef base::Callback<void()> CloseStorageCallback;

  // A callback to handle the result of ReadDirectoryByPath/Id.
  // The argument is a vector of file entries.
  typedef base::Callback<void(const std::vector<FileEntry>& file_entries)
                         > ReadDirectoryCallback;

  // A callback to handle the result of ReadFileByPath/Id.
  // The argument is a string containing the file data.
  // TODO(thestig) Consider using a file descriptor instead of the data.
  typedef base::Callback<void(const std::string& data)> ReadFileCallback;

  // A callback to handle the result of GetFileInfoByPath/Id.
  // The argument is a file entry.
  typedef base::Callback<void(const FileEntry& file_entry)
                         > GetFileInfoCallback;

  // A callback to handle storage attach/detach events.
  // The first argument is true for attach, false for detach.
  // The second argument is the storage name.
  typedef base::Callback<void(bool is_attach,
                              const std::string& storage_name)
                         > MTPStorageEventHandler;

  virtual ~MediaTransferProtocolDaemonClient();

  // Calls EnumerateStorage method. |callback| is called after the
  // method call succeeds, otherwise, |error_callback| is called.
  virtual void EnumerateStorage(
      const EnumerateStorageCallback& callback,
      const ErrorCallback& error_callback) = 0;

  // Calls GetStorageInfo method. |callback| is called after the method call
  // succeeds, otherwise, |error_callback| is called.
  virtual void GetStorageInfo(const std::string& storage_name,
                              const GetStorageInfoCallback& callback,
                              const ErrorCallback& error_callback) = 0;

  // Calls OpenStorage method. |callback| is called after the method call
  // succeeds, otherwise, |error_callback| is called.
  // OpenStorage returns a handle in |callback|.
  virtual void OpenStorage(const std::string& storage_name,
                           OpenStorageMode mode,
                           const OpenStorageCallback& callback,
                           const ErrorCallback& error_callback) = 0;

  // Calls CloseStorage method. |callback| is called after the method call
  // succeeds, otherwise, |error_callback| is called.
  // |handle| comes from a OpenStorageCallback.
  virtual void CloseStorage(const std::string& handle,
                            const CloseStorageCallback& callback,
                            const ErrorCallback& error_callback) = 0;

  // Calls ReadDirectoryByPath method. |callback| is called after the method
  // call succeeds, otherwise, |error_callback| is called.
  virtual void ReadDirectoryByPath(const std::string& handle,
                                   const std::string& path,
                                   const ReadDirectoryCallback& callback,
                                   const ErrorCallback& error_callback) = 0;

  // Calls ReadDirectoryById method. |callback| is called after the method
  // call succeeds, otherwise, |error_callback| is called.
  // |file_id| is a MTP-device specific id for a file.
  virtual void ReadDirectoryById(const std::string& handle,
                                 uint32 file_id,
                                 const ReadDirectoryCallback& callback,
                                 const ErrorCallback& error_callback) = 0;

  // Calls ReadFileByPath method. |callback| is called after the method call
  // succeeds, otherwise, |error_callback| is called.
  virtual void ReadFileByPath(const std::string& handle,
                              const std::string& path,
                              const ReadFileCallback& callback,
                              const ErrorCallback& error_callback) = 0;

  // Calls ReadFileById method. |callback| is called after the method call
  // succeeds, otherwise, |error_callback| is called.
  // |file_id| is a MTP-device specific id for a file.
  virtual void ReadFileById(const std::string& handle,
                            uint32 file_id,
                            const ReadFileCallback& callback,
                            const ErrorCallback& error_callback) = 0;

  // Calls GetFileInfoByPath method. |callback| is called after the method
  // call succeeds, otherwise, |error_callback| is called.
  virtual void GetFileInfoByPath(const std::string& handle,
                                 const std::string& path,
                                 const GetFileInfoCallback& callback,
                                 const ErrorCallback& error_callback) = 0;

  // Calls GetFileInfoById method. |callback| is called after the method
  // call succeeds, otherwise, |error_callback| is called.
  // |file_id| is a MTP-device specific id for a file.
  virtual void GetFileInfoById(const std::string& handle,
                               uint32 file_id,
                               const GetFileInfoCallback& callback,
                               const ErrorCallback& error_callback) = 0;

  // Registers given callback for events.
  // |storage_event_handler| is called when a mtp storage attach or detach
  // signal is received.
  virtual void SetUpConnections(const MTPStorageEventHandler& handler) = 0;

  // Factory function, creates a new instance and returns ownership.
  // For normal usage, access the singleton via DBusThreadManager::Get().
  static MediaTransferProtocolDaemonClient*
      Create(DBusClientImplementationType type, dbus::Bus* bus);

 protected:
  // Create() should be used instead.
  MediaTransferProtocolDaemonClient();

 private:
  DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDaemonClient);
};

}  // namespace chromeos

#endif  // CHROMEOS_DBUS_MEDIA_TRANSFER_PROTOCOL_DAEMON_CLIENT_H_