xref: /aosp_15_r20/external/cronet/components/metrics/motherboard_metrics_provider.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/metrics/motherboard_metrics_provider.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "base/functional/callback.h"
11 #include "base/task/thread_pool.h"
12 #include "components/metrics/motherboard.h"
13 #include "third_party/metrics_proto/system_profile.pb.h"
14 
15 namespace metrics {
16 
17 namespace {
18 // Constructs the Motherboard data on the blocking pool.
GetMotherboard()19 std::unique_ptr<Motherboard> GetMotherboard() {
20   return std::make_unique<Motherboard>();
21 }
22 }  // namespace
23 
24 MotherboardMetricsProvider::MotherboardMetricsProvider() = default;
25 MotherboardMetricsProvider::~MotherboardMetricsProvider() = default;
26 
ProvideSystemProfileMetrics(SystemProfileProto * system_profile)27 void MotherboardMetricsProvider::ProvideSystemProfileMetrics(
28     SystemProfileProto* system_profile) {
29   // If motherboard_info_ has not yet been initialized, skip metrics.
30   if (!motherboard_info_) {
31     return;
32   }
33   SystemProfileProto::Hardware::Motherboard* motherboard =
34       system_profile->mutable_hardware()->mutable_motherboard();
35   if (motherboard_info_->manufacturer().has_value()) {
36     motherboard->set_manufacturer(*motherboard_info_->manufacturer());
37   }
38   if (motherboard_info_->model().has_value()) {
39     motherboard->set_model(*motherboard_info_->model());
40   }
41   if (motherboard_info_->bios_manufacturer().has_value()) {
42     motherboard->set_bios_manufacturer(*motherboard_info_->bios_manufacturer());
43   }
44   if (motherboard_info_->bios_version().has_value()) {
45     motherboard->set_bios_version(*motherboard_info_->bios_version());
46   }
47   if (motherboard_info_->bios_type().has_value()) {
48     if (*motherboard_info_->bios_type() == Motherboard::BiosType::kLegacy) {
49       motherboard->set_bios_type(SystemProfileProto::Hardware::BIOS_TYPE_LEGACY);
50     } else if (*motherboard_info_->bios_type() ==
51                Motherboard::BiosType::kUefi) {
52       motherboard->set_bios_type(SystemProfileProto::Hardware::BIOS_TYPE_UEFI);
53     }
54   } else {
55     motherboard->set_bios_type(SystemProfileProto::Hardware::BIOS_TYPE_UNKNOWN);
56   }
57 }
58 
AsyncInit(base::OnceClosure done_callback)59 void MotherboardMetricsProvider::AsyncInit(base::OnceClosure done_callback) {
60   base::ThreadPool::PostTaskAndReplyWithResult(
61       FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
62       base::BindOnce(&GetMotherboard),
63       base::BindOnce(&MotherboardMetricsProvider::InitializeMotherboard,
64                      weak_ptr_factory_.GetWeakPtr(), std::move(done_callback)));
65 }
66 
InitializeMotherboard(base::OnceClosure done_callback,std::unique_ptr<Motherboard> motherboard_info)67 void MotherboardMetricsProvider::InitializeMotherboard(
68     base::OnceClosure done_callback,
69     std::unique_ptr<Motherboard> motherboard_info) {
70   motherboard_info_ = std::move(motherboard_info);
71   std::move(done_callback).Run();
72 }
73 
74 }  // namespace metrics
75