test_storage_monitor.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/test_storage_monitor.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/run_loop.h"
  8. #include "base/synchronization/waitable_event.h"
  9. #include "build/build_config.h"
  10. #include "build/chromeos_buildflags.h"
  11. #include "components/storage_monitor/storage_info.h"
  12. #if BUILDFLAG(IS_CHROMEOS_ASH)
  13. #include "components/storage_monitor/test_media_transfer_protocol_manager_chromeos.h"
  14. #endif
  15. namespace storage_monitor {
  16. TestStorageMonitor::TestStorageMonitor() : init_called_(false) {
  17. #if BUILDFLAG(IS_CHROMEOS_ASH)
  18. auto* fake_mtp_manager =
  19. TestMediaTransferProtocolManagerChromeOS::GetFakeMtpManager();
  20. fake_mtp_manager->AddReceiver(
  21. media_transfer_protocol_manager_.BindNewPipeAndPassReceiver());
  22. #endif
  23. }
  24. TestStorageMonitor::~TestStorageMonitor() {}
  25. // static
  26. TestStorageMonitor* TestStorageMonitor::CreateAndInstall() {
  27. TestStorageMonitor* monitor = new TestStorageMonitor();
  28. std::unique_ptr<StorageMonitor> pass_monitor(monitor);
  29. monitor->Init();
  30. monitor->MarkInitialized();
  31. if (StorageMonitor::GetInstance() == nullptr) {
  32. StorageMonitor::SetStorageMonitorForTesting(std::move(pass_monitor));
  33. return monitor;
  34. }
  35. return nullptr;
  36. }
  37. // static
  38. TestStorageMonitor* TestStorageMonitor::CreateForBrowserTests() {
  39. TestStorageMonitor* monitor = new TestStorageMonitor();
  40. monitor->Init();
  41. monitor->MarkInitialized();
  42. std::unique_ptr<StorageMonitor> pass_monitor(monitor);
  43. StorageMonitor::SetStorageMonitorForTesting(std::move(pass_monitor));
  44. return monitor;
  45. }
  46. // static
  47. void TestStorageMonitor::SyncInitialize() {
  48. StorageMonitor* monitor = StorageMonitor::GetInstance();
  49. if (monitor->IsInitialized())
  50. return;
  51. base::WaitableEvent event(base::WaitableEvent::ResetPolicy::MANUAL,
  52. base::WaitableEvent::InitialState::NOT_SIGNALED);
  53. monitor->EnsureInitialized(
  54. base::BindOnce(&base::WaitableEvent::Signal, base::Unretained(&event)));
  55. while (!event.IsSignaled()) {
  56. base::RunLoop().RunUntilIdle();
  57. }
  58. DCHECK(monitor->IsInitialized());
  59. }
  60. void TestStorageMonitor::Init() {
  61. init_called_ = true;
  62. }
  63. void TestStorageMonitor::MarkInitialized() {
  64. StorageMonitor::MarkInitialized();
  65. }
  66. bool TestStorageMonitor::GetStorageInfoForPath(
  67. const base::FilePath& path,
  68. StorageInfo* device_info) const {
  69. DCHECK(device_info);
  70. if (!path.IsAbsolute())
  71. return false;
  72. bool is_removable = false;
  73. for (const base::FilePath& removable : removable_paths_) {
  74. if (path == removable || removable.IsParent(path)) {
  75. is_removable = true;
  76. break;
  77. }
  78. }
  79. std::string device_id = StorageInfo::MakeDeviceId(
  80. is_removable ? StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM
  81. : StorageInfo::FIXED_MASS_STORAGE,
  82. path.AsUTF8Unsafe());
  83. *device_info = StorageInfo(device_id, path.value(), std::u16string(),
  84. std::u16string(), std::u16string(), 0);
  85. return true;
  86. }
  87. #if BUILDFLAG(IS_WIN)
  88. bool TestStorageMonitor::GetMTPStorageInfoFromDeviceId(
  89. const std::string& storage_device_id,
  90. std::wstring* device_location,
  91. std::wstring* storage_object_id) const {
  92. return false;
  93. }
  94. #endif
  95. #if BUILDFLAG(IS_CHROMEOS_ASH)
  96. device::mojom::MtpManager*
  97. TestStorageMonitor::media_transfer_protocol_manager() {
  98. return media_transfer_protocol_manager_.get();
  99. }
  100. #endif
  101. StorageMonitor::Receiver* TestStorageMonitor::receiver() const {
  102. return StorageMonitor::receiver();
  103. }
  104. void TestStorageMonitor::EjectDevice(
  105. const std::string& device_id,
  106. base::OnceCallback<void(EjectStatus)> callback) {
  107. ejected_device_ = device_id;
  108. std::move(callback).Run(EJECT_OK);
  109. }
  110. void TestStorageMonitor::AddRemovablePath(const base::FilePath& path) {
  111. CHECK(path.IsAbsolute());
  112. removable_paths_.push_back(path);
  113. }
  114. } // namespace storage_monitor