OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h
" | 5 #include "device/media_transfer_protocol/media_transfer_protocol_daemon_client.h
" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" |
10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
11 #include "dbus/bus.h" | 12 #include "dbus/bus.h" |
12 #include "dbus/message.h" | 13 #include "dbus/message.h" |
13 #include "dbus/object_path.h" | 14 #include "dbus/object_path.h" |
14 #include "dbus/object_proxy.h" | 15 #include "dbus/object_proxy.h" |
15 #include "device/media_transfer_protocol/mtp_file_entry.pb.h" | 16 #include "device/media_transfer_protocol/mtp_file_entry.pb.h" |
16 #include "device/media_transfer_protocol/mtp_storage_info.pb.h" | 17 #include "device/media_transfer_protocol/mtp_storage_info.pb.h" |
17 #include "third_party/cros_system_api/dbus/service_constants.h" | 18 #include "third_party/cros_system_api/dbus/service_constants.h" |
18 | 19 |
19 namespace device { | 20 namespace device { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 const char kInvalidResponseMsg[] = "Invalid Response: "; | 24 const char kInvalidResponseMsg[] = "Invalid Response: "; |
24 uint32 kMaxChunkSize = 1024*1024; // D-Bus has message size limits. | 25 uint32_t kMaxChunkSize = 1024 * 1024; // D-Bus has message size limits. |
25 | 26 |
26 // The MediaTransferProtocolDaemonClient implementation. | 27 // The MediaTransferProtocolDaemonClient implementation. |
27 class MediaTransferProtocolDaemonClientImpl | 28 class MediaTransferProtocolDaemonClientImpl |
28 : public MediaTransferProtocolDaemonClient { | 29 : public MediaTransferProtocolDaemonClient { |
29 public: | 30 public: |
30 explicit MediaTransferProtocolDaemonClientImpl(dbus::Bus* bus) | 31 explicit MediaTransferProtocolDaemonClientImpl(dbus::Bus* bus) |
31 : proxy_(bus->GetObjectProxy( | 32 : proxy_(bus->GetObjectProxy( |
32 mtpd::kMtpdServiceName, | 33 mtpd::kMtpdServiceName, |
33 dbus::ObjectPath(mtpd::kMtpdServicePath))), | 34 dbus::ObjectPath(mtpd::kMtpdServicePath))), |
34 listen_for_changes_called_(false), | 35 listen_for_changes_called_(false), |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 writer.AppendString(handle); | 106 writer.AppendString(handle); |
106 proxy_->CallMethod( | 107 proxy_->CallMethod( |
107 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 108 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
108 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCloseStorage, | 109 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCloseStorage, |
109 weak_ptr_factory_.GetWeakPtr(), | 110 weak_ptr_factory_.GetWeakPtr(), |
110 callback, | 111 callback, |
111 error_callback)); | 112 error_callback)); |
112 } | 113 } |
113 | 114 |
114 void CreateDirectory(const std::string& handle, | 115 void CreateDirectory(const std::string& handle, |
115 const uint32 parent_id, | 116 const uint32_t parent_id, |
116 const std::string& directory_name, | 117 const std::string& directory_name, |
117 const CreateDirectoryCallback& callback, | 118 const CreateDirectoryCallback& callback, |
118 const ErrorCallback& error_callback) override { | 119 const ErrorCallback& error_callback) override { |
119 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kCreateDirectory); | 120 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kCreateDirectory); |
120 dbus::MessageWriter writer(&method_call); | 121 dbus::MessageWriter writer(&method_call); |
121 writer.AppendString(handle); | 122 writer.AppendString(handle); |
122 writer.AppendUint32(parent_id); | 123 writer.AppendUint32(parent_id); |
123 writer.AppendString(directory_name); | 124 writer.AppendString(directory_name); |
124 proxy_->CallMethod( | 125 proxy_->CallMethod( |
125 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 126 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
126 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCreateDirectory, | 127 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCreateDirectory, |
127 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | 128 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); |
128 } | 129 } |
129 | 130 |
130 // MediaTransferProtocolDaemonClient override. | 131 // MediaTransferProtocolDaemonClient override. |
131 void ReadDirectoryEntryIds(const std::string& handle, | 132 void ReadDirectoryEntryIds(const std::string& handle, |
132 uint32 file_id, | 133 uint32_t file_id, |
133 const ReadDirectoryEntryIdsCallback& callback, | 134 const ReadDirectoryEntryIdsCallback& callback, |
134 const ErrorCallback& error_callback) override { | 135 const ErrorCallback& error_callback) override { |
135 dbus::MethodCall method_call(mtpd::kMtpdInterface, | 136 dbus::MethodCall method_call(mtpd::kMtpdInterface, |
136 mtpd::kReadDirectoryEntryIds); | 137 mtpd::kReadDirectoryEntryIds); |
137 dbus::MessageWriter writer(&method_call); | 138 dbus::MessageWriter writer(&method_call); |
138 writer.AppendString(handle); | 139 writer.AppendString(handle); |
139 writer.AppendUint32(file_id); | 140 writer.AppendUint32(file_id); |
140 proxy_->CallMethod( | 141 proxy_->CallMethod( |
141 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 142 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
142 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadDirectoryIds, | 143 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadDirectoryIds, |
143 weak_ptr_factory_.GetWeakPtr(), | 144 weak_ptr_factory_.GetWeakPtr(), |
144 callback, | 145 callback, |
145 error_callback)); | 146 error_callback)); |
146 } | 147 } |
147 | 148 |
148 void GetFileInfo(const std::string& handle, | 149 void GetFileInfo(const std::string& handle, |
149 const std::vector<uint32>& file_ids, | 150 const std::vector<uint32_t>& file_ids, |
150 size_t offset, | 151 size_t offset, |
151 size_t entries_to_read, | 152 size_t entries_to_read, |
152 const GetFileInfoCallback& callback, | 153 const GetFileInfoCallback& callback, |
153 const ErrorCallback& error_callback) override { | 154 const ErrorCallback& error_callback) override { |
154 if (offset >= file_ids.size()) { | 155 if (offset >= file_ids.size()) { |
155 error_callback.Run(); | 156 error_callback.Run(); |
156 return; | 157 return; |
157 } | 158 } |
158 | 159 |
159 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kGetFileInfo); | 160 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kGetFileInfo); |
(...skipping 14 matching lines...) Expand all Loading... |
174 proxy_->CallMethod( | 175 proxy_->CallMethod( |
175 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 176 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
176 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnGetFileInfo, | 177 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnGetFileInfo, |
177 weak_ptr_factory_.GetWeakPtr(), | 178 weak_ptr_factory_.GetWeakPtr(), |
178 callback, | 179 callback, |
179 error_callback)); | 180 error_callback)); |
180 } | 181 } |
181 | 182 |
182 // MediaTransferProtocolDaemonClient override. | 183 // MediaTransferProtocolDaemonClient override. |
183 void ReadFileChunk(const std::string& handle, | 184 void ReadFileChunk(const std::string& handle, |
184 uint32 file_id, | 185 uint32_t file_id, |
185 uint32 offset, | 186 uint32_t offset, |
186 uint32 bytes_to_read, | 187 uint32_t bytes_to_read, |
187 const ReadFileCallback& callback, | 188 const ReadFileCallback& callback, |
188 const ErrorCallback& error_callback) override { | 189 const ErrorCallback& error_callback) override { |
189 DCHECK_LE(bytes_to_read, kMaxChunkSize); | 190 DCHECK_LE(bytes_to_read, kMaxChunkSize); |
190 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kReadFileChunk); | 191 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kReadFileChunk); |
191 dbus::MessageWriter writer(&method_call); | 192 dbus::MessageWriter writer(&method_call); |
192 writer.AppendString(handle); | 193 writer.AppendString(handle); |
193 writer.AppendUint32(file_id); | 194 writer.AppendUint32(file_id); |
194 writer.AppendUint32(offset); | 195 writer.AppendUint32(offset); |
195 writer.AppendUint32(bytes_to_read); | 196 writer.AppendUint32(bytes_to_read); |
196 proxy_->CallMethod( | 197 proxy_->CallMethod( |
197 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 198 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
198 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadFile, | 199 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnReadFile, |
199 weak_ptr_factory_.GetWeakPtr(), | 200 weak_ptr_factory_.GetWeakPtr(), |
200 callback, | 201 callback, |
201 error_callback)); | 202 error_callback)); |
202 } | 203 } |
203 | 204 |
204 void RenameObject(const std::string& handle, | 205 void RenameObject(const std::string& handle, |
205 const uint32 object_id, | 206 const uint32_t object_id, |
206 const std::string& new_name, | 207 const std::string& new_name, |
207 const RenameObjectCallback& callback, | 208 const RenameObjectCallback& callback, |
208 const ErrorCallback& error_callback) override { | 209 const ErrorCallback& error_callback) override { |
209 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kRenameObject); | 210 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kRenameObject); |
210 dbus::MessageWriter writer(&method_call); | 211 dbus::MessageWriter writer(&method_call); |
211 writer.AppendString(handle); | 212 writer.AppendString(handle); |
212 writer.AppendUint32(object_id); | 213 writer.AppendUint32(object_id); |
213 writer.AppendString(new_name); | 214 writer.AppendString(new_name); |
214 proxy_->CallMethod( | 215 proxy_->CallMethod( |
215 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 216 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
216 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnRenameObject, | 217 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnRenameObject, |
217 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | 218 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); |
218 } | 219 } |
219 | 220 |
220 void CopyFileFromLocal(const std::string& handle, | 221 void CopyFileFromLocal(const std::string& handle, |
221 const int source_file_descriptor, | 222 const int source_file_descriptor, |
222 const uint32 parent_id, | 223 const uint32_t parent_id, |
223 const std::string& file_name, | 224 const std::string& file_name, |
224 const CopyFileFromLocalCallback& callback, | 225 const CopyFileFromLocalCallback& callback, |
225 const ErrorCallback& error_callback) override { | 226 const ErrorCallback& error_callback) override { |
226 dbus::FileDescriptor file_descriptor(source_file_descriptor); | 227 dbus::FileDescriptor file_descriptor(source_file_descriptor); |
227 file_descriptor.CheckValidity(); | 228 file_descriptor.CheckValidity(); |
228 | 229 |
229 dbus::MethodCall method_call(mtpd::kMtpdInterface, | 230 dbus::MethodCall method_call(mtpd::kMtpdInterface, |
230 mtpd::kCopyFileFromLocal); | 231 mtpd::kCopyFileFromLocal); |
231 dbus::MessageWriter writer(&method_call); | 232 dbus::MessageWriter writer(&method_call); |
232 writer.AppendString(handle); | 233 writer.AppendString(handle); |
233 writer.AppendFileDescriptor(file_descriptor); | 234 writer.AppendFileDescriptor(file_descriptor); |
234 writer.AppendUint32(parent_id); | 235 writer.AppendUint32(parent_id); |
235 writer.AppendString(file_name); | 236 writer.AppendString(file_name); |
236 proxy_->CallMethod( | 237 proxy_->CallMethod( |
237 &method_call, dbus::ObjectProxy::TIMEOUT_INFINITE, | 238 &method_call, dbus::ObjectProxy::TIMEOUT_INFINITE, |
238 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCopyFileFromLocal, | 239 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnCopyFileFromLocal, |
239 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | 240 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); |
240 } | 241 } |
241 | 242 |
242 void DeleteObject(const std::string& handle, | 243 void DeleteObject(const std::string& handle, |
243 const uint32 object_id, | 244 const uint32_t object_id, |
244 const DeleteObjectCallback& callback, | 245 const DeleteObjectCallback& callback, |
245 const ErrorCallback& error_callback) override { | 246 const ErrorCallback& error_callback) override { |
246 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kDeleteObject); | 247 dbus::MethodCall method_call(mtpd::kMtpdInterface, mtpd::kDeleteObject); |
247 dbus::MessageWriter writer(&method_call); | 248 dbus::MessageWriter writer(&method_call); |
248 writer.AppendString(handle); | 249 writer.AppendString(handle); |
249 writer.AppendUint32(object_id); | 250 writer.AppendUint32(object_id); |
250 proxy_->CallMethod( | 251 proxy_->CallMethod( |
251 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 252 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
252 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnDeleteObject, | 253 base::Bind(&MediaTransferProtocolDaemonClientImpl::OnDeleteObject, |
253 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); | 254 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 // Handles the result of ReadDirectoryEntryIds and calls |callback| or | 369 // Handles the result of ReadDirectoryEntryIds and calls |callback| or |
369 // |error_callback|. | 370 // |error_callback|. |
370 void OnReadDirectoryIds(const ReadDirectoryEntryIdsCallback& callback, | 371 void OnReadDirectoryIds(const ReadDirectoryEntryIdsCallback& callback, |
371 const ErrorCallback& error_callback, | 372 const ErrorCallback& error_callback, |
372 dbus::Response* response) { | 373 dbus::Response* response) { |
373 if (!response) { | 374 if (!response) { |
374 error_callback.Run(); | 375 error_callback.Run(); |
375 return; | 376 return; |
376 } | 377 } |
377 | 378 |
378 std::vector<uint32> file_ids; | 379 std::vector<uint32_t> file_ids; |
379 dbus::MessageReader reader(response); | 380 dbus::MessageReader reader(response); |
380 dbus::MessageReader array_reader(NULL); | 381 dbus::MessageReader array_reader(NULL); |
381 if (!reader.PopArray(&array_reader) || reader.HasMoreData()) { | 382 if (!reader.PopArray(&array_reader) || reader.HasMoreData()) { |
382 LOG(ERROR) << kInvalidResponseMsg << response->ToString(); | 383 LOG(ERROR) << kInvalidResponseMsg << response->ToString(); |
383 error_callback.Run(); | 384 error_callback.Run(); |
384 return; | 385 return; |
385 } | 386 } |
386 | 387 |
387 while (array_reader.HasMoreData()) { | 388 while (array_reader.HasMoreData()) { |
388 uint32 file_id; | 389 uint32_t file_id; |
389 if (array_reader.PopUint32(&file_id)) { | 390 if (array_reader.PopUint32(&file_id)) { |
390 file_ids.push_back(file_id); | 391 file_ids.push_back(file_id); |
391 } else { | 392 } else { |
392 LOG(ERROR) << kInvalidResponseMsg << response->ToString(); | 393 LOG(ERROR) << kInvalidResponseMsg << response->ToString(); |
393 error_callback.Run(); | 394 error_callback.Run(); |
394 return; | 395 return; |
395 } | 396 } |
396 } | 397 } |
397 callback.Run(file_ids); | 398 callback.Run(file_ids); |
398 } | 399 } |
(...skipping 24 matching lines...) Expand all Loading... |
423 // Handles the result of ReadFileChunk and calls |callback| or | 424 // Handles the result of ReadFileChunk and calls |callback| or |
424 // |error_callback|. | 425 // |error_callback|. |
425 void OnReadFile(const ReadFileCallback& callback, | 426 void OnReadFile(const ReadFileCallback& callback, |
426 const ErrorCallback& error_callback, | 427 const ErrorCallback& error_callback, |
427 dbus::Response* response) { | 428 dbus::Response* response) { |
428 if (!response) { | 429 if (!response) { |
429 error_callback.Run(); | 430 error_callback.Run(); |
430 return; | 431 return; |
431 } | 432 } |
432 | 433 |
433 const uint8* data_bytes = NULL; | 434 const uint8_t* data_bytes = NULL; |
434 size_t data_length = 0; | 435 size_t data_length = 0; |
435 dbus::MessageReader reader(response); | 436 dbus::MessageReader reader(response); |
436 if (!reader.PopArrayOfBytes(&data_bytes, &data_length)) { | 437 if (!reader.PopArrayOfBytes(&data_bytes, &data_length)) { |
437 error_callback.Run(); | 438 error_callback.Run(); |
438 return; | 439 return; |
439 } | 440 } |
440 std::string data(reinterpret_cast<const char*>(data_bytes), data_length); | 441 std::string data(reinterpret_cast<const char*>(data_bytes), data_length); |
441 callback.Run(data); | 442 callback.Run(data); |
442 } | 443 } |
443 | 444 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 | 518 |
518 MediaTransferProtocolDaemonClient::~MediaTransferProtocolDaemonClient() {} | 519 MediaTransferProtocolDaemonClient::~MediaTransferProtocolDaemonClient() {} |
519 | 520 |
520 // static | 521 // static |
521 MediaTransferProtocolDaemonClient* MediaTransferProtocolDaemonClient::Create( | 522 MediaTransferProtocolDaemonClient* MediaTransferProtocolDaemonClient::Create( |
522 dbus::Bus* bus) { | 523 dbus::Bus* bus) { |
523 return new MediaTransferProtocolDaemonClientImpl(bus); | 524 return new MediaTransferProtocolDaemonClientImpl(bus); |
524 } | 525 } |
525 | 526 |
526 } // namespace device | 527 } // namespace device |
OLD | NEW |