Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / base / process / process_iterator.cc
blobb9ef047732fe578958bd62743b63c9014e2731d7
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 #include "base/process/process_iterator.h"
7 namespace base {
9 #if defined(OS_POSIX)
10 ProcessEntry::ProcessEntry() : pid_(0), ppid_(0), gid_(0) {}
11 ProcessEntry::~ProcessEntry() {}
12 #endif
14 const ProcessEntry* ProcessIterator::NextProcessEntry() {
15 bool result = false;
16 do {
17 result = CheckForNextProcess();
18 } while (result && !IncludeEntry());
19 if (result)
20 return &entry_;
21 return NULL;
24 ProcessIterator::ProcessEntries ProcessIterator::Snapshot() {
25 ProcessEntries found;
26 while (const ProcessEntry* process_entry = NextProcessEntry()) {
27 found.push_back(*process_entry);
29 return found;
32 bool ProcessIterator::IncludeEntry() {
33 return !filter_ || filter_->Includes(entry_);
36 NamedProcessIterator::NamedProcessIterator(
37 const FilePath::StringType& executable_name,
38 const ProcessFilter* filter) : ProcessIterator(filter),
39 executable_name_(executable_name) {
40 #if defined(OS_ANDROID)
41 // On Android, the process name contains only the last 15 characters, which
42 // is in file /proc/<pid>/stat, the string between open parenthesis and close
43 // parenthesis. Please See ProcessIterator::CheckForNextProcess for details.
44 // Now if the length of input process name is greater than 15, only save the
45 // last 15 characters.
46 if (executable_name_.size() > 15) {
47 executable_name_ = FilePath::StringType(executable_name_,
48 executable_name_.size() - 15, 15);
50 #endif
53 NamedProcessIterator::~NamedProcessIterator() {
56 int GetProcessCount(const FilePath::StringType& executable_name,
57 const ProcessFilter* filter) {
58 int count = 0;
59 NamedProcessIterator iter(executable_name, filter);
60 while (iter.NextProcessEntry())
61 ++count;
62 return count;
65 } // namespace base