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 "build/build_config.h"
15 #include "base/win/scoped_handle.h"
20 // Provides a move-only encapsulation of a process.
22 // This object is not tied to the lifetime of the underlying process: the
23 // process may be killed and this object may still around, and it will still
24 // claim to be valid. The actual behavior in that case is OS dependent like so:
26 // Windows: The underlying ProcessHandle will be valid after the process dies
27 // and can be used to gather some information about that process, but most
28 // methods will obviously fail.
30 // POSIX: The underlying PorcessHandle is not guaranteed to remain valid after
31 // the process dies, and it may be reused by the system, which means that it may
32 // end up pointing to the wrong process.
33 class BASE_EXPORT Process
{
34 MOVE_ONLY_TYPE_FOR_CPP_03(Process
, RValue
)
37 explicit Process(ProcessHandle handle
= kNullProcessHandle
);
39 // Move constructor for C++03 move emulation of this type.
40 Process(RValue other
);
42 // The destructor does not terminate the process.
45 // Move operator= for C++03 move emulation of this type.
46 Process
& operator=(RValue other
);
48 // Returns an object for the current process.
49 static Process
Current();
51 // Creates an object from a |handle| owned by someone else.
52 // Don't use this for new code. It is only intended to ease the migration to
53 // a strict ownership model.
54 // TODO(rvargas) crbug.com/417532: Remove this code.
55 static Process
DeprecatedGetProcessFromHandle(ProcessHandle handle
);
57 // Returns true if processes can be backgrounded.
58 static bool CanBackgroundProcesses();
60 // Returns true if this objects represents a valid process.
63 // Returns a handle for this process. There is no guarantee about when that
64 // handle becomes invalid because this object retains ownership.
65 ProcessHandle
Handle() const;
67 // Returns a second object that represents this process.
68 Process
Duplicate() const;
70 // Get the PID for this process.
71 ProcessId
pid() const;
73 // Returns true if this process is the current process.
74 bool is_current() const;
76 // Close the process handle. This will not terminate the process.
79 // Terminates the process with extreme prejudice. The given |result_code| will
80 // be the exit code of the process.
81 // NOTE: On POSIX |result_code| is ignored.
82 void Terminate(int result_code
);
84 // A process is backgrounded when it's priority is lower than normal.
85 // Return true if this process is backgrounded, false otherwise.
86 bool IsProcessBackgrounded() const;
88 // Set a process as backgrounded. If value is true, the priority of the
89 // process will be lowered. If value is false, the priority of the process
90 // will be made "normal" - equivalent to default process priority.
91 // Returns true if the priority was changed, false otherwise.
92 bool SetProcessBackgrounded(bool value
);
94 // Returns an integer representing the priority of a process. The meaning
95 // of this value is OS dependent.
96 int GetPriority() const;
100 bool is_current_process_
;
101 win::ScopedHandle process_
;
103 ProcessHandle process_
;
109 #endif // BASE_PROCESS_PROCESS_PROCESS_H_