Chromium Code Reviews| Index: base/win/scoped_process_information.h |
| diff --git a/base/win/scoped_process_information.h b/base/win/scoped_process_information.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..241275476873c912719ba39b45cac5d9ed2dda02 |
| --- /dev/null |
| +++ b/base/win/scoped_process_information.h |
| @@ -0,0 +1,91 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ |
| +#define BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ |
| +#pragma once |
| + |
| +#include <windows.h> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/win/scoped_handle.h" |
| + |
| +namespace base { |
| +namespace win { |
| + |
| +class ProcessInfoTraits { |
| + public: |
| + typedef PROCESS_INFORMATION Handle; |
| + |
| + static bool CloseHandle(const PROCESS_INFORMATION& handle) { |
| + bool ret = true; |
| + if (handle.hThread && !::CloseHandle(handle.hThread)) |
| + ret = false; |
| + if (handle.hProcess && !::CloseHandle(handle.hProcess)) |
| + ret = false; |
| + return ret; |
| + } |
| + |
| + static bool IsHandleValid(const PROCESS_INFORMATION& handle) { |
| + return handle.hThread || handle.hProcess || |
| + handle.dwProcessId || handle.dwThreadId; |
| + } |
| + |
| + static bool IsSame(const PROCESS_INFORMATION& lhs, |
| + const PROCESS_INFORMATION& rhs) { |
| + return lhs.hProcess == rhs.hProcess && |
| + lhs.hThread == rhs.hThread && |
| + lhs.dwProcessId == rhs.dwProcessId && |
| + lhs.dwThreadId == rhs.dwThreadId; |
| + } |
| + |
| + static const PROCESS_INFORMATION& NullHandle() { |
| + return kNullHandle; |
| + } |
| + |
| + private: |
| + static const PROCESS_INFORMATION kNullHandle; |
| + |
| + DISALLOW_IMPLICIT_CONSTRUCTORS(ProcessInfoTraits); |
| +}; |
| + |
| +const PROCESS_INFORMATION ProcessInfoTraits::kNullHandle = {0}; |
| + |
| +// Manages the closing of process and thread handles from PROCESS_INFORMATION |
| +// structures. Allows clients to take ownership of either handle independently. |
| +class ScopedProcessInformation : public GenericScopedHandle<ProcessInfoTraits> { |
| + public: |
| + ScopedProcessInformation() |
| + : GenericScopedHandle(ProcessInfoTraits::NullHandle()) {} |
| + |
| + explicit ScopedProcessInformation(Handle handle) |
| + : GenericScopedHandle(handle) {} |
| + |
| + // Transfers ownership of the process handle away from this object. The |
| + // hProcess and dwProcessId members will be reset. |
| + HANDLE TakeProcessHandle() { |
| + PROCESS_INFORMATION process_info = Take(); |
| + HANDLE process = process_info.hProcess; |
| + process_info.hProcess = NULL; |
| + process_info.dwProcessId = 0; |
|
sanjeevr
2012/03/15 17:42:55
Drive-by: Zeroing out the process id here (and the
erikwright (departed)
2012/03/15 18:02:44
It's primarily to simplify the usage pattern. If s
|
| + Set(process_info); |
| + return process; |
| + } |
| + |
| + // Transfers ownership of the thread handle away from this object. The hThread |
| + // and dwThreadId members will be reset. |
| + HANDLE TakeThreadHandle() { |
| + PROCESS_INFORMATION process_info = Take(); |
| + HANDLE thread = process_info.hThread; |
| + process_info.hThread = NULL; |
| + process_info.dwThreadId = 0; |
| + Set(process_info); |
| + return thread; |
| + } |
| +}; |
| + |
| +} // namespace win |
| +} // namespace base |
| + |
| +#endif // BASE_SCOPED_HANDLE_WIN_H_ |