Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2008 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 #ifndef BASE_PROCESS_H_
      6 #define BASE_PROCESS_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "build/build_config.h"
     10 
     11 #include <sys/types.h>
     12 #ifdef OS_WIN
     13 #include <windows.h>
     14 #endif
     15 
     16 namespace base {
     17 
     18 // ProcessHandle is a platform specific type which represents the underlying OS
     19 // handle to a process.
     20 // ProcessId is a number which identifies the process in the OS.
     21 #if defined(OS_WIN)
     22 typedef HANDLE ProcessHandle;
     23 typedef DWORD ProcessId;
     24 typedef HANDLE UserTokenHandle;
     25 const ProcessHandle kNullProcessHandle = NULL;
     26 #elif defined(OS_POSIX)
     27 // On POSIX, our ProcessHandle will just be the PID.
     28 typedef pid_t ProcessHandle;
     29 typedef pid_t ProcessId;
     30 const ProcessHandle kNullProcessHandle = 0;
     31 #endif
     32 
     33 #if defined(OS_POSIX) && !defined(OS_MACOSX)
     34 // saved_priority_ will be set to this to indicate that it's not holding
     35 // a valid value. -20 to 19 are valid process priorities.
     36 const int kUnsetProcessPriority = 256;
     37 #endif
     38 
     39 class Process {
     40  public:
     41   Process() : process_(kNullProcessHandle) {
     42 #if defined(OS_POSIX) && !defined(OS_MACOSX)
     43     saved_priority_ = kUnsetProcessPriority;
     44 #endif
     45   }
     46 
     47   explicit Process(ProcessHandle handle) : process_(handle) {
     48 #if defined(OS_POSIX) && !defined(OS_MACOSX)
     49     saved_priority_ = kUnsetProcessPriority;
     50 #endif
     51   }
     52 
     53   // A handle to the current process.
     54   static Process Current();
     55 
     56   // Get/Set the handle for this process. The handle will be 0 if the process
     57   // is no longer running.
     58   ProcessHandle handle() const { return process_; }
     59   void set_handle(ProcessHandle handle) {
     60     process_ = handle;
     61 #if defined(OS_POSIX) && !defined(OS_MACOSX)
     62     saved_priority_ = kUnsetProcessPriority;
     63 #endif
     64   }
     65 
     66   // Get the PID for this process.
     67   ProcessId pid() const;
     68 
     69   // Is the this process the current process.
     70   bool is_current() const;
     71 
     72   // Close the process handle. This will not terminate the process.
     73   void Close();
     74 
     75   // Terminates the process with extreme prejudice. The given result code will
     76   // be the exit code of the process. If the process has already exited, this
     77   // will do nothing.
     78   void Terminate(int result_code);
     79 
     80   // A process is backgrounded when it's priority is lower than normal.
     81   // Return true if this process is backgrounded, false otherwise.
     82   bool IsProcessBackgrounded() const;
     83 
     84   // Set a process as backgrounded. If value is true, the priority
     85   // of the process will be lowered. If value is false, the priority
     86   // of the process will be made "normal" - equivalent to default
     87   // process priority.
     88   // Returns true if the priority was changed, false otherwise.
     89   bool SetProcessBackgrounded(bool value);
     90 
     91   // Returns an integer representing the priority of a process. The meaning
     92   // of this value is OS dependent.
     93   int GetPriority() const;
     94 
     95  private:
     96   ProcessHandle process_;
     97 #if defined(OS_POSIX) && !defined(OS_MACOSX)
     98   // Holds the priority that the process was set to when it was backgrounded.
     99   // If the process wasn't backgrounded it will be kUnsetProcessPriority.
    100   int saved_priority_;
    101 #endif
    102 };
    103 
    104 }  // namespace base
    105 
    106 #endif  // BASE_PROCESS_H_
    107