OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/win/scoped_process_information.h" | 5 #include "base/win/scoped_process_information.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/win/scoped_handle.h" | 8 #include "base/win/scoped_handle.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
11 namespace win { | 11 namespace win { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Duplicates source into target, returning true upon success. |target| is | 15 // Duplicates source into target, returning true upon success. |target| is |
16 // guaranteed to be untouched in case of failure. Succeeds with no side-effects | 16 // guaranteed to be untouched in case of failure. Succeeds with no side-effects |
17 // if source is NULL. | 17 // if source is NULL. |
18 bool CheckAndDuplicateHandle(HANDLE source, HANDLE* target) { | 18 bool CheckAndDuplicateHandle(HANDLE source, ScopedHandle* target) { |
19 if (!source) | 19 if (!source) |
20 return true; | 20 return true; |
21 | 21 |
22 HANDLE temp = NULL; | 22 HANDLE temp = NULL; |
23 if (!::DuplicateHandle(::GetCurrentProcess(), source, | 23 if (!::DuplicateHandle(::GetCurrentProcess(), source, |
24 ::GetCurrentProcess(), &temp, 0, FALSE, | 24 ::GetCurrentProcess(), &temp, 0, FALSE, |
25 DUPLICATE_SAME_ACCESS)) { | 25 DUPLICATE_SAME_ACCESS)) { |
26 DPLOG(ERROR) << "Failed to duplicate a handle."; | 26 DPLOG(ERROR) << "Failed to duplicate a handle."; |
27 return false; | 27 return false; |
28 } | 28 } |
29 *target = temp; | 29 target->Set(temp); |
30 return true; | 30 return true; |
31 } | 31 } |
32 | 32 |
33 } // namespace | 33 } // namespace |
34 | 34 |
35 ScopedProcessInformation::ScopedProcessInformation() | 35 ScopedProcessInformation::ScopedProcessInformation() |
36 : process_id_(0), thread_id_(0) { | 36 : process_id_(0), thread_id_(0) { |
37 } | 37 } |
38 | 38 |
39 ScopedProcessInformation::~ScopedProcessInformation() { | 39 ScopedProcessInformation::~ScopedProcessInformation() { |
40 Close(); | 40 Close(); |
41 } | 41 } |
42 | 42 |
43 ScopedProcessInformation::Receiver ScopedProcessInformation::Receive() { | |
44 DCHECK(!IsValid()) << "process_information_ must be NULL"; | |
45 return Receiver(this); | |
46 } | |
47 | |
48 bool ScopedProcessInformation::IsValid() const { | 43 bool ScopedProcessInformation::IsValid() const { |
49 return process_id_ || process_handle_.Get() || | 44 return process_id_ || process_handle_.Get() || |
50 thread_id_ || thread_handle_.Get(); | 45 thread_id_ || thread_handle_.Get(); |
51 } | 46 } |
52 | 47 |
53 void ScopedProcessInformation::Close() { | 48 void ScopedProcessInformation::Close() { |
54 process_handle_.Close(); | 49 process_handle_.Close(); |
55 thread_handle_.Close(); | 50 thread_handle_.Close(); |
56 process_id_ = 0; | 51 process_id_ = 0; |
57 thread_id_ = 0; | 52 thread_id_ = 0; |
58 } | 53 } |
59 | 54 |
60 void ScopedProcessInformation::Set(const PROCESS_INFORMATION& process_info) { | 55 void ScopedProcessInformation::Set(const PROCESS_INFORMATION& process_info) { |
61 if (IsValid()) | 56 if (IsValid()) |
62 Close(); | 57 Close(); |
63 | 58 |
64 process_handle_.Set(process_info.hProcess); | 59 process_handle_.Set(process_info.hProcess); |
65 thread_handle_.Set(process_info.hThread); | 60 thread_handle_.Set(process_info.hThread); |
66 process_id_ = process_info.dwProcessId; | 61 process_id_ = process_info.dwProcessId; |
67 thread_id_ = process_info.dwThreadId; | 62 thread_id_ = process_info.dwThreadId; |
68 } | 63 } |
69 | 64 |
70 bool ScopedProcessInformation::DuplicateFrom( | 65 bool ScopedProcessInformation::DuplicateFrom( |
71 const ScopedProcessInformation& other) { | 66 const ScopedProcessInformation& other) { |
72 DCHECK(!IsValid()) << "target ScopedProcessInformation must be NULL"; | 67 DCHECK(!IsValid()) << "target ScopedProcessInformation must be NULL"; |
73 DCHECK(other.IsValid()) << "source ScopedProcessInformation must be valid"; | 68 DCHECK(other.IsValid()) << "source ScopedProcessInformation must be valid"; |
74 | 69 |
75 if (CheckAndDuplicateHandle(other.process_handle(), | 70 if (CheckAndDuplicateHandle(other.process_handle(), &process_handle_) && |
76 process_handle_.Receive()) && | 71 CheckAndDuplicateHandle(other.thread_handle(), &thread_handle_)) { |
77 CheckAndDuplicateHandle(other.thread_handle(), | |
78 thread_handle_.Receive())) { | |
79 process_id_ = other.process_id(); | 72 process_id_ = other.process_id(); |
80 thread_id_ = other.thread_id(); | 73 thread_id_ = other.thread_id(); |
81 return true; | 74 return true; |
82 } | 75 } |
83 | 76 |
84 return false; | 77 return false; |
85 } | 78 } |
86 | 79 |
87 PROCESS_INFORMATION ScopedProcessInformation::Take() { | 80 PROCESS_INFORMATION ScopedProcessInformation::Take() { |
88 PROCESS_INFORMATION process_information = {}; | 81 PROCESS_INFORMATION process_information = {}; |
(...skipping 12 matching lines...) Expand all Loading... |
101 return process_handle_.Take(); | 94 return process_handle_.Take(); |
102 } | 95 } |
103 | 96 |
104 HANDLE ScopedProcessInformation::TakeThreadHandle() { | 97 HANDLE ScopedProcessInformation::TakeThreadHandle() { |
105 thread_id_ = 0; | 98 thread_id_ = 0; |
106 return thread_handle_.Take(); | 99 return thread_handle_.Take(); |
107 } | 100 } |
108 | 101 |
109 } // namespace win | 102 } // namespace win |
110 } // namespace base | 103 } // namespace base |
OLD | NEW |