[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 5 | #ifndef BASE_PROCESS_H_ |
6 | #define BASE_PROCESS_H_ | ||||
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 9 | #include "base/base_export.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | 57113ea | 2009-06-18 02:23:16 | [diff] [blame] | 11 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | |
[email protected] | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 13 | #include <sys/types.h> |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 14 | #if defined(OS_WIN) |
[email protected] | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 15 | #include <windows.h> |
16 | #endif | ||||
17 | |||||
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 18 | namespace base { |
19 | |||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 20 | // ProcessHandle is a platform specific type which represents the underlying OS |
21 | // handle to a process. | ||||
[email protected] | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 22 | // ProcessId is a number which identifies the process in the OS. |
[email protected] | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 23 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 24 | typedef HANDLE ProcessHandle; |
[email protected] | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 25 | typedef DWORD ProcessId; |
[email protected] | e50130b | 2010-02-01 03:28:47 | [diff] [blame] | 26 | typedef HANDLE UserTokenHandle; |
[email protected] | 659f73fa | 2009-10-13 13:43:42 | [diff] [blame] | 27 | const ProcessHandle kNullProcessHandle = NULL; |
[email protected] | 38788d9 | 2011-04-01 22:05:13 | [diff] [blame] | 28 | const ProcessId kNullProcessId = 0; |
[email protected] | 6165906 | 2008-08-06 01:04:18 | [diff] [blame] | 29 | #elif defined(OS_POSIX) |
[email protected] | db71728 | 2008-08-27 13:48:03 | [diff] [blame] | 30 | // On POSIX, our ProcessHandle will just be the PID. |
[email protected] | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 31 | typedef pid_t ProcessHandle; |
32 | typedef pid_t ProcessId; | ||||
[email protected] | 659f73fa | 2009-10-13 13:43:42 | [diff] [blame] | 33 | const ProcessHandle kNullProcessHandle = 0; |
[email protected] | 38788d9 | 2011-04-01 22:05:13 | [diff] [blame] | 34 | const ProcessId kNullProcessId = 0; |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 35 | #endif // defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 36 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 37 | class BASE_EXPORT Process { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 38 | public: |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 39 | Process() : process_(kNullProcessHandle) { |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 40 | } |
41 | |||||
42 | explicit Process(ProcessHandle handle) : process_(handle) { | ||||
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 43 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 44 | |
45 | // A handle to the current process. | ||||
46 | static Process Current(); | ||||
47 | |||||
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 48 | static bool CanBackgroundProcesses(); |
49 | |||||
[email protected] | 2f15de4 | 2008-11-11 22:35:19 | [diff] [blame] | 50 | // Get/Set the handle for this process. The handle will be 0 if the process |
51 | // is no longer running. | ||||
52 | ProcessHandle handle() const { return process_; } | ||||
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 53 | void set_handle(ProcessHandle handle) { |
54 | process_ = handle; | ||||
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 55 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 56 | |
57 | // Get the PID for this process. | ||||
[email protected] | 43cf325 | 2009-04-01 09:19:37 | [diff] [blame] | 58 | ProcessId pid() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 59 | |
60 | // Is the this process the current process. | ||||
61 | bool is_current() const; | ||||
62 | |||||
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 63 | // Close the process handle. This will not terminate the process. |
64 | void Close(); | ||||
65 | |||||
66 | // Terminates the process with extreme prejudice. The given result code will | ||||
67 | // be the exit code of the process. If the process has already exited, this | ||||
68 | // will do nothing. | ||||
69 | void Terminate(int result_code); | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 70 | |
71 | // A process is backgrounded when it's priority is lower than normal. | ||||
72 | // Return true if this process is backgrounded, false otherwise. | ||||
[email protected] | 2f15de4 | 2008-11-11 22:35:19 | [diff] [blame] | 73 | bool IsProcessBackgrounded() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 74 | |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 75 | // Set a process as backgrounded. If value is true, the priority |
76 | // of the process will be lowered. If value is false, the priority | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 77 | // of the process will be made "normal" - equivalent to default |
78 | // process priority. | ||||
79 | // Returns true if the priority was changed, false otherwise. | ||||
80 | bool SetProcessBackgrounded(bool value); | ||||
81 | |||||
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 82 | // Returns an integer representing the priority of a process. The meaning |
83 | // of this value is OS dependent. | ||||
84 | int GetPriority() const; | ||||
85 | |||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 86 | private: |
87 | ProcessHandle process_; | ||||
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 88 | }; |
89 | |||||
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 90 | } // namespace base |
91 | |||||
92 | #endif // BASE_PROCESS_H_ |