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.
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
9 #ifndef DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_DAEMON_CLIENT_H_
10 #define DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_DAEMON_CLIENT_H_
15 #include "base/basictypes.h"
16 #include "base/callback.h"
17 #include "build/build_config.h"
19 #if !defined(OS_LINUX)
20 #error "Only used on Linux and ChromeOS"
32 // A class to make the actual DBus calls for mtpd service.
33 // This class only makes calls, result/error handling should be done
35 class MediaTransferProtocolDaemonClient
{
37 // A callback to be called when DBus method call fails.
38 typedef base::Closure ErrorCallback
;
40 // A callback to handle the result of EnumerateAutoMountableDevices.
41 // The argument is the enumerated storage names.
42 typedef base::Callback
<void(const std::vector
<std::string
>& storage_names
)
43 > EnumerateStoragesCallback
;
45 // A callback to handle the result of GetStorageInfo.
46 // The argument is the information about the specified storage.
47 typedef base::Callback
<void(const MtpStorageInfo
& storage_info
)
48 > GetStorageInfoCallback
;
50 // A callback to handle the result of OpenStorage.
51 // The argument is the returned handle.
52 typedef base::Callback
<void(const std::string
& handle
)> OpenStorageCallback
;
54 // A callback to handle the result of CloseStorage.
55 typedef base::Closure CloseStorageCallback
;
57 // A callback to handle the result of CreateDirectory.
58 typedef base::Closure CreateDirectoryCallback
;
60 // A callback to handle the result of ReadDirectoryEntryIds.
61 // The argument is a vector of file ids.
62 typedef base::Callback
<void(const std::vector
<uint32
>& file_ids
)
63 > ReadDirectoryEntryIdsCallback
;
65 // A callback to handle the result of GetFileInfo.
66 // The argument is a vector of file entries.
67 typedef base::Callback
<void(const std::vector
<MtpFileEntry
>& file_entries
)
68 > GetFileInfoCallback
;
70 // A callback to handle the result of ReadFileChunkById.
71 // The argument is a string containing the file data.
72 typedef base::Callback
<void(const std::string
& data
)> ReadFileCallback
;
74 // A callback to handle the result of RenameObject.
75 typedef base::Closure RenameObjectCallback
;
77 // A callback to handle the result of CopyFileFromLocal.
78 typedef base::Closure CopyFileFromLocalCallback
;
80 // A callback to handle the result of DeleteObject.
81 typedef base::Closure DeleteObjectCallback
;
83 // A callback to handle storage attach/detach events.
84 // The first argument is true for attach, false for detach.
85 // The second argument is the storage name.
86 typedef base::Callback
<void(bool is_attach
,
87 const std::string
& storage_name
)
88 > MTPStorageEventHandler
;
90 virtual ~MediaTransferProtocolDaemonClient();
92 // Calls EnumerateStorages method. |callback| is called after the
93 // method call succeeds, otherwise, |error_callback| is called.
94 virtual void EnumerateStorages(
95 const EnumerateStoragesCallback
& callback
,
96 const ErrorCallback
& error_callback
) = 0;
98 // Calls GetStorageInfo method. |callback| is called after the method call
99 // succeeds, otherwise, |error_callback| is called.
100 virtual void GetStorageInfo(const std::string
& storage_name
,
101 const GetStorageInfoCallback
& callback
,
102 const ErrorCallback
& error_callback
) = 0;
104 // Calls GetStorageInfoFromDevice method. |callback| is called after the
105 // method call succeeds, otherwise, |error_callback| is called.
106 virtual void GetStorageInfoFromDevice(
107 const std::string
& storage_name
,
108 const GetStorageInfoCallback
& callback
,
109 const ErrorCallback
& error_callback
) = 0;
111 // Calls OpenStorage method. |callback| is called after the method call
112 // succeeds, otherwise, |error_callback| is called.
113 // OpenStorage returns a handle in |callback|.
114 virtual void OpenStorage(const std::string
& storage_name
,
115 const std::string
& mode
,
116 const OpenStorageCallback
& callback
,
117 const ErrorCallback
& error_callback
) = 0;
119 // Calls CloseStorage method. |callback| is called after the method call
120 // succeeds, otherwise, |error_callback| is called.
121 // |handle| comes from a OpenStorageCallback.
122 virtual void CloseStorage(const std::string
& handle
,
123 const CloseStorageCallback
& callback
,
124 const ErrorCallback
& error_callback
) = 0;
126 // Calls CreateDirectory method. |callback| is called after the method call
127 // succeeds, otherwise, |error_callback| is called.
128 // |parent_id| is an id of the parent directory.
129 // |directory_name| is name of new directory.
130 virtual void CreateDirectory(const std::string
& handle
,
131 const uint32 parent_id
,
132 const std::string
& directory_name
,
133 const CreateDirectoryCallback
& callback
,
134 const ErrorCallback
& error_callback
) = 0;
136 // Calls ReadDirectoryEntryIds method. |callback| is called after the method
137 // call succeeds, otherwise, |error_callback| is called.
138 // |file_id| is a MTP-device specific id for a file.
139 virtual void ReadDirectoryEntryIds(
140 const std::string
& handle
,
142 const ReadDirectoryEntryIdsCallback
& callback
,
143 const ErrorCallback
& error_callback
) = 0;
145 // Calls GetFileInfo method. |callback| is called after the method
146 // call succeeds, otherwise, |error_callback| is called.
147 // |file_ids| is a list of MTP-device specific file ids.
148 // |offset| is the index into |file_ids| to read from.
149 // |entries_to_read| is the maximum number of file entries to read.
150 virtual void GetFileInfo(const std::string
& handle
,
151 const std::vector
<uint32
>& file_ids
,
153 size_t entries_to_read
,
154 const GetFileInfoCallback
& callback
,
155 const ErrorCallback
& error_callback
) = 0;
157 // Calls ReadFileChunk method. |callback| is called after the method call
158 // succeeds, otherwise, |error_callback| is called.
159 // |file_id| is a MTP-device specific id for a file.
160 // |offset| is the offset into the file.
161 // |bytes_to_read| cannot exceed 1 MiB.
162 virtual void ReadFileChunk(const std::string
& handle
,
165 uint32 bytes_to_read
,
166 const ReadFileCallback
& callback
,
167 const ErrorCallback
& error_callback
) = 0;
169 // Calls RenameObject method. |callback| is called after the method call
170 // succeeds, otherwise, |error_callback| is called.
171 // |object_is| is an id of object to be renamed.
172 // |new_name| is new name of the object.
173 virtual void RenameObject(const std::string
& handle
,
174 const uint32 object_id
,
175 const std::string
& new_name
,
176 const RenameObjectCallback
& callback
,
177 const ErrorCallback
& error_callback
) = 0;
179 // Calls CopyFileFromLocal method. |callback| is called after the method call
180 // succeeds, otherwise, |error_callback| is called.
181 // |source_file_descriptor| is a file descriptor of source file.
182 // |parent_id| is a object id of a target directory.
183 // |file_name| is a file name of a target file.
184 virtual void CopyFileFromLocal(const std::string
& handle
,
185 const int source_file_descriptor
,
186 const uint32 parent_id
,
187 const std::string
& file_name
,
188 const CopyFileFromLocalCallback
& callback
,
189 const ErrorCallback
& error_callback
) = 0;
191 // Calls DeleteObject method. |callback| is called after the method call
192 // succeeds, otherwise, |error_callback| is called.
193 // |object_id| is an object id of a file or directory which is deleted.
194 virtual void DeleteObject(const std::string
& handle
,
195 const uint32 object_id
,
196 const DeleteObjectCallback
& callback
,
197 const ErrorCallback
& error_callback
) = 0;
199 // Registers given callback for events. Should only be called once.
200 // |storage_event_handler| is called when a mtp storage attach or detach
201 // signal is received.
202 virtual void ListenForChanges(const MTPStorageEventHandler
& handler
) = 0;
204 // Factory function, creates a new instance and returns ownership.
205 static MediaTransferProtocolDaemonClient
* Create(dbus::Bus
* bus
);
208 // Create() should be used instead.
209 MediaTransferProtocolDaemonClient();
212 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDaemonClient
);
215 } // namespace device
217 #endif // DEVICE_MEDIA_TRANSFER_PROTOCOL_MEDIA_TRANSFER_PROTOCOL_DAEMON_CLIENT_H_