#ifndef BASE_WIN_SCOPED_HANDLE_H_
#define BASE_WIN_SCOPED_HANDLE_H_
#include <windows.h>
#include "base/gtest_prod_util.h"
#include "base/logging.h"
#if defined(COMPILER_MSVC)
#include <intrin.h>
#define BASE_WIN_GET_CALLER _ReturnAddress()
#elif defined(COMPILER_GCC)
#define BASE_WIN_GET_CALLER \
__builtin_extract_return_addr(\ __builtin_return_address(0))
#endif
namespace base {
namespace win {
template <class Traits, class Verifier>
class GenericScopedHandle {
public:
typedef typename Traits::Handle Handle;
GenericScopedHandle() : handle_(Traits::NullHandle()) {}
explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) {
Set(handle);
}
GenericScopedHandle(GenericScopedHandle&& other)
: handle_(Traits::NullHandle()) {
Set(other.Take());
}
~GenericScopedHandle() { Close(); }
bool IsValid() const { return Traits::IsHandleValid(handle_); }
GenericScopedHandle& operator=(GenericScopedHandle&& other) {
DCHECK_NE(this, &other);
Set(other.Take());
return *this;
}
void Set(Handle handle) {
if (handle_ != handle) {
auto last_error = ::GetLastError();
Close();
if (Traits::IsHandleValid(handle)) {
handle_ = handle;
}
::SetLastError(last_error);
}
}
Handle Get() const { return handle_; }
Handle Take() {
Handle temp = handle_;
handle_ = Traits::NullHandle();
return temp;
}
void Close() {
if (Traits::IsHandleValid(handle_)) {
Traits::CloseHandle(handle_);
handle_ = Traits::NullHandle();
}
}
private:
FRIEND_TEST_ALL_PREFIXES(ScopedHandleTest, ActiveVerifierWrongOwner);
FRIEND_TEST_ALL_PREFIXES(ScopedHandleTest, ActiveVerifierUntrackedHandle);
Handle handle_;
GenericScopedHandle(const GenericScopedHandle&) = delete;
GenericScopedHandle& operator=(const GenericScopedHandle&) = delete;
};
#undef BASE_WIN_GET_CALLER
class HandleTraits {
public:
typedef HANDLE Handle;
static bool CloseHandle(HANDLE handle);
static bool IsHandleValid(HANDLE handle) {
return handle != NULL && handle != INVALID_HANDLE_VALUE;
}
static HANDLE NullHandle() { return NULL; }
private:
HandleTraits() = delete;
HandleTraits(const HandleTraits&) = delete;
HandleTraits& operator=(const HandleTraits&) = delete;
};
class DummyVerifierTraits {
public:
typedef HANDLE Handle;
static void StartTracking(HANDLE handle,
const void* owner,
const void* pc1,
const void* pc2) {}
static void StopTracking(HANDLE handle,
const void* owner,
const void* pc1,
const void* pc2) {}
private:
DummyVerifierTraits() = delete;
DummyVerifierTraits(const DummyVerifierTraits&) = delete;
DummyVerifierTraits& operator=(const DummyVerifierTraits&) = delete;
};
class VerifierTraits {
public:
typedef HANDLE Handle;
static void StartTracking(HANDLE handle,
const void* owner,
const void* pc1,
const void* pc2);
static void StopTracking(HANDLE handle,
const void* owner,
const void* pc1,
const void* pc2);
private:
VerifierTraits() = delete;
VerifierTraits(const VerifierTraits&) = delete;
VerifierTraits& operator=(const VerifierTraits&) = delete;
};
typedef GenericScopedHandle<HandleTraits, VerifierTraits> ScopedHandle;
void DisableHandleVerifier();
void OnHandleBeingClosed(HANDLE handle);
}
}
#endif