File: healthd_internals_message_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (461 lines) | stat: -rw-r--r-- 16,213 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/ash/healthd_internals/healthd_internals_message_handler.h"

#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/process/process_metrics.h"
#include "base/strings/string_number_conversions.h"
#include "chromeos/ash/services/cros_healthd/public/cpp/service_connection.h"
#include "chromeos/ash/services/cros_healthd/public/mojom/cros_healthd.mojom.h"
#include "chromeos/ash/services/cros_healthd/public/mojom/cros_healthd_probe.mojom.h"
#include "content/public/browser/browser_thread.h"

namespace ash {

namespace {

namespace mojom = ::ash::cros_healthd::mojom;

std::string Convert(mojom::ThermalSensorInfo::ThermalSensorSource source) {
  switch (source) {
    case mojom::ThermalSensorInfo::ThermalSensorSource::kEc:
      return "EC";
    case mojom::ThermalSensorInfo::ThermalSensorSource::kSysFs:
      return "SysFs";
    case mojom::ThermalSensorInfo::ThermalSensorSource::kUnmappedEnumField:
      return "Unknown";
  }
}

std::string Convert(mojom::CpuArchitectureEnum source) {
  switch (source) {
    case mojom::CpuArchitectureEnum::kX86_64:
      return "x86_64";
    case mojom::CpuArchitectureEnum::kAArch64:
      return "aarch64";
    case mojom::CpuArchitectureEnum::kArmv7l:
      return "armv7l";
    case mojom::CpuArchitectureEnum::kUnknown:
      return "Unknown";
  }
}

std::string Convert(mojom::ProcessState state) {
  switch (state) {
    case mojom::ProcessState::kUnknown:
      return "Unknown";
    case mojom::ProcessState::kRunning:
      return "Running";
    case mojom::ProcessState::kSleeping:
      return "Sleeping";
    case mojom::ProcessState::kWaiting:
      return "Waiting";
    case mojom::ProcessState::kZombie:
      return "Zombie";
    case mojom::ProcessState::kStopped:
      return "Stopped";
    case mojom::ProcessState::kTracingStop:
      return "TracingStop";
    case mojom::ProcessState::kDead:
      return "Dead";
    case mojom::ProcessState::kIdle:
      return "Idle";
  }
}

base::Value::Dict ConvertBatteryValue(const mojom::BatteryInfoPtr& info) {
  base::Value::Dict out_battery;
  if (info) {
    out_battery.Set("currentNow", info->current_now);
    out_battery.Set("voltageNow", info->voltage_now);
    out_battery.Set("chargeNow", info->charge_now);
  }
  return out_battery;
}

base::Value::List ConvertLogicalCpus(
    const std::vector<mojom::LogicalCpuInfoPtr>& logical_cpus) {
  base::Value::List out_logical_cpus;
  for (const auto& logical_cpu : logical_cpus) {
    if (!logical_cpu) {
      continue;
    }
    base::Value::Dict out_logical_cpu;
    out_logical_cpu.Set("coreId", base::NumberToString(logical_cpu->core_id));
    out_logical_cpu.SetByDottedPath(
        "frequency.current",
        base::NumberToString(logical_cpu->scaling_current_frequency_khz));
    out_logical_cpu.SetByDottedPath(
        "frequency.max",
        base::NumberToString(logical_cpu->scaling_max_frequency_khz));
    out_logical_cpu.SetByDottedPath(
        "executionTime.user",
        base::NumberToString(logical_cpu->user_time_user_hz));
    out_logical_cpu.SetByDottedPath(
        "executionTime.system",
        base::NumberToString(logical_cpu->system_time_user_hz));
    out_logical_cpu.SetByDottedPath(
        "executionTime.idle",
        base::NumberToString(logical_cpu->idle_time_user_hz));
    out_logical_cpus.Append(std::move(out_logical_cpu));
  }
  return out_logical_cpus;
}

base::Value::List ConvertPhysicalCpus(
    const std::vector<mojom::PhysicalCpuInfoPtr>& physical_cpus) {
  base::Value::List out_physical_cpus;
  for (const auto& physical_cpu : physical_cpus) {
    if (!physical_cpu) {
      continue;
    }
    base::Value::Dict out_physical_cpu;
    if (physical_cpu->model_name.has_value()) {
      out_physical_cpu.Set("modelName", physical_cpu->model_name.value());
    }
    out_physical_cpu.Set("logicalCpus",
                         ConvertLogicalCpus(physical_cpu->logical_cpus));
    out_physical_cpus.Append(std::move(out_physical_cpu));
  }
  return out_physical_cpus;
}

base::Value::List ConvertTemperatureChannels(
    const std::vector<mojom::CpuTemperatureChannelPtr>& temperature_channels) {
  base::Value::List out_temperature_channels;
  for (const auto& temperature_channel : temperature_channels) {
    if (!temperature_channel) {
      continue;
    }
    base::Value::Dict out_temperature_channel;
    if (temperature_channel->label.has_value()) {
      out_temperature_channel.Set("label", temperature_channel->label.value());
    }
    out_temperature_channel.Set("temperatureCelsius",
                                temperature_channel->temperature_celsius);
    out_temperature_channels.Append(std::move(out_temperature_channel));
  }
  return out_temperature_channels;
}

base::Value::Dict ConvertCpuValue(const mojom::CpuInfoPtr& info) {
  base::Value::Dict out_cpu;
  if (info) {
    out_cpu.Set("architecture", Convert(info->architecture));
    out_cpu.Set("numTotalThreads",
                base::NumberToString(info->num_total_threads));
    out_cpu.Set("physicalCpus", ConvertPhysicalCpus(info->physical_cpus));
    out_cpu.Set("temperatureChannels",
                ConvertTemperatureChannels(info->temperature_channels));
  }
  return out_cpu;
}

base::Value::List ConvertFanValue(const std::vector<mojom::FanInfoPtr>& info) {
  base::Value::List out_fans;
  for (const auto& fan : info) {
    if (fan) {
      base::Value::Dict fan_result;
      fan_result.Set("speedRpm", base::NumberToString(fan->speed_rpm));
      out_fans.Append(std::move(fan_result));
    }
  }
  return out_fans;
}

// Set the value to `output` for optional uint64 value.
void SetValue(std::string_view field_name,
              std::optional<uint64_t> value,
              base::Value::Dict& output) {
  if (value.has_value()) {
    output.Set(field_name, base::NumberToString(value.value()));
  }
}

base::Value::Dict ConvertMemoryValue(const mojom::MemoryInfoPtr& info) {
  base::Value::Dict out_memory;
  if (info) {
    out_memory.Set("availableMemoryKib",
                   base::NumberToString(info->available_memory_kib));
    out_memory.Set("freeMemoryKib",
                   base::NumberToString(info->free_memory_kib));
    out_memory.Set("totalMemoryKib",
                   base::NumberToString(info->total_memory_kib));
    SetValue("buffersKib", info->buffers_kib, out_memory);
    SetValue("pageCacheKib", info->page_cache_kib, out_memory);
    SetValue("sharedMemoryKib", info->shared_memory_kib, out_memory);

    SetValue("activeMemoryKib", info->active_memory_kib, out_memory);
    SetValue("inactiveMemoryKib", info->inactive_memory_kib, out_memory);

    SetValue("totalSwapMemoryKib", info->total_swap_memory_kib, out_memory);
    SetValue("freeSwapMemoryKib", info->free_swap_memory_kib, out_memory);
    SetValue("cachedSwapMemoryKib", info->cached_swap_memory_kib, out_memory);

    SetValue("totalSlabMemoryKib", info->total_slab_memory_kib, out_memory);
    SetValue("reclaimableSlabMemoryKib", info->reclaimable_slab_memory_kib,
             out_memory);
    SetValue("unreclaimableSlabMemoryKib", info->unreclaimable_slab_memory_kib,
             out_memory);
  }
  return out_memory;
}

base::Value::Dict ConvertProcessValue(const mojom::ProcessInfoPtr& info) {
  base::Value::Dict out_process;
  if (info) {
    out_process.Set("command", info->command);
    out_process.Set("userId", base::NumberToString(info->user_id));
    out_process.Set("priority", info->priority);
    out_process.Set("nice", info->nice);
    out_process.Set("uptimeTicks", base::NumberToString(info->uptime_ticks));
    out_process.Set("state", Convert(info->state));
    out_process.Set("residentMemoryKib",
                    base::NumberToString(info->resident_memory_kib));
    out_process.Set("readSystemCallsCount",
                    base::NumberToString(info->read_system_calls));
    out_process.Set("writeSystemCallsCount",
                    base::NumberToString(info->write_system_calls));
    if (info->name.has_value()) {
      out_process.Set("name", info->name.value());
    }
    out_process.Set("parentProcessId",
                    base::NumberToString(info->parent_process_id));
    out_process.Set("processGroupId",
                    base::NumberToString(info->process_group_id));
    out_process.Set("threadsNumber", base::NumberToString(info->threads));
    out_process.Set("processId", base::NumberToString(info->process_id));
  }
  return out_process;
}

base::Value::List ConvertThermalValue(const mojom::ThermalInfoPtr& info) {
  base::Value::List out_thermals;
  if (info) {
    for (const auto& thermal : info->thermal_sensors) {
      base::Value::Dict thermal_result;
      thermal_result.Set("name", thermal->name);
      thermal_result.Set("source", Convert(thermal->source));
      thermal_result.Set("temperatureCelsius", thermal->temperature_celsius);
      out_thermals.Append(std::move(thermal_result));
    }
  }
  return out_thermals;
}

std::optional<base::Value::Dict> GetZramValue() {
  base::SwapInfo swap_info;
  if (!GetSwapInfo(&swap_info)) {
    LOG(WARNING) << ("Unable to get system zram info.");
    return std::nullopt;
  }
  base::Value::Dict out_zram;
  out_zram.Set("totalUsedMemory",
               base::NumberToString(swap_info.mem_used_total));
  out_zram.Set("originalDataSize",
               base::NumberToString(swap_info.orig_data_size));
  out_zram.Set("compressedDataSize",
               base::NumberToString(swap_info.compr_data_size));
  return out_zram;
}

}  // namespace

HealthdInternalsMessageHandler::HealthdInternalsMessageHandler() = default;

HealthdInternalsMessageHandler::~HealthdInternalsMessageHandler() = default;

void HealthdInternalsMessageHandler::RegisterMessages() {
  web_ui()->RegisterMessageCallback(
      "getHealthdInternalsFeatureFlag",
      base::BindRepeating(
          &HealthdInternalsMessageHandler::HandleGetHealthdInternalsFeatureFlag,
          weak_ptr_factory_.GetWeakPtr()));
  web_ui()->RegisterMessageCallback(
      "getHealthdTelemetryInfo",
      base::BindRepeating(
          &HealthdInternalsMessageHandler::HandleGetHealthdTelemetryInfo,
          weak_ptr_factory_.GetWeakPtr()));
  web_ui()->RegisterMessageCallback(
      "getHealthdProcessInfo",
      base::BindRepeating(
          &HealthdInternalsMessageHandler::HandleGetHealthdProcessInfo,
          weak_ptr_factory_.GetWeakPtr()));
  web_ui()->RegisterMessageCallback(
      "getCrosSystemInfo",
      base::BindRepeating(
          &HealthdInternalsMessageHandler::HandleGetCrosSystemInfo,
          weak_ptr_factory_.GetWeakPtr()));
}

void HealthdInternalsMessageHandler::HandleGetHealthdInternalsFeatureFlag(
    const base::Value::List& list) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  AllowJavascript();
  if (list.size() != 1 || !list[0].is_string()) {
    NOTREACHED();
  }
  base::Value callback_id = list[0].Clone();
  base::Value::Dict result;
  result.Set("tabsDisplayed", features::AreHealthdInternalsTabsEnabled());
  ResolveJavascriptCallback(callback_id, result);
}

void HealthdInternalsMessageHandler::HandleGetHealthdTelemetryInfo(
    const base::Value::List& list) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  AllowJavascript();
  if (list.size() != 1 || !list[0].is_string()) {
    NOTREACHED();
  }

  base::Value callback_id = list[0].Clone();
  auto* service = GetProbeService();
  if (!service) {
    HandleTelemetryResult(std::move(callback_id), nullptr);
    return;
  }

  service->ProbeTelemetryInfo(
      {mojom::ProbeCategoryEnum::kBattery, mojom::ProbeCategoryEnum::kCpu,
       mojom::ProbeCategoryEnum::kFan, mojom::ProbeCategoryEnum::kMemory,
       mojom::ProbeCategoryEnum::kThermal},
      base::BindOnce(&HealthdInternalsMessageHandler::HandleTelemetryResult,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback_id)));
}

void HealthdInternalsMessageHandler::HandleTelemetryResult(
    base::Value callback_id,
    mojom::TelemetryInfoPtr info) {
  if (!info) {
    LOG(WARNING) << "Unable to access telemetry info from Healthd";
    ReplyHealthdInternalInfo(std::move(callback_id), base::Value::Dict());
    return;
  }

  base::Value::Dict result;
  if (info->battery_result && info->battery_result->is_battery_info()) {
    const auto& battery_info = info->battery_result->get_battery_info();
    if (battery_info) {
      result.Set("battery", ConvertBatteryValue(battery_info));
    }
  }
  if (info->cpu_result && info->cpu_result->is_cpu_info()) {
    result.Set("cpu", ConvertCpuValue(info->cpu_result->get_cpu_info()));
  }
  if (info->fan_result && info->fan_result->is_fan_info()) {
    result.Set("fans", ConvertFanValue(info->fan_result->get_fan_info()));
  }
  if (info->memory_result && info->memory_result->is_memory_info()) {
    result.Set("memory",
               ConvertMemoryValue(info->memory_result->get_memory_info()));
  }
  if (info->thermal_result && info->thermal_result->is_thermal_info()) {
    result.Set("thermals",
               ConvertThermalValue(info->thermal_result->get_thermal_info()));
  }

  ReplyHealthdInternalInfo(std::move(callback_id), std::move(result));
}

void HealthdInternalsMessageHandler::HandleGetHealthdProcessInfo(
    const base::Value::List& list) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  AllowJavascript();
  if (list.size() != 1 || !list[0].is_string()) {
    NOTREACHED();
  }

  base::Value callback_id = list[0].Clone();
  auto* service = GetProbeService();
  if (!service) {
    HandleMultipleProcessResult(std::move(callback_id), nullptr);
    return;
  }

  service->ProbeMultipleProcessInfo(
      /*process_ids=*/std::nullopt, /*ignore_single_process_error=*/true,
      base::BindOnce(
          &HealthdInternalsMessageHandler::HandleMultipleProcessResult,
          weak_ptr_factory_.GetWeakPtr(), std::move(callback_id)));
}

void HealthdInternalsMessageHandler::HandleMultipleProcessResult(
    base::Value callback_id,
    mojom::MultipleProcessResultPtr process_result) {
  base::Value::Dict result;
  if (!process_result) {
    LOG(WARNING) << "Unable to access process info from Healthd";
    result.Set("processes", base::Value::List());
    ReplyHealthdInternalInfo(std::move(callback_id), std::move(result));
    return;
  }

  base::Value::List out_processes;
  for (const auto& [_, process_info] : process_result->process_infos) {
    out_processes.Append(ConvertProcessValue(process_info));
  }
  result.Set("processes", std::move(out_processes));

  ReplyHealthdInternalInfo(std::move(callback_id), std::move(result));
}

void HealthdInternalsMessageHandler::HandleGetCrosSystemInfo(
    const base::Value::List& list) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  AllowJavascript();
  if (list.size() != 1 || !list[0].is_string()) {
    NOTREACHED();
  }
  base::Value callback_id = list[0].Clone();
  base::Value::Dict result;

  auto zram = GetZramValue();
  if (zram.has_value()) {
    result.Set("zram", std::move(zram.value()));
  }

  ResolveJavascriptCallback(callback_id, result);
}

void HealthdInternalsMessageHandler::ReplyHealthdInternalInfo(
    base::Value callback_id,
    base::Value::Dict result) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  ResolveJavascriptCallback(callback_id, result);
}

mojom::CrosHealthdProbeService*
HealthdInternalsMessageHandler::GetProbeService() {
  if (!probe_service_ || !probe_service_.is_connected()) {
    cros_healthd::ServiceConnection::GetInstance()->BindProbeService(
        probe_service_.BindNewPipeAndPassReceiver());
    probe_service_.set_disconnect_handler(base::BindOnce(
        &HealthdInternalsMessageHandler::OnProbeServiceDisconnect,
        weak_ptr_factory_.GetWeakPtr()));
  }
  return probe_service_.get();
}

void HealthdInternalsMessageHandler::OnProbeServiceDisconnect() {
  probe_service_.reset();
}

}  // namespace ash