OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/serial/serial_device_enumerator_win.h" | 5 #include "device/serial/serial_device_enumerator_win.h" |
6 | 6 |
7 #include <devguid.h> | 7 #include <devguid.h> |
8 #include <setupapi.h> | 8 #include <setupapi.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 #include <windows.h> | 10 #include <windows.h> |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 } | 81 } |
82 | 82 |
83 // Returns value clamped to the range of [min, max]. | 83 // Returns value clamped to the range of [min, max]. |
84 int Clamp(int value, int min, int max) { | 84 int Clamp(int value, int min, int max) { |
85 return std::min(std::max(value, min), max); | 85 return std::min(std::max(value, min), max); |
86 } | 86 } |
87 | 87 |
88 // Returns an array of devices as retrieved through the new method of | 88 // Returns an array of devices as retrieved through the new method of |
89 // enumerating serial devices (SetupDi). This new method gives more information | 89 // enumerating serial devices (SetupDi). This new method gives more information |
90 // about the devices than the old method. | 90 // about the devices than the old method. |
91 mojo::Array<serial::DeviceInfoPtr> GetDevicesNew() { | 91 std::vector<serial::DeviceInfoPtr> GetDevicesNew() { |
92 mojo::Array<serial::DeviceInfoPtr> devices; | 92 std::vector<serial::DeviceInfoPtr> devices; |
93 | 93 |
94 // Make a device interface query to find all serial devices. | 94 // Make a device interface query to find all serial devices. |
95 HDEVINFO dev_info = | 95 HDEVINFO dev_info = |
96 SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT); | 96 SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT); |
97 if (dev_info == INVALID_HANDLE_VALUE) | 97 if (dev_info == INVALID_HANDLE_VALUE) |
98 return devices; | 98 return devices; |
99 | 99 |
100 SP_DEVINFO_DATA dev_info_data; | 100 SP_DEVINFO_DATA dev_info_data; |
101 dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA); | 101 dev_info_data.cbSize = sizeof(SP_DEVINFO_DATA); |
102 for (DWORD i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) { | 102 for (DWORD i = 0; SetupDiEnumDeviceInfo(dev_info, i, &dev_info_data); i++) { |
(...skipping 30 matching lines...) Expand all Loading... |
133 devices.push_back(std::move(info)); | 133 devices.push_back(std::move(info)); |
134 } | 134 } |
135 | 135 |
136 SetupDiDestroyDeviceInfoList(dev_info); | 136 SetupDiDestroyDeviceInfoList(dev_info); |
137 return devices; | 137 return devices; |
138 } | 138 } |
139 | 139 |
140 // Returns an array of devices as retrieved through the old method of | 140 // Returns an array of devices as retrieved through the old method of |
141 // enumerating serial devices (searching the registry). This old method gives | 141 // enumerating serial devices (searching the registry). This old method gives |
142 // less information about the devices than the new method. | 142 // less information about the devices than the new method. |
143 mojo::Array<serial::DeviceInfoPtr> GetDevicesOld() { | 143 std::vector<serial::DeviceInfoPtr> GetDevicesOld() { |
144 base::win::RegistryValueIterator iter_key( | 144 base::win::RegistryValueIterator iter_key( |
145 HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\SERIALCOMM\\"); | 145 HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\SERIALCOMM\\"); |
146 mojo::Array<serial::DeviceInfoPtr> devices; | 146 std::vector<serial::DeviceInfoPtr> devices; |
147 for (; iter_key.Valid(); ++iter_key) { | 147 for (; iter_key.Valid(); ++iter_key) { |
148 serial::DeviceInfoPtr info(serial::DeviceInfo::New()); | 148 serial::DeviceInfoPtr info(serial::DeviceInfo::New()); |
149 info->path = base::UTF16ToASCII(iter_key.Value()); | 149 info->path = base::UTF16ToASCII(iter_key.Value()); |
150 devices.push_back(std::move(info)); | 150 devices.push_back(std::move(info)); |
151 } | 151 } |
152 return devices; | 152 return devices; |
153 } | 153 } |
154 | 154 |
155 } // namespace | 155 } // namespace |
156 | 156 |
157 // static | 157 // static |
158 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { | 158 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { |
159 return std::unique_ptr<SerialDeviceEnumerator>( | 159 return std::unique_ptr<SerialDeviceEnumerator>( |
160 new SerialDeviceEnumeratorWin()); | 160 new SerialDeviceEnumeratorWin()); |
161 } | 161 } |
162 | 162 |
163 SerialDeviceEnumeratorWin::SerialDeviceEnumeratorWin() {} | 163 SerialDeviceEnumeratorWin::SerialDeviceEnumeratorWin() {} |
164 | 164 |
165 SerialDeviceEnumeratorWin::~SerialDeviceEnumeratorWin() {} | 165 SerialDeviceEnumeratorWin::~SerialDeviceEnumeratorWin() {} |
166 | 166 |
167 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorWin::GetDevices() { | 167 std::vector<serial::DeviceInfoPtr> SerialDeviceEnumeratorWin::GetDevices() { |
168 mojo::Array<serial::DeviceInfoPtr> devices = GetDevicesNew(); | 168 std::vector<serial::DeviceInfoPtr> devices = GetDevicesNew(); |
169 mojo::Array<serial::DeviceInfoPtr> old_devices = GetDevicesOld(); | 169 std::vector<serial::DeviceInfoPtr> old_devices = GetDevicesOld(); |
170 | 170 |
171 UMA_HISTOGRAM_SPARSE_SLOWLY( | 171 UMA_HISTOGRAM_SPARSE_SLOWLY( |
172 "Hardware.Serial.NewMinusOldDeviceListSize", | 172 "Hardware.Serial.NewMinusOldDeviceListSize", |
173 Clamp(devices.size() - old_devices.size(), -10, 10)); | 173 Clamp(devices.size() - old_devices.size(), -10, 10)); |
174 | 174 |
175 // Add devices found from both the new and old methods of enumeration. If a | 175 // Add devices found from both the new and old methods of enumeration. If a |
176 // device is found using both the new and the old enumeration method, then we | 176 // device is found using both the new and the old enumeration method, then we |
177 // take the device from the new enumeration method because it's able to | 177 // take the device from the new enumeration method because it's able to |
178 // collect more information. We do this by inserting the new devices first, | 178 // collect more information. We do this by inserting the new devices first, |
179 // because insertions are ignored if the key already exists. | 179 // because insertions are ignored if the key already exists. |
180 std::unordered_set<std::string> devices_seen; | 180 std::unordered_set<std::string> devices_seen; |
181 for (const auto& device : devices) { | 181 for (const auto& device : devices) { |
182 bool inserted = devices_seen.insert(device->path).second; | 182 bool inserted = devices_seen.insert(device->path).second; |
183 DCHECK(inserted); | 183 DCHECK(inserted); |
184 } | 184 } |
185 for (auto& device : old_devices) { | 185 for (auto& device : old_devices) { |
186 if (devices_seen.insert(device->path).second) | 186 if (devices_seen.insert(device->path).second) |
187 devices.push_back(std::move(device)); | 187 devices.push_back(std::move(device)); |
188 } | 188 } |
189 return devices; | 189 return devices; |
190 } | 190 } |
191 | 191 |
192 } // namespace device | 192 } // namespace device |
OLD | NEW |