system_info.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/fuchsia/system_info.h"
  5. #include <fuchsia/buildinfo/cpp/fidl.h>
  6. #include <fuchsia/hwinfo/cpp/fidl.h>
  7. #include <lib/sys/cpp/component_context.h>
  8. #include "base/check.h"
  9. #include "base/fuchsia/fuchsia_logging.h"
  10. #include "base/fuchsia/process_context.h"
  11. #include "base/location.h"
  12. #include "base/logging.h"
  13. #include "base/no_destructor.h"
  14. #include "base/threading/scoped_blocking_call.h"
  15. namespace base {
  16. namespace {
  17. // Returns this process's cached object for `BuildInfo`.
  18. fuchsia::buildinfo::BuildInfo& CachedBuildInfo() {
  19. static NoDestructor<fuchsia::buildinfo::BuildInfo> build_info;
  20. return *build_info;
  21. }
  22. // Synchronously fetches BuildInfo from the system and caches it for use in this
  23. // process.
  24. void FetchAndCacheBuildInfo() {
  25. DCHECK(CachedBuildInfo().IsEmpty()) << "Only call once per process";
  26. fuchsia::buildinfo::ProviderSyncPtr provider_sync;
  27. ComponentContextForProcess()->svc()->Connect(provider_sync.NewRequest());
  28. zx_status_t status = provider_sync->GetBuildInfo(&CachedBuildInfo());
  29. ZX_CHECK(status == ZX_OK, status);
  30. CHECK(!CachedBuildInfo().IsEmpty());
  31. }
  32. } // namespace
  33. void FetchAndCacheSystemInfo() {
  34. ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::WILL_BLOCK);
  35. FetchAndCacheBuildInfo();
  36. }
  37. const fuchsia::buildinfo::BuildInfo& GetCachedBuildInfo() {
  38. DCHECK(!CachedBuildInfo().IsEmpty())
  39. << "FetchAndCacheSystemInfo() has not been called in this process";
  40. return CachedBuildInfo();
  41. }
  42. // Synchronously fetches ProductInfo from the system
  43. fuchsia::hwinfo::ProductInfo GetProductInfo() {
  44. ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::WILL_BLOCK);
  45. fuchsia::hwinfo::ProductSyncPtr provider_sync;
  46. ComponentContextForProcess()->svc()->Connect(provider_sync.NewRequest());
  47. fuchsia::hwinfo::ProductInfo product_info;
  48. [[maybe_unused]] zx_status_t status = provider_sync->GetInfo(&product_info);
  49. ZX_DLOG_IF(ERROR, status != ZX_OK, status);
  50. return product_info;
  51. }
  52. void ClearCachedSystemInfoForTesting() {
  53. CachedBuildInfo() = {};
  54. }
  55. } // namespace base