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 "chromeos/dbus/media_transfer_protocol_daemon_client.h" | 5 #include "chromeos/dbus/media_transfer_protocol_daemon_client.h" |
6 | 6 |
7 #include <map> | |
8 | |
9 #include "base/bind.h" | 7 #include "base/bind.h" |
10 #include "base/stl_util.h" | 8 #include "chromeos/dbus/mtp_file_entry.pb.h" |
11 #include "base/stringprintf.h" | 9 #include "chromeos/dbus/mtp_storage_info.pb.h" |
12 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
13 #include "dbus/message.h" | 11 #include "dbus/message.h" |
14 #include "dbus/object_path.h" | 12 #include "dbus/object_path.h" |
15 #include "dbus/object_proxy.h" | 13 #include "dbus/object_proxy.h" |
16 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
17 | 15 |
18 namespace chromeos { | 16 namespace chromeos { |
19 | 17 |
20 namespace { | 18 namespace { |
21 | 19 |
22 // Pops a string value when |reader| is not NULL. | |
23 // Returns true when a value is popped, false otherwise. | |
24 bool MaybePopString(dbus::MessageReader* reader, std::string* value) { | |
25 if (!reader) | |
26 return false; | |
27 return reader->PopString(value); | |
28 } | |
29 | |
30 // Pops a uint16 value when |reader| is not NULL. | |
31 // Returns true when a value is popped, false otherwise. | |
32 bool MaybePopUint16(dbus::MessageReader* reader, uint16* value) { | |
33 if (!reader) | |
34 return false; | |
35 return reader->PopUint16(value); | |
36 } | |
37 | |
38 // Pops a uint32 value when |reader| is not NULL. | |
39 // Returns true when a value is popped, false otherwise. | |
40 bool MaybePopUint32(dbus::MessageReader* reader, uint32* value) { | |
41 if (!reader) | |
42 return false; | |
43 return reader->PopUint32(value); | |
44 } | |
45 | |
46 // Pops a uint64 value when |reader| is not NULL. | |
47 // Returns true when a value is popped, false otherwise. | |
48 bool MaybePopUint64(dbus::MessageReader* reader, uint64* value) { | |
49 if (!reader) | |
50 return false; | |
51 return reader->PopUint64(value); | |
52 } | |
53 | |
54 // Pops a int64 value when |reader| is not NULL. | |
55 // Returns true when a value is popped, false otherwise. | |
56 bool MaybePopInt64(dbus::MessageReader* reader, int64* value) { | |
57 if (!reader) | |
58 return false; | |
59 return reader->PopInt64(value); | |
60 } | |
61 | |
62 // The MediaTransferProtocolDaemonClient implementation. | 20 // The MediaTransferProtocolDaemonClient implementation. |
63 class MediaTransferProtocolDaemonClientImpl | 21 class MediaTransferProtocolDaemonClientImpl |
64 : public MediaTransferProtocolDaemonClient { | 22 : public MediaTransferProtocolDaemonClient { |
65 public: | 23 public: |
66 explicit MediaTransferProtocolDaemonClientImpl(dbus::Bus* bus) | 24 explicit MediaTransferProtocolDaemonClientImpl(dbus::Bus* bus) |
67 : proxy_(bus->GetObjectProxy( | 25 : proxy_(bus->GetObjectProxy( |
68 mtpd::kMtpdServiceName, | 26 mtpd::kMtpdServiceName, |
69 dbus::ObjectPath(mtpd::kMtpdServicePath))), | 27 dbus::ObjectPath(mtpd::kMtpdServicePath))), |
70 weak_ptr_factory_(this) { | 28 weak_ptr_factory_(this) { |
71 } | 29 } |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 // Handles the result of GetStorageInfo and calls |callback| or | 247 // Handles the result of GetStorageInfo and calls |callback| or |
290 // |error_callback|. | 248 // |error_callback|. |
291 void OnGetStorageInfo(const std::string& storage_name, | 249 void OnGetStorageInfo(const std::string& storage_name, |
292 const GetStorageInfoCallback& callback, | 250 const GetStorageInfoCallback& callback, |
293 const ErrorCallback& error_callback, | 251 const ErrorCallback& error_callback, |
294 dbus::Response* response) { | 252 dbus::Response* response) { |
295 if (!response) { | 253 if (!response) { |
296 error_callback.Run(); | 254 error_callback.Run(); |
297 return; | 255 return; |
298 } | 256 } |
299 StorageInfo storage_info(storage_name, response); | 257 |
300 callback.Run(storage_info); | 258 dbus::MessageReader reader(response); |
| 259 MtpStorageInfo protobuf; |
| 260 reader.PopArrayOfBytesAsProto(&protobuf); |
| 261 callback.Run(protobuf); |
301 } | 262 } |
302 | 263 |
303 // Handles the result of OpenStorage and calls |callback| or |error_callback|. | 264 // Handles the result of OpenStorage and calls |callback| or |error_callback|. |
304 void OnOpenStorage(const OpenStorageCallback& callback, | 265 void OnOpenStorage(const OpenStorageCallback& callback, |
305 const ErrorCallback& error_callback, | 266 const ErrorCallback& error_callback, |
306 dbus::Response* response) { | 267 dbus::Response* response) { |
307 if (!response) { | 268 if (!response) { |
308 error_callback.Run(); | 269 error_callback.Run(); |
309 return; | 270 return; |
310 } | 271 } |
(...skipping 22 matching lines...) Expand all Loading... |
333 // Handles the result of ReadDirectoryByPath/Id and calls |callback| or | 294 // Handles the result of ReadDirectoryByPath/Id and calls |callback| or |
334 // |error_callback|. | 295 // |error_callback|. |
335 void OnReadDirectory(const ReadDirectoryCallback& callback, | 296 void OnReadDirectory(const ReadDirectoryCallback& callback, |
336 const ErrorCallback& error_callback, | 297 const ErrorCallback& error_callback, |
337 dbus::Response* response) { | 298 dbus::Response* response) { |
338 if (!response) { | 299 if (!response) { |
339 error_callback.Run(); | 300 error_callback.Run(); |
340 return; | 301 return; |
341 } | 302 } |
342 | 303 |
343 std::vector<FileEntry> file_entries; | 304 std::vector<MtpFileEntry> file_entries; |
344 dbus::MessageReader response_reader(response); | 305 dbus::MessageReader reader(response); |
345 dbus::MessageReader array_reader(response); | 306 MtpFileEntries entries_protobuf; |
346 if (!response_reader.PopArray(&array_reader)) { | 307 reader.PopArrayOfBytesAsProto(&entries_protobuf); |
347 LOG(ERROR) << "Invalid response: " << response->ToString(); | 308 for (int i = 0; i < entries_protobuf.file_entries_size(); ++i) |
348 error_callback.Run(); | 309 file_entries.push_back(entries_protobuf.file_entries(i)); |
349 return; | |
350 } | |
351 while (array_reader.HasMoreData()) { | |
352 FileEntry entry(response); | |
353 file_entries.push_back(entry); | |
354 } | |
355 callback.Run(file_entries); | 310 callback.Run(file_entries); |
356 } | 311 } |
357 | 312 |
358 // Handles the result of ReadFileByPath/Id and calls |callback| or | 313 // Handles the result of ReadFileByPath/Id and calls |callback| or |
359 // |error_callback|. | 314 // |error_callback|. |
360 void OnReadFile(const ReadFileCallback& callback, | 315 void OnReadFile(const ReadFileCallback& callback, |
361 const ErrorCallback& error_callback, | 316 const ErrorCallback& error_callback, |
362 dbus::Response* response) { | 317 dbus::Response* response) { |
363 if (!response) { | 318 if (!response) { |
364 error_callback.Run(); | 319 error_callback.Run(); |
(...skipping 14 matching lines...) Expand all Loading... |
379 // Handles the result of GetFileInfoByPath/Id and calls |callback| or | 334 // Handles the result of GetFileInfoByPath/Id and calls |callback| or |
380 // |error_callback|. | 335 // |error_callback|. |
381 void OnGetFileInfo(const GetFileInfoCallback& callback, | 336 void OnGetFileInfo(const GetFileInfoCallback& callback, |
382 const ErrorCallback& error_callback, | 337 const ErrorCallback& error_callback, |
383 dbus::Response* response) { | 338 dbus::Response* response) { |
384 if (!response) { | 339 if (!response) { |
385 error_callback.Run(); | 340 error_callback.Run(); |
386 return; | 341 return; |
387 } | 342 } |
388 | 343 |
389 FileEntry file_entry(response); | 344 dbus::MessageReader reader(response); |
390 callback.Run(file_entry); | 345 MtpFileEntry protobuf; |
| 346 reader.PopArrayOfBytesAsProto(&protobuf); |
| 347 callback.Run(protobuf); |
391 } | 348 } |
392 | 349 |
393 // Handles MTPStorageAttached/Dettached signals and calls |handler|. | 350 // Handles MTPStorageAttached/Dettached signals and calls |handler|. |
394 void OnMTPStorageSignal(MTPStorageEventHandler handler, | 351 void OnMTPStorageSignal(MTPStorageEventHandler handler, |
395 bool is_attach, | 352 bool is_attach, |
396 dbus::Signal* signal) { | 353 dbus::Signal* signal) { |
397 dbus::MessageReader reader(signal); | 354 dbus::MessageReader reader(signal); |
398 std::string storage_name; | 355 std::string storage_name; |
399 if (!reader.PopString(&storage_name)) { | 356 if (!reader.PopString(&storage_name)) { |
400 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | 357 LOG(ERROR) << "Invalid signal: " << signal->ToString(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 virtual void SetUpConnections( | 430 virtual void SetUpConnections( |
474 const MTPStorageEventHandler& handler) OVERRIDE {} | 431 const MTPStorageEventHandler& handler) OVERRIDE {} |
475 | 432 |
476 private: | 433 private: |
477 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDaemonClientStubImpl); | 434 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDaemonClientStubImpl); |
478 }; | 435 }; |
479 | 436 |
480 } // namespace | 437 } // namespace |
481 | 438 |
482 //////////////////////////////////////////////////////////////////////////////// | 439 //////////////////////////////////////////////////////////////////////////////// |
483 // StorageInfo | |
484 | |
485 StorageInfo::StorageInfo() | |
486 : vendor_id_(0), | |
487 product_id_(0), | |
488 device_flags_(0), | |
489 storage_type_(0), | |
490 filesystem_type_(0), | |
491 access_capability_(0), | |
492 max_capacity_(0), | |
493 free_space_in_bytes_(0), | |
494 free_space_in_objects_(0) { | |
495 } | |
496 | |
497 StorageInfo::StorageInfo(const std::string& storage_name, | |
498 dbus::Response* response) | |
499 : vendor_id_(0), | |
500 product_id_(0), | |
501 device_flags_(0), | |
502 storage_name_(storage_name), | |
503 storage_type_(0), | |
504 filesystem_type_(0), | |
505 access_capability_(0), | |
506 max_capacity_(0), | |
507 free_space_in_bytes_(0), | |
508 free_space_in_objects_(0) { | |
509 InitializeFromResponse(response); | |
510 } | |
511 | |
512 StorageInfo::~StorageInfo() { | |
513 } | |
514 | |
515 // Initializes |this| from |response| given by the mtpd service. | |
516 void StorageInfo::InitializeFromResponse(dbus::Response* response) { | |
517 dbus::MessageReader response_reader(response); | |
518 dbus::MessageReader array_reader(response); | |
519 if (!response_reader.PopArray(&array_reader)) { | |
520 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
521 return; | |
522 } | |
523 // TODO(thestig): Rework this code using Protocol Buffers. crosbug.com/22626 | |
524 typedef std::map<std::string, dbus::MessageReader*> PropertiesMap; | |
525 PropertiesMap properties; | |
526 STLValueDeleter<PropertiesMap> properties_value_deleter(&properties); | |
527 while (array_reader.HasMoreData()) { | |
528 dbus::MessageReader* value_reader = new dbus::MessageReader(response); | |
529 dbus::MessageReader dict_entry_reader(response); | |
530 std::string key; | |
531 if (!array_reader.PopDictEntry(&dict_entry_reader) || | |
532 !dict_entry_reader.PopString(&key) || | |
533 !dict_entry_reader.PopVariant(value_reader)) { | |
534 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
535 return; | |
536 } | |
537 properties[key] = value_reader; | |
538 } | |
539 // TODO(thestig) Add enums for fields below as appropriate. | |
540 MaybePopString(properties[mtpd::kVendor], &vendor_); | |
541 MaybePopString(properties[mtpd::kProduct], &product_); | |
542 MaybePopString(properties[mtpd::kStorageDescription], &storage_description_); | |
543 MaybePopString(properties[mtpd::kVolumeIdentifier], &volume_identifier_); | |
544 MaybePopUint16(properties[mtpd::kVendorId], &vendor_id_); | |
545 MaybePopUint16(properties[mtpd::kProductId], &product_id_); | |
546 MaybePopUint16(properties[mtpd::kStorageType], &storage_type_); | |
547 MaybePopUint16(properties[mtpd::kFilesystemType], &filesystem_type_); | |
548 MaybePopUint16(properties[mtpd::kAccessCapability], &access_capability_); | |
549 MaybePopUint32(properties[mtpd::kDeviceFlags], &device_flags_); | |
550 MaybePopUint64(properties[mtpd::kMaxCapacity], &max_capacity_); | |
551 MaybePopUint64(properties[mtpd::kFreeSpaceInBytes], &free_space_in_bytes_); | |
552 MaybePopUint64(properties[mtpd::kFreeSpaceInObjects], | |
553 &free_space_in_objects_); | |
554 } | |
555 | |
556 //////////////////////////////////////////////////////////////////////////////// | |
557 // FileEntry | |
558 | |
559 FileEntry::FileEntry() | |
560 : item_id_(0), | |
561 parent_id_(0), | |
562 file_size_(0), | |
563 file_type_(FILE_TYPE_UNKNOWN) { | |
564 } | |
565 | |
566 FileEntry::FileEntry(dbus::Response* response) | |
567 : item_id_(0), | |
568 parent_id_(0), | |
569 file_size_(0), | |
570 file_type_(FILE_TYPE_UNKNOWN) { | |
571 InitializeFromResponse(response); | |
572 } | |
573 | |
574 FileEntry::~FileEntry() { | |
575 } | |
576 | |
577 // Initializes |this| from |response| given by the mtpd service. | |
578 void FileEntry::InitializeFromResponse(dbus::Response* response) { | |
579 dbus::MessageReader response_reader(response); | |
580 dbus::MessageReader array_reader(response); | |
581 if (!response_reader.PopArray(&array_reader)) { | |
582 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
583 return; | |
584 } | |
585 // TODO(thestig): Rework this code using Protocol Buffers. crosbug.com/22626 | |
586 typedef std::map<std::string, dbus::MessageReader*> PropertiesMap; | |
587 PropertiesMap properties; | |
588 STLValueDeleter<PropertiesMap> properties_value_deleter(&properties); | |
589 while (array_reader.HasMoreData()) { | |
590 dbus::MessageReader* value_reader = new dbus::MessageReader(response); | |
591 dbus::MessageReader dict_entry_reader(response); | |
592 std::string key; | |
593 if (!array_reader.PopDictEntry(&dict_entry_reader) || | |
594 !dict_entry_reader.PopString(&key) || | |
595 !dict_entry_reader.PopVariant(value_reader)) { | |
596 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
597 return; | |
598 } | |
599 properties[key] = value_reader; | |
600 } | |
601 | |
602 MaybePopString(properties[mtpd::kFileName], &file_name_); | |
603 MaybePopUint32(properties[mtpd::kItemId], &item_id_); | |
604 MaybePopUint32(properties[mtpd::kParentId], &parent_id_); | |
605 MaybePopUint64(properties[mtpd::kFileSize], &file_size_); | |
606 | |
607 int64 modification_date = -1; | |
608 if (MaybePopInt64(properties[mtpd::kModificationDate], &modification_date)) | |
609 modification_date_ = base::Time::FromTimeT(modification_date); | |
610 | |
611 uint16 file_type = FILE_TYPE_OTHER; | |
612 if (MaybePopUint16(properties[mtpd::kFileType], &file_type)) { | |
613 switch (file_type) { | |
614 case FILE_TYPE_FOLDER: | |
615 case FILE_TYPE_JPEG: | |
616 case FILE_TYPE_JFIF: | |
617 case FILE_TYPE_TIFF: | |
618 case FILE_TYPE_BMP: | |
619 case FILE_TYPE_GIF: | |
620 case FILE_TYPE_PICT: | |
621 case FILE_TYPE_PNG: | |
622 case FILE_TYPE_WINDOWSIMAGEFORMAT: | |
623 case FILE_TYPE_JP2: | |
624 case FILE_TYPE_JPX: | |
625 case FILE_TYPE_UNKNOWN: | |
626 file_type_ = static_cast<FileType>(file_type); | |
627 break; | |
628 default: | |
629 file_type_ = FILE_TYPE_OTHER; | |
630 break; | |
631 } | |
632 } | |
633 } | |
634 | |
635 //////////////////////////////////////////////////////////////////////////////// | |
636 // MediaTransferProtocolDaemonClient | 440 // MediaTransferProtocolDaemonClient |
637 | 441 |
638 MediaTransferProtocolDaemonClient::MediaTransferProtocolDaemonClient() {} | 442 MediaTransferProtocolDaemonClient::MediaTransferProtocolDaemonClient() {} |
639 | 443 |
640 MediaTransferProtocolDaemonClient::~MediaTransferProtocolDaemonClient() {} | 444 MediaTransferProtocolDaemonClient::~MediaTransferProtocolDaemonClient() {} |
641 | 445 |
642 // static | 446 // static |
643 MediaTransferProtocolDaemonClient* | 447 MediaTransferProtocolDaemonClient* |
644 MediaTransferProtocolDaemonClient::Create(DBusClientImplementationType type, | 448 MediaTransferProtocolDaemonClient::Create(DBusClientImplementationType type, |
645 dbus::Bus* bus) { | 449 dbus::Bus* bus) { |
646 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 450 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
647 return new MediaTransferProtocolDaemonClientImpl(bus); | 451 return new MediaTransferProtocolDaemonClientImpl(bus); |
648 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 452 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
649 return new MediaTransferProtocolDaemonClientStubImpl(); | 453 return new MediaTransferProtocolDaemonClientStubImpl(); |
650 } | 454 } |
651 | 455 |
652 } // namespace chromeos | 456 } // namespace chromeos |
OLD | NEW |