// Copyright 2013 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // This file contains methods to iterate over processes on the system. #ifndef BASE_PROCESS_PROCESS_ITERATOR_H_ #define BASE_PROCESS_PROCESS_ITERATOR_H_ #include <stddef.h> #include <list> #include <string> #include <vector> #include "base/base_export.h" #include "base/files/file_path.h" #include "base/memory/raw_ptr.h" #include "base/process/process.h" #include "base/strings/string_util.h" #include "build/build_config.h" #if BUILDFLAG(IS_WIN) #include <windows.h> #include <tlhelp32.h> #elif BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_OPENBSD) #include <sys/sysctl.h> #elif BUILDFLAG(IS_FREEBSD) #include <sys/user.h> #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) #include <dirent.h> #endif namespace base { #if BUILDFLAG(IS_WIN) struct ProcessEntry : public PROCESSENTRY32 { ProcessId pid() const { return th32ProcessID; } ProcessId parent_pid() const { return th32ParentProcessID; } const wchar_t* exe_file() const { return szExeFile; } }; #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) struct BASE_EXPORT ProcessEntry { … }; #endif // BUILDFLAG(IS_WIN) // Used to filter processes by process ID. class ProcessFilter { … }; // This class provides a way to iterate through a list of processes on the // current machine with a specified filter. // To use, create an instance and then call NextProcessEntry() until it returns // false. class BASE_EXPORT ProcessIterator { … }; // This class provides a way to iterate through the list of processes // on the current machine that were started from the given executable // name. To use, create an instance and then call NextProcessEntry() // until it returns false. // If `use_prefix_match` is true, this iterates all processes that // begin with `executable_name`; for example, "Google Chrome Helper" would // match "Google Chrome Helper", "Google Chrome Helper (Renderer)" and // "Google Chrome Helper (GPU)" if `use_prefix_match` is true and otherwise // only "Google Chrome Helper". This option is only implemented on Mac. class BASE_EXPORT NamedProcessIterator : public ProcessIterator { … }; // Returns the number of processes on the machine that are running from the // given executable name. If filter is non-null, then only processes selected // by the filter will be counted. BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name, const ProcessFilter* filter); } // namespace base #endif // BASE_PROCESS_PROCESS_ITERATOR_H_