Chromium Code Reviews| 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" | |
| 11 #include "base/stringprintf.h" | |
| 12 #include "dbus/bus.h" | 8 #include "dbus/bus.h" |
| 13 #include "dbus/message.h" | 9 #include "dbus/message.h" |
| 14 #include "dbus/object_path.h" | 10 #include "dbus/object_path.h" |
| 15 #include "dbus/object_proxy.h" | 11 #include "dbus/object_proxy.h" |
| 12 #include "chromeos/dbus/mtp_file_entry.pb.h" | |
| 13 #include "chromeos/dbus/mtp_storage_info.pb.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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 470 virtual void SetUpConnections( | 427 virtual void SetUpConnections( |
| 471 const MTPStorageEventHandler& handler) OVERRIDE {} | 428 const MTPStorageEventHandler& handler) OVERRIDE {} |
| 472 | 429 |
| 473 private: | 430 private: |
| 474 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDaemonClientStubImpl); | 431 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDaemonClientStubImpl); |
| 475 }; | 432 }; |
| 476 | 433 |
| 477 } // namespace | 434 } // namespace |
| 478 | 435 |
| 479 //////////////////////////////////////////////////////////////////////////////// | 436 //////////////////////////////////////////////////////////////////////////////// |
| 480 // StorageInfo | |
| 481 | |
| 482 StorageInfo::StorageInfo() | |
| 483 : vendor_id_(0), | |
| 484 product_id_(0), | |
| 485 device_flags_(0), | |
| 486 storage_type_(0), | |
| 487 filesystem_type_(0), | |
| 488 access_capability_(0), | |
| 489 max_capacity_(0), | |
| 490 free_space_in_bytes_(0), | |
| 491 free_space_in_objects_(0) { | |
| 492 } | |
| 493 | |
| 494 StorageInfo::StorageInfo(const std::string& storage_name, | |
| 495 dbus::Response* response) | |
| 496 : vendor_id_(0), | |
| 497 product_id_(0), | |
| 498 device_flags_(0), | |
| 499 storage_name_(storage_name), | |
| 500 storage_type_(0), | |
| 501 filesystem_type_(0), | |
| 502 access_capability_(0), | |
| 503 max_capacity_(0), | |
| 504 free_space_in_bytes_(0), | |
| 505 free_space_in_objects_(0) { | |
| 506 InitializeFromResponse(response); | |
| 507 } | |
| 508 | |
| 509 StorageInfo::~StorageInfo() { | |
| 510 } | |
| 511 | |
| 512 // Initializes |this| from |response| given by the mtpd service. | |
| 513 void StorageInfo::InitializeFromResponse(dbus::Response* response) { | |
| 514 dbus::MessageReader response_reader(response); | |
| 515 dbus::MessageReader array_reader(response); | |
| 516 if (!response_reader.PopArray(&array_reader)) { | |
| 517 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
| 518 return; | |
| 519 } | |
| 520 // TODO(thestig): Rework this code using Protocol Buffers. crosbug.com/22626 | |
| 521 typedef std::map<std::string, dbus::MessageReader*> PropertiesMap; | |
| 522 PropertiesMap properties; | |
| 523 STLValueDeleter<PropertiesMap> properties_value_deleter(&properties); | |
| 524 while (array_reader.HasMoreData()) { | |
| 525 dbus::MessageReader* value_reader = new dbus::MessageReader(response); | |
| 526 dbus::MessageReader dict_entry_reader(response); | |
| 527 std::string key; | |
| 528 if (!array_reader.PopDictEntry(&dict_entry_reader) || | |
| 529 !dict_entry_reader.PopString(&key) || | |
| 530 !dict_entry_reader.PopVariant(value_reader)) { | |
| 531 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
| 532 return; | |
| 533 } | |
| 534 properties[key] = value_reader; | |
| 535 } | |
| 536 // TODO(thestig) Add enums for fields below as appropriate. | |
| 537 MaybePopString(properties[mtpd::kVendor], &vendor_); | |
| 538 MaybePopString(properties[mtpd::kProduct], &product_); | |
| 539 MaybePopString(properties[mtpd::kStorageDescription], &storage_description_); | |
| 540 MaybePopString(properties[mtpd::kVolumeIdentifier], &volume_identifier_); | |
| 541 MaybePopUint16(properties[mtpd::kVendorId], &vendor_id_); | |
| 542 MaybePopUint16(properties[mtpd::kProductId], &product_id_); | |
| 543 MaybePopUint16(properties[mtpd::kStorageType], &storage_type_); | |
| 544 MaybePopUint16(properties[mtpd::kFilesystemType], &filesystem_type_); | |
| 545 MaybePopUint16(properties[mtpd::kAccessCapability], &access_capability_); | |
| 546 MaybePopUint32(properties[mtpd::kDeviceFlags], &device_flags_); | |
| 547 MaybePopUint64(properties[mtpd::kMaxCapacity], &max_capacity_); | |
| 548 MaybePopUint64(properties[mtpd::kFreeSpaceInBytes], &free_space_in_bytes_); | |
| 549 MaybePopUint64(properties[mtpd::kFreeSpaceInObjects], | |
| 550 &free_space_in_objects_); | |
| 551 } | |
| 552 | |
| 553 //////////////////////////////////////////////////////////////////////////////// | |
| 554 // FileEntry | |
| 555 | |
| 556 FileEntry::FileEntry() | |
| 557 : item_id_(0), | |
| 558 parent_id_(0), | |
| 559 file_size_(0), | |
| 560 file_type_(FILE_TYPE_UNKNOWN) { | |
| 561 } | |
| 562 | |
| 563 FileEntry::FileEntry(dbus::Response* response) | |
| 564 : item_id_(0), | |
| 565 parent_id_(0), | |
| 566 file_size_(0), | |
| 567 file_type_(FILE_TYPE_UNKNOWN) { | |
| 568 InitializeFromResponse(response); | |
| 569 } | |
| 570 | |
| 571 FileEntry::~FileEntry() { | |
| 572 } | |
| 573 | |
| 574 // Initializes |this| from |response| given by the mtpd service. | |
| 575 void FileEntry::InitializeFromResponse(dbus::Response* response) { | |
| 576 dbus::MessageReader response_reader(response); | |
| 577 dbus::MessageReader array_reader(response); | |
| 578 if (!response_reader.PopArray(&array_reader)) { | |
| 579 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
| 580 return; | |
| 581 } | |
| 582 // TODO(thestig): Rework this code using Protocol Buffers. crosbug.com/22626 | |
| 583 typedef std::map<std::string, dbus::MessageReader*> PropertiesMap; | |
| 584 PropertiesMap properties; | |
| 585 STLValueDeleter<PropertiesMap> properties_value_deleter(&properties); | |
| 586 while (array_reader.HasMoreData()) { | |
| 587 dbus::MessageReader* value_reader = new dbus::MessageReader(response); | |
| 588 dbus::MessageReader dict_entry_reader(response); | |
| 589 std::string key; | |
| 590 if (!array_reader.PopDictEntry(&dict_entry_reader) || | |
| 591 !dict_entry_reader.PopString(&key) || | |
| 592 !dict_entry_reader.PopVariant(value_reader)) { | |
| 593 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
| 594 return; | |
| 595 } | |
| 596 properties[key] = value_reader; | |
| 597 } | |
| 598 | |
| 599 MaybePopString(properties[mtpd::kFileName], &file_name_); | |
| 600 MaybePopUint32(properties[mtpd::kItemId], &item_id_); | |
| 601 MaybePopUint32(properties[mtpd::kParentId], &parent_id_); | |
| 602 MaybePopUint64(properties[mtpd::kFileSize], &file_size_); | |
| 603 | |
| 604 int64 modification_date = -1; | |
| 605 if (MaybePopInt64(properties[mtpd::kModificationDate], &modification_date)) | |
| 606 modification_date_ = base::Time::FromTimeT(modification_date); | |
| 607 | |
| 608 uint16 file_type = FILE_TYPE_OTHER; | |
| 609 if (MaybePopUint16(properties[mtpd::kFileType], &file_type)) { | |
| 610 switch (file_type) { | |
| 611 case FILE_TYPE_FOLDER: | |
| 612 case FILE_TYPE_JPEG: | |
| 613 case FILE_TYPE_JFIF: | |
| 614 case FILE_TYPE_TIFF: | |
| 615 case FILE_TYPE_BMP: | |
| 616 case FILE_TYPE_GIF: | |
| 617 case FILE_TYPE_PICT: | |
| 618 case FILE_TYPE_PNG: | |
| 619 case FILE_TYPE_WINDOWSIMAGEFORMAT: | |
| 620 case FILE_TYPE_JP2: | |
| 621 case FILE_TYPE_JPX: | |
| 622 case FILE_TYPE_UNKNOWN: | |
| 623 file_type_ = static_cast<FileType>(file_type); | |
| 624 break; | |
| 625 default: | |
| 626 file_type_ = FILE_TYPE_OTHER; | |
| 627 break; | |
| 628 } | |
| 629 } | |
| 630 } | |
| 631 | |
| 632 //////////////////////////////////////////////////////////////////////////////// | |
| 633 // MediaTransferProtocolDaemonClient | 437 // MediaTransferProtocolDaemonClient |
| 634 | 438 |
| 635 MediaTransferProtocolDaemonClient::MediaTransferProtocolDaemonClient() {} | 439 MediaTransferProtocolDaemonClient::MediaTransferProtocolDaemonClient() {} |
| 636 | 440 |
| 637 MediaTransferProtocolDaemonClient::~MediaTransferProtocolDaemonClient() {} | 441 MediaTransferProtocolDaemonClient::~MediaTransferProtocolDaemonClient() {} |
| 638 | 442 |
| 639 // static | 443 // static |
| 640 MediaTransferProtocolDaemonClient* | 444 MediaTransferProtocolDaemonClient* |
| 641 MediaTransferProtocolDaemonClient::Create(DBusClientImplementationType type, | 445 MediaTransferProtocolDaemonClient::Create(DBusClientImplementationType type, |
| 642 dbus::Bus* bus) { | 446 dbus::Bus* bus) { |
| 643 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 447 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 644 return new MediaTransferProtocolDaemonClientImpl(bus); | 448 return new MediaTransferProtocolDaemonClientImpl(bus); |
| 645 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 449 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 646 return new MediaTransferProtocolDaemonClientStubImpl(); | 450 return new MediaTransferProtocolDaemonClientStubImpl(); |
| 647 } | 451 } |
| 648 | 452 |
| 649 } // namespace chromeos | 453 } // namespace chromeos |
|
satorux1
2012/09/06 17:20:05
almost 200 lines gone!
| |
| OLD | NEW |