Browse Source

feat: add name to app.getAppMetrics() output (#24359)

Milan Burda 4 years ago
parent
commit
7fd96cd188

+ 2 - 0
docs/api/structures/process-metric.md

@@ -11,6 +11,8 @@
   * `Pepper Plugin`
   * `Pepper Plugin Broker`
   * `Unknown`
+* `name` String (optional) - The name of the process. i.e. for plugins it might be Flash.
+    Examples for utility: `Audio Service`, `Content Decryption Module Service`, `Network Service`, `Video Capture`, etc.
 * `cpu` [CPUUsage](cpu-usage.md) - CPU usage of the process.
 * `creationTime` Number - Creation time for this process.
     The time is represented as number of milliseconds since epoch.

+ 10 - 3
shell/browser/api/electron_api_app.cc

@@ -790,7 +790,8 @@ void App::OnGpuProcessCrashed(base::TerminationStatus status) {
 
 void App::BrowserChildProcessLaunchedAndConnected(
     const content::ChildProcessData& data) {
-  ChildProcessLaunched(data.process_type, data.GetProcess().Handle());
+  ChildProcessLaunched(data.process_type, data.GetProcess().Handle(),
+                       base::UTF16ToUTF8(data.name));
 }
 
 void App::BrowserChildProcessHostDisconnected(
@@ -830,7 +831,9 @@ void App::RenderProcessDisconnected(base::ProcessId host_pid) {
   ChildProcessDisconnected(host_pid);
 }
 
-void App::ChildProcessLaunched(int process_type, base::ProcessHandle handle) {
+void App::ChildProcessLaunched(int process_type,
+                               base::ProcessHandle handle,
+                               const std::string& name) {
   auto pid = base::GetProcId(handle);
 
 #if defined(OS_MACOSX)
@@ -840,7 +843,7 @@ void App::ChildProcessLaunched(int process_type, base::ProcessHandle handle) {
   auto metrics = base::ProcessMetrics::CreateProcessMetrics(handle);
 #endif
   app_metrics_[pid] = std::make_unique<electron::ProcessMetric>(
-      process_type, handle, std::move(metrics));
+      process_type, handle, std::move(metrics), name);
 }
 
 void App::ChildProcessDisconnected(base::ProcessId pid) {
@@ -1281,6 +1284,10 @@ std::vector<gin_helper::Dictionary> App::GetAppMetrics(v8::Isolate* isolate) {
     pid_dict.Set("creationTime",
                  process_metric.second->process.CreationTime().ToJsTime());
 
+    if (!process_metric.second->name.empty()) {
+      pid_dict.Set("name", process_metric.second->name);
+    }
+
 #if !defined(OS_LINUX)
     auto memory_info = process_metric.second->GetMemoryInfo();
 

+ 3 - 1
shell/browser/api/electron_api_app.h

@@ -158,7 +158,9 @@ class App : public ElectronBrowserClient::Delegate,
 
  private:
   void SetAppPath(const base::FilePath& app_path);
-  void ChildProcessLaunched(int process_type, base::ProcessHandle handle);
+  void ChildProcessLaunched(int process_type,
+                            base::ProcessHandle handle,
+                            const std::string& name = std::string());
   void ChildProcessDisconnected(base::ProcessId pid);
 
   void SetAppLogsPath(gin_helper::ErrorThrower thrower,

+ 3 - 1
shell/browser/api/process_metric.cc

@@ -52,9 +52,11 @@ namespace electron {
 
 ProcessMetric::ProcessMetric(int type,
                              base::ProcessHandle handle,
-                             std::unique_ptr<base::ProcessMetrics> metrics) {
+                             std::unique_ptr<base::ProcessMetrics> metrics,
+                             const std::string& name) {
   this->type = type;
   this->metrics = std::move(metrics);
+  this->name = name;
 
 #if defined(OS_WIN)
   HANDLE duplicate_handle = INVALID_HANDLE_VALUE;

+ 4 - 1
shell/browser/api/process_metric.h

@@ -6,6 +6,7 @@
 #define SHELL_BROWSER_API_PROCESS_METRIC_H_
 
 #include <memory>
+#include <string>
 
 #include "base/process/process.h"
 #include "base/process/process_handle.h"
@@ -37,10 +38,12 @@ struct ProcessMetric {
   int type;
   base::Process process;
   std::unique_ptr<base::ProcessMetrics> metrics;
+  std::string name;
 
   ProcessMetric(int type,
                 base::ProcessHandle handle,
-                std::unique_ptr<base::ProcessMetrics> metrics);
+                std::unique_ptr<base::ProcessMetrics> metrics,
+                const std::string& name = std::string());
   ~ProcessMetric();
 
 #if !defined(OS_LINUX)

+ 4 - 0
spec-main/api-app-spec.ts

@@ -1079,6 +1079,10 @@ describe('app module', () => {
         expect(entry.memory).to.have.property('workingSetSize').that.is.greaterThan(0);
         expect(entry.memory).to.have.property('peakWorkingSetSize').that.is.greaterThan(0);
 
+        if (entry.type === 'Utility') {
+          expect(entry).to.have.property('name').that.is.a('string');
+        }
+
         if (process.platform === 'win32') {
           expect(entry.memory).to.have.property('privateBytes').that.is.greaterThan(0);
         }