xref: /aosp_15_r20/external/cronet/base/fuchsia/system_info.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 "base/fuchsia/system_info.h"
6 
7 #include <fidl/fuchsia.buildinfo/cpp/fidl.h>
8 #include <fidl/fuchsia.hwinfo/cpp/fidl.h>
9 #include <lib/sys/cpp/component_context.h>
10 
11 #include "base/check.h"
12 #include "base/fuchsia/fuchsia_component_connect.h"
13 #include "base/fuchsia/fuchsia_logging.h"
14 #include "base/location.h"
15 #include "base/logging.h"
16 #include "base/no_destructor.h"
17 #include "base/threading/scoped_blocking_call.h"
18 
19 namespace base {
20 
21 namespace {
22 
23 // Returns this process's cached object for `BuildInfo`.
CachedBuildInfo()24 fuchsia_buildinfo::BuildInfo& CachedBuildInfo() {
25   static NoDestructor<fuchsia_buildinfo::BuildInfo> build_info;
26   return *build_info;
27 }
28 
29 // Synchronously fetches BuildInfo from the system and caches it for use in this
30 // process.
31 // Returns whether the system info was successfully cached.
FetchAndCacheBuildInfo()32 bool FetchAndCacheBuildInfo() {
33   DCHECK(CachedBuildInfo().IsEmpty()) << "Only call once per process";
34 
35   auto provider_client_end =
36       fuchsia_component::Connect<fuchsia_buildinfo::Provider>();
37   if (provider_client_end.is_error()) {
38     DLOG(ERROR) << base::FidlConnectionErrorMessage(provider_client_end);
39     return false;
40   }
41   fidl::SyncClient provider_sync(std::move(provider_client_end.value()));
42 
43   auto build_info_result = provider_sync->GetBuildInfo();
44   if (build_info_result.is_error()) {
45     ZX_DLOG(ERROR, build_info_result.error_value().status());
46     return false;
47   }
48 
49   if (build_info_result->build_info().IsEmpty()) {
50     DLOG(ERROR) << "Received empty BuildInfo";
51     return false;
52   }
53 
54   CachedBuildInfo() = std::move(build_info_result->build_info());
55   return true;
56 }
57 
58 }  // namespace
59 
FetchAndCacheSystemInfo()60 bool FetchAndCacheSystemInfo() {
61   ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::WILL_BLOCK);
62   return FetchAndCacheBuildInfo();
63 }
64 
GetCachedBuildInfo()65 const fuchsia_buildinfo::BuildInfo& GetCachedBuildInfo() {
66   DCHECK(!CachedBuildInfo().IsEmpty())
67       << "FetchAndCacheSystemInfo() has not been called in this process";
68   return CachedBuildInfo();
69 }
70 
71 // Synchronously fetches ProductInfo from the system
GetProductInfo()72 fuchsia_hwinfo::ProductInfo GetProductInfo() {
73   ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::WILL_BLOCK);
74   auto product_client_end =
75       fuchsia_component::Connect<fuchsia_hwinfo::Product>();
76   if (product_client_end.is_error()) {
77     DLOG(ERROR) << base::FidlConnectionErrorMessage(product_client_end);
78     return {};
79   }
80   fidl::SyncClient provider_sync(std::move(product_client_end.value()));
81 
82   auto product_info_result = provider_sync->GetInfo();
83   if (product_info_result.is_error()) {
84     ZX_DLOG(ERROR, product_info_result.error_value().status());
85     return {};
86   }
87 
88   return product_info_result->info();
89 }
90 
ClearCachedSystemInfoForTesting()91 void ClearCachedSystemInfoForTesting() {
92   CachedBuildInfo() = {};
93 }
94 
95 }  // namespace base
96