1*6777b538SAndroid Build Coastguard Worker // Copyright 2011 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_PROCESS_PROCESS_H_ 6*6777b538SAndroid Build Coastguard Worker #define BASE_PROCESS_PROCESS_H_ 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker #include <string_view> 9*6777b538SAndroid Build Coastguard Worker 10*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h" 11*6777b538SAndroid Build Coastguard Worker #include "base/process/process_handle.h" 12*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h" 13*6777b538SAndroid Build Coastguard Worker #include "build/blink_buildflags.h" 14*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h" 15*6777b538SAndroid Build Coastguard Worker #include "build/chromeos_buildflags.h" 16*6777b538SAndroid Build Coastguard Worker 17*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 18*6777b538SAndroid Build Coastguard Worker #include "base/win/scoped_handle.h" 19*6777b538SAndroid Build Coastguard Worker #endif 20*6777b538SAndroid Build Coastguard Worker 21*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_FUCHSIA) 22*6777b538SAndroid Build Coastguard Worker #include <lib/zx/process.h> 23*6777b538SAndroid Build Coastguard Worker #endif 24*6777b538SAndroid Build Coastguard Worker 25*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) 26*6777b538SAndroid Build Coastguard Worker #include "base/feature_list.h" 27*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) 28*6777b538SAndroid Build Coastguard Worker 29*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_APPLE) 30*6777b538SAndroid Build Coastguard Worker #include "base/process/port_provider_mac.h" 31*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_APPLE) 32*6777b538SAndroid Build Coastguard Worker 33*6777b538SAndroid Build Coastguard Worker namespace base { 34*6777b538SAndroid Build Coastguard Worker 35*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_CHROMEOS) 36*6777b538SAndroid Build Coastguard Worker // OneGroupPerRenderer feature places each foreground renderer process into 37*6777b538SAndroid Build Coastguard Worker // its own cgroup. This will cause the scheduler to use the aggregate runtime 38*6777b538SAndroid Build Coastguard Worker // of all threads in the process when deciding on the next thread to schedule. 39*6777b538SAndroid Build Coastguard Worker // It will help guarantee fairness between renderers. 40*6777b538SAndroid Build Coastguard Worker BASE_EXPORT BASE_DECLARE_FEATURE(kOneGroupPerRenderer); 41*6777b538SAndroid Build Coastguard Worker 42*6777b538SAndroid Build Coastguard Worker // Set all threads of a background process as backgrounded, which changes the 43*6777b538SAndroid Build Coastguard Worker // thread attributes including c-group, latency sensitivity. But the nice value 44*6777b538SAndroid Build Coastguard Worker // is unchanged, since background process is under the spell of the background 45*6777b538SAndroid Build Coastguard Worker // CPU c-group (via cgroup.procs). 46*6777b538SAndroid Build Coastguard Worker BASE_EXPORT BASE_DECLARE_FEATURE(kSetThreadBgForBgProcess); 47*6777b538SAndroid Build Coastguard Worker 48*6777b538SAndroid Build Coastguard Worker class ProcessPriorityDelegate; 49*6777b538SAndroid Build Coastguard Worker #endif 50*6777b538SAndroid Build Coastguard Worker 51*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 52*6777b538SAndroid Build Coastguard Worker BASE_EXPORT BASE_DECLARE_FEATURE(kUseEcoQoSForBackgroundProcess); 53*6777b538SAndroid Build Coastguard Worker #endif 54*6777b538SAndroid Build Coastguard Worker 55*6777b538SAndroid Build Coastguard Worker // Provides a move-only encapsulation of a process. 56*6777b538SAndroid Build Coastguard Worker // 57*6777b538SAndroid Build Coastguard Worker // This object is not tied to the lifetime of the underlying process: the 58*6777b538SAndroid Build Coastguard Worker // process may be killed and this object may still around, and it will still 59*6777b538SAndroid Build Coastguard Worker // claim to be valid. The actual behavior in that case is OS dependent like so: 60*6777b538SAndroid Build Coastguard Worker // 61*6777b538SAndroid Build Coastguard Worker // Windows: The underlying ProcessHandle will be valid after the process dies 62*6777b538SAndroid Build Coastguard Worker // and can be used to gather some information about that process, but most 63*6777b538SAndroid Build Coastguard Worker // methods will obviously fail. 64*6777b538SAndroid Build Coastguard Worker // 65*6777b538SAndroid Build Coastguard Worker // POSIX: The underlying ProcessHandle is not guaranteed to remain valid after 66*6777b538SAndroid Build Coastguard Worker // the process dies, and it may be reused by the system, which means that it may 67*6777b538SAndroid Build Coastguard Worker // end up pointing to the wrong process. 68*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT Process { 69*6777b538SAndroid Build Coastguard Worker public: 70*6777b538SAndroid Build Coastguard Worker // On Windows, this takes ownership of |handle|. On POSIX, this does not take 71*6777b538SAndroid Build Coastguard Worker // ownership of |handle|. 72*6777b538SAndroid Build Coastguard Worker explicit Process(ProcessHandle handle = kNullProcessHandle); 73*6777b538SAndroid Build Coastguard Worker 74*6777b538SAndroid Build Coastguard Worker Process(Process&& other); 75*6777b538SAndroid Build Coastguard Worker 76*6777b538SAndroid Build Coastguard Worker Process(const Process&) = delete; 77*6777b538SAndroid Build Coastguard Worker Process& operator=(const Process&) = delete; 78*6777b538SAndroid Build Coastguard Worker 79*6777b538SAndroid Build Coastguard Worker // The destructor does not terminate the process. 80*6777b538SAndroid Build Coastguard Worker ~Process(); 81*6777b538SAndroid Build Coastguard Worker 82*6777b538SAndroid Build Coastguard Worker Process& operator=(Process&& other); 83*6777b538SAndroid Build Coastguard Worker 84*6777b538SAndroid Build Coastguard Worker // Returns an object for the current process. 85*6777b538SAndroid Build Coastguard Worker static Process Current(); 86*6777b538SAndroid Build Coastguard Worker 87*6777b538SAndroid Build Coastguard Worker // Returns a Process for the given |pid|. 88*6777b538SAndroid Build Coastguard Worker static Process Open(ProcessId pid); 89*6777b538SAndroid Build Coastguard Worker 90*6777b538SAndroid Build Coastguard Worker // Returns a Process for the given |pid|. On Windows the handle is opened 91*6777b538SAndroid Build Coastguard Worker // with more access rights and must only be used by trusted code (can read the 92*6777b538SAndroid Build Coastguard Worker // address space and duplicate handles). 93*6777b538SAndroid Build Coastguard Worker static Process OpenWithExtraPrivileges(ProcessId pid); 94*6777b538SAndroid Build Coastguard Worker 95*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 96*6777b538SAndroid Build Coastguard Worker // Returns a Process for the given |pid|, using some |desired_access|. 97*6777b538SAndroid Build Coastguard Worker // See ::OpenProcess documentation for valid |desired_access|. 98*6777b538SAndroid Build Coastguard Worker static Process OpenWithAccess(ProcessId pid, DWORD desired_access); 99*6777b538SAndroid Build Coastguard Worker #endif 100*6777b538SAndroid Build Coastguard Worker 101*6777b538SAndroid Build Coastguard Worker // Returns true if changing the priority of processes through `SetPriority()` 102*6777b538SAndroid Build Coastguard Worker // is possible. 103*6777b538SAndroid Build Coastguard Worker static bool CanSetPriority(); 104*6777b538SAndroid Build Coastguard Worker 105*6777b538SAndroid Build Coastguard Worker // Terminates the current process immediately with |exit_code|. 106*6777b538SAndroid Build Coastguard Worker [[noreturn]] static void TerminateCurrentProcessImmediately(int exit_code); 107*6777b538SAndroid Build Coastguard Worker 108*6777b538SAndroid Build Coastguard Worker // Returns true if this objects represents a valid process. 109*6777b538SAndroid Build Coastguard Worker bool IsValid() const; 110*6777b538SAndroid Build Coastguard Worker 111*6777b538SAndroid Build Coastguard Worker // Returns a handle for this process. There is no guarantee about when that 112*6777b538SAndroid Build Coastguard Worker // handle becomes invalid because this object retains ownership. 113*6777b538SAndroid Build Coastguard Worker ProcessHandle Handle() const; 114*6777b538SAndroid Build Coastguard Worker 115*6777b538SAndroid Build Coastguard Worker // Returns a second object that represents this process. 116*6777b538SAndroid Build Coastguard Worker Process Duplicate() const; 117*6777b538SAndroid Build Coastguard Worker 118*6777b538SAndroid Build Coastguard Worker // Relinquishes ownership of the handle and sets this to kNullProcessHandle. 119*6777b538SAndroid Build Coastguard Worker // The result may be a pseudo-handle, depending on the OS and value stored in 120*6777b538SAndroid Build Coastguard Worker // this. 121*6777b538SAndroid Build Coastguard Worker [[nodiscard]] ProcessHandle Release(); 122*6777b538SAndroid Build Coastguard Worker 123*6777b538SAndroid Build Coastguard Worker // Get the PID for this process. 124*6777b538SAndroid Build Coastguard Worker ProcessId Pid() const; 125*6777b538SAndroid Build Coastguard Worker 126*6777b538SAndroid Build Coastguard Worker // Get the creation time for this process. Since the Pid can be reused after a 127*6777b538SAndroid Build Coastguard Worker // process dies, it is useful to use both the Pid and the creation time to 128*6777b538SAndroid Build Coastguard Worker // uniquely identify a process. 129*6777b538SAndroid Build Coastguard Worker // 130*6777b538SAndroid Build Coastguard Worker // On Android, works only if |this| is the current process, as security 131*6777b538SAndroid Build Coastguard Worker // features prevent an application from getting data about other processes, 132*6777b538SAndroid Build Coastguard Worker // even if they belong to us. Otherwise, returns Time(). 133*6777b538SAndroid Build Coastguard Worker Time CreationTime() const; 134*6777b538SAndroid Build Coastguard Worker 135*6777b538SAndroid Build Coastguard Worker // Returns true if this process is the current process. 136*6777b538SAndroid Build Coastguard Worker bool is_current() const; 137*6777b538SAndroid Build Coastguard Worker 138*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_CHROMEOS) 139*6777b538SAndroid Build Coastguard Worker // A unique token generated for each process, this is used to create a unique 140*6777b538SAndroid Build Coastguard Worker // cgroup for each renderer. unique_token()141*6777b538SAndroid Build Coastguard Worker const std::string& unique_token() const { return unique_token_; } 142*6777b538SAndroid Build Coastguard Worker #endif 143*6777b538SAndroid Build Coastguard Worker 144*6777b538SAndroid Build Coastguard Worker // Close the process handle. This will not terminate the process. 145*6777b538SAndroid Build Coastguard Worker void Close(); 146*6777b538SAndroid Build Coastguard Worker 147*6777b538SAndroid Build Coastguard Worker // Returns true if this process is still running. This is only safe on Windows 148*6777b538SAndroid Build Coastguard Worker // (and maybe Fuchsia?), because the ProcessHandle will keep the zombie 149*6777b538SAndroid Build Coastguard Worker // process information available until itself has been released. But on Posix, 150*6777b538SAndroid Build Coastguard Worker // the OS may reuse the ProcessId. 151*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) IsRunning()152*6777b538SAndroid Build Coastguard Worker bool IsRunning() const { 153*6777b538SAndroid Build Coastguard Worker return !WaitForExitWithTimeout(base::TimeDelta(), nullptr); 154*6777b538SAndroid Build Coastguard Worker } 155*6777b538SAndroid Build Coastguard Worker #endif 156*6777b538SAndroid Build Coastguard Worker 157*6777b538SAndroid Build Coastguard Worker // Terminates the process with extreme prejudice. The given |exit_code| will 158*6777b538SAndroid Build Coastguard Worker // be the exit code of the process. If |wait| is true, this method will wait 159*6777b538SAndroid Build Coastguard Worker // for up to one minute for the process to actually terminate. 160*6777b538SAndroid Build Coastguard Worker // Returns true if the process terminates within the allowed time. 161*6777b538SAndroid Build Coastguard Worker // NOTE: |exit_code| is only used on OS_WIN. 162*6777b538SAndroid Build Coastguard Worker bool Terminate(int exit_code, bool wait) const; 163*6777b538SAndroid Build Coastguard Worker 164*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 165*6777b538SAndroid Build Coastguard Worker enum class WaitExitStatus { 166*6777b538SAndroid Build Coastguard Worker PROCESS_EXITED, 167*6777b538SAndroid Build Coastguard Worker STOP_EVENT_SIGNALED, 168*6777b538SAndroid Build Coastguard Worker FAILED, 169*6777b538SAndroid Build Coastguard Worker }; 170*6777b538SAndroid Build Coastguard Worker 171*6777b538SAndroid Build Coastguard Worker // Waits for the process to exit, or the specified |stop_event_handle| to be 172*6777b538SAndroid Build Coastguard Worker // set. Returns value indicating which event was set. The given |exit_code| 173*6777b538SAndroid Build Coastguard Worker // will be the exit code of the process. 174*6777b538SAndroid Build Coastguard Worker WaitExitStatus WaitForExitOrEvent( 175*6777b538SAndroid Build Coastguard Worker const base::win::ScopedHandle& stop_event_handle, 176*6777b538SAndroid Build Coastguard Worker int* exit_code) const; 177*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_WIN) 178*6777b538SAndroid Build Coastguard Worker 179*6777b538SAndroid Build Coastguard Worker // Waits for the process to exit. Returns true on success. 180*6777b538SAndroid Build Coastguard Worker // On POSIX, if the process has been signaled then |exit_code| is set to -1. 181*6777b538SAndroid Build Coastguard Worker // On Linux this must be a child process, however on Mac and Windows it can be 182*6777b538SAndroid Build Coastguard Worker // any process. 183*6777b538SAndroid Build Coastguard Worker // NOTE: |exit_code| is optional, nullptr can be passed if the exit code is 184*6777b538SAndroid Build Coastguard Worker // not required. 185*6777b538SAndroid Build Coastguard Worker bool WaitForExit(int* exit_code) const; 186*6777b538SAndroid Build Coastguard Worker 187*6777b538SAndroid Build Coastguard Worker // Same as WaitForExit() but only waits for up to |timeout|. 188*6777b538SAndroid Build Coastguard Worker // NOTE: |exit_code| is optional, nullptr can be passed if the exit code 189*6777b538SAndroid Build Coastguard Worker // is not required. 190*6777b538SAndroid Build Coastguard Worker bool WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const; 191*6777b538SAndroid Build Coastguard Worker 192*6777b538SAndroid Build Coastguard Worker // Indicates that the process has exited with the specified |exit_code|. 193*6777b538SAndroid Build Coastguard Worker // This should be called if process exit is observed outside of this class. 194*6777b538SAndroid Build Coastguard Worker // (i.e. Not because Terminate or WaitForExit, above, was called.) 195*6777b538SAndroid Build Coastguard Worker // Note that nothing prevents this being called multiple times for a dead 196*6777b538SAndroid Build Coastguard Worker // process though that should be avoided. 197*6777b538SAndroid Build Coastguard Worker void Exited(int exit_code) const; 198*6777b538SAndroid Build Coastguard Worker 199*6777b538SAndroid Build Coastguard Worker // The different priorities that a process can have. 200*6777b538SAndroid Build Coastguard Worker // TODO(pmonette): Consider merging with base::TaskPriority when the API is 201*6777b538SAndroid Build Coastguard Worker // stable. 202*6777b538SAndroid Build Coastguard Worker enum class Priority { 203*6777b538SAndroid Build Coastguard Worker // The process does not contribute to content that is currently important 204*6777b538SAndroid Build Coastguard Worker // to the user. Lowest priority. 205*6777b538SAndroid Build Coastguard Worker kBestEffort, 206*6777b538SAndroid Build Coastguard Worker 207*6777b538SAndroid Build Coastguard Worker // The process contributes to content that is visible to the user. High 208*6777b538SAndroid Build Coastguard Worker // priority. 209*6777b538SAndroid Build Coastguard Worker kUserVisible, 210*6777b538SAndroid Build Coastguard Worker 211*6777b538SAndroid Build Coastguard Worker // The process contributes to content that is of the utmost importance to 212*6777b538SAndroid Build Coastguard Worker // the user, like producing audible content, or visible content in the 213*6777b538SAndroid Build Coastguard Worker // focused window. Highest priority. 214*6777b538SAndroid Build Coastguard Worker kUserBlocking, 215*6777b538SAndroid Build Coastguard Worker }; 216*6777b538SAndroid Build Coastguard Worker 217*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK)) 218*6777b538SAndroid Build Coastguard Worker // The Mac needs a Mach port in order to manipulate a process's priority, 219*6777b538SAndroid Build Coastguard Worker // and there's no good way to get that from base given the pid. These Mac 220*6777b538SAndroid Build Coastguard Worker // variants of the `GetPriority()` and `SetPriority()` API take a port 221*6777b538SAndroid Build Coastguard Worker // provider for this reason. See crbug.com/460102. 222*6777b538SAndroid Build Coastguard Worker 223*6777b538SAndroid Build Coastguard Worker // Retrieves the priority of the process. Defaults to Priority::kUserBlocking 224*6777b538SAndroid Build Coastguard Worker // if the priority could not be retrieved, or if `port_provider` is null. 225*6777b538SAndroid Build Coastguard Worker Priority GetPriority(PortProvider* port_provider) const; 226*6777b538SAndroid Build Coastguard Worker 227*6777b538SAndroid Build Coastguard Worker // Sets the priority of the process process. Returns true if the priority was 228*6777b538SAndroid Build Coastguard Worker // changed, false otherwise. If `port_provider` is null, this is a no-op and 229*6777b538SAndroid Build Coastguard Worker // it returns false. 230*6777b538SAndroid Build Coastguard Worker bool SetPriority(PortProvider* port_provider, Priority priority); 231*6777b538SAndroid Build Coastguard Worker #else 232*6777b538SAndroid Build Coastguard Worker // Retrieves the priority of the process. Defaults to Priority::kUserBlocking 233*6777b538SAndroid Build Coastguard Worker // if the priority could not be retrieved. 234*6777b538SAndroid Build Coastguard Worker Priority GetPriority() const; 235*6777b538SAndroid Build Coastguard Worker 236*6777b538SAndroid Build Coastguard Worker // Sets the priority of the process process. Returns true if the priority was 237*6777b538SAndroid Build Coastguard Worker // changed, false otherwise. 238*6777b538SAndroid Build Coastguard Worker bool SetPriority(Priority priority); 239*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK)) 240*6777b538SAndroid Build Coastguard Worker 241*6777b538SAndroid Build Coastguard Worker // Returns an integer representing the priority of a process. The meaning 242*6777b538SAndroid Build Coastguard Worker // of this value is OS dependent. 243*6777b538SAndroid Build Coastguard Worker int GetOSPriority() const; 244*6777b538SAndroid Build Coastguard Worker 245*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_CHROMEOS_ASH) 246*6777b538SAndroid Build Coastguard Worker // Get the PID in its PID namespace. 247*6777b538SAndroid Build Coastguard Worker // If the process is not in a PID namespace or /proc/<pid>/status does not 248*6777b538SAndroid Build Coastguard Worker // report NSpid, kNullProcessId is returned. 249*6777b538SAndroid Build Coastguard Worker ProcessId GetPidInNamespace() const; 250*6777b538SAndroid Build Coastguard Worker #endif 251*6777b538SAndroid Build Coastguard Worker 252*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) 253*6777b538SAndroid Build Coastguard Worker // Returns true if the process has any seccomp policy applied. 254*6777b538SAndroid Build Coastguard Worker bool IsSeccompSandboxed(); 255*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) 256*6777b538SAndroid Build Coastguard Worker 257*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_CHROMEOS) 258*6777b538SAndroid Build Coastguard Worker // Sets a delegate which handles process priority changes. This 259*6777b538SAndroid Build Coastguard Worker // must be externally synchronized with any call to base::Process methods. 260*6777b538SAndroid Build Coastguard Worker static void SetProcessPriorityDelegate(ProcessPriorityDelegate* delegate); 261*6777b538SAndroid Build Coastguard Worker 262*6777b538SAndroid Build Coastguard Worker // Exposes OneGroupPerRendererEnabled() to unit tests. 263*6777b538SAndroid Build Coastguard Worker static bool OneGroupPerRendererEnabledForTesting(); 264*6777b538SAndroid Build Coastguard Worker 265*6777b538SAndroid Build Coastguard Worker // If OneGroupPerRenderer is enabled, runs at process startup to clean up 266*6777b538SAndroid Build Coastguard Worker // any stale cgroups that were left behind from any unclean exits of the 267*6777b538SAndroid Build Coastguard Worker // browser process. 268*6777b538SAndroid Build Coastguard Worker static void CleanUpStaleProcessStates(); 269*6777b538SAndroid Build Coastguard Worker 270*6777b538SAndroid Build Coastguard Worker // Initializes the process's priority. 271*6777b538SAndroid Build Coastguard Worker // 272*6777b538SAndroid Build Coastguard Worker // This should be called before SetPriority(). 273*6777b538SAndroid Build Coastguard Worker // 274*6777b538SAndroid Build Coastguard Worker // If SchedQoSOnResourcedForChrome is enabled, this creates a cache entry for 275*6777b538SAndroid Build Coastguard Worker // the process priority. The returned `base::Process::PriorityEntry` should be 276*6777b538SAndroid Build Coastguard Worker // freed when the process is terminated so that the cached entry is freed from 277*6777b538SAndroid Build Coastguard Worker // the internal map. 278*6777b538SAndroid Build Coastguard Worker // 279*6777b538SAndroid Build Coastguard Worker // If OneGroupPerRenderer is enabled, it also creates a unique cgroup for the 280*6777b538SAndroid Build Coastguard Worker // process. 281*6777b538SAndroid Build Coastguard Worker // This is a no-op if the Process is not valid or if it has already been 282*6777b538SAndroid Build Coastguard Worker // called. 283*6777b538SAndroid Build Coastguard Worker void InitializePriority(); 284*6777b538SAndroid Build Coastguard Worker 285*6777b538SAndroid Build Coastguard Worker // Clears the entities initialized by InitializePriority(). 286*6777b538SAndroid Build Coastguard Worker // 287*6777b538SAndroid Build Coastguard Worker // This is no-op if SchedQoSOnResourcedForChrome is disabled. 288*6777b538SAndroid Build Coastguard Worker void ForgetPriority(); 289*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_CHROMEOS) 290*6777b538SAndroid Build Coastguard Worker 291*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_APPLE) 292*6777b538SAndroid Build Coastguard Worker // Sets the priority of the current process to its default value. 293*6777b538SAndroid Build Coastguard Worker static void SetCurrentTaskDefaultRole(); 294*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_MAC) 295*6777b538SAndroid Build Coastguard Worker 296*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK) 297*6777b538SAndroid Build Coastguard Worker using TerminateCallback = bool (*)(ProcessHandle handle); 298*6777b538SAndroid Build Coastguard Worker using WaitForExitCallback = bool (*)(ProcessHandle handle, 299*6777b538SAndroid Build Coastguard Worker int* exit_code, 300*6777b538SAndroid Build Coastguard Worker base::TimeDelta timeout); 301*6777b538SAndroid Build Coastguard Worker // Function ptrs to implement termination without polluting //base with 302*6777b538SAndroid Build Coastguard Worker // BrowserEngineKit APIs. 303*6777b538SAndroid Build Coastguard Worker static void SetTerminationHooks(TerminateCallback terminate_callback, 304*6777b538SAndroid Build Coastguard Worker WaitForExitCallback wait_callback); 305*6777b538SAndroid Build Coastguard Worker #if TARGET_OS_SIMULATOR 306*6777b538SAndroid Build Coastguard Worker // Methods for supporting both "content processes" and traditional 307*6777b538SAndroid Build Coastguard Worker // forked processes. For non-simulator builds on iOS every process would 308*6777b538SAndroid Build Coastguard Worker // be a "content process" so we don't need the conditionals. 309*6777b538SAndroid Build Coastguard Worker void SetIsContentProcess(); 310*6777b538SAndroid Build Coastguard Worker bool IsContentProcess() const; 311*6777b538SAndroid Build Coastguard Worker #endif 312*6777b538SAndroid Build Coastguard Worker #endif 313*6777b538SAndroid Build Coastguard Worker 314*6777b538SAndroid Build Coastguard Worker private: 315*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_CHROMEOS) 316*6777b538SAndroid Build Coastguard Worker // Cleans up process state. If OneGroupPerRenderer is enabled, it cleans up 317*6777b538SAndroid Build Coastguard Worker // the cgroup created by InitializePriority(). If the process has not 318*6777b538SAndroid Build Coastguard Worker // fully terminated yet, it will post a background task to try again. 319*6777b538SAndroid Build Coastguard Worker void CleanUpProcess(int remaining_retries) const; 320*6777b538SAndroid Build Coastguard Worker 321*6777b538SAndroid Build Coastguard Worker // Calls CleanUpProcess() on a background thread. 322*6777b538SAndroid Build Coastguard Worker void CleanUpProcessAsync() const; 323*6777b538SAndroid Build Coastguard Worker 324*6777b538SAndroid Build Coastguard Worker // Used to call CleanUpProcess() on a background thread because Process is not 325*6777b538SAndroid Build Coastguard Worker // refcounted. 326*6777b538SAndroid Build Coastguard Worker static void CleanUpProcessScheduled(Process process, int remaining_retries); 327*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_CHROMEOS) 328*6777b538SAndroid Build Coastguard Worker 329*6777b538SAndroid Build Coastguard Worker #if !BUILDFLAG(IS_IOS) || (BUILDFLAG(IS_IOS) && TARGET_OS_SIMULATOR) 330*6777b538SAndroid Build Coastguard Worker bool TerminateInternal(int exit_code, bool wait) const; 331*6777b538SAndroid Build Coastguard Worker bool WaitForExitWithTimeoutImpl(base::ProcessHandle handle, 332*6777b538SAndroid Build Coastguard Worker int* exit_code, 333*6777b538SAndroid Build Coastguard Worker base::TimeDelta timeout) const; 334*6777b538SAndroid Build Coastguard Worker #endif 335*6777b538SAndroid Build Coastguard Worker 336*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 337*6777b538SAndroid Build Coastguard Worker win::ScopedHandle process_; 338*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_FUCHSIA) 339*6777b538SAndroid Build Coastguard Worker zx::process process_; 340*6777b538SAndroid Build Coastguard Worker #else 341*6777b538SAndroid Build Coastguard Worker ProcessHandle process_; 342*6777b538SAndroid Build Coastguard Worker #endif 343*6777b538SAndroid Build Coastguard Worker 344*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA) 345*6777b538SAndroid Build Coastguard Worker bool is_current_process_; 346*6777b538SAndroid Build Coastguard Worker #endif 347*6777b538SAndroid Build Coastguard Worker 348*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_IOS) && BUILDFLAG(USE_BLINK) && TARGET_OS_SIMULATOR 349*6777b538SAndroid Build Coastguard Worker // A flag indicating that this is a "content process". iOS does not support 350*6777b538SAndroid Build Coastguard Worker // generic process invocation but it does support some types of well defined 351*6777b538SAndroid Build Coastguard Worker // processes. These types of processes are defined at the //content layer so 352*6777b538SAndroid Build Coastguard Worker // for termination we defer to some globally initialized callbacks. 353*6777b538SAndroid Build Coastguard Worker bool content_process_ = false; 354*6777b538SAndroid Build Coastguard Worker #endif 355*6777b538SAndroid Build Coastguard Worker 356*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_CHROMEOS) 357*6777b538SAndroid Build Coastguard Worker // A unique token per process not per class instance (`base::Process`). This 358*6777b538SAndroid Build Coastguard Worker // is similar to the PID of a process but should not be reused after the 359*6777b538SAndroid Build Coastguard Worker // process's termination. The token will be copied during Duplicate() 360*6777b538SAndroid Build Coastguard Worker // and move semantics as is the PID/ProcessHandle. 361*6777b538SAndroid Build Coastguard Worker std::string unique_token_; 362*6777b538SAndroid Build Coastguard Worker #endif 363*6777b538SAndroid Build Coastguard Worker }; 364*6777b538SAndroid Build Coastguard Worker 365*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_CHROMEOS) 366*6777b538SAndroid Build Coastguard Worker // Exposed for testing. 367*6777b538SAndroid Build Coastguard Worker // Given the contents of the /proc/<pid>/cgroup file, determine whether the 368*6777b538SAndroid Build Coastguard Worker // process is backgrounded or not. 369*6777b538SAndroid Build Coastguard Worker BASE_EXPORT Process::Priority GetProcessPriorityCGroup( 370*6777b538SAndroid Build Coastguard Worker std::string_view cgroup_contents); 371*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_CHROMEOS) 372*6777b538SAndroid Build Coastguard Worker 373*6777b538SAndroid Build Coastguard Worker } // namespace base 374*6777b538SAndroid Build Coastguard Worker 375*6777b538SAndroid Build Coastguard Worker #endif // BASE_PROCESS_PROCESS_H_ 376