| Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
| [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 | |
| Takuto Ikuta | c8d6b16f | 2024-04-15 16:59:19 | [diff] [blame] | 5 | #include "chrome/browser/memory_details.h" |
| 6 | |
| Erik Chen | 7970a42 | 2018-08-10 18:02:12 | [diff] [blame] | 7 | #include <windows.h> |
| [email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 8 | |
| Takuto Ikuta | c8d6b16f | 2024-04-15 16:59:19 | [diff] [blame] | 9 | #include <TlHelp32.h> |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 10 | #include <psapi.h> |
| avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 11 | #include <stddef.h> |
| Erik Chen | 7970a42 | 2018-08-10 18:02:12 | [diff] [blame] | 12 | |
| dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 15 | #include "base/file_version_info.h" |
| [email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
| Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 17 | #include "base/functional/bind.h" |
| Lei Zhang | 46a1cad | 2022-01-17 10:40:31 | [diff] [blame] | 18 | #include "base/logging.h" |
| asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 19 | #include "base/path_service.h" |
| [email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
| [email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
| Etienne Pierre-doray | 0fbce43 | 2018-08-27 20:27:51 | [diff] [blame] | 22 | #include "base/threading/scoped_blocking_call.h" |
| [email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 23 | #include "base/win/scoped_handle.h" |
| [email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 24 | #include "base/win/windows_version.h" |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 25 | #include "chrome/common/url_constants.h" |
| Henrique Ferreiro | d78ab26 | 2023-09-13 11:21:43 | [diff] [blame] | 26 | #include "chrome/grit/branded_strings.h" |
| sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 27 | #include "components/version_info/version_info.h" |
| Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 28 | #include "content/public/browser/browser_task_traits.h" |
| [email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 29 | #include "content/public/browser/browser_thread.h" |
| [email protected] | bd5d6cf | 2011-12-01 00:39:12 | [diff] [blame] | 30 | #include "content/public/common/process_type.h" |
| [email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 31 | #include "ui/base/l10n/l10n_util.h" |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 32 | |
| asvitkine | 89406d1f | 2015-01-17 06:57:10 | [diff] [blame] | 33 | MemoryDetails::MemoryDetails() { |
| asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 34 | base::FilePath browser_process_path; |
| Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 35 | base::PathService::Get(base::FILE_EXE, &browser_process_path); |
| asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 36 | |
| ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 37 | ProcessData process; |
| 38 | process.name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| Peter Kasting | 5105d61 | 2021-02-09 22:41:42 | [diff] [blame] | 39 | process.process_name = browser_process_path.BaseName().AsUTF16Unsafe(); |
| ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 40 | process_data_.push_back(process); |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ProcessData* MemoryDetails::ChromeBrowser() { |
| ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 44 | return &process_data_[0]; |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void MemoryDetails::CollectProcessData( |
| [email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 48 | const std::vector<ProcessMemoryInformation>& child_info) { |
| Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 49 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 50 | base::BlockingType::MAY_BLOCK); |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 51 | |
| 52 | // Clear old data. |
| ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 53 | process_data_[0].processes.clear(); |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 54 | |
| [email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 55 | base::win::ScopedHandle snapshot( |
| 56 | ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)); |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 57 | PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)}; |
| 58 | if (!snapshot.Get()) { |
| jsbell | 3785ea0 | 2016-07-29 16:50:03 | [diff] [blame] | 59 | LOG(ERROR) << "CreateToolhelp32Snapshot failed: " << GetLastError(); |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 60 | return; |
| 61 | } |
| rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 62 | if (!::Process32First(snapshot.Get(), &process_entry)) { |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 63 | LOG(ERROR) << "Process32First failed: " << GetLastError(); |
| 64 | return; |
| 65 | } |
| 66 | do { |
| [email protected] | a4dc33f | 2009-10-20 15:09:55 | [diff] [blame] | 67 | base::ProcessId pid = process_entry.th32ProcessID; |
| [email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 68 | base::win::ScopedHandle process_handle(::OpenProcess( |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 69 | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid)); |
| Rafael Cintron | 3ca38fad | 2025-11-10 17:43:47 | [diff] [blame] | 70 | if (!process_handle.is_valid()) { |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 71 | continue; |
| Rafael Cintron | 3ca38fad | 2025-11-10 17:43:47 | [diff] [blame] | 72 | } |
| Peter Kasting | 5105d61 | 2021-02-09 22:41:42 | [diff] [blame] | 73 | if (_wcsicmp(base::as_wcstr(process_data_[0].process_name), |
| thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 74 | process_entry.szExeFile) != 0) { |
| ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 75 | continue; |
| thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 76 | } |
| 77 | |
| ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 78 | // Get Memory Information. |
| 79 | ProcessMemoryInformation info; |
| 80 | info.pid = pid; |
| thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 81 | info.process_type = pid == GetCurrentProcessId() |
| 82 | ? content::PROCESS_TYPE_BROWSER |
| 83 | : content::PROCESS_TYPE_UNKNOWN; |
| ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 84 | |
| ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 85 | // Get Version Information. |
| 86 | info.version = base::ASCIIToUTF16(version_info::GetVersionNumber()); |
| 87 | // Check if this is one of the child processes whose data we collected |
| 88 | // on the IO thread, and if so copy over that data. |
| thestig | 0df2bae8 | 2016-07-26 17:59:36 | [diff] [blame] | 89 | for (const ProcessMemoryInformation& child : child_info) { |
| 90 | if (child.pid == info.pid) { |
| 91 | info.titles = child.titles; |
| 92 | info.process_type = child.process_type; |
| 93 | break; |
| 94 | } |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 95 | } |
| ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 96 | |
| 97 | // Add the process info to our list. |
| 98 | process_data_[0].processes.push_back(info); |
| rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 99 | } while (::Process32Next(snapshot.Get(), &process_entry)); |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 100 | |
| 101 | // Finally return to the browser thread. |
| Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 102 | content::GetUIThreadTaskRunner({})->PostTask( |
| 103 | FROM_HERE, |
| kylechar | 99ef904 | 2019-02-25 18:09:43 | [diff] [blame] | 104 | base::BindOnce(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
| [email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 105 | } |