1 // Copyright (c) 2011 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/process/process.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "base/process/kill.h"
12 #include "base/win/windows_version.h"
16 DWORD kBasicProcessAccess
=
17 PROCESS_TERMINATE
| PROCESS_QUERY_INFORMATION
| SYNCHRONIZE
;
23 Process::Process(ProcessHandle handle
)
24 : is_current_process_(false),
26 CHECK_NE(handle
, ::GetCurrentProcess());
29 Process::Process(RValue other
)
30 : is_current_process_(other
.object
->is_current_process_
),
31 process_(other
.object
->process_
.Take()) {
32 other
.object
->Close();
38 Process
& Process::operator=(RValue other
) {
39 if (this != other
.object
) {
40 process_
.Set(other
.object
->process_
.Take());
41 is_current_process_
= other
.object
->is_current_process_
;
42 other
.object
->Close();
48 Process
Process::Current() {
50 process
.is_current_process_
= true;
51 return process
.Pass();
55 Process
Process::Open(ProcessId pid
) {
56 return Process(::OpenProcess(kBasicProcessAccess
, FALSE
, pid
));
60 Process
Process::OpenWithExtraPrivileges(ProcessId pid
) {
61 DWORD access
= kBasicProcessAccess
| PROCESS_DUP_HANDLE
| PROCESS_VM_READ
;
62 return Process(::OpenProcess(access
, FALSE
, pid
));
66 Process
Process::OpenWithAccess(ProcessId pid
, DWORD desired_access
) {
67 return Process(::OpenProcess(desired_access
, FALSE
, pid
));
71 Process
Process::DeprecatedGetProcessFromHandle(ProcessHandle handle
) {
72 DCHECK_NE(handle
, ::GetCurrentProcess());
73 ProcessHandle out_handle
;
74 if (!::DuplicateHandle(GetCurrentProcess(), handle
,
75 GetCurrentProcess(), &out_handle
,
76 0, FALSE
, DUPLICATE_SAME_ACCESS
)) {
79 return Process(out_handle
);
83 bool Process::CanBackgroundProcesses() {
87 bool Process::IsValid() const {
88 return process_
.IsValid() || is_current();
91 ProcessHandle
Process::Handle() const {
92 return is_current_process_
? GetCurrentProcess() : process_
.Get();
95 Process
Process::Duplicate() const {
99 ProcessHandle out_handle
;
100 if (!IsValid() || !::DuplicateHandle(GetCurrentProcess(),
106 DUPLICATE_SAME_ACCESS
)) {
109 return Process(out_handle
);
112 ProcessId
Process::Pid() const {
114 return GetProcId(Handle());
117 bool Process::is_current() const {
118 return is_current_process_
;
121 void Process::Close() {
122 is_current_process_
= false;
123 if (!process_
.IsValid())
129 bool Process::Terminate(int exit_code
, bool wait
) const {
131 bool result
= (::TerminateProcess(Handle(), exit_code
) != FALSE
);
132 if (result
&& wait
) {
133 // The process may not end immediately due to pending I/O
134 if (::WaitForSingleObject(Handle(), 60 * 1000) != WAIT_OBJECT_0
)
135 DPLOG(ERROR
) << "Error waiting for process exit";
136 } else if (!result
) {
137 DPLOG(ERROR
) << "Unable to terminate process";
142 bool Process::WaitForExit(int* exit_code
) {
143 return WaitForExitWithTimeout(TimeDelta::FromMilliseconds(INFINITE
),
147 bool Process::WaitForExitWithTimeout(TimeDelta timeout
, int* exit_code
) {
148 // Limit timeout to INFINITE.
149 DWORD timeout_ms
= saturated_cast
<DWORD
>(timeout
.InMilliseconds());
150 if (::WaitForSingleObject(Handle(), timeout_ms
) != WAIT_OBJECT_0
)
153 DWORD temp_code
; // Don't clobber out-parameters in case of failure.
154 if (!::GetExitCodeProcess(Handle(), &temp_code
))
157 *exit_code
= temp_code
;
161 bool Process::IsProcessBackgrounded() const {
163 DWORD priority
= GetPriority();
165 return false; // Failure case.
166 return ((priority
== BELOW_NORMAL_PRIORITY_CLASS
) ||
167 (priority
== IDLE_PRIORITY_CLASS
));
170 bool Process::SetProcessBackgrounded(bool value
) {
172 // Vista and above introduce a real background mode, which not only
173 // sets the priority class on the threads but also on the IO generated
174 // by it. Unfortunately it can only be set for the calling process.
176 if ((base::win::GetVersion() >= base::win::VERSION_VISTA
) && (is_current())) {
177 priority
= value
? PROCESS_MODE_BACKGROUND_BEGIN
:
178 PROCESS_MODE_BACKGROUND_END
;
180 // Experiment (http://crbug.com/458594) with using IDLE_PRIORITY_CLASS as a
181 // background priority for background renderers (this code path is
182 // technically for more than just the renderers but they're the only use
183 // case in practice and experimenting here direclty is thus easier -- plus
184 // it doesn't really hurt as above we already state our intent of using
185 // PROCESS_MODE_BACKGROUND_BEGIN if available which is essentially
186 // IDLE_PRIORITY_CLASS plus lowered IO priority). Enabled by default in the
187 // asbence of field trials to get coverage on the perf waterfall.
188 DWORD background_priority
= IDLE_PRIORITY_CLASS
;
189 base::FieldTrial
* trial
=
190 base::FieldTrialList::Find("BackgroundRendererProcesses");
191 if (trial
&& trial
->group_name() == "AllowBelowNormalFromBrowser")
192 background_priority
= BELOW_NORMAL_PRIORITY_CLASS
;
194 priority
= value
? background_priority
: NORMAL_PRIORITY_CLASS
;
197 return (::SetPriorityClass(Handle(), priority
) != 0);
200 int Process::GetPriority() const {
202 return ::GetPriorityClass(Handle());