1 // Copyright (c) 2013 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 // 5 // TestVolumeMountWatcherWin implementation. 6 7 #include "chrome/browser/storage_monitor/test_volume_mount_watcher_win.h" 8 9 #include "base/bind.h" 10 #include "base/files/file_path.h" 11 #include "base/files/scoped_temp_dir.h" 12 #include "base/strings/utf_string_conversions.h" 13 #include "chrome/browser/storage_monitor/storage_info.h" 14 15 namespace chrome { 16 namespace test { 17 18 namespace { 19 20 base::FilePath GetTempRoot() { 21 base::ScopedTempDir temp_dir; 22 temp_dir.CreateUniqueTempDir(); 23 base::FilePath temp_root = temp_dir.path(); 24 while (temp_root.DirName() != temp_root) 25 temp_root = temp_root.DirName(); 26 return temp_root; 27 } 28 29 std::vector<base::FilePath> FakeGetSingleAttachedDevice() { 30 std::vector<base::FilePath> result; 31 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2)); // C 32 33 // Make sure we are adding the drive on which ScopedTempDir will make 34 // test directories. 35 base::FilePath temp_root = GetTempRoot(); 36 if (temp_root != VolumeMountWatcherWin::DriveNumberToFilePath(2)) 37 result.push_back(temp_root); 38 39 return result; 40 } 41 42 std::vector<base::FilePath> FakeGetAttachedDevices() { 43 std::vector<base::FilePath> result; 44 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(0)); // A 45 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(1)); // B 46 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2)); // C 47 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(3)); // D 48 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(5)); // F 49 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(7)); // H 50 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(13)); // N 51 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(25)); // Z 52 return result; 53 } 54 55 // Gets the details of the mass storage device specified by the |device_path|. 56 // |device_path| inputs of 'A:\' - 'Z:\' are valid. 'N:\' is not removable. 57 // 'C:\' is not removable (so that auto-added paths are correctly handled). 58 bool GetMassStorageDeviceDetails(const base::FilePath& device_path, 59 StorageInfo* info) { 60 DCHECK(info); 61 62 // Truncate to root path. 63 base::FilePath path(device_path); 64 if (device_path.value().length() > 3) 65 path = base::FilePath(device_path.value().substr(0, 3)); 66 if (path.value()[0] < L'A' || path.value()[0] > L'Z') 67 return false; 68 69 StorageInfo::Type type = StorageInfo::FIXED_MASS_STORAGE; 70 if (path.value() != ASCIIToUTF16("N:\\") && 71 path.value() != ASCIIToUTF16("C:\\") && 72 path.value() != GetTempRoot().value()) { 73 type = StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM; 74 } 75 std::string unique_id = 76 "\\\\?\\Volume{00000000-0000-0000-0000-000000000000}\\"; 77 unique_id[11] = device_path.value()[0]; 78 std::string device_id = StorageInfo::MakeDeviceId(type, unique_id); 79 base::string16 storage_label = path.Append(L" Drive").LossyDisplayName(); 80 *info = StorageInfo(device_id, base::string16(), path.value(), storage_label, 81 base::string16(), base::string16(), 1000000); 82 83 return true; 84 } 85 86 } // namespace 87 88 // TestVolumeMountWatcherWin --------------------------------------------------- 89 90 TestVolumeMountWatcherWin::TestVolumeMountWatcherWin() 91 : attached_devices_fake_(false) {} 92 93 TestVolumeMountWatcherWin::~TestVolumeMountWatcherWin() { 94 } 95 96 void TestVolumeMountWatcherWin::AddDeviceForTesting( 97 const base::FilePath& device_path, 98 const std::string& device_id, 99 const base::string16& device_name, 100 uint64 total_size_in_bytes) { 101 StorageInfo info(device_id, device_name, device_path.value(), 102 base::string16(), base::string16(), base::string16(), 103 total_size_in_bytes); 104 HandleDeviceAttachEventOnUIThread(device_path, info); 105 } 106 107 void TestVolumeMountWatcherWin::SetAttachedDevicesFake() { 108 attached_devices_fake_ = true; 109 } 110 111 void TestVolumeMountWatcherWin::FlushWorkerPoolForTesting() { 112 device_info_worker_pool_->FlushForTesting(); 113 } 114 115 void TestVolumeMountWatcherWin::DeviceCheckComplete( 116 const base::FilePath& device_path) { 117 devices_checked_.push_back(device_path); 118 if (device_check_complete_event_.get()) 119 device_check_complete_event_->Wait(); 120 VolumeMountWatcherWin::DeviceCheckComplete(device_path); 121 } 122 123 void TestVolumeMountWatcherWin::BlockDeviceCheckForTesting() { 124 device_check_complete_event_.reset(new base::WaitableEvent(false, false)); 125 devices_checked_.clear(); 126 } 127 128 void TestVolumeMountWatcherWin::ReleaseDeviceCheck() { 129 device_check_complete_event_->Signal(); 130 } 131 132 // static 133 bool TestVolumeMountWatcherWin::GetDeviceRemovable( 134 const base::FilePath& device_path, 135 bool* removable) { 136 StorageInfo info; 137 bool success = GetMassStorageDeviceDetails(device_path, &info); 138 *removable = StorageInfo::IsRemovableDevice(info.device_id()); 139 return success; 140 } 141 142 VolumeMountWatcherWin::GetDeviceDetailsCallbackType 143 TestVolumeMountWatcherWin::GetDeviceDetailsCallback() const { 144 return base::Bind(&GetMassStorageDeviceDetails); 145 } 146 147 VolumeMountWatcherWin::GetAttachedDevicesCallbackType 148 TestVolumeMountWatcherWin::GetAttachedDevicesCallback() const { 149 if (attached_devices_fake_) 150 return base::Bind(&FakeGetAttachedDevices); 151 return base::Bind(&FakeGetSingleAttachedDevice); 152 } 153 154 void TestVolumeMountWatcherWin::ShutdownWorkerPool() { 155 device_info_worker_pool_->Shutdown(); 156 } 157 158 } // namespace test 159 } // namespace chrome 160