77ea57b282f8557a411400a89fcd90d1ec74314b
[platform/framework/web/crosswalk.git] / src / components / storage_monitor / test_volume_mount_watcher_win.cc
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 //
5 // TestVolumeMountWatcherWin implementation.
6
7 #include "components/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 "components/storage_monitor/storage_info.h"
14
15 namespace storage_monitor {
16
17 namespace {
18
19 base::FilePath GetTempRoot() {
20   base::ScopedTempDir temp_dir;
21   temp_dir.CreateUniqueTempDir();
22   base::FilePath temp_root = temp_dir.path();
23   while (temp_root.DirName() != temp_root)
24     temp_root = temp_root.DirName();
25   return temp_root;
26 }
27
28 std::vector<base::FilePath> FakeGetSingleAttachedDevice() {
29   std::vector<base::FilePath> result;
30   result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2));  // C
31
32   // Make sure we are adding the drive on which ScopedTempDir will make
33   // test directories.
34   base::FilePath temp_root = GetTempRoot();
35   if (temp_root != VolumeMountWatcherWin::DriveNumberToFilePath(2))
36     result.push_back(temp_root);
37
38   return result;
39 }
40
41 std::vector<base::FilePath> FakeGetAttachedDevices() {
42   std::vector<base::FilePath> result;
43   result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(0));  // A
44   result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(1));  // B
45   result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(2));  // C
46   result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(3));  // D
47   result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(5));  // F
48   result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(7));  // H
49   result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(13));  // N
50   result.push_back(VolumeMountWatcherWin::DriveNumberToFilePath(25));  // Z
51   return result;
52 }
53
54 // Gets the details of the mass storage device specified by the |device_path|.
55 // |device_path| inputs of 'A:\' - 'Z:\' are valid. 'N:\' is not removable.
56 // 'C:\' is not removable (so that auto-added paths are correctly handled).
57 bool GetMassStorageDeviceDetails(const base::FilePath& device_path,
58                                  StorageInfo* info) {
59   DCHECK(info);
60
61   // Truncate to root path.
62   base::FilePath path(device_path);
63   if (device_path.value().length() > 3)
64     path = base::FilePath(device_path.value().substr(0, 3));
65   base::FilePath::CharType drive_letter = path.value()[0];
66   if (drive_letter < L'A' || drive_letter > L'Z')
67     return false;
68
69   StorageInfo::Type type = StorageInfo::FIXED_MASS_STORAGE;
70   if (path.value() != base::ASCIIToUTF16("N:\\") &&
71       path.value() != base::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] = static_cast<char>(drive_letter);
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, path.value(), storage_label, base::string16(),
81                       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& storage_label,
100     uint64 total_size_in_bytes) {
101   StorageInfo info(device_id, device_path.value(), storage_label,
102                    base::string16(), base::string16(), total_size_in_bytes);
103   HandleDeviceAttachEventOnUIThread(device_path, info);
104 }
105
106 void TestVolumeMountWatcherWin::SetAttachedDevicesFake() {
107   attached_devices_fake_ = true;
108 }
109
110 void TestVolumeMountWatcherWin::FlushWorkerPoolForTesting() {
111   device_info_worker_pool_->FlushForTesting();
112 }
113
114 void TestVolumeMountWatcherWin::DeviceCheckComplete(
115     const base::FilePath& device_path) {
116   devices_checked_.push_back(device_path);
117   if (device_check_complete_event_.get())
118     device_check_complete_event_->Wait();
119   VolumeMountWatcherWin::DeviceCheckComplete(device_path);
120 }
121
122 void TestVolumeMountWatcherWin::BlockDeviceCheckForTesting() {
123   device_check_complete_event_.reset(new base::WaitableEvent(false, false));
124   devices_checked_.clear();
125 }
126
127 void TestVolumeMountWatcherWin::ReleaseDeviceCheck() {
128   device_check_complete_event_->Signal();
129 }
130
131 // static
132 bool TestVolumeMountWatcherWin::GetDeviceRemovable(
133     const base::FilePath& device_path,
134     bool* removable) {
135   StorageInfo info;
136   bool success = GetMassStorageDeviceDetails(device_path, &info);
137   *removable = StorageInfo::IsRemovableDevice(info.device_id());
138   return success;
139 }
140
141 VolumeMountWatcherWin::GetDeviceDetailsCallbackType
142 TestVolumeMountWatcherWin::GetDeviceDetailsCallback() const {
143   return base::Bind(&GetMassStorageDeviceDetails);
144 }
145
146 VolumeMountWatcherWin::GetAttachedDevicesCallbackType
147   TestVolumeMountWatcherWin::GetAttachedDevicesCallback() const {
148   if (attached_devices_fake_)
149     return base::Bind(&FakeGetAttachedDevices);
150   return base::Bind(&FakeGetSingleAttachedDevice);
151 }
152
153 void TestVolumeMountWatcherWin::ShutdownWorkerPool() {
154   device_info_worker_pool_->Shutdown();
155 }
156
157 }  // namespace storage_monitor