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.
5 #ifndef BASE_PROCESS_H_
6 #define BASE_PROCESS_H_
9 #include "base/base_export.h"
10 #include "base/basictypes.h"
11 #include "build/build_config.h"
13 #include <sys/types.h>
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.
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)
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;
43 class BASE_EXPORT Process
{
45 Process() : process_(kNullProcessHandle
) {
46 #if defined(OS_POSIX) && !defined(OS_MACOSX)
47 saved_priority_
= kUnsetProcessPriority
;
51 explicit Process(ProcessHandle handle
) : process_(handle
) {
52 #if defined(OS_POSIX) && !defined(OS_MACOSX)
53 saved_priority_
= kUnsetProcessPriority
;
57 // A handle to the current process.
58 static Process
Current();
60 // Get/Set the handle for this process. The handle will be 0 if the process
61 // is no longer running.
62 ProcessHandle
handle() const { return process_
; }
63 void set_handle(ProcessHandle handle
) {
65 #if defined(OS_POSIX) && !defined(OS_MACOSX)
66 saved_priority_
= kUnsetProcessPriority
;
70 // Get the PID for this process.
71 ProcessId
pid() const;
73 // Is the this process the current process.
74 bool is_current() const;
76 // Close the process handle. This will not terminate the process.
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
82 void Terminate(int result_code
);
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;
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
92 // Returns true if the priority was changed, false otherwise.
93 bool SetProcessBackgrounded(bool value
);
95 // Returns an integer representing the priority of a process. The meaning
96 // of this value is OS dependent.
97 int GetPriority() const;
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.
110 #endif // BASE_PROCESS_H_