Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ | |
| 6 #define BASE_WIN_SCOPED_PROCESS_INFORMATION_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <windows.h> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/win/scoped_handle.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace win { | |
| 16 | |
| 17 class ProcessInfoTraits { | |
| 18 public: | |
| 19 typedef PROCESS_INFORMATION Handle; | |
| 20 | |
| 21 static bool CloseHandle(const PROCESS_INFORMATION& handle) { | |
| 22 bool ret = true; | |
| 23 if (handle.hThread && !::CloseHandle(handle.hThread)) | |
| 24 ret = false; | |
| 25 if (handle.hProcess && !::CloseHandle(handle.hProcess)) | |
| 26 ret = false; | |
| 27 return ret; | |
| 28 } | |
| 29 | |
| 30 static bool IsHandleValid(const PROCESS_INFORMATION& handle) { | |
| 31 return handle.hThread || handle.hProcess || | |
| 32 handle.dwProcessId || handle.dwThreadId; | |
| 33 } | |
| 34 | |
| 35 static bool IsSame(const PROCESS_INFORMATION& lhs, | |
| 36 const PROCESS_INFORMATION& rhs) { | |
| 37 return lhs.hProcess == rhs.hProcess && | |
| 38 lhs.hThread == rhs.hThread && | |
| 39 lhs.dwProcessId == rhs.dwProcessId && | |
| 40 lhs.dwThreadId == rhs.dwThreadId; | |
| 41 } | |
| 42 | |
| 43 static const PROCESS_INFORMATION& NullHandle() { | |
| 44 return kNullHandle; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 static const PROCESS_INFORMATION kNullHandle; | |
| 49 | |
| 50 DISALLOW_IMPLICIT_CONSTRUCTORS(ProcessInfoTraits); | |
| 51 }; | |
| 52 | |
| 53 const PROCESS_INFORMATION ProcessInfoTraits::kNullHandle = {0}; | |
| 54 | |
| 55 // Manages the closing of process and thread handles from PROCESS_INFORMATION | |
| 56 // structures. Allows clients to take ownership of either handle independently. | |
| 57 class ScopedProcessInformation : public GenericScopedHandle<ProcessInfoTraits> { | |
| 58 public: | |
| 59 ScopedProcessInformation() | |
| 60 : GenericScopedHandle(ProcessInfoTraits::NullHandle()) {} | |
| 61 | |
| 62 explicit ScopedProcessInformation(Handle handle) | |
| 63 : GenericScopedHandle(handle) {} | |
| 64 | |
| 65 // Transfers ownership of the process handle away from this object. The | |
| 66 // hProcess and dwProcessId members will be reset. | |
| 67 HANDLE TakeProcessHandle() { | |
| 68 PROCESS_INFORMATION process_info = Take(); | |
| 69 HANDLE process = process_info.hProcess; | |
| 70 process_info.hProcess = NULL; | |
| 71 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
| |
| 72 Set(process_info); | |
| 73 return process; | |
| 74 } | |
| 75 | |
| 76 // Transfers ownership of the thread handle away from this object. The hThread | |
| 77 // and dwThreadId members will be reset. | |
| 78 HANDLE TakeThreadHandle() { | |
| 79 PROCESS_INFORMATION process_info = Take(); | |
| 80 HANDLE thread = process_info.hThread; | |
| 81 process_info.hThread = NULL; | |
| 82 process_info.dwThreadId = 0; | |
| 83 Set(process_info); | |
| 84 return thread; | |
| 85 } | |
| 86 }; | |
| 87 | |
| 88 } // namespace win | |
| 89 } // namespace base | |
| 90 | |
| 91 #endif // BASE_SCOPED_HANDLE_WIN_H_ | |
| OLD | NEW |