| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
| [email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 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_iterator.h" |
| 6 | |
| Tom Sepez | 978847c | 2025-03-22 03:43:59 | [diff] [blame] | 7 | #include "base/compiler_specific.h" |
| jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 8 | #include "base/strings/string_util.h" |
| 9 | |
| [email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 10 | namespace base { |
| 11 | |
| 12 | ProcessIterator::ProcessIterator(const ProcessFilter* filter) |
| Lei Zhang | 6f418e0 | 2023-02-08 21:55:43 | [diff] [blame] | 13 | : snapshot_(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)), |
| 14 | filter_(filter) {} |
| [email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 15 | |
| 16 | ProcessIterator::~ProcessIterator() { |
| 17 | CloseHandle(snapshot_); |
| 18 | } |
| 19 | |
| 20 | bool ProcessIterator::CheckForNextProcess() { |
| 21 | InitProcessEntry(&entry_); |
| 22 | |
| 23 | if (!started_iteration_) { |
| 24 | started_iteration_ = true; |
| 25 | return !!Process32First(snapshot_, &entry_); |
| 26 | } |
| 27 | |
| 28 | return !!Process32Next(snapshot_, &entry_); |
| 29 | } |
| 30 | |
| 31 | void ProcessIterator::InitProcessEntry(ProcessEntry* entry) { |
| Tom Sepez | 978847c | 2025-03-22 03:43:59 | [diff] [blame] | 32 | UNSAFE_TODO(memset(entry, 0, sizeof(*entry))); |
| [email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 33 | entry->dwSize = sizeof(*entry); |
| 34 | } |
| 35 | |
| 36 | bool NamedProcessIterator::IncludeEntry() { |
| 37 | // Case insensitive. |
| Tom Sepez | 978847c | 2025-03-22 03:43:59 | [diff] [blame] | 38 | return UNSAFE_TODO(!_wcsicmp(executable_name_.c_str(), entry().exe_file())) && |
| [email protected] | d3acb67 | 2013-05-31 15:38:01 | [diff] [blame] | 39 | ProcessIterator::IncludeEntry(); |
| 40 | } |
| 41 | |
| 42 | } // namespace base |