Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include "base/process.h"
      6 #include "base/logging.h"
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/process_util.h"
      9 
     10 namespace base {
     11 
     12 void Process::Close() {
     13   if (!process_)
     14     return;
     15   ::CloseHandle(process_);
     16   process_ = NULL;
     17 }
     18 
     19 void Process::Terminate(int result_code) {
     20   if (!process_)
     21     return;
     22   ::TerminateProcess(process_, result_code);
     23 }
     24 
     25 bool Process::IsProcessBackgrounded() const {
     26   if (!process_)
     27     return false;  // Failure case.
     28   DWORD priority = GetPriority();
     29   if (priority == 0)
     30     return false;  // Failure case.
     31   return priority == BELOW_NORMAL_PRIORITY_CLASS;
     32 }
     33 
     34 bool Process::SetProcessBackgrounded(bool value) {
     35   if (!process_)
     36     return false;
     37   DWORD priority = value ? BELOW_NORMAL_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS;
     38   return (SetPriorityClass(process_, priority) != 0);
     39 }
     40 
     41 ProcessId Process::pid() const {
     42   if (process_ == 0)
     43     return 0;
     44 
     45   return GetProcId(process_);
     46 }
     47 
     48 bool Process::is_current() const {
     49   return process_ == GetCurrentProcess();
     50 }
     51 
     52 // static
     53 Process Process::Current() {
     54   return Process(GetCurrentProcess());
     55 }
     56 
     57 int Process::GetPriority() const {
     58   DCHECK(process_);
     59   return GetPriorityClass(process_);
     60 }
     61 
     62 }  // namespace base
     63