Add ICU message format support
[chromium-blink-merge.git] / base / process / process_win.cc
blob818864fa528dbab283e975983683ed337c3753e1
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/numerics/safe_conversions.h"
10 #include "base/process/kill.h"
11 #include "base/win/windows_version.h"
13 namespace {
15 DWORD kBasicProcessAccess =
16 PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE;
18 } // namespace
20 namespace base {
22 Process::Process(ProcessHandle handle)
23 : is_current_process_(false),
24 process_(handle) {
25 CHECK_NE(handle, ::GetCurrentProcess());
28 Process::Process(RValue other)
29 : is_current_process_(other.object->is_current_process_),
30 process_(other.object->process_.Take()) {
31 other.object->Close();
34 Process::~Process() {
37 Process& Process::operator=(RValue other) {
38 if (this != other.object) {
39 process_.Set(other.object->process_.Take());
40 is_current_process_ = other.object->is_current_process_;
41 other.object->Close();
43 return *this;
46 // static
47 Process Process::Current() {
48 Process process;
49 process.is_current_process_ = true;
50 return process.Pass();
53 // static
54 Process Process::Open(ProcessId pid) {
55 return Process(::OpenProcess(kBasicProcessAccess, FALSE, pid));
58 // static
59 Process Process::OpenWithExtraPrivileges(ProcessId pid) {
60 DWORD access = kBasicProcessAccess | PROCESS_DUP_HANDLE | PROCESS_VM_READ;
61 return Process(::OpenProcess(access, FALSE, pid));
64 // static
65 Process Process::OpenWithAccess(ProcessId pid, DWORD desired_access) {
66 return Process(::OpenProcess(desired_access, FALSE, pid));
69 // static
70 Process Process::DeprecatedGetProcessFromHandle(ProcessHandle handle) {
71 DCHECK_NE(handle, ::GetCurrentProcess());
72 ProcessHandle out_handle;
73 if (!::DuplicateHandle(GetCurrentProcess(), handle,
74 GetCurrentProcess(), &out_handle,
75 0, FALSE, DUPLICATE_SAME_ACCESS)) {
76 return Process();
78 return Process(out_handle);
81 // static
82 bool Process::CanBackgroundProcesses() {
83 return true;
86 bool Process::IsValid() const {
87 return process_.IsValid() || is_current();
90 ProcessHandle Process::Handle() const {
91 return is_current_process_ ? GetCurrentProcess() : process_.Get();
94 Process Process::Duplicate() const {
95 if (is_current())
96 return Current();
98 ProcessHandle out_handle;
99 if (!IsValid() || !::DuplicateHandle(GetCurrentProcess(),
100 Handle(),
101 GetCurrentProcess(),
102 &out_handle,
104 FALSE,
105 DUPLICATE_SAME_ACCESS)) {
106 return Process();
108 return Process(out_handle);
111 ProcessId Process::Pid() const {
112 DCHECK(IsValid());
113 return GetProcId(Handle());
116 bool Process::is_current() const {
117 return is_current_process_;
120 void Process::Close() {
121 is_current_process_ = false;
122 if (!process_.IsValid())
123 return;
125 process_.Close();
128 bool Process::Terminate(int exit_code, bool wait) const {
129 DCHECK(IsValid());
130 bool result = (::TerminateProcess(Handle(), exit_code) != FALSE);
131 if (result && wait) {
132 // The process may not end immediately due to pending I/O
133 if (::WaitForSingleObject(Handle(), 60 * 1000) != WAIT_OBJECT_0)
134 DPLOG(ERROR) << "Error waiting for process exit";
135 } else if (!result) {
136 DPLOG(ERROR) << "Unable to terminate process";
138 return result;
141 bool Process::WaitForExit(int* exit_code) {
142 return WaitForExitWithTimeout(TimeDelta::FromMilliseconds(INFINITE),
143 exit_code);
146 bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) {
147 // Limit timeout to INFINITE.
148 DWORD timeout_ms = saturated_cast<DWORD>(timeout.InMilliseconds());
149 if (::WaitForSingleObject(Handle(), timeout_ms) != WAIT_OBJECT_0)
150 return false;
152 DWORD temp_code; // Don't clobber out-parameters in case of failure.
153 if (!::GetExitCodeProcess(Handle(), &temp_code))
154 return false;
156 if (exit_code)
157 *exit_code = temp_code;
158 return true;
161 bool Process::IsProcessBackgrounded() const {
162 DCHECK(IsValid());
163 DWORD priority = GetPriority();
164 if (priority == 0)
165 return false; // Failure case.
166 return ((priority == BELOW_NORMAL_PRIORITY_CLASS) ||
167 (priority == IDLE_PRIORITY_CLASS));
170 bool Process::SetProcessBackgrounded(bool value) {
171 DCHECK(IsValid());
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.
175 DWORD priority;
176 if ((base::win::GetVersion() >= base::win::VERSION_VISTA) && (is_current())) {
177 priority = value ? PROCESS_MODE_BACKGROUND_BEGIN :
178 PROCESS_MODE_BACKGROUND_END;
179 } else {
180 priority = value ? IDLE_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS;
183 return (::SetPriorityClass(Handle(), priority) != 0);
186 int Process::GetPriority() const {
187 DCHECK(IsValid());
188 return ::GetPriorityClass(Handle());
191 } // namespace base