summaryrefslogtreecommitdiffstats
path: root/chrome/test/base/chrome_process_util.cc
blob: b513c813d83a825f1abf2d19a725f9938f63ae1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/base/chrome_process_util.h"

#include <set>
#include <string>
#include <vector>

#include "base/command_line.h"
#include "base/process_util.h"
#include "base/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;

void TerminateAllChromeProcesses(base::ProcessId browser_pid) {
  ChromeProcessList process_pids(GetRunningChromeProcesses(browser_pid));

  ChromeProcessList::const_iterator it;
  for (it = process_pids.begin(); it != process_pids.end(); ++it) {
    base::ProcessHandle handle;
    if (!base::OpenPrivilegedProcessHandle(*it, &handle)) {
      // Ignore processes for which we can't open the handle. We don't
      // guarantee that all processes will terminate, only try to do so.
      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(std::vector<base::ProcessId> parent_pids)
      : parent_pids_(parent_pids.begin(), parent_pids.end()) {}

  virtual bool Includes(const base::ProcessEntry& entry) const {
    return parent_pids_.find(entry.parent_pid()) != parent_pids_.end();
  }

 private:
  const std::set<base::ProcessId> parent_pids_;

  DISALLOW_COPY_AND_ASSIGN(ChildProcessFilter);
};

const FilePath::CharType* GetRunningBrowserExecutableName() {
  const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
  if (cmd_line->HasSwitch(switches::kEnableChromiumBranding))
    return chrome::kBrowserProcessExecutableNameChromium;
  return chrome::kBrowserProcessExecutableName;
}

std::vector<FilePath::StringType> GetRunningHelperExecutableNames() {
  FilePath::StringType name;
  const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
  if (cmd_line->HasSwitch(switches::kEnableChromiumBranding)) {
    name = chrome::kHelperProcessExecutableNameChromium;
  } else {
    name = chrome::kHelperProcessExecutableName;
  }

  std::vector<FilePath::StringType> names;
  names.push_back(name);

#if defined(OS_MACOSX)
  // The helper might show up as these different flavors depending on the
  // executable flags required.
  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;
}

ChromeProcessList GetRunningChromeProcesses(base::ProcessId browser_pid) {
  const 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)
  // On Unix we might be running with a zygote process for the renderers.
  // Because of that we sweep the list of processes again and pick those which
  // are children of one of the processes that we've already seen.
  {
    ChildProcessFilter filter(result);
    base::NamedProcessIterator it(executable_name, &filter);
    while (const base::ProcessEntry* process_entry = it.NextProcessEntry())
      result.push_back(process_entry->pid());
  }
#endif  // defined(OS_POSIX) && !defined(OS_MACOSX)

#if defined(OS_POSIX)
  // On Mac OS X we run the subprocesses with a different bundle, and
  // on Linux via /proc/self/exe, so they end up with a different
  // name.  We must collect them in a second pass.
  {
    std::vector<FilePath::StringType> names = GetRunningHelperExecutableNames();
    for (size_t i = 0; i < names.size(); ++i) {
      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  // defined(OS_POSIX)

  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  // !defined(OS_MACOSX)

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;
}