[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/memory_details.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 6 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 7 | #include <psapi.h> |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 9 | #include <TlHelp32.h> |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 10 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 13 | #include "base/bind.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 14 | #include "base/file_version_info.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 16 | #include "base/path_service.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 17 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
fdoray | e9c201b | 2017-05-02 19:49:25 | [diff] [blame] | 19 | #include "base/threading/thread_restrictions.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 20 | #include "base/win/scoped_handle.h" |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 21 | #include "base/win/windows_version.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 22 | #include "chrome/common/url_constants.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 23 | #include "chrome/grit/chromium_strings.h" |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 24 | #include "components/version_info/version_info.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 25 | #include "content/public/browser/browser_thread.h" |
[email protected] | bd5d6cf | 2011-12-01 00:39:12 | [diff] [blame] | 26 | #include "content/public/common/process_type.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 27 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 28 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 29 | using content::BrowserThread; |
| 30 | |
asvitkine | 89406d1f | 2015-01-17 06:57:10 | [diff] [blame] | 31 | MemoryDetails::MemoryDetails() { |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 32 | base::FilePath browser_process_path; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 33 | base::PathService::Get(base::FILE_EXE, &browser_process_path); |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 34 | |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 35 | ProcessData process; |
| 36 | process.name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| 37 | process.process_name = browser_process_path.BaseName().value(); |
| 38 | process_data_.push_back(process); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | ProcessData* MemoryDetails::ChromeBrowser() { |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 42 | return &process_data_[0]; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void MemoryDetails::CollectProcessData( |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 46 | const std::vector<ProcessMemoryInformation>& child_info) { |
Francois Doray | 66bdfd8 | 2017-10-20 13:50:37 | [diff] [blame] | 47 | base::AssertBlockingAllowed(); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 48 | |
| 49 | // Clear old data. |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 50 | process_data_[0].processes.clear(); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 51 | |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 52 | base::win::ScopedHandle snapshot( |
| 53 | ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 54 | PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)}; |
| 55 | if (!snapshot.Get()) { |
jsbell | 3785ea0 | 2016-07-29 16:50:03 | [diff] [blame] | 56 | LOG(ERROR) << "CreateToolhelp32Snapshot failed: " << GetLastError(); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 57 | return; |
| 58 | } |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 59 | if (!::Process32First(snapshot.Get(), &process_entry)) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 60 | LOG(ERROR) << "Process32First failed: " << GetLastError(); |
| 61 | return; |
| 62 | } |
| 63 | do { |
[email protected] | a4dc33f | 2009-10-20 15:09:55 | [diff] [blame] | 64 | base::ProcessId pid = process_entry.th32ProcessID; |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 65 | base::win::ScopedHandle process_handle(::OpenProcess( |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 66 | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid)); |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 67 | if (!process_handle.IsValid()) |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 68 | continue; |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 69 | if (_wcsicmp(process_data_[0].process_name.c_str(), |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 70 | process_entry.szExeFile) != 0) { |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 71 | continue; |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 72 | } |
| 73 | |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 74 | // Get Memory Information. |
| 75 | ProcessMemoryInformation info; |
| 76 | info.pid = pid; |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 77 | info.process_type = pid == GetCurrentProcessId() |
| 78 | ? content::PROCESS_TYPE_BROWSER |
| 79 | : content::PROCESS_TYPE_UNKNOWN; |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 80 | |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 81 | // Get Version Information. |
| 82 | info.version = base::ASCIIToUTF16(version_info::GetVersionNumber()); |
| 83 | // Check if this is one of the child processes whose data we collected |
| 84 | // on the IO thread, and if so copy over that data. |
thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 85 | for (const ProcessMemoryInformation& child : child_info) { |
| 86 | if (child.pid == info.pid) { |
| 87 | info.titles = child.titles; |
| 88 | info.process_type = child.process_type; |
| 89 | break; |
| 90 | } |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 91 | } |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 92 | |
| 93 | // Add the process info to our list. |
| 94 | process_data_[0].processes.push_back(info); |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 95 | } while (::Process32Next(snapshot.Get(), &process_entry)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 96 | |
| 97 | // Finally return to the browser thread. |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 98 | BrowserThread::PostTask( |
| 99 | BrowserThread::UI, FROM_HERE, |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 100 | base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 101 | } |