Fix style nits in gdata directory.
[chromium-blink-merge.git] / base / process_util.cc
blob9979dfa07b1b06ee9d8aca814da04907200f077d
1 // Copyright (c) 2011 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 #include "base/process_util.h"
7 namespace base {
9 #if defined(OS_POSIX)
10 ProcessEntry::ProcessEntry() : pid_(0), ppid_(0), gid_(0) {}
11 ProcessEntry::~ProcessEntry() {}
12 #endif
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;
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
34 return result;
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;
47 ProcessIterator::ProcessEntries ProcessIterator::Snapshot() {
48 ProcessEntries found;
49 while (const ProcessEntry* process_entry = NextProcessEntry()) {
50 found.push_back(*process_entry);
52 return found;
55 bool ProcessIterator::IncludeEntry() {
56 return !filter_ || filter_->Includes(entry_);
59 NamedProcessIterator::NamedProcessIterator(
60 const FilePath::StringType& executable_name,
61 const ProcessFilter* filter) : ProcessIterator(filter),
62 executable_name_(executable_name) {
63 #if defined(OS_ANDROID)
64 // On Android, the process name contains only the last 15 characters, which
65 // is in file /proc/<pid>/stat, the string between open parenthesis and close
66 // parenthesis. Please See ProcessIterator::CheckForNextProcess for details.
67 // Now if the length of input process name is greater than 15, only save the
68 // last 15 characters.
69 if (executable_name_.size() > 15) {
70 executable_name_ = FilePath::StringType(executable_name_,
71 executable_name_.size() - 15, 15);
73 #endif
76 NamedProcessIterator::~NamedProcessIterator() {
79 } // namespace base