OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
Reilly Grant (use Gerrit)
2015/11/12 19:30:02
nit: No (c) in new files.
juncai
2015/11/12 23:21:39
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_WIN_SCOPED_DEVICE_INFO_OBJECT_H_ | |
6 #define BASE_WIN_SCOPED_DEVICE_INFO_OBJECT_H_ | |
7 | |
8 #include <windows.h> | |
9 #include <setupapi.h> | |
10 | |
11 #include "base/win/scoped_handle.h" | |
12 | |
13 namespace base { | |
14 namespace win { | |
15 | |
16 // Traits for HDEVINFO that properly destroys it. | |
17 struct DeviceInfoListTraits { | |
18 typedef HDEVINFO Handle; | |
19 | |
20 static void CloseHandle(HDEVINFO handle) { | |
21 SetupDiDestroyDeviceInfoList(handle); | |
22 } | |
23 | |
24 static bool IsHandleValid(HDEVINFO handle) { | |
25 return handle != INVALID_HANDLE_VALUE; | |
26 } | |
27 | |
28 static HDEVINFO NullHandle() { return INVALID_HANDLE_VALUE; } | |
29 }; | |
30 | |
31 typedef base::win::GenericScopedHandle<DeviceInfoListTraits, | |
32 base::win::DummyVerifierTraits> | |
33 ScopedDeviceInfoList; | |
Reilly Grant (use Gerrit)
2015/11/12 19:30:02
I think this should be in a separate scoped_device
juncai
2015/11/12 23:21:39
Done.
| |
34 | |
35 // Wrapper around an SP_DEVINFO_DATA that initializes it properly and | |
36 // automatically deletes it. | |
37 class ScopedDeviceInfo { | |
38 public: | |
39 ScopedDeviceInfo() { | |
40 memset(&dev_info_data_, 0, sizeof(dev_info_data_)); | |
41 dev_info_data_.cbSize = sizeof(dev_info_data_); | |
42 } | |
43 | |
44 ~ScopedDeviceInfo() { | |
45 if (dev_info_set_ != INVALID_HANDLE_VALUE) { | |
46 SetupDiDeleteDeviceInfo(dev_info_set_, &dev_info_data_); | |
47 } | |
48 } | |
49 | |
50 // Once the SP_DEVINFO_DATA has been populated it must be freed using the | |
51 // HDEVINFO it was created from. | |
52 void set_valid(HDEVINFO dev_info_set) { | |
53 DCHECK(dev_info_set_ == INVALID_HANDLE_VALUE); | |
54 DCHECK(dev_info_set != INVALID_HANDLE_VALUE); | |
55 dev_info_set_ = dev_info_set; | |
56 } | |
57 | |
58 PSP_DEVINFO_DATA get() { return &dev_info_data_; } | |
59 | |
60 private: | |
61 HDEVINFO dev_info_set_ = INVALID_HANDLE_VALUE; | |
62 SP_DEVINFO_DATA dev_info_data_; | |
63 }; | |
64 | |
65 } // namespace win | |
66 } // namespace base | |
67 | |
68 #endif // BASE_WIN_SCOPED_DEVICE_INFO_OBJECT_H_ | |
OLD | NEW |