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)16Process::Process(ProcessHandle handle) : process_(handle) { 17 DCHECK(handle == kNullProcessHandle || handle == kCurrentProcessHandle); 18 } 19 Process(Process && other)20Process::Process(Process&& other) : process_(other.process_) { 21 other.Close(); 22 } 23 24 Process::~Process() = default; 25 operator =(Process && other)26Process& Process::operator=(Process&& other) { 27 process_ = other.process_; 28 other.Close(); 29 return *this; 30 } 31 32 // static Current()33Process Process::Current() { 34 return Process(kCurrentProcessHandle); 35 } 36 37 // static Open(ProcessId pid)38Process Process::Open(ProcessId pid) { 39 if (pid == GetCurrentProcId()) { 40 return Current(); 41 } 42 return Process(pid); 43 } 44 45 // static OpenWithExtraPrivileges(ProcessId pid)46Process Process::OpenWithExtraPrivileges(ProcessId pid) { 47 return Open(pid); 48 } 49 50 // static TerminateCurrentProcessImmediately(int exit_code)51void 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) const57bool Process::Terminate(int exit_code, bool wait) const { 58 return false; 59 } 60 IsValid() const61bool Process::IsValid() const { 62 return process_ != kNullProcessHandle; 63 } 64 Handle() const65ProcessHandle Process::Handle() const { 66 return process_; 67 } 68 Duplicate() const69Process Process::Duplicate() const { 70 return Process(process_); 71 } 72 Release()73ProcessHandle Process::Release() { 74 ProcessHandle handle = process_; 75 Close(); 76 return handle; 77 } 78 Pid() const79ProcessId Process::Pid() const { 80 return process_; 81 } 82 CreationTime() const83Time Process::CreationTime() const { 84 return Time(); 85 } 86 is_current() const87bool Process::is_current() const { 88 return Handle() == kCurrentProcessHandle; 89 } 90 Close()91void Process::Close() { 92 process_ = kNullProcessHandle; 93 } 94 WaitForExit(int * exit_code) const95bool Process::WaitForExit(int* exit_code) const { 96 return false; 97 } 98 WaitForExitWithTimeout(TimeDelta timeout,int * exit_code) const99bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const { 100 return false; 101 } 102 Exited(int exit_code) const103void Process::Exited(int exit_code) const {} 104 GetPriority() const105Process::Priority Process::GetPriority() const { 106 return Priority::kUserBlocking; 107 } 108 SetPriority(Priority priority)109bool Process::SetPriority(Priority priority) { 110 return false; 111 } 112 GetOSPriority() const113int Process::GetOSPriority() const { 114 return -1; 115 } 116 117 } // namespace base 118