<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/process.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/process_util.h"
#include "base/win/windows_version.h"

namespace base {

void Process::Close() {
  if (!process_)
    return;
  // Don't call CloseHandle on a pseudo-handle.
  if (process_ != ::GetCurrentProcess())
    ::CloseHandle(process_);
  process_ = NULL;
}

void Process::Terminate(int result_code) {
  if (!process_)
    return;
  ::TerminateProcess(process_, result_code);
}

bool Process::IsProcessBackgrounded() const {
  if (!process_)
    return false;  // Failure case.
  DWORD priority = GetPriority();
  if (priority == 0)
    return false;  // Failure case.
  return ((priority == BELOW_NORMAL_PRIORITY_CLASS) ||
          (priority == IDLE_PRIORITY_CLASS));
}

bool Process::SetProcessBackgrounded(bool value) {
  if (!process_)
    return false;
  // Vista and above introduce a real background mode, which not only
  // sets the priority class on the threads but also on the IO generated
  // by it. Unfortunately it can only be set for the calling process.<commit_msg>Call ntdll.dll!NtTerminateProcess directly, without going through the import table for kernel31.dll!TerminateProcess to determine if TerminateProcess is being hooked.<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/process.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/process_util.h"
#include "base/win/windows_version.h"

namespace base {

void Process::Close() {
  if (!process_)
    return;
  // Don't call CloseHandle on a pseudo-handle.
  if (process_ != ::GetCurrentProcess()) {
    // TODO(apatrick): Call NtCloseHandle directly, without going through the
    // import table to determine if CloseHandle is being hooked.
    // http://crbug.com/81449.
    HMODULE module = GetModuleHandle(L"ntdll.dll");
    typedef UINT (WINAPI *CloseHandlePtr)(HANDLE handle);
    CloseHandlePtr close_handle = reinterpret_cast<CloseHandlePtr>(
        GetProcAddress(module, "NtClose"));
    close_handle(process_);

    // It used to look like this:
    // ::CloseHandle(process_);
  }

  process_ = NULL;
}

void Process::Terminate(int result_code) {
  if (!process_)
    return;

  // TODO(apatrick): Call NtTerminateProcess directly, without going through the
  // import table to determine if TerminateProcess is being hooked.
  // http://crbug.com/81449.
  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);

  // It used to look like this:
  // ::TerminateProcess(process_, result_code);
}

bool Process::IsProcessBackgrounded() const {
  if (!process_)
    return false;  // Failure case.
  DWORD priority = GetPriority();
  if (priority == 0)
    return false;  // Failure case.
  return ((priority == BELOW_NORMAL_PRIORITY_CLASS) ||
          (priority == IDLE_PRIORITY_CLASS));
}

bool Process::SetProcessBackgrounded(bool value) {
  if (!process_)
    return false;
  // Vista and above introduce a real background mode, which not only
  // sets the priority class on the threads but also on the IO generated
  // by it. Unfortunately it can only be set for the calling process.
{'fraction_non_alphanumeric': 0.10777145951800704, 'fraction_numerical': 0.005957216355266721, 'mean_word_length': 3.9385026737967914, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7514723', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1069, 'n_words': 361}