summaryrefslogtreecommitdiff
path: root/chromium/base/fuchsia/system_info.cc
blob: c29235706d61a5317f8f0bd39f9eebf12ef4438b (plain)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/fuchsia/system_info.h"

#include <fuchsia/buildinfo/cpp/fidl.h>
#include <fuchsia/hwinfo/cpp/fidl.h>
#include <lib/sys/cpp/component_context.h>

#include "base/check.h"
#include "base/fuchsia/fuchsia_logging.h"
#include "base/fuchsia/process_context.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/threading/scoped_blocking_call.h"

namespace base {

namespace {

// Returns this process's cached object for `BuildInfo`.
fuchsia::buildinfo::BuildInfo& CachedBuildInfo() {
  static NoDestructor<fuchsia::buildinfo::BuildInfo> build_info;
  return *build_info;
}

// Synchronously fetches BuildInfo from the system and caches it for use in this
// process.
// Returns whether the system info was successfully cached.
bool FetchAndCacheBuildInfo() {
  DCHECK(CachedBuildInfo().IsEmpty()) << "Only call once per process";

  fuchsia::buildinfo::ProviderSyncPtr provider_sync;
  ComponentContextForProcess()->svc()->Connect(provider_sync.NewRequest());

  zx_status_t status = provider_sync->GetBuildInfo(&CachedBuildInfo());
  ZX_DLOG_IF(ERROR, status != ZX_OK, status);
  DLOG_IF(ERROR, CachedBuildInfo().IsEmpty()) << "Received empty BuildInfo";
  return status == ZX_OK && !CachedBuildInfo().IsEmpty();
}

}  // namespace

bool FetchAndCacheSystemInfo() {
  ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::WILL_BLOCK);
  return FetchAndCacheBuildInfo();
}

const fuchsia::buildinfo::BuildInfo& GetCachedBuildInfo() {
  DCHECK(!CachedBuildInfo().IsEmpty())
      << "FetchAndCacheSystemInfo() has not been called in this process";
  return CachedBuildInfo();
}

// Synchronously fetches ProductInfo from the system
fuchsia::hwinfo::ProductInfo GetProductInfo() {
  ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::WILL_BLOCK);
  fuchsia::hwinfo::ProductSyncPtr provider_sync;
  ComponentContextForProcess()->svc()->Connect(provider_sync.NewRequest());

  fuchsia::hwinfo::ProductInfo product_info;
  [[maybe_unused]] zx_status_t status = provider_sync->GetInfo(&product_info);
  ZX_DLOG_IF(ERROR, status != ZX_OK, status);
  return product_info;
}

void ClearCachedSystemInfoForTesting() {
  CachedBuildInfo() = {};
}

}  // namespace base