Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chrome / test / base / chrome_process_util.cc
blob201b6802e4a64bd19115d5d957b8c2cbe15cc87d
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 "chrome/test/base/chrome_process_util.h"
7 #include <set>
8 #include <string>
9 #include <vector>
11 #include "base/command_line.h"
12 #include "base/process/process.h"
13 #include "base/process/process_iterator.h"
14 #include "base/time/time.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/test/base/test_switches.h"
17 #include "content/public/common/result_codes.h"
19 using base::TimeDelta;
20 using base::TimeTicks;
22 namespace {
24 #if defined(OS_POSIX)
25 // Returns the executable name of the current Chrome helper process.
26 std::vector<base::FilePath::StringType> GetRunningHelperExecutableNames() {
27 base::FilePath::StringType name = chrome::kHelperProcessExecutableName;
29 std::vector<base::FilePath::StringType> names;
30 names.push_back(name);
32 #if defined(OS_MACOSX)
33 // The helper might show up as these different flavors depending on the
34 // executable flags required.
35 for (const char* const* suffix = chrome::kHelperFlavorSuffixes;
36 *suffix;
37 ++suffix) {
38 std::string flavor_name(name);
39 flavor_name.append(1, ' ');
40 flavor_name.append(*suffix);
41 names.push_back(flavor_name);
43 #endif
45 return names;
47 #endif // defined(OS_POSIX)
49 } // namespace
51 void TerminateAllChromeProcesses(const ChromeProcessList& process_pids) {
52 ChromeProcessList::const_iterator it;
53 for (it = process_pids.begin(); it != process_pids.end(); ++it) {
54 base::Process process = base::Process::Open(*it);
55 if (process.IsValid()) {
56 // Ignore processes for which we can't open the handle. We don't
57 // guarantee that all processes will terminate, only try to do so.
58 process.Terminate(content::RESULT_CODE_KILLED, true);
63 class ChildProcessFilter : public base::ProcessFilter {
64 public:
65 explicit ChildProcessFilter(base::ProcessId parent_pid)
66 : parent_pids_(&parent_pid, (&parent_pid) + 1) {}
68 explicit ChildProcessFilter(const std::vector<base::ProcessId>& parent_pids)
69 : parent_pids_(parent_pids.begin(), parent_pids.end()) {}
71 bool Includes(const base::ProcessEntry& entry) const override {
72 return parent_pids_.find(entry.parent_pid()) != parent_pids_.end();
75 private:
76 const std::set<base::ProcessId> parent_pids_;
78 DISALLOW_COPY_AND_ASSIGN(ChildProcessFilter);
81 ChromeProcessList GetRunningChromeProcesses(base::ProcessId browser_pid) {
82 const base::FilePath::CharType* executable_name =
83 chrome::kBrowserProcessExecutableName;
84 ChromeProcessList result;
85 if (browser_pid == static_cast<base::ProcessId>(-1))
86 return result;
88 ChildProcessFilter filter(browser_pid);
89 base::NamedProcessIterator it(executable_name, &filter);
90 while (const base::ProcessEntry* process_entry = it.NextProcessEntry()) {
91 result.push_back(process_entry->pid());
94 #if defined(OS_POSIX) && !defined(OS_MACOSX)
95 // On Unix we might be running with a zygote process for the renderers.
96 // Because of that we sweep the list of processes again and pick those which
97 // are children of one of the processes that we've already seen.
99 ChildProcessFilter filter(result);
100 base::NamedProcessIterator it(executable_name, &filter);
101 while (const base::ProcessEntry* process_entry = it.NextProcessEntry())
102 result.push_back(process_entry->pid());
104 #endif // defined(OS_POSIX) && !defined(OS_MACOSX)
106 #if defined(OS_POSIX)
107 // On Mac OS X we run the subprocesses with a different bundle, and
108 // on Linux via /proc/self/exe, so they end up with a different
109 // name. We must collect them in a second pass.
111 std::vector<base::FilePath::StringType> names =
112 GetRunningHelperExecutableNames();
113 for (size_t i = 0; i < names.size(); ++i) {
114 base::FilePath::StringType name = names[i];
115 ChildProcessFilter filter(browser_pid);
116 base::NamedProcessIterator it(name, &filter);
117 while (const base::ProcessEntry* process_entry = it.NextProcessEntry())
118 result.push_back(process_entry->pid());
121 #endif // defined(OS_POSIX)
123 result.push_back(browser_pid);
125 return result;
128 #if !defined(OS_MACOSX)
130 size_t ChromeTestProcessMetrics::GetPagefileUsage() {
131 return process_metrics_->GetPagefileUsage();
134 size_t ChromeTestProcessMetrics::GetWorkingSetSize() {
135 return process_metrics_->GetWorkingSetSize();
138 #endif // !defined(OS_MACOSX)
140 ChromeTestProcessMetrics::~ChromeTestProcessMetrics() {}
142 ChromeTestProcessMetrics::ChromeTestProcessMetrics(
143 base::ProcessHandle process) {
144 #if !defined(OS_MACOSX)
145 process_metrics_.reset(
146 base::ProcessMetrics::CreateProcessMetrics(process));
147 #else
148 process_metrics_.reset(
149 base::ProcessMetrics::CreateProcessMetrics(process, NULL));
150 #endif
151 process_handle_ = process;