| 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/cros_disks_client.h" | 5 #include "chromeos/dbus/cros_disks_client.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
| 17 #include "base/task_runner_util.h" | 17 #include "base/task_runner_util.h" |
| 18 #include "base/threading/worker_pool.h" | 18 #include "base/threading/worker_pool.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chromeos/dbus/fake_cros_disks_client.h" |
| 20 #include "dbus/bus.h" | 21 #include "dbus/bus.h" |
| 21 #include "dbus/message.h" | 22 #include "dbus/message.h" |
| 22 #include "dbus/object_path.h" | 23 #include "dbus/object_path.h" |
| 23 #include "dbus/object_proxy.h" | 24 #include "dbus/object_proxy.h" |
| 24 #include "dbus/values_util.h" | 25 #include "dbus/values_util.h" |
| 25 #include "third_party/cros_system_api/dbus/service_constants.h" | 26 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 26 | 27 |
| 27 namespace chromeos { | 28 namespace chromeos { |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 | 448 |
| 448 dbus::ObjectProxy* proxy_; | 449 dbus::ObjectProxy* proxy_; |
| 449 | 450 |
| 450 // Note: This should remain the last member so it'll be destroyed and | 451 // Note: This should remain the last member so it'll be destroyed and |
| 451 // invalidate its weak pointers before any other members are destroyed. | 452 // invalidate its weak pointers before any other members are destroyed. |
| 452 base::WeakPtrFactory<CrosDisksClientImpl> weak_ptr_factory_; | 453 base::WeakPtrFactory<CrosDisksClientImpl> weak_ptr_factory_; |
| 453 | 454 |
| 454 DISALLOW_COPY_AND_ASSIGN(CrosDisksClientImpl); | 455 DISALLOW_COPY_AND_ASSIGN(CrosDisksClientImpl); |
| 455 }; | 456 }; |
| 456 | 457 |
| 457 // A stub implementaion of CrosDisksClient. | |
| 458 class CrosDisksClientStubImpl : public CrosDisksClient { | |
| 459 public: | |
| 460 CrosDisksClientStubImpl() | |
| 461 : weak_ptr_factory_(this) {} | |
| 462 | |
| 463 virtual ~CrosDisksClientStubImpl() {} | |
| 464 | |
| 465 // CrosDisksClient overrides: | |
| 466 virtual void Init(dbus::Bus* bus) override {} | |
| 467 virtual void Mount(const std::string& source_path, | |
| 468 const std::string& source_format, | |
| 469 const std::string& mount_label, | |
| 470 const base::Closure& callback, | |
| 471 const base::Closure& error_callback) override { | |
| 472 // This stub implementation only accepts archive mount requests. | |
| 473 const MountType type = MOUNT_TYPE_ARCHIVE; | |
| 474 | |
| 475 const base::FilePath mounted_path = GetArchiveMountPoint().Append( | |
| 476 base::FilePath::FromUTF8Unsafe(mount_label)); | |
| 477 | |
| 478 // Already mounted path. | |
| 479 if (mounted_to_source_path_map_.count(mounted_path.value()) != 0) { | |
| 480 FinishMount(MOUNT_ERROR_PATH_ALREADY_MOUNTED, source_path, type, | |
| 481 std::string(), callback); | |
| 482 return; | |
| 483 } | |
| 484 | |
| 485 // Perform fake mount. | |
| 486 base::PostTaskAndReplyWithResult( | |
| 487 base::WorkerPool::GetTaskRunner(true /* task_is_slow */).get(), | |
| 488 FROM_HERE, | |
| 489 base::Bind(&PerformFakeMount, source_path, mounted_path), | |
| 490 base::Bind(&CrosDisksClientStubImpl::ContinueMount, | |
| 491 weak_ptr_factory_.GetWeakPtr(), | |
| 492 source_path, | |
| 493 type, | |
| 494 callback, | |
| 495 mounted_path)); | |
| 496 } | |
| 497 | |
| 498 virtual void Unmount(const std::string& device_path, | |
| 499 UnmountOptions options, | |
| 500 const base::Closure& callback, | |
| 501 const base::Closure& error_callback) override { | |
| 502 // Not mounted. | |
| 503 if (mounted_to_source_path_map_.count(device_path) == 0) { | |
| 504 base::MessageLoopProxy::current()->PostTask(FROM_HERE, error_callback); | |
| 505 return; | |
| 506 } | |
| 507 | |
| 508 mounted_to_source_path_map_.erase(device_path); | |
| 509 | |
| 510 // Remove the directory created in Mount(). | |
| 511 base::WorkerPool::PostTaskAndReply( | |
| 512 FROM_HERE, | |
| 513 base::Bind(base::IgnoreResult(&base::DeleteFile), | |
| 514 base::FilePath::FromUTF8Unsafe(device_path), | |
| 515 true /* recursive */), | |
| 516 callback, | |
| 517 true /* task_is_slow */); | |
| 518 } | |
| 519 | |
| 520 virtual void EnumerateAutoMountableDevices( | |
| 521 const EnumerateAutoMountableDevicesCallback& callback, | |
| 522 const base::Closure& error_callback) override { | |
| 523 std::vector<std::string> device_paths; | |
| 524 base::MessageLoopProxy::current()->PostTask( | |
| 525 FROM_HERE, base::Bind(callback, device_paths)); | |
| 526 } | |
| 527 | |
| 528 virtual void EnumerateMountEntries( | |
| 529 const EnumerateMountEntriesCallback& callback, | |
| 530 const base::Closure& error_callback) override { | |
| 531 std::vector<MountEntry> entries; | |
| 532 base::MessageLoopProxy::current()->PostTask( | |
| 533 FROM_HERE, base::Bind(callback, entries)); | |
| 534 } | |
| 535 | |
| 536 virtual void Format(const std::string& device_path, | |
| 537 const std::string& filesystem, | |
| 538 const base::Closure& callback, | |
| 539 const base::Closure& error_callback) override { | |
| 540 base::MessageLoopProxy::current()->PostTask(FROM_HERE, error_callback); | |
| 541 } | |
| 542 | |
| 543 virtual void GetDeviceProperties( | |
| 544 const std::string& device_path, | |
| 545 const GetDevicePropertiesCallback& callback, | |
| 546 const base::Closure& error_callback) override { | |
| 547 base::MessageLoopProxy::current()->PostTask(FROM_HERE, error_callback); | |
| 548 } | |
| 549 | |
| 550 virtual void SetMountEventHandler( | |
| 551 const MountEventHandler& mount_event_handler) override { | |
| 552 mount_event_handler_ = mount_event_handler; | |
| 553 } | |
| 554 | |
| 555 virtual void SetMountCompletedHandler( | |
| 556 const MountCompletedHandler& mount_completed_handler) override { | |
| 557 mount_completed_handler_ = mount_completed_handler; | |
| 558 } | |
| 559 | |
| 560 virtual void SetFormatCompletedHandler( | |
| 561 const FormatCompletedHandler& format_completed_handler) override { | |
| 562 format_completed_handler_ = format_completed_handler; | |
| 563 } | |
| 564 | |
| 565 private: | |
| 566 // Performs file actions for Mount(). | |
| 567 static MountError PerformFakeMount(const std::string& source_path, | |
| 568 const base::FilePath& mounted_path) { | |
| 569 // Check the source path exists. | |
| 570 if (!base::PathExists(base::FilePath::FromUTF8Unsafe(source_path))) { | |
| 571 DLOG(ERROR) << "Source does not exist at " << source_path; | |
| 572 return MOUNT_ERROR_INVALID_PATH; | |
| 573 } | |
| 574 | |
| 575 // Just create an empty directory and shows it as the mounted directory. | |
| 576 if (!base::CreateDirectory(mounted_path)) { | |
| 577 DLOG(ERROR) << "Failed to create directory at " << mounted_path.value(); | |
| 578 return MOUNT_ERROR_DIRECTORY_CREATION_FAILED; | |
| 579 } | |
| 580 | |
| 581 // Put a dummy file. | |
| 582 const base::FilePath dummy_file_path = | |
| 583 mounted_path.Append("SUCCESSFULLY_PERFORMED_FAKE_MOUNT.txt"); | |
| 584 const std::string dummy_file_content = "This is a dummy file."; | |
| 585 const int write_result = base::WriteFile( | |
| 586 dummy_file_path, dummy_file_content.data(), dummy_file_content.size()); | |
| 587 if (write_result != static_cast<int>(dummy_file_content.size())) { | |
| 588 DLOG(ERROR) << "Failed to put a dummy file at " | |
| 589 << dummy_file_path.value(); | |
| 590 return MOUNT_ERROR_MOUNT_PROGRAM_FAILED; | |
| 591 } | |
| 592 | |
| 593 return MOUNT_ERROR_NONE; | |
| 594 } | |
| 595 | |
| 596 // Part of Mount() implementation. | |
| 597 void ContinueMount(const std::string& source_path, | |
| 598 MountType type, | |
| 599 const base::Closure& callback, | |
| 600 const base::FilePath& mounted_path, | |
| 601 MountError mount_error) { | |
| 602 if (mount_error != MOUNT_ERROR_NONE) { | |
| 603 FinishMount(mount_error, source_path, type, std::string(), callback); | |
| 604 return; | |
| 605 } | |
| 606 mounted_to_source_path_map_[mounted_path.value()] = source_path; | |
| 607 FinishMount(MOUNT_ERROR_NONE, source_path, type, | |
| 608 mounted_path.AsUTF8Unsafe(), callback); | |
| 609 } | |
| 610 | |
| 611 // Runs |callback| and sends MountCompleted signal. | |
| 612 // Part of Mount() implementation. | |
| 613 void FinishMount(MountError error, | |
| 614 const std::string& source_path, | |
| 615 MountType type, | |
| 616 const std::string& mounted_path, | |
| 617 const base::Closure& callback) { | |
| 618 base::MessageLoopProxy::current()->PostTask(FROM_HERE, callback); | |
| 619 if (!mount_completed_handler_.is_null()) { | |
| 620 base::MessageLoopProxy::current()->PostTask( | |
| 621 FROM_HERE, | |
| 622 base::Bind(mount_completed_handler_, | |
| 623 MountEntry(error, source_path, type, mounted_path))); | |
| 624 } | |
| 625 } | |
| 626 | |
| 627 // Mounted path to source path map. | |
| 628 std::map<std::string, std::string> mounted_to_source_path_map_; | |
| 629 | |
| 630 MountEventHandler mount_event_handler_; | |
| 631 MountCompletedHandler mount_completed_handler_; | |
| 632 FormatCompletedHandler format_completed_handler_; | |
| 633 | |
| 634 base::WeakPtrFactory<CrosDisksClientStubImpl> weak_ptr_factory_; | |
| 635 | |
| 636 DISALLOW_COPY_AND_ASSIGN(CrosDisksClientStubImpl); | |
| 637 }; | |
| 638 | |
| 639 } // namespace | 458 } // namespace |
| 640 | 459 |
| 641 //////////////////////////////////////////////////////////////////////////////// | 460 //////////////////////////////////////////////////////////////////////////////// |
| 642 // DiskInfo | 461 // DiskInfo |
| 643 | 462 |
| 644 DiskInfo::DiskInfo(const std::string& device_path, dbus::Response* response) | 463 DiskInfo::DiskInfo(const std::string& device_path, dbus::Response* response) |
| 645 : device_path_(device_path), | 464 : device_path_(device_path), |
| 646 is_drive_(false), | 465 is_drive_(false), |
| 647 has_media_(false), | 466 has_media_(false), |
| 648 on_boot_device_(false), | 467 on_boot_device_(false), |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 815 | 634 |
| 816 CrosDisksClient::CrosDisksClient() {} | 635 CrosDisksClient::CrosDisksClient() {} |
| 817 | 636 |
| 818 CrosDisksClient::~CrosDisksClient() {} | 637 CrosDisksClient::~CrosDisksClient() {} |
| 819 | 638 |
| 820 // static | 639 // static |
| 821 CrosDisksClient* CrosDisksClient::Create(DBusClientImplementationType type) { | 640 CrosDisksClient* CrosDisksClient::Create(DBusClientImplementationType type) { |
| 822 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 641 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 823 return new CrosDisksClientImpl(); | 642 return new CrosDisksClientImpl(); |
| 824 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 643 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 825 return new CrosDisksClientStubImpl(); | 644 return new FakeCrosDisksClient(); |
| 826 } | 645 } |
| 827 | 646 |
| 828 // static | 647 // static |
| 829 base::FilePath CrosDisksClient::GetArchiveMountPoint() { | 648 base::FilePath CrosDisksClient::GetArchiveMountPoint() { |
| 830 return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ? | 649 return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ? |
| 831 FILE_PATH_LITERAL("/media/archive") : | 650 FILE_PATH_LITERAL("/media/archive") : |
| 832 FILE_PATH_LITERAL("/tmp/chromeos/media/archive")); | 651 FILE_PATH_LITERAL("/tmp/chromeos/media/archive")); |
| 833 } | 652 } |
| 834 | 653 |
| 835 // static | 654 // static |
| 836 base::FilePath CrosDisksClient::GetRemovableDiskMountPoint() { | 655 base::FilePath CrosDisksClient::GetRemovableDiskMountPoint() { |
| 837 return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ? | 656 return base::FilePath(base::SysInfo::IsRunningOnChromeOS() ? |
| 838 FILE_PATH_LITERAL("/media/removable") : | 657 FILE_PATH_LITERAL("/media/removable") : |
| 839 FILE_PATH_LITERAL("/tmp/chromeos/media/removable")); | 658 FILE_PATH_LITERAL("/tmp/chromeos/media/removable")); |
| 840 } | 659 } |
| 841 | 660 |
| 842 } // namespace chromeos | 661 } // namespace chromeos |
| OLD | NEW |