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_PROCESS_PROCESS_H_
6 #define BASE_PROCESS_PROCESS_PROCESS_H_
8 #include "base/base_export.h"
9 #include "base/basictypes.h"
10 #include "base/move.h"
11 #include "base/process/process_handle.h"
12 #include "base/time/time.h"
13 #include "build/build_config.h"
16 #include "base/win/scoped_handle.h"
21 // Provides a move-only encapsulation of a process.
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:
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.
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.
34 class BASE_EXPORT Process
{
35 MOVE_ONLY_TYPE_FOR_CPP_03(Process
, RValue
)
38 explicit Process(ProcessHandle handle
= kNullProcessHandle
);
40 // Move constructor for C++03 move emulation of this type.
41 Process(RValue other
);
43 // The destructor does not terminate the process.
46 // Move operator= for C++03 move emulation of this type.
47 Process
& operator=(RValue other
);
49 // Returns an object for the current process.
50 static Process
Current();
52 // Returns a Process for the given |pid|. On Windows the handle is opened
53 // with more access rights and must only be used by trusted code (can read the
54 // address space and duplicate handles).
55 static Process
OpenWithExtraPriviles(ProcessId pid
);
57 // Creates an object from a |handle| owned by someone else.
58 // Don't use this for new code. It is only intended to ease the migration to
59 // a strict ownership model.
60 // TODO(rvargas) crbug.com/417532: Remove this code.
61 static Process
DeprecatedGetProcessFromHandle(ProcessHandle handle
);
63 // Returns true if processes can be backgrounded.
64 static bool CanBackgroundProcesses();
66 // Returns true if this objects represents a valid process.
69 // Returns a handle for this process. There is no guarantee about when that
70 // handle becomes invalid because this object retains ownership.
71 ProcessHandle
Handle() const;
73 // Returns a second object that represents this process.
74 Process
Duplicate() const;
76 // Get the PID for this process.
77 ProcessId
pid() const;
79 // Returns true if this process is the current process.
80 bool is_current() const;
82 // Close the process handle. This will not terminate the process.
85 // Terminates the process with extreme prejudice. The given |result_code| will
86 // be the exit code of the process.
87 // NOTE: On POSIX |result_code| is ignored.
88 void Terminate(int result_code
);
90 // Waits for the process to exit. Returns true on success.
91 // On POSIX, if the process has been signaled then |exit_code| is set to -1.
92 bool WaitForExit(int* exit_code
);
94 // Same as WaitForExit() but only waits for up to |timeout|.
95 bool WaitForExitWithTimeout(TimeDelta timeout
, int* exit_code
);
97 // A process is backgrounded when it's priority is lower than normal.
98 // Return true if this process is backgrounded, false otherwise.
99 bool IsProcessBackgrounded() const;
101 // Set a process as backgrounded. If value is true, the priority of the
102 // process will be lowered. If value is false, the priority of the process
103 // will be made "normal" - equivalent to default process priority.
104 // Returns true if the priority was changed, false otherwise.
105 bool SetProcessBackgrounded(bool value
);
107 // Returns an integer representing the priority of a process. The meaning
108 // of this value is OS dependent.
109 int GetPriority() const;
113 bool is_current_process_
;
114 win::ScopedHandle process_
;
116 ProcessHandle process_
;
122 #endif // BASE_PROCESS_PROCESS_PROCESS_H_