OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_PROCESS_PROCESS_PROCESS_H_ | 5 #ifndef BASE_PROCESS_PROCESS_PROCESS_H_ |
6 #define BASE_PROCESS_PROCESS_PROCESS_H_ | 6 #define BASE_PROCESS_PROCESS_PROCESS_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/move.h" |
10 #include "base/process/process_handle.h" | 11 #include "base/process/process_handle.h" |
11 #include "build/build_config.h" | 12 #include "build/build_config.h" |
12 | 13 |
| 14 #if defined(OS_WIN) |
| 15 #include "base/win/scoped_handle.h" |
| 16 #endif |
| 17 |
13 namespace base { | 18 namespace base { |
14 | 19 |
| 20 // Provides a move-only encapsulation of a process. |
| 21 // |
| 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: |
| 25 // |
| 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. |
| 29 // |
| 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. |
15 class BASE_EXPORT Process { | 33 class BASE_EXPORT Process { |
| 34 MOVE_ONLY_TYPE_FOR_CPP_03(Process, RValue) |
| 35 |
16 public: | 36 public: |
17 Process() : process_(kNullProcessHandle) { | 37 explicit Process(ProcessHandle handle = kNullProcessHandle); |
18 } | |
19 | 38 |
20 explicit Process(ProcessHandle handle) : process_(handle) { | 39 // Move constructor for C++03 move emulation of this type. |
21 } | 40 Process(RValue other); |
22 | 41 |
23 // A handle to the current process. | 42 // The destructor does not terminate the process. |
| 43 ~Process() {} |
| 44 |
| 45 // Move operator= for C++03 move emulation of this type. |
| 46 Process& operator=(RValue other); |
| 47 |
| 48 // Returns an object for the current process. |
24 static Process Current(); | 49 static Process Current(); |
25 | 50 |
| 51 // Returns true if processes can be backgrounded. |
26 static bool CanBackgroundProcesses(); | 52 static bool CanBackgroundProcesses(); |
27 | 53 |
28 // Get/Set the handle for this process. The handle will be 0 if the process | 54 // Returns true if this objects represents a valid process. |
29 // is no longer running. | 55 bool IsValid() const; |
30 ProcessHandle handle() const { return process_; } | 56 |
31 void set_handle(ProcessHandle handle) { | 57 // Returns a handle for this process. There is no guarantee about when that |
32 process_ = handle; | 58 // handle becomes invalid because this object retains ownership. |
33 } | 59 ProcessHandle Handle() const; |
| 60 |
| 61 // Returns a second object that represents this process. |
| 62 Process Duplicate() const; |
34 | 63 |
35 // Get the PID for this process. | 64 // Get the PID for this process. |
36 ProcessId pid() const; | 65 ProcessId pid() const; |
37 | 66 |
38 // Is the this process the current process. | 67 // Returns true if this process is the current process. |
39 bool is_current() const; | 68 bool is_current() const; |
40 | 69 |
41 // Close the process handle. This will not terminate the process. | 70 // Close the process handle. This will not terminate the process. |
42 void Close(); | 71 void Close(); |
43 | 72 |
44 // Terminates the process with extreme prejudice. The given result code will | 73 // Terminates the process with extreme prejudice. The given |result_code| will |
45 // be the exit code of the process. If the process has already exited, this | 74 // be the exit code of the process. |
46 // will do nothing. | 75 // NOTE: On POSIX |result_code| is ignored. |
47 void Terminate(int result_code); | 76 void Terminate(int result_code); |
48 | 77 |
49 // A process is backgrounded when it's priority is lower than normal. | 78 // A process is backgrounded when it's priority is lower than normal. |
50 // Return true if this process is backgrounded, false otherwise. | 79 // Return true if this process is backgrounded, false otherwise. |
51 bool IsProcessBackgrounded() const; | 80 bool IsProcessBackgrounded() const; |
52 | 81 |
53 // Set a process as backgrounded. If value is true, the priority | 82 // Set a process as backgrounded. If value is true, the priority of the |
54 // of the process will be lowered. If value is false, the priority | 83 // process will be lowered. If value is false, the priority of the process |
55 // of the process will be made "normal" - equivalent to default | 84 // will be made "normal" - equivalent to default process priority. |
56 // process priority. | |
57 // Returns true if the priority was changed, false otherwise. | 85 // Returns true if the priority was changed, false otherwise. |
58 bool SetProcessBackgrounded(bool value); | 86 bool SetProcessBackgrounded(bool value); |
59 | 87 |
60 // Returns an integer representing the priority of a process. The meaning | 88 // Returns an integer representing the priority of a process. The meaning |
61 // of this value is OS dependent. | 89 // of this value is OS dependent. |
62 int GetPriority() const; | 90 int GetPriority() const; |
63 | 91 |
64 private: | 92 private: |
| 93 #if defined(OS_WIN) |
| 94 bool is_current_process_; |
| 95 win::ScopedHandle process_; |
| 96 #else |
65 ProcessHandle process_; | 97 ProcessHandle process_; |
| 98 #endif |
66 }; | 99 }; |
67 | 100 |
68 } // namespace base | 101 } // namespace base |
69 | 102 |
70 #endif // BASE_PROCESS_PROCESS_PROCESS_H_ | 103 #endif // BASE_PROCESS_PROCESS_PROCESS_H_ |
OLD | NEW |