blob: 050620d3ebe5ed1ba30d787d260b8f2daacaa0df [file] [log] [blame]
[email protected]4306df72012-04-20 18:58:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]54fd1d32009-09-01 00:12:582// 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]b90d7e802011-01-09 16:32:206
[email protected]54fd1d32009-09-01 00:12:587#include <psapi.h>
avi6846aef2015-12-26 01:09:388#include <stddef.h>
[email protected]d09a4ce1c2013-07-24 17:37:029#include <TlHelp32.h>
[email protected]54fd1d32009-09-01 00:12:5810
dcheng4af48582016-04-19 00:29:3511#include <memory>
12
[email protected]24d69692011-10-21 18:26:5113#include "base/bind.h"
[email protected]54fd1d32009-09-01 00:12:5814#include "base/file_version_info.h"
[email protected]57999812013-02-24 05:40:5215#include "base/files/file_path.h"
asvitkine58409e4c2015-01-15 01:25:4516#include "base/path_service.h"
[email protected]f9b294362013-06-10 20:22:3117#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1818#include "base/strings/utf_string_conversions.h"
fdoraye9c201b2017-05-02 19:49:2519#include "base/threading/thread_restrictions.h"
[email protected]b90d7e802011-01-09 16:32:2020#include "base/win/scoped_handle.h"
[email protected]1e67c2b2011-03-04 01:17:3721#include "base/win/windows_version.h"
[email protected]54fd1d32009-09-01 00:12:5822#include "chrome/common/url_constants.h"
[email protected]af39f002014-08-22 10:18:1823#include "chrome/grit/chromium_strings.h"
sdefresne9fb67692015-08-03 18:48:2224#include "components/version_info/version_info.h"
[email protected]c38831a12011-10-28 12:44:4925#include "content/public/browser/browser_thread.h"
[email protected]bd5d6cf2011-12-01 00:39:1226#include "content/public/common/process_type.h"
[email protected]c051a1b2011-01-21 23:30:1727#include "ui/base/l10n/l10n_util.h"
[email protected]54fd1d32009-09-01 00:12:5828
[email protected]631bb742011-11-02 11:29:3929using content::BrowserThread;
30
asvitkine89406d1f2015-01-17 06:57:1031MemoryDetails::MemoryDetails() {
asvitkine58409e4c2015-01-15 01:25:4532 base::FilePath browser_process_path;
Avi Drissman9098f9002018-05-04 00:11:5233 base::PathService::Get(base::FILE_EXE, &browser_process_path);
asvitkine58409e4c2015-01-15 01:25:4534
ellyjonescd6e449d2016-04-13 19:31:1535 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]54fd1d32009-09-01 00:12:5839}
40
41ProcessData* MemoryDetails::ChromeBrowser() {
ellyjonescd6e449d2016-04-13 19:31:1542 return &process_data_[0];
[email protected]54fd1d32009-09-01 00:12:5843}
44
45void MemoryDetails::CollectProcessData(
[email protected]4df3ac62011-03-11 04:38:5246 const std::vector<ProcessMemoryInformation>& child_info) {
Francois Doray66bdfd82017-10-20 13:50:3747 base::AssertBlockingAllowed();
[email protected]54fd1d32009-09-01 00:12:5848
49 // Clear old data.
ellyjonescd6e449d2016-04-13 19:31:1550 process_data_[0].processes.clear();
[email protected]54fd1d32009-09-01 00:12:5851
[email protected]b90d7e802011-01-09 16:32:2052 base::win::ScopedHandle snapshot(
53 ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
[email protected]54fd1d32009-09-01 00:12:5854 PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)};
55 if (!snapshot.Get()) {
jsbell3785ea02016-07-29 16:50:0356 LOG(ERROR) << "CreateToolhelp32Snapshot failed: " << GetLastError();
[email protected]54fd1d32009-09-01 00:12:5857 return;
58 }
rvargasd7743ba2014-09-25 01:35:2859 if (!::Process32First(snapshot.Get(), &process_entry)) {
[email protected]54fd1d32009-09-01 00:12:5860 LOG(ERROR) << "Process32First failed: " << GetLastError();
61 return;
62 }
63 do {
[email protected]a4dc33f2009-10-20 15:09:5564 base::ProcessId pid = process_entry.th32ProcessID;
[email protected]1e67c2b2011-03-04 01:17:3765 base::win::ScopedHandle process_handle(::OpenProcess(
[email protected]54fd1d32009-09-01 00:12:5866 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid));
rvargasd7743ba2014-09-25 01:35:2867 if (!process_handle.IsValid())
[email protected]54fd1d32009-09-01 00:12:5868 continue;
ellyjonescd6e449d2016-04-13 19:31:1569 if (_wcsicmp(process_data_[0].process_name.c_str(),
thestig0df2bae82016-07-26 17:59:3670 process_entry.szExeFile) != 0) {
ellyjonescd6e449d2016-04-13 19:31:1571 continue;
thestig0df2bae82016-07-26 17:59:3672 }
73
ellyjonescd6e449d2016-04-13 19:31:1574 // Get Memory Information.
75 ProcessMemoryInformation info;
76 info.pid = pid;
thestig0df2bae82016-07-26 17:59:3677 info.process_type = pid == GetCurrentProcessId()
78 ? content::PROCESS_TYPE_BROWSER
79 : content::PROCESS_TYPE_UNKNOWN;
ellyjonescd6e449d2016-04-13 19:31:1580
ellyjonescd6e449d2016-04-13 19:31:1581 // 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.
thestig0df2bae82016-07-26 17:59:3685 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]54fd1d32009-09-01 00:12:5891 }
ellyjonescd6e449d2016-04-13 19:31:1592
93 // Add the process info to our list.
94 process_data_[0].processes.push_back(info);
rvargasd7743ba2014-09-25 01:35:2895 } while (::Process32Next(snapshot.Get(), &process_entry));
[email protected]54fd1d32009-09-01 00:12:5896
97 // Finally return to the browser thread.
[email protected]f8b3ef82010-10-11 02:45:5298 BrowserThread::PostTask(
99 BrowserThread::UI, FROM_HERE,
[email protected]24d69692011-10-21 18:26:51100 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this));
[email protected]54fd1d32009-09-01 00:12:58101}