Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / memory_details_mac.cc
blob89ae9c1cfb9b3fe3b777cff52f486be5d235756c
1 // Copyright (c) 2012 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/browser/memory_details.h"
7 #include <set>
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/file_version_info.h"
13 #include "base/files/file_path.h"
14 #include "base/mac/foundation_util.h"
15 #include "base/process/process_iterator.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread.h"
19 #include "chrome/browser/process_info_snapshot.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/url_constants.h"
22 #include "chrome/grit/chromium_strings.h"
23 #include "components/version_info/version_info.h"
24 #include "content/public/browser/browser_child_process_host.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/common/process_type.h"
27 #include "ui/base/l10n/l10n_util.h"
29 using content::BrowserThread;
31 // TODO(viettrungluu): Many of the TODOs below are subsumed by a general need to
32 // refactor the about:memory code (not just on Mac, but probably on other
33 // platforms as well). I've filed crbug.com/25456.
35 // Known browsers which we collect details for. |CHROME_BROWSER| *must* be the
36 // first browser listed. The order here must match those in |process_template|
37 // (in |MemoryDetails::MemoryDetails()| below).
38 // TODO(viettrungluu): In the big refactoring (see above), get rid of this order
39 // dependence.
40 enum BrowserType {
41 // TODO(viettrungluu): possibly add more?
42 CHROME_BROWSER = 0,
43 SAFARI_BROWSER,
44 FIREFOX_BROWSER,
45 CAMINO_BROWSER,
46 OPERA_BROWSER,
47 OMNIWEB_BROWSER,
48 MAX_BROWSERS
51 namespace {
53 // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium
54 // process with PID |pid|. The collected data is added to |processes|.
55 void CollectProcessDataForChromeProcess(
56 const std::vector<ProcessMemoryInformation>& child_info,
57 base::ProcessId pid,
58 ProcessMemoryInformationList* processes) {
59 ProcessMemoryInformation info;
60 info.pid = pid;
61 if (info.pid == base::GetCurrentProcId())
62 info.process_type = content::PROCESS_TYPE_BROWSER;
63 else
64 info.process_type = content::PROCESS_TYPE_UNKNOWN;
66 info.product_name = base::ASCIIToUTF16(version_info::GetProductName());
67 info.version = base::ASCIIToUTF16(version_info::GetVersionNumber());
69 // Check if this is one of the child processes whose data was already
70 // collected and exists in |child_data|.
71 for (const ProcessMemoryInformation& child : child_info) {
72 if (child.pid == info.pid) {
73 info.titles = child.titles;
74 info.process_type = child.process_type;
75 break;
79 scoped_ptr<base::ProcessMetrics> metrics;
80 metrics.reset(base::ProcessMetrics::CreateProcessMetrics(
81 pid, content::BrowserChildProcessHost::GetPortProvider()));
82 metrics->GetCommittedAndWorkingSetKBytes(&info.committed, &info.working_set);
84 processes->push_back(info);
87 // Collects memory information from non-Chrome browser processes, using the
88 // ProcessInfoSnapshot helper (which runs an external command - PS or TOP).
89 // Updates |process_data| with the collected information.
90 void CollectProcessDataAboutNonChromeProcesses(
91 const std::vector<base::ProcessId>& all_pids,
92 const std::vector<base::ProcessId> pids_by_browser[MAX_BROWSERS],
93 std::vector<ProcessData>* process_data) {
94 DCHECK_EQ(MAX_BROWSERS, process_data->size());
96 // Capture information about the processes we're interested in.
97 ProcessInfoSnapshot process_info;
98 process_info.Sample(all_pids);
100 // Handle the other processes first.
101 for (size_t index = CHROME_BROWSER + 1; index < MAX_BROWSERS; ++index) {
102 for (const base::ProcessId& pid : pids_by_browser[index]) {
103 ProcessMemoryInformation info;
104 info.pid = pid;
105 info.process_type = content::PROCESS_TYPE_UNKNOWN;
107 // Try to get version information. To do this, we need first to get the
108 // executable's name (we can only believe |proc_info.command| if it looks
109 // like an absolute path). Then we need strip the executable's name back
110 // to the bundle's name. And only then can we try to get the version.
111 scoped_ptr<FileVersionInfo> version_info;
112 ProcessInfoSnapshot::ProcInfoEntry proc_info;
113 if (process_info.GetProcInfo(info.pid, &proc_info)) {
114 if (proc_info.command.length() > 1 && proc_info.command[0] == '/') {
115 base::FilePath bundle_name =
116 base::mac::GetAppBundlePath(base::FilePath(proc_info.command));
117 if (!bundle_name.empty()) {
118 version_info.reset(
119 FileVersionInfo::CreateFileVersionInfo(bundle_name));
123 if (version_info) {
124 info.product_name = version_info->product_name();
125 info.version = version_info->product_version();
126 } else {
127 info.product_name = (*process_data)[index].name;
128 info.version.clear();
131 // Memory info.
132 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed);
133 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set);
135 // Add the process info to our list.
136 (*process_data)[index].processes.push_back(info);
141 } // namespace
143 MemoryDetails::MemoryDetails() {
144 const base::FilePath browser_process_path =
145 base::GetProcessExecutablePath(base::GetCurrentProcessHandle());
146 const std::string browser_process_name =
147 browser_process_path.BaseName().value();
148 const std::string google_browser_name =
149 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME);
151 // (Human and process) names of browsers; should match the ordering for
152 // |BrowserType| enum.
153 // TODO(viettrungluu): The current setup means that we can't detect both
154 // Chrome and Chromium at the same time!
155 // TODO(viettrungluu): Get localized browser names for other browsers
156 // (crbug.com/25779).
157 struct {
158 const char* name;
159 const char* process_name;
160 } process_template[MAX_BROWSERS] = {
161 { google_browser_name.c_str(), browser_process_name.c_str(), },
162 { "Safari", "Safari", },
163 { "Firefox", "firefox-bin", },
164 { "Camino", "Camino", },
165 { "Opera", "Opera", },
166 { "OmniWeb", "OmniWeb", },
169 for (size_t index = 0; index < MAX_BROWSERS; ++index) {
170 ProcessData process;
171 process.name = base::UTF8ToUTF16(process_template[index].name);
172 process.process_name =
173 base::UTF8ToUTF16(process_template[index].process_name);
174 process_data_.push_back(process);
178 ProcessData* MemoryDetails::ChromeBrowser() {
179 return &process_data_[CHROME_BROWSER];
182 void MemoryDetails::CollectProcessData(
183 CollectionMode mode,
184 const std::vector<ProcessMemoryInformation>& child_info) {
185 // This must be run on the blocking pool to avoid jank (|ProcessInfoSnapshot|
186 // runs /bin/ps, which isn't instantaneous).
187 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
189 // Clear old data.
190 for (size_t index = 0; index < MAX_BROWSERS; index++)
191 process_data_[index].processes.clear();
193 // First, we use |NamedProcessIterator| to get the PIDs of the processes we're
194 // interested in; we save our results to avoid extra calls to
195 // |NamedProcessIterator| (for performance reasons) and to avoid additional
196 // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get
197 // information on those PIDs. Then we used our saved information to iterate
198 // over browsers, then over PIDs.
200 // Get PIDs of main browser processes.
201 std::vector<base::ProcessId> pids_by_browser[MAX_BROWSERS];
202 std::vector<base::ProcessId> all_pids;
203 for (size_t index = CHROME_BROWSER; index < MAX_BROWSERS; index++) {
204 base::NamedProcessIterator process_it(
205 base::UTF16ToUTF8(process_data_[index].process_name), NULL);
207 while (const base::ProcessEntry* entry = process_it.NextProcessEntry()) {
208 pids_by_browser[index].push_back(entry->pid());
209 all_pids.push_back(entry->pid());
213 // The helper might show up as these different flavors depending on the
214 // executable flags required.
215 std::vector<std::string> helper_names;
216 helper_names.push_back(chrome::kHelperProcessExecutableName);
217 for (const char* const* suffix = chrome::kHelperFlavorSuffixes;
218 *suffix;
219 ++suffix) {
220 std::string helper_name = chrome::kHelperProcessExecutableName;
221 helper_name.append(1, ' ');
222 helper_name.append(*suffix);
223 helper_names.push_back(helper_name);
226 // Get PIDs of helpers.
227 std::vector<base::ProcessId> helper_pids;
228 for (size_t i = 0; i < helper_names.size(); ++i) {
229 std::string helper_name = helper_names[i];
230 base::NamedProcessIterator helper_it(helper_name, NULL);
231 while (const base::ProcessEntry* entry = helper_it.NextProcessEntry()) {
232 helper_pids.push_back(entry->pid());
233 all_pids.push_back(entry->pid());
237 if (mode == FROM_ALL_BROWSERS) {
238 CollectProcessDataAboutNonChromeProcesses(all_pids, pids_by_browser,
239 &process_data_);
242 ProcessMemoryInformationList* chrome_processes =
243 &process_data_[CHROME_BROWSER].processes;
245 // Collect data about Chrome/Chromium.
246 for (const base::ProcessId& pid : pids_by_browser[CHROME_BROWSER])
247 CollectProcessDataForChromeProcess(child_info, pid, chrome_processes);
249 // And collect data about the helpers.
250 for (const base::ProcessId& pid : helper_pids)
251 CollectProcessDataForChromeProcess(child_info, pid, chrome_processes);
253 // Finally return to the browser thread.
254 BrowserThread::PostTask(
255 BrowserThread::UI, FROM_HERE,
256 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this));