Protect WebURLLoaderImpl::Context while receiving responses.
[chromium-blink-merge.git] / base / process_win.cc
blob3f683ab3e5a70a58dacddb5798dfaafdbf8ea15f
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"
11 namespace base {
13 void Process::Close() {
14 if (!process_)
15 return;
17 // Don't call CloseHandle on a pseudo-handle.
18 if (process_ != ::GetCurrentProcess())
19 ::CloseHandle(process_);
21 process_ = NULL;
24 void Process::Terminate(int result_code) {
25 if (!process_)
26 return;
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 {
39 if (!process_)
40 return false; // Failure case.
41 DWORD priority = GetPriority();
42 if (priority == 0)
43 return false; // Failure case.
44 return ((priority == BELOW_NORMAL_PRIORITY_CLASS) ||
45 (priority == IDLE_PRIORITY_CLASS));
48 bool Process::SetProcessBackgrounded(bool value) {
49 if (!process_)
50 return false;
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.
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;
63 return (::SetPriorityClass(process_, priority) != 0);
66 ProcessId Process::pid() const {
67 if (process_ == 0)
68 return 0;
70 return GetProcId(process_);
73 bool Process::is_current() const {
74 return process_ == GetCurrentProcess();
77 // static
78 Process Process::Current() {
79 return Process(::GetCurrentProcess());
82 // static
83 bool Process::CanBackgroundProcesses() {
84 return true;
87 int Process::GetPriority() const {
88 DCHECK(process_);
89 return ::GetPriorityClass(process_);
92 } // namespace base