media_storage_util.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2014 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/media_storage_util.h"
  5. #include <vector>
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/check_op.h"
  9. #include "base/files/file_util.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "base/task/task_traits.h"
  13. #include "base/task/thread_pool.h"
  14. #include "build/build_config.h"
  15. #include "components/storage_monitor/removable_device_constants.h"
  16. #include "components/storage_monitor/storage_monitor.h"
  17. #include "content/public/browser/browser_thread.h"
  18. namespace storage_monitor {
  19. namespace {
  20. #if !BUILDFLAG(IS_WIN)
  21. const char kRootPath[] = "/";
  22. #endif
  23. typedef std::vector<StorageInfo> StorageInfoList;
  24. base::FilePath::StringType FindRemovableStorageLocationById(
  25. const std::string& device_id) {
  26. StorageInfoList devices =
  27. StorageMonitor::GetInstance()->GetAllAvailableStorages();
  28. for (StorageInfoList::const_iterator it = devices.begin();
  29. it != devices.end(); ++it) {
  30. if (it->device_id() == device_id
  31. && StorageInfo::IsRemovableDevice(device_id))
  32. return it->location();
  33. }
  34. return base::FilePath::StringType();
  35. }
  36. void FilterAttachedDevicesOnBackgroundSequence(
  37. MediaStorageUtil::DeviceIdSet* devices) {
  38. MediaStorageUtil::DeviceIdSet missing_devices;
  39. for (auto it = devices->begin(); it != devices->end(); ++it) {
  40. StorageInfo::Type type;
  41. std::string unique_id;
  42. if (!StorageInfo::CrackDeviceId(*it, &type, &unique_id)) {
  43. missing_devices.insert(*it);
  44. continue;
  45. }
  46. if (type == StorageInfo::FIXED_MASS_STORAGE) {
  47. if (!base::PathExists(base::FilePath::FromUTF8Unsafe(unique_id)))
  48. missing_devices.insert(*it);
  49. continue;
  50. }
  51. if (!MediaStorageUtil::IsRemovableStorageAttached(*it))
  52. missing_devices.insert(*it);
  53. }
  54. for (auto it = missing_devices.begin(); it != missing_devices.end(); ++it) {
  55. devices->erase(*it);
  56. }
  57. }
  58. } // namespace
  59. // static
  60. bool MediaStorageUtil::HasDcim(const base::FilePath& mount_point) {
  61. base::FilePath::StringType dcim_dir(kDCIMDirectoryName);
  62. if (!base::DirectoryExists(mount_point.Append(dcim_dir))) {
  63. // Check for lowercase 'dcim' as well.
  64. base::FilePath dcim_path_lower(
  65. mount_point.Append(base::ToLowerASCII(dcim_dir)));
  66. if (!base::DirectoryExists(dcim_path_lower))
  67. return false;
  68. }
  69. return true;
  70. }
  71. // static
  72. bool MediaStorageUtil::CanCreateFileSystem(const std::string& device_id,
  73. const base::FilePath& path) {
  74. StorageInfo::Type type;
  75. if (!StorageInfo::CrackDeviceId(device_id, &type, nullptr))
  76. return false;
  77. if (type == StorageInfo::MAC_IMAGE_CAPTURE)
  78. return true;
  79. return !path.empty() && path.IsAbsolute() && !path.ReferencesParent();
  80. }
  81. // static
  82. void MediaStorageUtil::FilterAttachedDevices(DeviceIdSet* devices,
  83. base::OnceClosure done) {
  84. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  85. base::ThreadPool::PostTaskAndReply(
  86. FROM_HERE,
  87. {base::TaskPriority::BEST_EFFORT, base::MayBlock(),
  88. base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
  89. base::BindOnce(&FilterAttachedDevicesOnBackgroundSequence, devices),
  90. std::move(done));
  91. }
  92. // TODO(kmadhusu) Write unit tests for GetDeviceInfoFromPath().
  93. // static
  94. bool MediaStorageUtil::GetDeviceInfoFromPath(const base::FilePath& path,
  95. StorageInfo* device_info,
  96. base::FilePath* relative_path) {
  97. DCHECK(device_info);
  98. DCHECK(relative_path);
  99. if (!path.IsAbsolute())
  100. return false;
  101. StorageInfo info;
  102. StorageMonitor* monitor = StorageMonitor::GetInstance();
  103. bool found_device = monitor->GetStorageInfoForPath(path, &info);
  104. if (found_device && StorageInfo::IsRemovableDevice(info.device_id())) {
  105. base::FilePath sub_folder_path;
  106. base::FilePath device_path(info.location());
  107. if (path != device_path) {
  108. bool success = device_path.AppendRelativePath(path, &sub_folder_path);
  109. DCHECK(success);
  110. }
  111. *device_info = info;
  112. *relative_path = sub_folder_path;
  113. return true;
  114. }
  115. // On Posix systems, there's one root so any absolute path could be valid.
  116. // TODO(gbillock): Delete this stanza? Posix systems should have the root
  117. // volume information. If not, we should move the below into the
  118. // right GetStorageInfoForPath implementations.
  119. #if !BUILDFLAG(IS_POSIX)
  120. if (!found_device)
  121. return false;
  122. #endif
  123. // Handle non-removable devices. Note: this is just overwriting
  124. // good values from StorageMonitor.
  125. // TODO(gbillock): Make sure return values from that class are definitive,
  126. // and don't do this here.
  127. info.set_device_id(
  128. StorageInfo::MakeDeviceId(StorageInfo::FIXED_MASS_STORAGE,
  129. path.AsUTF8Unsafe()));
  130. *device_info = info;
  131. *relative_path = base::FilePath();
  132. return true;
  133. }
  134. // static
  135. base::FilePath MediaStorageUtil::FindDevicePathById(
  136. const std::string& device_id) {
  137. StorageInfo::Type type;
  138. std::string unique_id;
  139. if (!StorageInfo::CrackDeviceId(device_id, &type, &unique_id))
  140. return base::FilePath();
  141. if (type == StorageInfo::FIXED_MASS_STORAGE) {
  142. // For this type, the unique_id is the path.
  143. return base::FilePath::FromUTF8Unsafe(unique_id);
  144. }
  145. // For ImageCapture, the synthetic filesystem will be rooted at a fake
  146. // top-level directory which is the device_id.
  147. if (type == StorageInfo::MAC_IMAGE_CAPTURE) {
  148. #if !BUILDFLAG(IS_WIN)
  149. return base::FilePath(kRootPath + device_id);
  150. #endif
  151. }
  152. DCHECK(type == StorageInfo::MTP_OR_PTP ||
  153. type == StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM ||
  154. type == StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM);
  155. return base::FilePath(FindRemovableStorageLocationById(device_id));
  156. }
  157. // static
  158. bool MediaStorageUtil::IsRemovableStorageAttached(const std::string& id) {
  159. StorageMonitor* monitor = StorageMonitor::GetInstance();
  160. if (!monitor)
  161. return false;
  162. StorageInfoList devices = monitor->GetAllAvailableStorages();
  163. for (const auto& device : devices) {
  164. if (StorageInfo::IsRemovableDevice(id) && device.device_id() == id)
  165. return true;
  166. }
  167. return false;
  168. }
  169. } // namespace storage_monitor