This source file includes following definitions.
- Close
- Terminate
- IsProcessBackgrounded
- SetProcessBackgrounded
- pid
- is_current
- Current
- CanBackgroundProcesses
- GetPriority
#include "base/process/process.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/win/windows_version.h"
namespace base {
void Process::Close() {
if (!process_)
return;
if (process_ != ::GetCurrentProcess())
::CloseHandle(process_);
process_ = NULL;
}
void Process::Terminate(int result_code) {
if (!process_)
return;
HMODULE module = GetModuleHandle(L"ntdll.dll");
typedef UINT (WINAPI *TerminateProcessPtr)(HANDLE handle, UINT code);
TerminateProcessPtr terminate_process = reinterpret_cast<TerminateProcessPtr>(
GetProcAddress(module, "NtTerminateProcess"));
terminate_process(process_, result_code);
}
bool Process::IsProcessBackgrounded() const {
if (!process_)
return false;
DWORD priority = GetPriority();
if (priority == 0)
return false;
return ((priority == BELOW_NORMAL_PRIORITY_CLASS) ||
(priority == IDLE_PRIORITY_CLASS));
}
bool Process::SetProcessBackgrounded(bool value) {
if (!process_)
return false;
DWORD priority;
if ((base::win::GetVersion() >= base::win::VERSION_VISTA) &&
(process_ == ::GetCurrentProcess())) {
priority = value ? PROCESS_MODE_BACKGROUND_BEGIN :
PROCESS_MODE_BACKGROUND_END;
} else {
priority = value ? BELOW_NORMAL_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS;
}
return (::SetPriorityClass(process_, priority) != 0);
}
ProcessId Process::pid() const {
if (process_ == 0)
return 0;
return GetProcId(process_);
}
bool Process::is_current() const {
return process_ == GetCurrentProcess();
}
Process Process::Current() {
return Process(::GetCurrentProcess());
}
bool Process::CanBackgroundProcesses() {
return true;
}
int Process::GetPriority() const {
DCHECK(process_);
return ::GetPriorityClass(process_);
}
}