From 4a9b858fbd1fcddcddfff00c8537a48f07f7ae69 Mon Sep 17 00:00:00 2001 From: Scott Graham Date: Thu, 18 Dec 2014 09:51:20 -0800 Subject: [PATCH] win: Add Scoped...Handle Intended for future use to implement util/file/file_writer. There's a similar class in base: https://code.google.com/p/chromium/codesearch#chromium/src/base/win/scoped_handle.h&l=28 However (perhaps for historical reasons) it does not distinguish between the possible types of HANDLEs which have different invalid values, resulting in a need to copy a bunch of code rather than simply using ScopedGeneric. Instead, distinguish between the types so the caller can use the correct one. Refs: http://blogs.msdn.com/b/oldnewthing/archive/2004/03/02/82639.aspx http://msdn.microsoft.com/en-us/magazine/cc302328.aspx (Figure 2) R=mark@chromium.org BUG=crashpad:1 Review URL: https://codereview.chromium.org/813873004 --- util/util.gyp | 2 ++ util/win/scoped_handle.cc | 31 +++++++++++++++++++++++++ util/win/scoped_handle.h | 49 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 util/win/scoped_handle.cc create mode 100644 util/win/scoped_handle.h diff --git a/util/util.gyp b/util/util.gyp index fe2a5096..b3bdf0ff 100644 --- a/util/util.gyp +++ b/util/util.gyp @@ -116,6 +116,8 @@ 'stdlib/strnlen.h', 'synchronization/semaphore.cc', 'synchronization/semaphore.h', + 'win/scoped_handle.cc', + 'win/scoped_handle.h', ], 'conditions': [ ['OS=="mac"', { diff --git a/util/win/scoped_handle.cc b/util/win/scoped_handle.cc new file mode 100644 index 00000000..34c1ec7b --- /dev/null +++ b/util/win/scoped_handle.cc @@ -0,0 +1,31 @@ +// Copyright 2014 The Crashpad Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "util/win/scoped_handle.h" + +#include "base/logging.h" + +namespace crashpad { +namespace internal { + +void ScopedFileHANDLECloseTraits::Free(HANDLE handle) { + PCHECK(CloseHandle(handle)); +} + +void ScopedKernelHANDLECloseTraits::Free(HANDLE handle) { + PCHECK(CloseHandle(handle)); +} + +} // namespace internal +} // namespace crashpad diff --git a/util/win/scoped_handle.h b/util/win/scoped_handle.h new file mode 100644 index 00000000..68be4f7b --- /dev/null +++ b/util/win/scoped_handle.h @@ -0,0 +1,49 @@ +// Copyright 2014 The Crashpad Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef CRASHPAD_UTIL_WIN_SCOPED_HANDLE_H_ +#define CRASHPAD_UTIL_WIN_SCOPED_HANDLE_H_ + +#include + +#include "base/scoped_generic.h" + +namespace crashpad { + +namespace internal { + +struct ScopedFileHANDLECloseTraits { + static HANDLE InvalidValue() { + return INVALID_HANDLE_VALUE; + } + static void Free(HANDLE handle); +}; + +struct ScopedKernelHANDLECloseTraits { + static HANDLE InvalidValue() { + return nullptr; + } + static void Free(HANDLE handle); +}; + +} // namespace internal + +using ScopedFileHANDLE = + base::ScopedGeneric; +using ScopedKernelHANDLE = + base::ScopedGeneric; + +} // namespace crashpad + +#endif // CRASHPAD_UTIL_WIN_SCOPED_HANDLE_H_