Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / storage_monitor / test_volume_mount_watcher_win.cc
blob151bc1fca10103d58ca505cbcd6e1245593acb08
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.
7 #include "chrome/browser/storage_monitor/test_volume_mount_watcher_win.h"
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"
15 namespace {
17 base::FilePath GetTempRoot() {
18 base::ScopedTempDir temp_dir;
19 temp_dir.CreateUniqueTempDir();
20 base::FilePath temp_root = temp_dir.path();
21 while (temp_root.DirName() != temp_root)
22 temp_root = temp_root.DirName();
23 return temp_root;
26 std::vector<base::FilePath> FakeGetSingleAttachedDevice() {
27 std::vector<base::FilePath> result;
28 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2)); // C
30 // Make sure we are adding the drive on which ScopedTempDir will make
31 // test directories.
32 base::FilePath temp_root = GetTempRoot();
33 if (temp_root != VolumeMountWatcherWin::DriveNumberToFilePath(2))
34 result.push_back(temp_root);
36 return result;
39 std::vector<base::FilePath> FakeGetAttachedDevices() {
40 std::vector<base::FilePath> result;
41 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(0)); // A
42 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(1)); // B
43 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2)); // C
44 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(3)); // D
45 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(5)); // F
46 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(7)); // H
47 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(13)); // N
48 result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(25)); // Z
49 return result;
52 // Gets the details of the mass storage device specified by the |device_path|.
53 // |device_path| inputs of 'A:\' - 'Z:\' are valid. 'N:\' is not removable.
54 // 'C:\' is not removable (so that auto-added paths are correctly handled).
55 bool GetMassStorageDeviceDetails(const base::FilePath& device_path,
56 StorageInfo* info) {
57 DCHECK(info);
59 // Truncate to root path.
60 base::FilePath path(device_path);
61 if (device_path.value().length() > 3)
62 path = base::FilePath(device_path.value().substr(0, 3));
63 if (path.value()[0] < L'A' || path.value()[0] > L'Z')
64 return false;
66 StorageInfo::Type type = StorageInfo::FIXED_MASS_STORAGE;
67 if (path.value() != base::ASCIIToUTF16("N:\\") &&
68 path.value() != base::ASCIIToUTF16("C:\\") &&
69 path.value() != GetTempRoot().value()) {
70 type = StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM;
72 std::string unique_id =
73 "\\\\?\\Volume{00000000-0000-0000-0000-000000000000}\\";
74 unique_id[11] = device_path.value()[0];
75 std::string device_id = StorageInfo::MakeDeviceId(type, unique_id);
76 base::string16 storage_label = path.Append(L" Drive").LossyDisplayName();
77 *info = StorageInfo(device_id, base::string16(), path.value(), storage_label,
78 base::string16(), base::string16(), 1000000);
80 return true;
83 } // namespace
85 // TestVolumeMountWatcherWin ---------------------------------------------------
87 TestVolumeMountWatcherWin::TestVolumeMountWatcherWin()
88 : attached_devices_fake_(false) {}
90 TestVolumeMountWatcherWin::~TestVolumeMountWatcherWin() {
93 void TestVolumeMountWatcherWin::AddDeviceForTesting(
94 const base::FilePath& device_path,
95 const std::string& device_id,
96 const base::string16& device_name,
97 uint64 total_size_in_bytes) {
98 StorageInfo info(device_id, device_name, device_path.value(),
99 base::string16(), base::string16(), base::string16(),
100 total_size_in_bytes);
101 HandleDeviceAttachEventOnUIThread(device_path, info);
104 void TestVolumeMountWatcherWin::SetAttachedDevicesFake() {
105 attached_devices_fake_ = true;
108 void TestVolumeMountWatcherWin::FlushWorkerPoolForTesting() {
109 device_info_worker_pool_->FlushForTesting();
112 void TestVolumeMountWatcherWin::DeviceCheckComplete(
113 const base::FilePath& device_path) {
114 devices_checked_.push_back(device_path);
115 if (device_check_complete_event_.get())
116 device_check_complete_event_->Wait();
117 VolumeMountWatcherWin::DeviceCheckComplete(device_path);
120 void TestVolumeMountWatcherWin::BlockDeviceCheckForTesting() {
121 device_check_complete_event_.reset(new base::WaitableEvent(false, false));
122 devices_checked_.clear();
125 void TestVolumeMountWatcherWin::ReleaseDeviceCheck() {
126 device_check_complete_event_->Signal();
129 // static
130 bool TestVolumeMountWatcherWin::GetDeviceRemovable(
131 const base::FilePath& device_path,
132 bool* removable) {
133 StorageInfo info;
134 bool success = GetMassStorageDeviceDetails(device_path, &info);
135 *removable = StorageInfo::IsRemovableDevice(info.device_id());
136 return success;
139 VolumeMountWatcherWin::GetDeviceDetailsCallbackType
140 TestVolumeMountWatcherWin::GetDeviceDetailsCallback() const {
141 return base::Bind(&GetMassStorageDeviceDetails);
144 VolumeMountWatcherWin::GetAttachedDevicesCallbackType
145 TestVolumeMountWatcherWin::GetAttachedDevicesCallback() const {
146 if (attached_devices_fake_)
147 return base::Bind(&FakeGetAttachedDevices);
148 return base::Bind(&FakeGetSingleAttachedDevice);
151 void TestVolumeMountWatcherWin::ShutdownWorkerPool() {
152 device_info_worker_pool_->Shutdown();