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 #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h"
10 #include "base/memory/weak_ptr.h"
12 #include "dbus/message.h"
13 #include "dbus/object_path.h"
14 #include "dbus/object_proxy.h"
15 #include "device/media_transfer_protocol/mtp_file_entry.pb.h"
16 #include "device/media_transfer_protocol/mtp_storage_info.pb.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
23 const char kInvalidResponseMsg
[] = "Invalid Response: ";
24 uint32 kMaxChunkSize
= 1024*1024; // D-Bus has message size limits.
26 // The MediaTransferProtocolDaemonClient implementation.
27 class MediaTransferProtocolDaemonClientImpl
28 : public MediaTransferProtocolDaemonClient
{
30 explicit MediaTransferProtocolDaemonClientImpl(dbus::Bus
* bus
)
31 : proxy_(bus
->GetObjectProxy(
32 mtpd::kMtpdServiceName
,
33 dbus::ObjectPath(mtpd::kMtpdServicePath
))),
34 listen_for_changes_called_(false),
35 weak_ptr_factory_(this) {
38 // MediaTransferProtocolDaemonClient override.
39 void EnumerateStorages(const EnumerateStoragesCallback
& callback
,
40 const ErrorCallback
& error_callback
) override
{
41 dbus::MethodCall
method_call(mtpd::kMtpdInterface
,
42 mtpd::kEnumerateStorages
);
44 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
45 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnEnumerateStorages
,
46 weak_ptr_factory_
.GetWeakPtr(),
51 // MediaTransferProtocolDaemonClient override.
52 void GetStorageInfo(const std::string
& storage_name
,
53 const GetStorageInfoCallback
& callback
,
54 const ErrorCallback
& error_callback
) override
{
55 dbus::MethodCall
method_call(mtpd::kMtpdInterface
, mtpd::kGetStorageInfo
);
56 dbus::MessageWriter
writer(&method_call
);
57 writer
.AppendString(storage_name
);
59 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
60 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnGetStorageInfo
,
61 weak_ptr_factory_
.GetWeakPtr(),
67 // MediaTransferProtocolDaemonClient override.
68 void OpenStorage(const std::string
& storage_name
,
69 const std::string
& mode
,
70 const OpenStorageCallback
& callback
,
71 const ErrorCallback
& error_callback
) override
{
72 dbus::MethodCall
method_call(mtpd::kMtpdInterface
, mtpd::kOpenStorage
);
73 dbus::MessageWriter
writer(&method_call
);
74 writer
.AppendString(storage_name
);
75 DCHECK_EQ(mtpd::kReadOnlyMode
, mode
);
76 writer
.AppendString(mtpd::kReadOnlyMode
);
78 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
79 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnOpenStorage
,
80 weak_ptr_factory_
.GetWeakPtr(),
85 // MediaTransferProtocolDaemonClient override.
86 void CloseStorage(const std::string
& handle
,
87 const CloseStorageCallback
& callback
,
88 const ErrorCallback
& error_callback
) override
{
89 dbus::MethodCall
method_call(mtpd::kMtpdInterface
, mtpd::kCloseStorage
);
90 dbus::MessageWriter
writer(&method_call
);
91 writer
.AppendString(handle
);
93 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
94 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCloseStorage
,
95 weak_ptr_factory_
.GetWeakPtr(),
100 // MediaTransferProtocolDaemonClient override.
101 void ReadDirectoryEntryIds(const std::string
& handle
,
103 const ReadDirectoryEntryIdsCallback
& callback
,
104 const ErrorCallback
& error_callback
) override
{
105 dbus::MethodCall
method_call(mtpd::kMtpdInterface
,
106 mtpd::kReadDirectoryEntryIds
);
107 dbus::MessageWriter
writer(&method_call
);
108 writer
.AppendString(handle
);
109 writer
.AppendUint32(file_id
);
111 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
112 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadDirectoryIds
,
113 weak_ptr_factory_
.GetWeakPtr(),
118 void GetFileInfo(const std::string
& handle
,
119 const std::vector
<uint32
>& file_ids
,
121 size_t entries_to_read
,
122 const GetFileInfoCallback
& callback
,
123 const ErrorCallback
& error_callback
) override
{
124 if (offset
>= file_ids
.size()) {
125 error_callback
.Run();
129 dbus::MethodCall
method_call(mtpd::kMtpdInterface
, mtpd::kGetFileInfo
);
130 dbus::MessageWriter
writer(&method_call
);
131 writer
.AppendString(handle
);
133 dbus::MessageWriter
array_writer(NULL
);
134 writer
.OpenArray("u", &array_writer
);
136 size_t end_offset
= file_ids
.size();
137 if (offset
<= SIZE_MAX
- entries_to_read
)
138 end_offset
= std::min(end_offset
, offset
+ entries_to_read
);
139 for (size_t i
= offset
; i
< end_offset
; ++i
)
140 array_writer
.AppendUint32(file_ids
[i
]);
142 writer
.CloseContainer(&array_writer
);
145 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
146 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnGetFileInfo
,
147 weak_ptr_factory_
.GetWeakPtr(),
152 // MediaTransferProtocolDaemonClient override.
153 void ReadFileChunk(const std::string
& handle
,
156 uint32 bytes_to_read
,
157 const ReadFileCallback
& callback
,
158 const ErrorCallback
& error_callback
) override
{
159 DCHECK_LE(bytes_to_read
, kMaxChunkSize
);
160 dbus::MethodCall
method_call(mtpd::kMtpdInterface
, mtpd::kReadFileChunk
);
161 dbus::MessageWriter
writer(&method_call
);
162 writer
.AppendString(handle
);
163 writer
.AppendUint32(file_id
);
164 writer
.AppendUint32(offset
);
165 writer
.AppendUint32(bytes_to_read
);
167 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
168 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadFile
,
169 weak_ptr_factory_
.GetWeakPtr(),
174 // MediaTransferProtocolDaemonClient override.
175 void ListenForChanges(const MTPStorageEventHandler
& handler
) override
{
176 DCHECK(!listen_for_changes_called_
);
177 listen_for_changes_called_
= true;
179 static const SignalEventTuple kSignalEventTuples
[] = {
180 { mtpd::kMTPStorageAttached
, true },
181 { mtpd::kMTPStorageDetached
, false },
183 const size_t kNumSignalEventTuples
= arraysize(kSignalEventTuples
);
185 for (size_t i
= 0; i
< kNumSignalEventTuples
; ++i
) {
186 proxy_
->ConnectToSignal(
187 mtpd::kMtpdInterface
,
188 kSignalEventTuples
[i
].signal_name
,
189 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnMTPStorageSignal
,
190 weak_ptr_factory_
.GetWeakPtr(),
192 kSignalEventTuples
[i
].is_attach
),
193 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnSignalConnected
,
194 weak_ptr_factory_
.GetWeakPtr()));
199 // A struct to contain a pair of signal name and attachment event type.
200 // Used by SetUpConnections.
201 struct SignalEventTuple
{
202 const char* signal_name
;
206 // Handles the result of EnumerateStorages and calls |callback| or
208 void OnEnumerateStorages(const EnumerateStoragesCallback
& callback
,
209 const ErrorCallback
& error_callback
,
210 dbus::Response
* response
) {
212 error_callback
.Run();
215 dbus::MessageReader
reader(response
);
216 std::vector
<std::string
> storage_names
;
217 if (!reader
.PopArrayOfStrings(&storage_names
)) {
218 LOG(ERROR
) << kInvalidResponseMsg
<< response
->ToString();
219 error_callback
.Run();
222 callback
.Run(storage_names
);
225 // Handles the result of GetStorageInfo and calls |callback| or
227 void OnGetStorageInfo(const std::string
& storage_name
,
228 const GetStorageInfoCallback
& callback
,
229 const ErrorCallback
& error_callback
,
230 dbus::Response
* response
) {
232 error_callback
.Run();
236 dbus::MessageReader
reader(response
);
237 MtpStorageInfo protobuf
;
238 if (!reader
.PopArrayOfBytesAsProto(&protobuf
)) {
239 LOG(ERROR
) << kInvalidResponseMsg
<< response
->ToString();
240 error_callback
.Run();
243 callback
.Run(protobuf
);
246 // Handles the result of OpenStorage and calls |callback| or |error_callback|.
247 void OnOpenStorage(const OpenStorageCallback
& callback
,
248 const ErrorCallback
& error_callback
,
249 dbus::Response
* response
) {
251 error_callback
.Run();
254 dbus::MessageReader
reader(response
);
256 if (!reader
.PopString(&handle
)) {
257 LOG(ERROR
) << kInvalidResponseMsg
<< response
->ToString();
258 error_callback
.Run();
261 callback
.Run(handle
);
264 // Handles the result of CloseStorage and calls |callback| or
266 void OnCloseStorage(const CloseStorageCallback
& callback
,
267 const ErrorCallback
& error_callback
,
268 dbus::Response
* response
) {
270 error_callback
.Run();
276 // Handles the result of ReadDirectoryEntryIds and calls |callback| or
278 void OnReadDirectoryIds(const ReadDirectoryEntryIdsCallback
& callback
,
279 const ErrorCallback
& error_callback
,
280 dbus::Response
* response
) {
282 error_callback
.Run();
286 std::vector
<uint32
> file_ids
;
287 dbus::MessageReader
reader(response
);
288 dbus::MessageReader
array_reader(NULL
);
289 if (!reader
.PopArray(&array_reader
) || reader
.HasMoreData()) {
290 LOG(ERROR
) << kInvalidResponseMsg
<< response
->ToString();
291 error_callback
.Run();
295 while (array_reader
.HasMoreData()) {
297 if (array_reader
.PopUint32(&file_id
)) {
298 file_ids
.push_back(file_id
);
300 LOG(ERROR
) << kInvalidResponseMsg
<< response
->ToString();
301 error_callback
.Run();
305 callback
.Run(file_ids
);
308 // Handles the result of GetFileInfo and calls |callback| or |error_callback|.
309 void OnGetFileInfo(const GetFileInfoCallback
& callback
,
310 const ErrorCallback
& error_callback
,
311 dbus::Response
* response
) {
313 error_callback
.Run();
317 std::vector
<MtpFileEntry
> file_entries
;
318 dbus::MessageReader
reader(response
);
319 MtpFileEntries entries_protobuf
;
320 if (!reader
.PopArrayOfBytesAsProto(&entries_protobuf
)) {
321 LOG(ERROR
) << kInvalidResponseMsg
<< response
->ToString();
322 error_callback
.Run();
326 for (int i
= 0; i
< entries_protobuf
.file_entries_size(); ++i
)
327 file_entries
.push_back(entries_protobuf
.file_entries(i
));
328 callback
.Run(file_entries
);
331 // Handles the result of ReadFileChunk and calls |callback| or
333 void OnReadFile(const ReadFileCallback
& callback
,
334 const ErrorCallback
& error_callback
,
335 dbus::Response
* response
) {
337 error_callback
.Run();
341 const uint8
* data_bytes
= NULL
;
342 size_t data_length
= 0;
343 dbus::MessageReader
reader(response
);
344 if (!reader
.PopArrayOfBytes(&data_bytes
, &data_length
)) {
345 error_callback
.Run();
348 std::string
data(reinterpret_cast<const char*>(data_bytes
), data_length
);
352 // Handles MTPStorageAttached/Dettached signals and calls |handler|.
353 void OnMTPStorageSignal(MTPStorageEventHandler handler
,
355 dbus::Signal
* signal
) {
356 dbus::MessageReader
reader(signal
);
357 std::string storage_name
;
358 if (!reader
.PopString(&storage_name
)) {
359 LOG(ERROR
) << "Invalid signal: " << signal
->ToString();
362 DCHECK(!storage_name
.empty());
363 handler
.Run(is_attach
, storage_name
);
367 // Handles the result of signal connection setup.
368 void OnSignalConnected(const std::string
& interface
,
369 const std::string
& signal
,
371 LOG_IF(ERROR
, !succeeded
) << "Connect to " << interface
<< " "
372 << signal
<< " failed.";
375 dbus::ObjectProxy
* proxy_
;
377 bool listen_for_changes_called_
;
379 // Note: This should remain the last member so it'll be destroyed and
380 // invalidate its weak pointers before any other members are destroyed.
381 base::WeakPtrFactory
<MediaTransferProtocolDaemonClientImpl
> weak_ptr_factory_
;
383 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDaemonClientImpl
);
388 ////////////////////////////////////////////////////////////////////////////////
389 // MediaTransferProtocolDaemonClient
391 MediaTransferProtocolDaemonClient::MediaTransferProtocolDaemonClient() {}
393 MediaTransferProtocolDaemonClient::~MediaTransferProtocolDaemonClient() {}
396 MediaTransferProtocolDaemonClient
* MediaTransferProtocolDaemonClient::Create(
398 return new MediaTransferProtocolDaemonClientImpl(bus
);
401 } // namespace device