This source file includes following definitions.
- GetRunningBrowserExecutableName
- GetRunningHelperExecutableNames
- TerminateAllChromeProcesses
- Includes
- GetRunningChromeProcesses
- GetPagefileUsage
- GetWorkingSetSize
#include "chrome/test/base/chrome_process_util.h"
#include <set>
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/process/kill.h"
#include "base/process/process_iterator.h"
#include "base/time/time.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/test/base/test_switches.h"
#include "content/public/common/result_codes.h"
using base::TimeDelta;
using base::TimeTicks;
namespace {
const base::FilePath::CharType* GetRunningBrowserExecutableName() {
const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
if (cmd_line->HasSwitch(switches::kEnableChromiumBranding))
return chrome::kBrowserProcessExecutableNameChromium;
return chrome::kBrowserProcessExecutableName;
}
std::vector<base::FilePath::StringType> GetRunningHelperExecutableNames() {
base::FilePath::StringType name;
const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
if (cmd_line->HasSwitch(switches::kEnableChromiumBranding)) {
name = chrome::kHelperProcessExecutableNameChromium;
} else {
name = chrome::kHelperProcessExecutableName;
}
std::vector<base::FilePath::StringType> names;
names.push_back(name);
#if defined(OS_MACOSX)
for (const char* const* suffix = chrome::kHelperFlavorSuffixes;
*suffix;
++suffix) {
std::string flavor_name(name);
flavor_name.append(1, ' ');
flavor_name.append(*suffix);
names.push_back(flavor_name);
}
#endif
return names;
}
}
void TerminateAllChromeProcesses(const ChromeProcessList& process_pids) {
ChromeProcessList::const_iterator it;
for (it = process_pids.begin(); it != process_pids.end(); ++it) {
base::ProcessHandle handle;
if (!base::OpenProcessHandle(*it, &handle)) {
continue;
}
base::KillProcess(handle, content::RESULT_CODE_KILLED, true);
base::CloseProcessHandle(handle);
}
}
class ChildProcessFilter : public base::ProcessFilter {
public:
explicit ChildProcessFilter(base::ProcessId parent_pid)
: parent_pids_(&parent_pid, (&parent_pid) + 1) {}
explicit ChildProcessFilter(const std::vector<base::ProcessId>& parent_pids)
: parent_pids_(parent_pids.begin(), parent_pids.end()) {}
virtual bool Includes(const base::ProcessEntry& entry) const OVERRIDE {
return parent_pids_.find(entry.parent_pid()) != parent_pids_.end();
}
private:
const std::set<base::ProcessId> parent_pids_;
DISALLOW_COPY_AND_ASSIGN(ChildProcessFilter);
};
ChromeProcessList GetRunningChromeProcesses(base::ProcessId browser_pid) {
const base::FilePath::CharType* executable_name =
GetRunningBrowserExecutableName();
ChromeProcessList result;
if (browser_pid == static_cast<base::ProcessId>(-1))
return result;
ChildProcessFilter filter(browser_pid);
base::NamedProcessIterator it(executable_name, &filter);
while (const base::ProcessEntry* process_entry = it.NextProcessEntry()) {
result.push_back(process_entry->pid());
}
#if defined(OS_POSIX) && !defined(OS_MACOSX)
{
ChildProcessFilter filter(result);
base::NamedProcessIterator it(executable_name, &filter);
while (const base::ProcessEntry* process_entry = it.NextProcessEntry())
result.push_back(process_entry->pid());
}
#endif
#if defined(OS_POSIX)
{
std::vector<base::FilePath::StringType> names =
GetRunningHelperExecutableNames();
for (size_t i = 0; i < names.size(); ++i) {
base::FilePath::StringType name = names[i];
ChildProcessFilter filter(browser_pid);
base::NamedProcessIterator it(name, &filter);
while (const base::ProcessEntry* process_entry = it.NextProcessEntry())
result.push_back(process_entry->pid());
}
}
#endif
result.push_back(browser_pid);
return result;
}
#if !defined(OS_MACOSX)
size_t ChromeTestProcessMetrics::GetPagefileUsage() {
return process_metrics_->GetPagefileUsage();
}
size_t ChromeTestProcessMetrics::GetWorkingSetSize() {
return process_metrics_->GetWorkingSetSize();
}
#endif
ChromeTestProcessMetrics::~ChromeTestProcessMetrics() {}
ChromeTestProcessMetrics::ChromeTestProcessMetrics(
base::ProcessHandle process) {
#if !defined(OS_MACOSX)
process_metrics_.reset(
base::ProcessMetrics::CreateProcessMetrics(process));
#else
process_metrics_.reset(
base::ProcessMetrics::CreateProcessMetrics(process, NULL));
#endif
process_handle_ = process;
}