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