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.
5 #ifndef BASE_PROCESS_H_
6 #define BASE_PROCESS_H_
8 #include "base/basictypes.h"
9 #include "build/build_config.h"
11 #include <sys/types.h>
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.
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 // defined(OS_WIN)
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;
41 Process() : process_(kNullProcessHandle
) {
42 #if defined(OS_POSIX) && !defined(OS_MACOSX)
43 saved_priority_
= kUnsetProcessPriority
;
47 explicit Process(ProcessHandle handle
) : process_(handle
) {
48 #if defined(OS_POSIX) && !defined(OS_MACOSX)
49 saved_priority_
= kUnsetProcessPriority
;
53 // A handle to the current process.
54 static Process
Current();
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
) {
61 #if defined(OS_POSIX) && !defined(OS_MACOSX)
62 saved_priority_
= kUnsetProcessPriority
;
66 // Get the PID for this process.
67 ProcessId
pid() const;
69 // Is the this process the current process.
70 bool is_current() const;
72 // Close the process handle. This will not terminate the process.
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
78 void Terminate(int result_code
);
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;
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
88 // Returns true if the priority was changed, false otherwise.
89 bool SetProcessBackgrounded(bool value
);
91 // Returns an integer representing the priority of a process. The meaning
92 // of this value is OS dependent.
93 int GetPriority() const;
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.
106 #endif // BASE_PROCESS_H_