xref: /aosp_15_r20/external/cronet/base/process/process_stubs.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/process/process.h"
6 
7 #include <limits>
8 
9 #include "base/immediate_crash.h"
10 
11 namespace base {
12 
13 static constexpr ProcessHandle kCurrentProcessHandle =
14     std::numeric_limits<ProcessHandle>::max();
15 
Process(ProcessHandle handle)16 Process::Process(ProcessHandle handle) : process_(handle) {
17   DCHECK(handle == kNullProcessHandle || handle == kCurrentProcessHandle);
18 }
19 
Process(Process && other)20 Process::Process(Process&& other) : process_(other.process_) {
21   other.Close();
22 }
23 
24 Process::~Process() = default;
25 
operator =(Process && other)26 Process& Process::operator=(Process&& other) {
27   process_ = other.process_;
28   other.Close();
29   return *this;
30 }
31 
32 // static
Current()33 Process Process::Current() {
34   return Process(kCurrentProcessHandle);
35 }
36 
37 // static
Open(ProcessId pid)38 Process Process::Open(ProcessId pid) {
39   if (pid == GetCurrentProcId()) {
40     return Current();
41   }
42   return Process(pid);
43 }
44 
45 // static
OpenWithExtraPrivileges(ProcessId pid)46 Process Process::OpenWithExtraPrivileges(ProcessId pid) {
47   return Open(pid);
48 }
49 
50 // static
TerminateCurrentProcessImmediately(int exit_code)51 void Process::TerminateCurrentProcessImmediately(int exit_code) {
52   // This method is marked noreturn, so we crash rather than just provide an
53   // empty stub implementation.
54   ImmediateCrash();
55 }
56 
Terminate(int exit_code,bool wait) const57 bool Process::Terminate(int exit_code, bool wait) const {
58   return false;
59 }
60 
IsValid() const61 bool Process::IsValid() const {
62   return process_ != kNullProcessHandle;
63 }
64 
Handle() const65 ProcessHandle Process::Handle() const {
66   return process_;
67 }
68 
Duplicate() const69 Process Process::Duplicate() const {
70   return Process(process_);
71 }
72 
Release()73 ProcessHandle Process::Release() {
74   ProcessHandle handle = process_;
75   Close();
76   return handle;
77 }
78 
Pid() const79 ProcessId Process::Pid() const {
80   return process_;
81 }
82 
CreationTime() const83 Time Process::CreationTime() const {
84   return Time();
85 }
86 
is_current() const87 bool Process::is_current() const {
88   return Handle() == kCurrentProcessHandle;
89 }
90 
Close()91 void Process::Close() {
92   process_ = kNullProcessHandle;
93 }
94 
WaitForExit(int * exit_code) const95 bool Process::WaitForExit(int* exit_code) const {
96   return false;
97 }
98 
WaitForExitWithTimeout(TimeDelta timeout,int * exit_code) const99 bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const {
100   return false;
101 }
102 
Exited(int exit_code) const103 void Process::Exited(int exit_code) const {}
104 
GetPriority() const105 Process::Priority Process::GetPriority() const {
106   return Priority::kUserBlocking;
107 }
108 
SetPriority(Priority priority)109 bool Process::SetPriority(Priority priority) {
110   return false;
111 }
112 
GetOSPriority() const113 int Process::GetOSPriority() const {
114   return -1;
115 }
116 
117 }  // namespace base
118