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();
35 Process
& Process::operator=(RValue other
) {
36 if (this != other
.object
) {
37 process_
.Set(other
.object
->process_
.Take());
38 is_current_process_
= other
.object
->is_current_process_
;
39 other
.object
->Close();
45 Process
Process::Current() {
47 process
.is_current_process_
= true;
48 return process
.Pass();
52 Process
Process::Open(ProcessId pid
) {
53 return Process(::OpenProcess(kBasicProcessAccess
, FALSE
, pid
));
57 Process
Process::OpenWithExtraPrivileges(ProcessId pid
) {
58 DWORD access
= kBasicProcessAccess
| PROCESS_DUP_HANDLE
| PROCESS_VM_READ
;
59 return Process(::OpenProcess(access
, FALSE
, pid
));
63 Process
Process::OpenWithAccess(ProcessId pid
, DWORD desired_access
) {
64 return Process(::OpenProcess(desired_access
, FALSE
, pid
));
68 Process
Process::DeprecatedGetProcessFromHandle(ProcessHandle handle
) {
69 DCHECK_NE(handle
, ::GetCurrentProcess());
70 ProcessHandle out_handle
;
71 if (!::DuplicateHandle(GetCurrentProcess(), handle
,
72 GetCurrentProcess(), &out_handle
,
73 0, FALSE
, DUPLICATE_SAME_ACCESS
)) {
76 return Process(out_handle
);
80 bool Process::CanBackgroundProcesses() {
84 bool Process::IsValid() const {
85 return process_
.IsValid() || is_current();
88 ProcessHandle
Process::Handle() const {
89 return is_current_process_
? GetCurrentProcess() : process_
.Get();
92 Process
Process::Duplicate() const {
96 ProcessHandle out_handle
;
97 if (!IsValid() || !::DuplicateHandle(GetCurrentProcess(),
103 DUPLICATE_SAME_ACCESS
)) {
106 return Process(out_handle
);
109 ProcessId
Process::Pid() const {
111 return GetProcId(Handle());
114 bool Process::is_current() const {
115 return is_current_process_
;
118 void Process::Close() {
119 is_current_process_
= false;
120 if (!process_
.IsValid())
126 bool Process::Terminate(int exit_code
, bool wait
) const {
128 bool result
= (::TerminateProcess(Handle(), exit_code
) != FALSE
);
129 if (result
&& wait
) {
130 // The process may not end immediately due to pending I/O
131 if (::WaitForSingleObject(Handle(), 60 * 1000) != WAIT_OBJECT_0
)
132 DPLOG(ERROR
) << "Error waiting for process exit";
133 } else if (!result
) {
134 DPLOG(ERROR
) << "Unable to terminate process";
139 bool Process::WaitForExit(int* exit_code
) {
140 return WaitForExitWithTimeout(TimeDelta::FromMilliseconds(INFINITE
),
144 bool Process::WaitForExitWithTimeout(TimeDelta timeout
, int* exit_code
) {
145 // Limit timeout to INFINITE.
146 DWORD timeout_ms
= saturated_cast
<DWORD
>(timeout
.InMilliseconds());
147 if (::WaitForSingleObject(Handle(), timeout_ms
) != WAIT_OBJECT_0
)
150 DWORD temp_code
; // Don't clobber out-parameters in case of failure.
151 if (!::GetExitCodeProcess(Handle(), &temp_code
))
154 *exit_code
= temp_code
;
158 bool Process::IsProcessBackgrounded() const {
160 DWORD priority
= GetPriority();
162 return false; // Failure case.
163 return ((priority
== BELOW_NORMAL_PRIORITY_CLASS
) ||
164 (priority
== IDLE_PRIORITY_CLASS
));
167 bool Process::SetProcessBackgrounded(bool value
) {
169 // Vista and above introduce a real background mode, which not only
170 // sets the priority class on the threads but also on the IO generated
171 // by it. Unfortunately it can only be set for the calling process.
173 if ((base::win::GetVersion() >= base::win::VERSION_VISTA
) && (is_current())) {
174 priority
= value
? PROCESS_MODE_BACKGROUND_BEGIN
:
175 PROCESS_MODE_BACKGROUND_END
;
177 // Experiment (http://crbug.com/458594) with using IDLE_PRIORITY_CLASS as a
178 // background priority for background renderers (this code path is
179 // technically for more than just the renderers but they're the only use
180 // case in practice and experimenting here direclty is thus easier -- plus
181 // it doesn't really hurt as above we already state our intent of using
182 // PROCESS_MODE_BACKGROUND_BEGIN if available which is essentially
183 // IDLE_PRIORITY_CLASS plus lowered IO priority). Enabled by default in the
184 // asbence of field trials to get coverage on the perf waterfall.
185 DWORD background_priority
= IDLE_PRIORITY_CLASS
;
186 base::FieldTrial
* trial
=
187 base::FieldTrialList::Find("BackgroundRendererProcesses");
188 if (trial
&& trial
->group_name() == "AllowBelowNormalFromBrowser")
189 background_priority
= BELOW_NORMAL_PRIORITY_CLASS
;
191 priority
= value
? background_priority
: NORMAL_PRIORITY_CLASS
;
194 return (::SetPriorityClass(Handle(), priority
) != 0);
197 int Process::GetPriority() const {
199 return ::GetPriorityClass(Handle());