Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / process / process_iterator.h
blobec6500e653cfbf2e6ff3240a9ea59857c7d1e64c
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file contains methods to iterate over processes on the system.
7 #ifndef BASE_PROCESS_PROCESS_ITERATOR_H_
8 #define BASE_PROCESS_PROCESS_ITERATOR_H_
10 #include <list>
11 #include <string>
12 #include <vector>
14 #include "base/base_export.h"
15 #include "base/basictypes.h"
16 #include "base/files/file_path.h"
17 #include "base/process/process.h"
18 #include "build/build_config.h"
20 #if defined(OS_WIN)
21 #include <windows.h>
22 #include <tlhelp32.h>
23 #elif defined(OS_MACOSX) || defined(OS_OPENBSD)
24 #include <sys/sysctl.h>
25 #elif defined(OS_FREEBSD)
26 #include <sys/user.h>
27 #elif defined(OS_POSIX)
28 #include <dirent.h>
29 #endif
31 namespace base {
33 #if defined(OS_WIN)
34 struct ProcessEntry : public PROCESSENTRY32 {
35 ProcessId pid() const { return th32ProcessID; }
36 ProcessId parent_pid() const { return th32ParentProcessID; }
37 const wchar_t* exe_file() const { return szExeFile; }
39 #elif defined(OS_POSIX)
40 struct BASE_EXPORT ProcessEntry {
41 ProcessEntry();
42 ~ProcessEntry();
44 ProcessId pid() const { return pid_; }
45 ProcessId parent_pid() const { return ppid_; }
46 ProcessId gid() const { return gid_; }
47 const char* exe_file() const { return exe_file_.c_str(); }
48 const std::vector<std::string>& cmd_line_args() const {
49 return cmd_line_args_;
52 ProcessId pid_;
53 ProcessId ppid_;
54 ProcessId gid_;
55 std::string exe_file_;
56 std::vector<std::string> cmd_line_args_;
58 #endif // defined(OS_POSIX)
60 // Used to filter processes by process ID.
61 class ProcessFilter {
62 public:
63 // Returns true to indicate set-inclusion and false otherwise. This method
64 // should not have side-effects and should be idempotent.
65 virtual bool Includes(const ProcessEntry& entry) const = 0;
67 protected:
68 virtual ~ProcessFilter() {}
71 // This class provides a way to iterate through a list of processes on the
72 // current machine with a specified filter.
73 // To use, create an instance and then call NextProcessEntry() until it returns
74 // false.
75 class BASE_EXPORT ProcessIterator {
76 public:
77 typedef std::list<ProcessEntry> ProcessEntries;
79 explicit ProcessIterator(const ProcessFilter* filter);
80 virtual ~ProcessIterator();
82 // If there's another process that matches the given executable name,
83 // returns a const pointer to the corresponding PROCESSENTRY32.
84 // If there are no more matching processes, returns NULL.
85 // The returned pointer will remain valid until NextProcessEntry()
86 // is called again or this NamedProcessIterator goes out of scope.
87 const ProcessEntry* NextProcessEntry();
89 // Takes a snapshot of all the ProcessEntry found.
90 ProcessEntries Snapshot();
92 protected:
93 virtual bool IncludeEntry();
94 const ProcessEntry& entry() { return entry_; }
96 private:
97 // Determines whether there's another process (regardless of executable)
98 // left in the list of all processes. Returns true and sets entry_ to
99 // that process's info if there is one, false otherwise.
100 bool CheckForNextProcess();
102 // Initializes a PROCESSENTRY32 data structure so that it's ready for
103 // use with Process32First/Process32Next.
104 void InitProcessEntry(ProcessEntry* entry);
106 #if defined(OS_WIN)
107 HANDLE snapshot_;
108 bool started_iteration_;
109 #elif defined(OS_MACOSX) || defined(OS_BSD)
110 std::vector<kinfo_proc> kinfo_procs_;
111 size_t index_of_kinfo_proc_;
112 #elif defined(OS_POSIX)
113 DIR* procfs_dir_;
114 #endif
115 ProcessEntry entry_;
116 const ProcessFilter* filter_;
118 DISALLOW_COPY_AND_ASSIGN(ProcessIterator);
121 // This class provides a way to iterate through the list of processes
122 // on the current machine that were started from the given executable
123 // name. To use, create an instance and then call NextProcessEntry()
124 // until it returns false.
125 class BASE_EXPORT NamedProcessIterator : public ProcessIterator {
126 public:
127 NamedProcessIterator(const FilePath::StringType& executable_name,
128 const ProcessFilter* filter);
129 ~NamedProcessIterator() override;
131 protected:
132 bool IncludeEntry() override;
134 private:
135 FilePath::StringType executable_name_;
137 DISALLOW_COPY_AND_ASSIGN(NamedProcessIterator);
140 // Returns the number of processes on the machine that are running from the
141 // given executable name. If filter is non-null, then only processes selected
142 // by the filter will be counted.
143 BASE_EXPORT int GetProcessCount(const FilePath::StringType& executable_name,
144 const ProcessFilter* filter);
146 } // namespace base
148 #endif // BASE_PROCESS_PROCESS_ITERATOR_H_