blob: 3f683ab3e5a70a58dacddb5798dfaafdbf8ea15f [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5#include "base/process.h"
6#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:157#include "base/memory/scoped_ptr.h"
initial.commitd7cae122008-07-26 21:49:388#include "base/process_util.h"
[email protected]568bfb02011-04-28 23:51:539#include "base/win/windows_version.h"
initial.commitd7cae122008-07-26 21:49:3810
[email protected]176aa482008-11-14 03:25:1511namespace base {
12
13void Process::Close() {
14 if (!process_)
15 return;
[email protected]b987e90e2011-08-15 19:22:4416
[email protected]c4ed8022011-08-19 01:36:2117 // Don't call CloseHandle on a pseudo-handle.
18 if (process_ != ::GetCurrentProcess())
19 ::CloseHandle(process_);
[email protected]b987e90e2011-08-15 19:22:4420
[email protected]176aa482008-11-14 03:25:1521 process_ = NULL;
22}
23
24void Process::Terminate(int result_code) {
25 if (!process_)
26 return;
[email protected]b987e90e2011-08-15 19:22:4427
[email protected]c4ed8022011-08-19 01:36:2128 // 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.
[email protected]b987e90e2011-08-15 19:22:4431 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);
[email protected]176aa482008-11-14 03:25:1536}
37
[email protected]2f15de42008-11-11 22:35:1938bool Process::IsProcessBackgrounded() const {
[email protected]b195ea82009-07-16 03:49:2839 if (!process_)
40 return false; // Failure case.
[email protected]276aa6a2009-10-29 17:43:4441 DWORD priority = GetPriority();
initial.commitd7cae122008-07-26 21:49:3842 if (priority == 0)
43 return false; // Failure case.
[email protected]568bfb02011-04-28 23:51:5344 return ((priority == BELOW_NORMAL_PRIORITY_CLASS) ||
45 (priority == IDLE_PRIORITY_CLASS));
initial.commitd7cae122008-07-26 21:49:3846}
47
48bool Process::SetProcessBackgrounded(bool value) {
[email protected]b195ea82009-07-16 03:49:2849 if (!process_)
50 return false;
[email protected]568bfb02011-04-28 23:51:5351 // 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.
54 DWORD priority;
55 if ((base::win::GetVersion() >= base::win::VERSION_VISTA) &&
56 (process_ == ::GetCurrentProcess())) {
57 priority = value ? PROCESS_MODE_BACKGROUND_BEGIN :
58 PROCESS_MODE_BACKGROUND_END;
59 } else {
60 priority = value ? BELOW_NORMAL_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS;
61 }
62
63 return (::SetPriorityClass(process_, priority) != 0);
initial.commitd7cae122008-07-26 21:49:3864}
65
[email protected]43cf3252009-04-01 09:19:3766ProcessId Process::pid() const {
initial.commitd7cae122008-07-26 21:49:3867 if (process_ == 0)
68 return 0;
69
[email protected]176aa482008-11-14 03:25:1570 return GetProcId(process_);
initial.commitd7cae122008-07-26 21:49:3871}
72
73bool Process::is_current() const {
74 return process_ == GetCurrentProcess();
75}
76
77// static
78Process Process::Current() {
[email protected]568bfb02011-04-28 23:51:5379 return Process(::GetCurrentProcess());
initial.commitd7cae122008-07-26 21:49:3880}
license.botbf09a502008-08-24 00:55:5581
[email protected]8a420802011-12-02 16:14:4682// static
83bool Process::CanBackgroundProcesses() {
84 return true;
85}
86
[email protected]276aa6a2009-10-29 17:43:4487int Process::GetPriority() const {
88 DCHECK(process_);
[email protected]568bfb02011-04-28 23:51:5389 return ::GetPriorityClass(process_);
[email protected]276aa6a2009-10-29 17:43:4490}
91
[email protected]176aa482008-11-14 03:25:1592} // namespace base