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 :
10 : #include <sys/types.h>
11 : #ifdef OS_WIN
12 : #include <windows.h>
13 : #endif
14 :
15 : namespace base {
16 :
17 : // ProcessHandle is a platform specific type which represents the underlying OS
18 : // handle to a process.
19 : // ProcessId is a number which identifies the process in the OS.
20 : #if defined(OS_WIN)
21 : typedef HANDLE ProcessHandle;
22 : typedef DWORD ProcessId;
23 : #elif defined(OS_POSIX)
24 : // On POSIX, our ProcessHandle will just be the PID.
25 : typedef pid_t ProcessHandle;
26 : typedef pid_t ProcessId;
27 : #endif
28 :
29 : class Process {
30 : public:
31 0 : Process() : process_(0), last_working_set_size_(0) {}
32 0 : explicit Process(ProcessHandle handle) :
33 0 : process_(handle), last_working_set_size_(0) {}
34 :
35 : // A handle to the current process.
36 : static Process Current();
37 :
38 : // Get/Set the handle for this process. The handle will be 0 if the process
39 : // is no longer running.
40 0 : ProcessHandle handle() const { return process_; }
41 : void set_handle(ProcessHandle handle) { process_ = handle; }
42 :
43 : // Get the PID for this process.
44 : ProcessId pid() const;
45 :
46 : // Is the this process the current process.
47 : bool is_current() const;
48 :
49 : // Close the process handle. This will not terminate the process.
50 : void Close();
51 :
52 : // Terminates the process with extreme prejudice. The given result code will
53 : // be the exit code of the process. If the process has already exited, this
54 : // will do nothing.
55 : void Terminate(int result_code);
56 :
57 : // A process is backgrounded when it's priority is lower than normal.
58 : // Return true if this process is backgrounded, false otherwise.
59 : bool IsProcessBackgrounded() const;
60 :
61 : // Set a prcess as backgrounded. If value is true, the priority
62 : // of the process will be lowered. If value is false, the priority
63 : // of the process will be made "normal" - equivalent to default
64 : // process priority.
65 : // Returns true if the priority was changed, false otherwise.
66 : bool SetProcessBackgrounded(bool value);
67 :
68 : // Reduces the working set of memory used by the process.
69 : // The algorithm used by this function is intentionally vague. Repeated calls
70 : // to this function consider the process' previous required Working Set sizes
71 : // to determine a reasonable reduction. This helps give memory back to the OS
72 : // in increments without over releasing memory.
73 : // When the WorkingSet is reduced, it is permanent, until the caller calls
74 : // UnReduceWorkingSet.
75 : // Returns true if successful, false otherwise.
76 : bool ReduceWorkingSet();
77 :
78 : // Undoes the effects of prior calls to ReduceWorkingSet().
79 : // Returns true if successful, false otherwise.
80 : bool UnReduceWorkingSet();
81 :
82 : // Releases as much of the working set back to the OS as possible.
83 : // Returns true if successful, false otherwise.
84 : bool EmptyWorkingSet();
85 :
86 : private:
87 : ProcessHandle process_;
88 : size_t last_working_set_size_;
89 : };
90 :
91 : } // namespace base
92 :
93 : #endif // BASE_PROCESS_H_
|