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"
10 #include "base/bind.h"
11 #include "base/file_version_info.h"
12 #include "base/files/file_path.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/win/scoped_handle.h"
16 #include "base/win/windows_version.h"
17 #include "chrome/common/chrome_version_info.h"
18 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/common/process_type.h"
21 #include "grit/chromium_strings.h"
22 #include "ui/base/l10n/l10n_util.h"
24 using content::BrowserThread
;
26 // Known browsers which we collect details for.
39 MemoryDetails::MemoryDetails()
40 : user_metrics_mode_(UPDATE_USER_METRICS
) {
41 static const std::wstring google_browser_name
=
42 base::UTF16ToWide(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME
));
45 const wchar_t* process_name
;
46 } process_template
[MAX_BROWSERS
] = {
47 { google_browser_name
.c_str(), L
"chrome.exe", },
48 { google_browser_name
.c_str(), L
"nacl64.exe", },
49 { L
"IE", L
"iexplore.exe", },
50 { L
"Firefox", L
"firefox.exe", },
51 { L
"Opera", L
"opera.exe", },
52 { L
"Safari", L
"safari.exe", },
53 { L
"IE (64bit)", L
"iexplore.exe", },
54 { L
"Konqueror", L
"konqueror.exe", },
57 for (int index
= 0; index
< MAX_BROWSERS
; ++index
) {
59 process
.name
= process_template
[index
].name
;
60 process
.process_name
= process_template
[index
].process_name
;
61 process_data_
.push_back(process
);
65 ProcessData
* MemoryDetails::ChromeBrowser() {
66 return &process_data_
[CHROME_BROWSER
];
69 void MemoryDetails::CollectProcessData(
70 const std::vector
<ProcessMemoryInformation
>& child_info
) {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
74 for (unsigned int index
= 0; index
< process_data_
.size(); index
++)
75 process_data_
[index
].processes
.clear();
77 base::win::OSInfo::WindowsArchitecture windows_architecture
=
78 base::win::OSInfo::GetInstance()->architecture();
80 base::win::ScopedHandle
snapshot(
81 ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS
, 0));
82 PROCESSENTRY32 process_entry
= {sizeof(PROCESSENTRY32
)};
83 if (!snapshot
.Get()) {
84 LOG(ERROR
) << "CreateToolhelp32Snaphot failed: " << GetLastError();
87 if (!::Process32First(snapshot
, &process_entry
)) {
88 LOG(ERROR
) << "Process32First failed: " << GetLastError();
92 base::ProcessId pid
= process_entry
.th32ProcessID
;
93 base::win::ScopedHandle
process_handle(::OpenProcess(
94 PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
, FALSE
, pid
));
95 if (!process_handle
.Get())
97 bool is_64bit_process
=
98 ((windows_architecture
== base::win::OSInfo::X64_ARCHITECTURE
) ||
99 (windows_architecture
== base::win::OSInfo::IA64_ARCHITECTURE
)) &&
100 (base::win::OSInfo::GetWOW64StatusForProcess(process_handle
) ==
101 base::win::OSInfo::WOW64_DISABLED
);
102 for (unsigned int index2
= 0; index2
< process_data_
.size(); index2
++) {
103 if (_wcsicmp(process_data_
[index2
].process_name
.c_str(),
104 process_entry
.szExeFile
) != 0)
106 if (index2
== IE_BROWSER
&& is_64bit_process
)
107 continue; // Should use IE_64BIT_BROWSER
108 // Get Memory Information.
109 ProcessMemoryInformation info
;
111 if (info
.pid
== GetCurrentProcessId())
112 info
.process_type
= content::PROCESS_TYPE_BROWSER
;
114 info
.process_type
= content::PROCESS_TYPE_UNKNOWN
;
116 scoped_ptr
<base::ProcessMetrics
> metrics
;
117 metrics
.reset(base::ProcessMetrics::CreateProcessMetrics(process_handle
));
118 metrics
->GetCommittedKBytes(&info
.committed
);
119 metrics
->GetWorkingSetKBytes(&info
.working_set
);
121 // Get Version Information.
122 TCHAR name
[MAX_PATH
];
123 if (index2
== CHROME_BROWSER
|| index2
== CHROME_NACL_PROCESS
) {
124 chrome::VersionInfo version_info
;
125 if (version_info
.is_valid())
126 info
.version
= base::ASCIIToWide(version_info
.Version());
127 // Check if this is one of the child processes whose data we collected
128 // on the IO thread, and if so copy over that data.
129 for (size_t child
= 0; child
< child_info
.size(); child
++) {
130 if (child_info
[child
].pid
!= info
.pid
)
132 info
.titles
= child_info
[child
].titles
;
133 info
.process_type
= child_info
[child
].process_type
;
136 } else if (GetModuleFileNameEx(process_handle
, NULL
, name
,
138 std::wstring
str_name(name
);
139 scoped_ptr
<FileVersionInfo
> version_info(
140 FileVersionInfo::CreateFileVersionInfo(base::FilePath(str_name
)));
141 if (version_info
!= NULL
) {
142 info
.version
= version_info
->product_version();
143 info
.product_name
= version_info
->product_name();
147 // Add the process info to our list.
148 if (index2
== CHROME_NACL_PROCESS
) {
149 // Add NaCl processes to Chrome's list
150 process_data_
[CHROME_BROWSER
].processes
.push_back(info
);
152 process_data_
[index2
].processes
.push_back(info
);
156 } while (::Process32Next(snapshot
, &process_entry
));
158 // Finally return to the browser thread.
159 BrowserThread::PostTask(
160 BrowserThread::UI
, FROM_HERE
,
161 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread
, this));