mount_point.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2019 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.
  4. #include "ash/components/disks/mount_point.h"
  5. #include <string>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/logging.h"
  9. #include "base/threading/sequenced_task_runner_handle.h"
  10. namespace ash {
  11. namespace disks {
  12. namespace {
  13. void OnMountDone(DiskMountManager* disk_mount_manager,
  14. MountPoint::DoneCallback callback,
  15. MountError error_code,
  16. const DiskMountManager::MountPoint& mount_info) {
  17. std::unique_ptr<MountPoint> mount_point;
  18. if (error_code == MountError::kNone) {
  19. DCHECK(!mount_info.mount_path.empty());
  20. mount_point = std::make_unique<MountPoint>(
  21. base::FilePath(mount_info.mount_path), disk_mount_manager);
  22. }
  23. // Post a task to guarantee the callback isn't called inline with the
  24. // Mount() call.
  25. base::SequencedTaskRunnerHandle::Get()->PostTask(
  26. FROM_HERE,
  27. base::BindOnce(std::move(callback), error_code, std::move(mount_point)));
  28. }
  29. } // namespace
  30. // static
  31. void MountPoint::Mount(DiskMountManager* disk_mount_manager,
  32. const std::string& source_path,
  33. const std::string& source_format,
  34. const std::string& mount_label,
  35. const std::vector<std::string>& mount_options,
  36. MountType mount_type,
  37. MountAccessMode access_mode,
  38. DoneCallback callback) {
  39. // |disk_mount_manager| owns the callback to OnMountDone, so we can bind it as
  40. // unretained.
  41. disk_mount_manager->MountPath(
  42. source_path, source_format, mount_label, mount_options, mount_type,
  43. access_mode,
  44. base::BindOnce(&OnMountDone, base::Unretained(disk_mount_manager),
  45. std::move(callback)));
  46. }
  47. MountPoint::MountPoint(const base::FilePath& mount_path,
  48. DiskMountManager* disk_mount_manager)
  49. : mount_path_(mount_path), disk_mount_manager_(disk_mount_manager) {
  50. DCHECK(!mount_path_.empty());
  51. }
  52. MountPoint::~MountPoint() {
  53. if (!mount_path_.empty()) {
  54. disk_mount_manager_->UnmountPath(
  55. mount_path_.value(), base::BindOnce([](MountError error_code) {
  56. LOG_IF(WARNING, error_code != MountError::kNone)
  57. << "Failed to unmount with error code: " << error_code;
  58. }));
  59. }
  60. }
  61. void MountPoint::Unmount(MountPoint::UnmountCallback callback) {
  62. DCHECK(callback);
  63. DCHECK(!mount_path_.empty());
  64. // Make a copy of the |mount_path_| on the stack and clear it, in case the
  65. // callback runs inline and deletes |this|.
  66. const std::string mount_path = mount_path_.value();
  67. mount_path_.clear();
  68. disk_mount_manager_->UnmountPath(
  69. mount_path,
  70. base::BindOnce(&MountPoint::OnUmountDone, weak_factory_.GetWeakPtr(),
  71. std::move(callback)));
  72. }
  73. void MountPoint::OnUmountDone(MountPoint::UnmountCallback callback,
  74. MountError unmount_error) {
  75. std::move(callback).Run(unmount_error);
  76. }
  77. } // namespace disks
  78. } // namespace ash