WebKit roll 98705:98715
[chromium-blink-merge.git] / base / process.h
blob267092479ca6c8219c885f99bbff0721543d7eb3
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_
7 #pragma once
9 #include "base/base_export.h"
10 #include "base/basictypes.h"
11 #include "build/build_config.h"
13 #include <sys/types.h>
14 #if defined(OS_WIN)
15 #include <windows.h>
16 #endif
18 namespace base {
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)
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
43 class BASE_EXPORT Process {
44 public:
45 Process() : process_(kNullProcessHandle) {
46 #if defined(OS_POSIX) && !defined(OS_MACOSX)
47 saved_priority_ = kUnsetProcessPriority;
48 #endif
51 explicit Process(ProcessHandle handle) : process_(handle) {
52 #if defined(OS_POSIX) && !defined(OS_MACOSX)
53 saved_priority_ = kUnsetProcessPriority;
54 #endif
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) {
64 process_ = handle;
65 #if defined(OS_POSIX) && !defined(OS_MACOSX)
66 saved_priority_ = kUnsetProcessPriority;
67 #endif
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.
77 void Close();
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);
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
91 // process priority.
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;
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
108 } // namespace base
110 #endif // BASE_PROCESS_H_