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.h"
6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/process_util.h"
9 #include "base/win/windows_version.h"
13 void Process::Close() {
17 // Don't call CloseHandle on a pseudo-handle.
18 if (process_
!= ::GetCurrentProcess())
19 ::CloseHandle(process_
);
24 void Process::Terminate(int result_code
) {
28 // Call NtTerminateProcess directly, without going through the import table,
29 // which might have been hooked with a buggy replacement by third party
30 // software. http://crbug.com/81449.
31 HMODULE module
= GetModuleHandle(L
"ntdll.dll");
32 typedef UINT (WINAPI
*TerminateProcessPtr
)(HANDLE handle
, UINT code
);
33 TerminateProcessPtr terminate_process
= reinterpret_cast<TerminateProcessPtr
>(
34 GetProcAddress(module
, "NtTerminateProcess"));
35 terminate_process(process_
, result_code
);
38 bool Process::IsProcessBackgrounded() const {
40 return false; // Failure case.
41 DWORD priority
= GetPriority();
43 return false; // Failure case.
44 return ((priority
== BELOW_NORMAL_PRIORITY_CLASS
) ||
45 (priority
== IDLE_PRIORITY_CLASS
));
48 bool Process::SetProcessBackgrounded(bool value
) {
51 // Vista and above introduce a real background mode, which not only
52 // sets the priority class on the threads but also on the IO generated
53 // by it. Unfortunately it can only be set for the calling process.
55 if ((base::win::GetVersion() >= base::win::VERSION_VISTA
) &&
56 (process_
== ::GetCurrentProcess())) {
57 priority
= value
? PROCESS_MODE_BACKGROUND_BEGIN
:
58 PROCESS_MODE_BACKGROUND_END
;
60 priority
= value
? BELOW_NORMAL_PRIORITY_CLASS
: NORMAL_PRIORITY_CLASS
;
63 return (::SetPriorityClass(process_
, priority
) != 0);
66 ProcessId
Process::pid() const {
70 return GetProcId(process_
);
73 bool Process::is_current() const {
74 return process_
== GetCurrentProcess();
78 Process
Process::Current() {
79 return Process(::GetCurrentProcess());
83 bool Process::CanBackgroundProcesses() {
87 int Process::GetPriority() const {
89 return ::GetPriorityClass(process_
);