feat: add creationTime / sandboxed / integrityLevel to app.getAppMetrics() (#18718)
This is useful for checking which processes are sandboxed on OS level. Regarding creationTime, since the pid can be reused after a process dies, it is useful to use both the pid and the creationTime to uniquely identify a process.
This commit is contained in:
parent
0bdc05bf24
commit
d9215dd4ce
7 changed files with 222 additions and 49 deletions
|
@ -69,6 +69,25 @@ using atom::Browser;
|
|||
namespace mate {
|
||||
|
||||
#if defined(OS_WIN)
|
||||
template <>
|
||||
struct Converter<atom::ProcessIntegrityLevel> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
atom::ProcessIntegrityLevel value) {
|
||||
switch (value) {
|
||||
case atom::ProcessIntegrityLevel::Untrusted:
|
||||
return mate::StringToV8(isolate, "untrusted");
|
||||
case atom::ProcessIntegrityLevel::Low:
|
||||
return mate::StringToV8(isolate, "low");
|
||||
case atom::ProcessIntegrityLevel::Medium:
|
||||
return mate::StringToV8(isolate, "medium");
|
||||
case atom::ProcessIntegrityLevel::High:
|
||||
return mate::StringToV8(isolate, "high");
|
||||
default:
|
||||
return mate::StringToV8(isolate, "unknown");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<Browser::UserTask> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
|
@ -357,31 +376,10 @@ struct Converter<content::CertificateRequestResultType> {
|
|||
|
||||
namespace atom {
|
||||
|
||||
ProcessMetric::ProcessMetric(int type,
|
||||
base::ProcessId pid,
|
||||
std::unique_ptr<base::ProcessMetrics> metrics) {
|
||||
this->type = type;
|
||||
this->pid = pid;
|
||||
this->metrics = std::move(metrics);
|
||||
}
|
||||
|
||||
ProcessMetric::~ProcessMetric() = default;
|
||||
|
||||
namespace api {
|
||||
|
||||
namespace {
|
||||
|
||||
class AppIdProcessIterator : public base::ProcessIterator {
|
||||
public:
|
||||
AppIdProcessIterator() : base::ProcessIterator(nullptr) {}
|
||||
|
||||
protected:
|
||||
bool IncludeEntry() override {
|
||||
return (entry().parent_pid() == base::GetCurrentProcId() ||
|
||||
entry().pid() == base::GetCurrentProcId());
|
||||
}
|
||||
};
|
||||
|
||||
IconLoader::IconSize GetIconSizeByString(const std::string& size) {
|
||||
if (size == "small") {
|
||||
return IconLoader::IconSize::SMALL;
|
||||
|
@ -550,7 +548,7 @@ App::App(v8::Isolate* isolate) {
|
|||
|
||||
base::ProcessId pid = base::GetCurrentProcId();
|
||||
auto process_metric = std::make_unique<atom::ProcessMetric>(
|
||||
content::PROCESS_TYPE_BROWSER, pid,
|
||||
content::PROCESS_TYPE_BROWSER, base::GetCurrentProcessHandle(),
|
||||
base::ProcessMetrics::CreateCurrentProcessMetrics());
|
||||
app_metrics_[pid] = std::move(process_metric);
|
||||
Init(isolate);
|
||||
|
@ -825,15 +823,13 @@ void App::ChildProcessLaunched(int process_type, base::ProcessHandle handle) {
|
|||
auto pid = base::GetProcId(handle);
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
std::unique_ptr<base::ProcessMetrics> metrics(
|
||||
base::ProcessMetrics::CreateProcessMetrics(
|
||||
handle, content::BrowserChildProcessHost::GetPortProvider()));
|
||||
auto metrics = base::ProcessMetrics::CreateProcessMetrics(
|
||||
handle, content::BrowserChildProcessHost::GetPortProvider());
|
||||
#else
|
||||
std::unique_ptr<base::ProcessMetrics> metrics(
|
||||
base::ProcessMetrics::CreateProcessMetrics(handle));
|
||||
auto metrics = base::ProcessMetrics::CreateProcessMetrics(handle);
|
||||
#endif
|
||||
app_metrics_[pid] = std::make_unique<atom::ProcessMetric>(process_type, pid,
|
||||
std::move(metrics));
|
||||
app_metrics_[pid] = std::make_unique<atom::ProcessMetric>(
|
||||
process_type, handle, std::move(metrics));
|
||||
}
|
||||
|
||||
void App::ChildProcessDisconnected(base::ProcessId pid) {
|
||||
|
@ -1215,9 +1211,21 @@ std::vector<mate::Dictionary> App::GetAppMetrics(v8::Isolate* isolate) {
|
|||
#endif
|
||||
|
||||
pid_dict.Set("cpu", cpu_dict);
|
||||
pid_dict.Set("pid", process_metric.second->pid);
|
||||
pid_dict.Set("pid", process_metric.second->process.Pid());
|
||||
pid_dict.Set("type", content::GetProcessTypeNameInEnglish(
|
||||
process_metric.second->type));
|
||||
pid_dict.Set("creationTime",
|
||||
process_metric.second->process.CreationTime().ToJsTime());
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
pid_dict.Set("sandboxed", process_metric.second->IsSandboxed());
|
||||
#elif defined(OS_WIN)
|
||||
auto integrity_level = process_metric.second->GetIntegrityLevel();
|
||||
auto sandboxed = ProcessMetric::IsSandboxed(integrity_level);
|
||||
pid_dict.Set("integrityLevel", integrity_level);
|
||||
pid_dict.Set("sandboxed", sandboxed);
|
||||
#endif
|
||||
|
||||
result.push_back(pid_dict);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue