1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/process/process_iterator.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace base {
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)ProcessIterator::ProcessIterator(const ProcessFilter* filter)
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : started_iteration_(false),
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      filter_(filter) {
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)ProcessIterator::~ProcessIterator() {
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CloseHandle(snapshot_);
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool ProcessIterator::CheckForNextProcess() {
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  InitProcessEntry(&entry_);
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!started_iteration_) {
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    started_iteration_ = true;
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return !!Process32First(snapshot_, &entry_);
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return !!Process32Next(snapshot_, &entry_);
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ProcessIterator::InitProcessEntry(ProcessEntry* entry) {
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  memset(entry, 0, sizeof(*entry));
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  entry->dwSize = sizeof(*entry);
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool NamedProcessIterator::IncludeEntry() {
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Case insensitive.
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return _wcsicmp(executable_name_.c_str(), entry().exe_file()) == 0 &&
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)         ProcessIterator::IncludeEntry();
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace base
42