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|.
53 static Process
Open(ProcessId pid
);
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
);
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
);
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
);
72 // Returns true if processes can be backgrounded.
73 static bool CanBackgroundProcesses();
75 // Returns true if this objects represents a valid process.
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;
82 // Returns a second object that represents this process.
83 Process
Duplicate() const;
85 // Get the PID for this process.
86 ProcessId
Pid() const;
88 // Returns true if this process is the current process.
89 bool is_current() const;
91 // Close the process handle. This will not terminate the process.
94 // Terminates the process with extreme prejudice. The given |result_code| will
95 // be the exit code of the process.
96 // NOTE: On POSIX |result_code| is ignored.
97 void Terminate(int result_code
);
99 // Waits for the process to exit. Returns true on success.
100 // On POSIX, if the process has been signaled then |exit_code| is set to -1.
101 bool WaitForExit(int* exit_code
);
103 // Same as WaitForExit() but only waits for up to |timeout|.
104 bool WaitForExitWithTimeout(TimeDelta timeout
, int* exit_code
);
106 // A process is backgrounded when it's priority is lower than normal.
107 // Return true if this process is backgrounded, false otherwise.
108 bool IsProcessBackgrounded() const;
110 // Set a process as backgrounded. If value is true, the priority of the
111 // process will be lowered. If value is false, the priority of the process
112 // will be made "normal" - equivalent to default process priority.
113 // Returns true if the priority was changed, false otherwise.
114 bool SetProcessBackgrounded(bool value
);
116 // Returns an integer representing the priority of a process. The meaning
117 // of this value is OS dependent.
118 int GetPriority() const;
122 bool is_current_process_
;
123 win::ScopedHandle process_
;
125 ProcessHandle process_
;
131 #endif // BASE_PROCESS_PROCESS_PROCESS_H_