mtp_manager_client_chromeos.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2018 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 "components/storage_monitor/mtp_manager_client_chromeos.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/containers/contains.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "components/storage_monitor/storage_info.h"
  10. #include "components/storage_monitor/storage_info_utils.h"
  11. namespace storage_monitor {
  12. MtpManagerClientChromeOS::MtpManagerClientChromeOS(
  13. StorageMonitor::Receiver* receiver,
  14. device::mojom::MtpManager* mtp_manager)
  15. : mtp_manager_(mtp_manager), notifications_(receiver) {
  16. mtp_manager_->EnumerateStoragesAndSetClient(
  17. receiver_.BindNewEndpointAndPassRemote(),
  18. base::BindOnce(&MtpManagerClientChromeOS::OnReceivedStorages,
  19. weak_ptr_factory_.GetWeakPtr()));
  20. }
  21. MtpManagerClientChromeOS::~MtpManagerClientChromeOS() {}
  22. bool MtpManagerClientChromeOS::GetStorageInfoForPath(
  23. const base::FilePath& path,
  24. StorageInfo* storage_info) const {
  25. DCHECK(storage_info);
  26. if (!path.IsAbsolute())
  27. return false;
  28. std::vector<base::FilePath::StringType> path_components =
  29. path.GetComponents();
  30. if (path_components.size() < 2)
  31. return false;
  32. // First and second component of the path specifies the device location.
  33. // E.g.: If |path| is "/usb:2,2:65537/DCIM/Folder_a", "/usb:2,2:65537" is the
  34. // device location.
  35. const auto info_it =
  36. storage_map_.find(GetDeviceLocationFromStorageName(path_components[1]));
  37. if (info_it == storage_map_.end())
  38. return false;
  39. *storage_info = info_it->second;
  40. return true;
  41. }
  42. void MtpManagerClientChromeOS::EjectDevice(
  43. const std::string& device_id,
  44. base::OnceCallback<void(StorageMonitor::EjectStatus)> callback) {
  45. std::string location;
  46. if (!GetLocationForDeviceId(device_id, &location)) {
  47. std::move(callback).Run(StorageMonitor::EJECT_NO_SUCH_DEVICE);
  48. return;
  49. }
  50. // TODO(thestig): Change this to tell the MTP manager to eject the device.
  51. StorageDetached(location);
  52. std::move(callback).Run(StorageMonitor::EJECT_OK);
  53. }
  54. // device::mojom::MtpManagerClient override.
  55. void MtpManagerClientChromeOS::StorageAttached(
  56. device::mojom::MtpStorageInfoPtr mtp_storage_info) {
  57. if (!mtp_storage_info)
  58. return;
  59. // Create StorageMonitor format StorageInfo and update the local map.
  60. std::string device_id = GetDeviceIdFromStorageInfo(*mtp_storage_info);
  61. std::u16string storage_label =
  62. GetDeviceLabelFromStorageInfo(*mtp_storage_info);
  63. std::string location =
  64. GetDeviceLocationFromStorageName(mtp_storage_info->storage_name);
  65. std::u16string vendor_name = base::UTF8ToUTF16(mtp_storage_info->vendor);
  66. std::u16string product_name = base::UTF8ToUTF16(mtp_storage_info->product);
  67. if (device_id.empty() || storage_label.empty())
  68. return;
  69. DCHECK(!base::Contains(storage_map_, location));
  70. StorageInfo storage_info(device_id, location, storage_label, vendor_name,
  71. product_name, 0);
  72. storage_map_[location] = storage_info;
  73. // Notify StorageMonitor observers about the event.
  74. notifications_->ProcessAttach(storage_info);
  75. }
  76. // device::mojom::MtpManagerClient override.
  77. void MtpManagerClientChromeOS::StorageDetached(
  78. const std::string& storage_name) {
  79. DCHECK(!storage_name.empty());
  80. StorageLocationToInfoMap::iterator it =
  81. storage_map_.find(GetDeviceLocationFromStorageName(storage_name));
  82. if (it == storage_map_.end())
  83. return;
  84. // Notify StorageMonitor observers about the event.
  85. notifications_->ProcessDetach(it->second.device_id());
  86. storage_map_.erase(it);
  87. }
  88. void MtpManagerClientChromeOS::OnReceivedStorages(
  89. std::vector<device::mojom::MtpStorageInfoPtr> storage_info_list) {
  90. for (auto& storage_info : storage_info_list)
  91. StorageAttached(std::move(storage_info));
  92. }
  93. bool MtpManagerClientChromeOS::GetLocationForDeviceId(
  94. const std::string& device_id,
  95. std::string* location) const {
  96. for (const auto& it : storage_map_) {
  97. if (it.second.device_id() == device_id) {
  98. *location = it.first;
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. } // namespace storage_monitor