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
This commit is contained in:
Scott Graham 2014-12-18 09:51:20 -08:00
parent 26c6f19927
commit 4a9b858fbd
3 changed files with 82 additions and 0 deletions

View File

@ -116,6 +116,8 @@
'stdlib/strnlen.h',
'synchronization/semaphore.cc',
'synchronization/semaphore.h',
'win/scoped_handle.cc',
'win/scoped_handle.h',
],
'conditions': [
['OS=="mac"', {

31
util/win/scoped_handle.cc Normal file
View File

@ -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

49
util/win/scoped_handle.h Normal file
View File

@ -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 <windows.h>
#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<HANDLE, internal::ScopedFileHANDLECloseTraits>;
using ScopedKernelHANDLE =
base::ScopedGeneric<HANDLE, internal::ScopedKernelHANDLECloseTraits>;
} // namespace crashpad
#endif // CRASHPAD_UTIL_WIN_SCOPED_HANDLE_H_