Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(839)

Unified Diff: base/win/scoped_device_info_object.h

Issue 1439443002: Reland: Add code to deal with serial device disconnection detection on Windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: initialize helper_ to be nullptr Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/win/scoped_device_info_object.h
diff --git a/base/win/scoped_device_info_object.h b/base/win/scoped_device_info_object.h
new file mode 100644
index 0000000000000000000000000000000000000000..7802567f803c53dd92262a2883159de417fb7d66
--- /dev/null
+++ b/base/win/scoped_device_info_object.h
@@ -0,0 +1,68 @@
+// 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.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_WIN_SCOPED_DEVICE_INFO_OBJECT_H_
+#define BASE_WIN_SCOPED_DEVICE_INFO_OBJECT_H_
+
+#include <windows.h>
+#include <setupapi.h>
+
+#include "base/win/scoped_handle.h"
+
+namespace base {
+namespace win {
+
+// Traits for HDEVINFO that properly destroys it.
+struct DeviceInfoListTraits {
+ typedef HDEVINFO Handle;
+
+ static void CloseHandle(HDEVINFO handle) {
+ SetupDiDestroyDeviceInfoList(handle);
+ }
+
+ static bool IsHandleValid(HDEVINFO handle) {
+ return handle != INVALID_HANDLE_VALUE;
+ }
+
+ static HDEVINFO NullHandle() { return INVALID_HANDLE_VALUE; }
+};
+
+typedef base::win::GenericScopedHandle<DeviceInfoListTraits,
+ base::win::DummyVerifierTraits>
+ 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.
+
+// Wrapper around an SP_DEVINFO_DATA that initializes it properly and
+// automatically deletes it.
+class ScopedDeviceInfo {
+ public:
+ ScopedDeviceInfo() {
+ memset(&dev_info_data_, 0, sizeof(dev_info_data_));
+ dev_info_data_.cbSize = sizeof(dev_info_data_);
+ }
+
+ ~ScopedDeviceInfo() {
+ if (dev_info_set_ != INVALID_HANDLE_VALUE) {
+ SetupDiDeleteDeviceInfo(dev_info_set_, &dev_info_data_);
+ }
+ }
+
+ // Once the SP_DEVINFO_DATA has been populated it must be freed using the
+ // HDEVINFO it was created from.
+ void set_valid(HDEVINFO dev_info_set) {
+ DCHECK(dev_info_set_ == INVALID_HANDLE_VALUE);
+ DCHECK(dev_info_set != INVALID_HANDLE_VALUE);
+ dev_info_set_ = dev_info_set;
+ }
+
+ PSP_DEVINFO_DATA get() { return &dev_info_data_; }
+
+ private:
+ HDEVINFO dev_info_set_ = INVALID_HANDLE_VALUE;
+ SP_DEVINFO_DATA dev_info_data_;
+};
+
+} // namespace win
+} // namespace base
+
+#endif // BASE_WIN_SCOPED_DEVICE_INFO_OBJECT_H_

Powered by Google App Engine
This is Rietveld 408576698