blob: 41eef10aed475f8972dbb16a6a7938a97e410b75 [file] [log] [blame]
[email protected]76bea672013-07-19 16:48:561// 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
danakj0a448602015-03-10 00:31:165#ifndef BASE_PROCESS_PROCESS_H_
6#define BASE_PROCESS_PROCESS_H_
[email protected]76bea672013-07-19 16:48:567
8#include "base/base_export.h"
9#include "base/basictypes.h"
rvargas079d1842014-10-17 22:32:1610#include "base/move.h"
[email protected]76bea672013-07-19 16:48:5611#include "base/process/process_handle.h"
rvargas126fd5822014-12-12 00:25:1412#include "base/time/time.h"
[email protected]76bea672013-07-19 16:48:5613#include "build/build_config.h"
14
rvargas079d1842014-10-17 22:32:1615#if defined(OS_WIN)
16#include "base/win/scoped_handle.h"
17#endif
18
[email protected]76bea672013-07-19 16:48:5619namespace base {
20
rvargas079d1842014-10-17 22:32:1621// 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]76bea672013-07-19 16:48:5634class BASE_EXPORT Process {
rvargas079d1842014-10-17 22:32:1635 MOVE_ONLY_TYPE_FOR_CPP_03(Process, RValue)
36
[email protected]76bea672013-07-19 16:48:5637 public:
rvargas079d1842014-10-17 22:32:1638 explicit Process(ProcessHandle handle = kNullProcessHandle);
[email protected]76bea672013-07-19 16:48:5639
rvargas079d1842014-10-17 22:32:1640 // Move constructor for C++03 move emulation of this type.
41 Process(RValue other);
[email protected]76bea672013-07-19 16:48:5642
rvargas079d1842014-10-17 22:32:1643 // 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]76bea672013-07-19 16:48:5650 static Process Current();
51
rvargas6b039c372015-02-04 21:11:2952 // Returns a Process for the given |pid|.
53 static Process Open(ProcessId pid);
54
rvargas747ff242015-01-17 02:46:4755 // 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
rvargas17a407d2015-01-23 20:36:4460#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
rvargas5779b382014-11-18 20:44:1166 // 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
rvargas079d1842014-10-17 22:32:1672 // Returns true if processes can be backgrounded.
[email protected]76bea672013-07-19 16:48:5673 static bool CanBackgroundProcesses();
74
rvargas079d1842014-10-17 22:32:1675 // 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]76bea672013-07-19 16:48:5684
85 // Get the PID for this process.
rvargas960db882015-01-24 00:27:2586 ProcessId Pid() const;
[email protected]76bea672013-07-19 16:48:5687
rvargas079d1842014-10-17 22:32:1688 // Returns true if this process is the current process.
[email protected]76bea672013-07-19 16:48:5689 bool is_current() const;
90
91 // Close the process handle. This will not terminate the process.
92 void Close();
93
rvargas079d1842014-10-17 22:32:1694 // Terminates the process with extreme prejudice. The given |result_code| will
rvargaseedb7632015-03-09 23:53:4595 // 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.
rvargas079d1842014-10-17 22:32:1698 // NOTE: On POSIX |result_code| is ignored.
rvargaseedb7632015-03-09 23:53:4599 bool Terminate(int result_code, bool wait) const;
[email protected]76bea672013-07-19 16:48:56100
rvargas126fd5822014-12-12 00:25:14101 // 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.
rvargas2f70a152015-02-24 00:28:11103 // On Linux this must be a child process, however on Mac and Windows it can be
104 // any process.
rvargas126fd5822014-12-12 00:25:14105 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]76bea672013-07-19 16:48:56110 // 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
rvargas079d1842014-10-17 22:32:16114 // 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]76bea672013-07-19 16:48:56117 // 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:
rvargas079d1842014-10-17 22:32:16125#if defined(OS_WIN)
126 bool is_current_process_;
127 win::ScopedHandle process_;
128#else
[email protected]76bea672013-07-19 16:48:56129 ProcessHandle process_;
rvargas079d1842014-10-17 22:32:16130#endif
[email protected]76bea672013-07-19 16:48:56131};
132
[email protected]76bea672013-07-19 16:48:56133} // namespace base
134
danakj0a448602015-03-10 00:31:16135#endif // BASE_PROCESS_PROCESS_H_