Enterprise policy: Ignore the deprecated ForceSafeSearch if ForceGoogleSafeSearch...
[chromium-blink-merge.git] / base / process / process_win.cc
blobe701eceed30565dcbafe6d2fc0bc77a295f24c87
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"
14 namespace {
16 DWORD kBasicProcessAccess =
17 PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE;
19 } // namespace
21 namespace base {
23 Process::Process(ProcessHandle handle)
24 : is_current_process_(false),
25 process_(handle) {
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();
41 return *this;
44 // static
45 Process Process::Current() {
46 Process process;
47 process.is_current_process_ = true;
48 return process.Pass();
51 // static
52 Process Process::Open(ProcessId pid) {
53 return Process(::OpenProcess(kBasicProcessAccess, FALSE, pid));
56 // static
57 Process Process::OpenWithExtraPrivileges(ProcessId pid) {
58 DWORD access = kBasicProcessAccess | PROCESS_DUP_HANDLE | PROCESS_VM_READ;
59 return Process(::OpenProcess(access, FALSE, pid));
62 // static
63 Process Process::OpenWithAccess(ProcessId pid, DWORD desired_access) {
64 return Process(::OpenProcess(desired_access, FALSE, pid));
67 // static
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)) {
74 return Process();
76 return Process(out_handle);
79 // static
80 bool Process::CanBackgroundProcesses() {
81 return true;
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 {
93 if (is_current())
94 return Current();
96 ProcessHandle out_handle;
97 if (!IsValid() || !::DuplicateHandle(GetCurrentProcess(),
98 Handle(),
99 GetCurrentProcess(),
100 &out_handle,
102 FALSE,
103 DUPLICATE_SAME_ACCESS)) {
104 return Process();
106 return Process(out_handle);
109 ProcessId Process::Pid() const {
110 DCHECK(IsValid());
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())
121 return;
123 process_.Close();
126 bool Process::Terminate(int exit_code, bool wait) const {
127 DCHECK(IsValid());
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";
136 return result;
139 bool Process::WaitForExit(int* exit_code) {
140 return WaitForExitWithTimeout(TimeDelta::FromMilliseconds(INFINITE),
141 exit_code);
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)
148 return false;
150 DWORD temp_code; // Don't clobber out-parameters in case of failure.
151 if (!::GetExitCodeProcess(Handle(), &temp_code))
152 return false;
154 *exit_code = temp_code;
155 return true;
158 bool Process::IsProcessBackgrounded() const {
159 DCHECK(IsValid());
160 DWORD priority = GetPriority();
161 if (priority == 0)
162 return false; // Failure case.
163 return ((priority == BELOW_NORMAL_PRIORITY_CLASS) ||
164 (priority == IDLE_PRIORITY_CLASS));
167 bool Process::SetProcessBackgrounded(bool value) {
168 DCHECK(IsValid());
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.
172 DWORD priority;
173 if ((base::win::GetVersion() >= base::win::VERSION_VISTA) && (is_current())) {
174 priority = value ? PROCESS_MODE_BACKGROUND_BEGIN :
175 PROCESS_MODE_BACKGROUND_END;
176 } else {
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 {
198 DCHECK(IsValid());
199 return ::GetPriorityClass(Handle());
202 } // namespace base