• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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.
4 
5 #include "base/process_util.h"
6 
7 namespace base {
8 
9 #if defined(OS_POSIX)
ProcessEntry()10 ProcessEntry::ProcessEntry() {}
~ProcessEntry()11 ProcessEntry::~ProcessEntry() {}
12 #endif
13 
GetProcessCount(const FilePath::StringType & executable_name,const ProcessFilter * filter)14 int GetProcessCount(const FilePath::StringType& executable_name,
15                     const ProcessFilter* filter) {
16   int count = 0;
17   NamedProcessIterator iter(executable_name, filter);
18   while (iter.NextProcessEntry())
19     ++count;
20   return count;
21 }
22 
KillProcesses(const FilePath::StringType & executable_name,int exit_code,const ProcessFilter * filter)23 bool KillProcesses(const FilePath::StringType& executable_name, int exit_code,
24                    const ProcessFilter* filter) {
25   bool result = true;
26   NamedProcessIterator iter(executable_name, filter);
27   while (const ProcessEntry* entry = iter.NextProcessEntry()) {
28 #if defined(OS_WIN)
29     result &= KillProcessById(entry->pid(), exit_code, true);
30 #else
31     result &= KillProcess(entry->pid(), exit_code, true);
32 #endif
33   }
34   return result;
35 }
36 
NextProcessEntry()37 const ProcessEntry* ProcessIterator::NextProcessEntry() {
38   bool result = false;
39   do {
40     result = CheckForNextProcess();
41   } while (result && !IncludeEntry());
42   if (result)
43     return &entry_;
44   return NULL;
45 }
46 
Snapshot()47 ProcessIterator::ProcessEntries ProcessIterator::Snapshot() {
48   ProcessEntries found;
49   while (const ProcessEntry* process_entry = NextProcessEntry()) {
50     found.push_back(*process_entry);
51   }
52   return found;
53 }
54 
IncludeEntry()55 bool ProcessIterator::IncludeEntry() {
56   return !filter_ || filter_->Includes(entry_);
57 }
58 
NamedProcessIterator(const FilePath::StringType & executable_name,const ProcessFilter * filter)59 NamedProcessIterator::NamedProcessIterator(
60     const FilePath::StringType& executable_name,
61     const ProcessFilter* filter) : ProcessIterator(filter),
62                                    executable_name_(executable_name) {
63 }
64 
~NamedProcessIterator()65 NamedProcessIterator::~NamedProcessIterator() {
66 }
67 
68 }  // namespace base
69