[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 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. |
| 4 | |
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame^] | 5 | #ifndef BASE_PROCESS_PROCESS_H_ |
| 6 | #define BASE_PROCESS_PROCESS_H_ |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 7 | |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/basictypes.h" |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 10 | #include "base/move.h" |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 11 | #include "base/process/process_handle.h" |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 12 | #include "base/time/time.h" |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 13 | #include "build/build_config.h" |
| 14 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 15 | #if defined(OS_WIN) |
| 16 | #include "base/win/scoped_handle.h" |
| 17 | #endif |
| 18 | |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 19 | namespace base { |
| 20 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 21 | // Provides a move-only encapsulation of a process. |
| 22 | // |
| 23 | // This object is not tied to the lifetime of the underlying process: the |
| 24 | // process may be killed and this object may still around, and it will still |
| 25 | // claim to be valid. The actual behavior in that case is OS dependent like so: |
| 26 | // |
| 27 | // Windows: The underlying ProcessHandle will be valid after the process dies |
| 28 | // and can be used to gather some information about that process, but most |
| 29 | // methods will obviously fail. |
| 30 | // |
| 31 | // POSIX: The underlying PorcessHandle is not guaranteed to remain valid after |
| 32 | // the process dies, and it may be reused by the system, which means that it may |
| 33 | // end up pointing to the wrong process. |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 34 | class BASE_EXPORT Process { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 35 | MOVE_ONLY_TYPE_FOR_CPP_03(Process, RValue) |
| 36 | |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 37 | public: |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 38 | explicit Process(ProcessHandle handle = kNullProcessHandle); |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 39 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 40 | // Move constructor for C++03 move emulation of this type. |
| 41 | Process(RValue other); |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 42 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 43 | // The destructor does not terminate the process. |
| 44 | ~Process() {} |
| 45 | |
| 46 | // Move operator= for C++03 move emulation of this type. |
| 47 | Process& operator=(RValue other); |
| 48 | |
| 49 | // Returns an object for the current process. |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 50 | static Process Current(); |
| 51 | |
rvargas | 6b039c37 | 2015-02-04 21:11:29 | [diff] [blame] | 52 | // Returns a Process for the given |pid|. |
| 53 | static Process Open(ProcessId pid); |
| 54 | |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 55 | // Returns a Process for the given |pid|. On Windows the handle is opened |
| 56 | // with more access rights and must only be used by trusted code (can read the |
| 57 | // address space and duplicate handles). |
| 58 | static Process OpenWithExtraPriviles(ProcessId pid); |
| 59 | |
rvargas | 17a407d | 2015-01-23 20:36:44 | [diff] [blame] | 60 | #if defined(OS_WIN) |
| 61 | // Returns a Process for the given |pid|, using some |desired_access|. |
| 62 | // See ::OpenProcess documentation for valid |desired_access|. |
| 63 | static Process OpenWithAccess(ProcessId pid, DWORD desired_access); |
| 64 | #endif |
| 65 | |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 66 | // Creates an object from a |handle| owned by someone else. |
| 67 | // Don't use this for new code. It is only intended to ease the migration to |
| 68 | // a strict ownership model. |
| 69 | // TODO(rvargas) crbug.com/417532: Remove this code. |
| 70 | static Process DeprecatedGetProcessFromHandle(ProcessHandle handle); |
| 71 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 72 | // Returns true if processes can be backgrounded. |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 73 | static bool CanBackgroundProcesses(); |
| 74 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 75 | // Returns true if this objects represents a valid process. |
| 76 | bool IsValid() const; |
| 77 | |
| 78 | // Returns a handle for this process. There is no guarantee about when that |
| 79 | // handle becomes invalid because this object retains ownership. |
| 80 | ProcessHandle Handle() const; |
| 81 | |
| 82 | // Returns a second object that represents this process. |
| 83 | Process Duplicate() const; |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 84 | |
| 85 | // Get the PID for this process. |
rvargas | 960db88 | 2015-01-24 00:27:25 | [diff] [blame] | 86 | ProcessId Pid() const; |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 87 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 88 | // Returns true if this process is the current process. |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 89 | bool is_current() const; |
| 90 | |
| 91 | // Close the process handle. This will not terminate the process. |
| 92 | void Close(); |
| 93 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 94 | // Terminates the process with extreme prejudice. The given |result_code| will |
rvargas | eedb763 | 2015-03-09 23:53:45 | [diff] [blame] | 95 | // be the exit code of the process. If |wait| is true, this method will wait |
| 96 | // for up to one minute for the process to actually terminate. |
| 97 | // Returns true if the process terminates within the allowed time. |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 98 | // NOTE: On POSIX |result_code| is ignored. |
rvargas | eedb763 | 2015-03-09 23:53:45 | [diff] [blame] | 99 | bool Terminate(int result_code, bool wait) const; |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 100 | |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 101 | // Waits for the process to exit. Returns true on success. |
| 102 | // On POSIX, if the process has been signaled then |exit_code| is set to -1. |
rvargas | 2f70a15 | 2015-02-24 00:28:11 | [diff] [blame] | 103 | // On Linux this must be a child process, however on Mac and Windows it can be |
| 104 | // any process. |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 105 | bool WaitForExit(int* exit_code); |
| 106 | |
| 107 | // Same as WaitForExit() but only waits for up to |timeout|. |
| 108 | bool WaitForExitWithTimeout(TimeDelta timeout, int* exit_code); |
| 109 | |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 110 | // A process is backgrounded when it's priority is lower than normal. |
| 111 | // Return true if this process is backgrounded, false otherwise. |
| 112 | bool IsProcessBackgrounded() const; |
| 113 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 114 | // Set a process as backgrounded. If value is true, the priority of the |
| 115 | // process will be lowered. If value is false, the priority of the process |
| 116 | // will be made "normal" - equivalent to default process priority. |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 117 | // Returns true if the priority was changed, false otherwise. |
| 118 | bool SetProcessBackgrounded(bool value); |
| 119 | |
| 120 | // Returns an integer representing the priority of a process. The meaning |
| 121 | // of this value is OS dependent. |
| 122 | int GetPriority() const; |
| 123 | |
| 124 | private: |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 125 | #if defined(OS_WIN) |
| 126 | bool is_current_process_; |
| 127 | win::ScopedHandle process_; |
| 128 | #else |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 129 | ProcessHandle process_; |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 130 | #endif |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 131 | }; |
| 132 | |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 133 | } // namespace base |
| 134 | |
danakj | 0a44860 | 2015-03-10 00:31:16 | [diff] [blame^] | 135 | #endif // BASE_PROCESS_PROCESS_H_ |