Implement Site Settings \ Images category.
[chromium-blink-merge.git] / base / win / scoped_process_information.cc
blobbb2463774a8a326ffb769d7aadf6852bf53e802f
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.
5 #include "base/win/scoped_process_information.h"
7 #include "base/logging.h"
8 #include "base/win/scoped_handle.h"
10 namespace base {
11 namespace win {
13 namespace {
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
17 // if source is NULL.
18 bool CheckAndDuplicateHandle(HANDLE source, ScopedHandle* target) {
19 if (!source)
20 return true;
22 HANDLE temp = NULL;
23 if (!::DuplicateHandle(::GetCurrentProcess(), source,
24 ::GetCurrentProcess(), &temp, 0, FALSE,
25 DUPLICATE_SAME_ACCESS)) {
26 DPLOG(ERROR) << "Failed to duplicate a handle.";
27 return false;
29 target->Set(temp);
30 return true;
33 } // namespace
35 ScopedProcessInformation::ScopedProcessInformation()
36 : process_id_(0), thread_id_(0) {
39 ScopedProcessInformation::ScopedProcessInformation(
40 const PROCESS_INFORMATION& process_info) : process_id_(0), thread_id_(0) {
41 Set(process_info);
44 ScopedProcessInformation::~ScopedProcessInformation() {
45 Close();
48 bool ScopedProcessInformation::IsValid() const {
49 return process_id_ || process_handle_.Get() ||
50 thread_id_ || thread_handle_.Get();
53 void ScopedProcessInformation::Close() {
54 process_handle_.Close();
55 thread_handle_.Close();
56 process_id_ = 0;
57 thread_id_ = 0;
60 void ScopedProcessInformation::Set(const PROCESS_INFORMATION& process_info) {
61 if (IsValid())
62 Close();
64 process_handle_.Set(process_info.hProcess);
65 thread_handle_.Set(process_info.hThread);
66 process_id_ = process_info.dwProcessId;
67 thread_id_ = process_info.dwThreadId;
70 bool ScopedProcessInformation::DuplicateFrom(
71 const ScopedProcessInformation& other) {
72 DCHECK(!IsValid()) << "target ScopedProcessInformation must be NULL";
73 DCHECK(other.IsValid()) << "source ScopedProcessInformation must be valid";
75 if (CheckAndDuplicateHandle(other.process_handle(), &process_handle_) &&
76 CheckAndDuplicateHandle(other.thread_handle(), &thread_handle_)) {
77 process_id_ = other.process_id();
78 thread_id_ = other.thread_id();
79 return true;
82 return false;
85 PROCESS_INFORMATION ScopedProcessInformation::Take() {
86 PROCESS_INFORMATION process_information = {};
87 process_information.hProcess = process_handle_.Take();
88 process_information.hThread = thread_handle_.Take();
89 process_information.dwProcessId = process_id();
90 process_information.dwThreadId = thread_id();
91 process_id_ = 0;
92 thread_id_ = 0;
94 return process_information;
97 HANDLE ScopedProcessInformation::TakeProcessHandle() {
98 process_id_ = 0;
99 return process_handle_.Take();
102 HANDLE ScopedProcessInformation::TakeThreadHandle() {
103 thread_id_ = 0;
104 return thread_handle_.Take();
107 } // namespace win
108 } // namespace base