Use bucket parameter in GetIfChanged for support binaries.
[chromium-blink-merge.git] / device / media_transfer_protocol / media_transfer_protocol_daemon_client.cc
blob0f7267721d3a7d04fd88665ba0a2016c30c3bd7b
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"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/memory/weak_ptr.h"
11 #include "dbus/bus.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"
19 namespace device {
21 namespace {
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 {
29 public:
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);
43 proxy_->CallMethod(
44 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
45 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnEnumerateStorages,
46 weak_ptr_factory_.GetWeakPtr(),
47 callback,
48 error_callback));
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);
58 proxy_->CallMethod(
59 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
60 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnGetStorageInfo,
61 weak_ptr_factory_.GetWeakPtr(),
62 storage_name,
63 callback,
64 error_callback));
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);
77 proxy_->CallMethod(
78 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
79 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnOpenStorage,
80 weak_ptr_factory_.GetWeakPtr(),
81 callback,
82 error_callback));
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);
92 proxy_->CallMethod(
93 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
94 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCloseStorage,
95 weak_ptr_factory_.GetWeakPtr(),
96 callback,
97 error_callback));
100 // MediaTransferProtocolDaemonClient override.
101 void ReadDirectoryEntryIds(const std::string& handle,
102 uint32 file_id,
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);
110 proxy_->CallMethod(
111 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
112 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadDirectoryIds,
113 weak_ptr_factory_.GetWeakPtr(),
114 callback,
115 error_callback));
118 void GetFileInfo(const std::string& handle,
119 const std::vector<uint32>& file_ids,
120 size_t offset,
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();
126 return;
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);
144 proxy_->CallMethod(
145 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
146 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnGetFileInfo,
147 weak_ptr_factory_.GetWeakPtr(),
148 callback,
149 error_callback));
152 // MediaTransferProtocolDaemonClient override.
153 void ReadFileChunk(const std::string& handle,
154 uint32 file_id,
155 uint32 offset,
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);
166 proxy_->CallMethod(
167 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
168 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadFile,
169 weak_ptr_factory_.GetWeakPtr(),
170 callback,
171 error_callback));
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(),
191 handler,
192 kSignalEventTuples[i].is_attach),
193 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnSignalConnected,
194 weak_ptr_factory_.GetWeakPtr()));
198 private:
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;
203 bool is_attach;
206 // Handles the result of EnumerateStorages and calls |callback| or
207 // |error_callback|.
208 void OnEnumerateStorages(const EnumerateStoragesCallback& callback,
209 const ErrorCallback& error_callback,
210 dbus::Response* response) {
211 if (!response) {
212 error_callback.Run();
213 return;
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();
220 return;
222 callback.Run(storage_names);
225 // Handles the result of GetStorageInfo and calls |callback| or
226 // |error_callback|.
227 void OnGetStorageInfo(const std::string& storage_name,
228 const GetStorageInfoCallback& callback,
229 const ErrorCallback& error_callback,
230 dbus::Response* response) {
231 if (!response) {
232 error_callback.Run();
233 return;
236 dbus::MessageReader reader(response);
237 MtpStorageInfo protobuf;
238 if (!reader.PopArrayOfBytesAsProto(&protobuf)) {
239 LOG(ERROR) << kInvalidResponseMsg << response->ToString();
240 error_callback.Run();
241 return;
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) {
250 if (!response) {
251 error_callback.Run();
252 return;
254 dbus::MessageReader reader(response);
255 std::string handle;
256 if (!reader.PopString(&handle)) {
257 LOG(ERROR) << kInvalidResponseMsg << response->ToString();
258 error_callback.Run();
259 return;
261 callback.Run(handle);
264 // Handles the result of CloseStorage and calls |callback| or
265 // |error_callback|.
266 void OnCloseStorage(const CloseStorageCallback& callback,
267 const ErrorCallback& error_callback,
268 dbus::Response* response) {
269 if (!response) {
270 error_callback.Run();
271 return;
273 callback.Run();
276 // Handles the result of ReadDirectoryEntryIds and calls |callback| or
277 // |error_callback|.
278 void OnReadDirectoryIds(const ReadDirectoryEntryIdsCallback& callback,
279 const ErrorCallback& error_callback,
280 dbus::Response* response) {
281 if (!response) {
282 error_callback.Run();
283 return;
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();
292 return;
295 while (array_reader.HasMoreData()) {
296 uint32 file_id;
297 if (array_reader.PopUint32(&file_id)) {
298 file_ids.push_back(file_id);
299 } else {
300 LOG(ERROR) << kInvalidResponseMsg << response->ToString();
301 error_callback.Run();
302 return;
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) {
312 if (!response) {
313 error_callback.Run();
314 return;
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();
323 return;
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
332 // |error_callback|.
333 void OnReadFile(const ReadFileCallback& callback,
334 const ErrorCallback& error_callback,
335 dbus::Response* response) {
336 if (!response) {
337 error_callback.Run();
338 return;
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();
346 return;
348 std::string data(reinterpret_cast<const char*>(data_bytes), data_length);
349 callback.Run(data);
352 // Handles MTPStorageAttached/Dettached signals and calls |handler|.
353 void OnMTPStorageSignal(MTPStorageEventHandler handler,
354 bool is_attach,
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();
360 return;
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,
370 bool succeeded) {
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);
386 } // namespace
388 ////////////////////////////////////////////////////////////////////////////////
389 // MediaTransferProtocolDaemonClient
391 MediaTransferProtocolDaemonClient::MediaTransferProtocolDaemonClient() {}
393 MediaTransferProtocolDaemonClient::~MediaTransferProtocolDaemonClient() {}
395 // static
396 MediaTransferProtocolDaemonClient* MediaTransferProtocolDaemonClient::Create(
397 dbus::Bus* bus) {
398 return new MediaTransferProtocolDaemonClientImpl(bus);
401 } // namespace device