[fuchsia] Implement OperatingSystemVersion() using fuchsia.buildinfo
SysInfo::OperatingSystemVersion() is expected to be non-blocking, and
therefore callable from any thread. On Fuchsia, fetching the system
BuildInfo requires a blocking call to the buildinfo provider. To avoid
making SysInfo::OperatingSystemVersion() more difficult to use on all
platforms, we introduce a process-wide cache for the Fuchsia BuildInfo
that must be primed in each process before OperatingSystemVersion() is
called. This priming function is called in each content process and in
all test processes so that OperatingSystemVersion() may continue to be
used everywhere.
Bug: 1275791, 1286960
Change-Id: I57eddb7dbd6520b312432c6f907004e9787917ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3322067
Auto-Submit: Mason Bendixen <[email protected]>
Reviewed-by: Wez <[email protected]>
Reviewed-by: Kevin Marshall <[email protected]>
Reviewed-by: Robert Sesek <[email protected]>
Reviewed-by: Arthur Sonzogni <[email protected]>
Reviewed-by: Charles Reis <[email protected]>
Reviewed-by: David Dorwin <[email protected]>
Commit-Queue: David Dorwin <[email protected]>
Cr-Commit-Position: refs/heads/main@{#964329}
diff --git a/base/fuchsia/build_info.h b/base/fuchsia/build_info.h
new file mode 100644
index 0000000..6384501
--- /dev/null
+++ b/base/fuchsia/build_info.h
@@ -0,0 +1,35 @@
+// Copyright 2022 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_FUCHSIA_BUILD_INFO_H_
+#define BASE_FUCHSIA_BUILD_INFO_H_
+
+#include "base/base_export.h"
+#include "base/strings/string_piece_forward.h"
+
+namespace fuchsia {
+namespace buildinfo {
+class BuildInfo;
+}
+} // namespace fuchsia
+
+namespace base {
+
+// Fetches the build info from the system and caches it before returning.
+// Must be called in each process before calling other non-test functions.
+BASE_EXPORT void FetchAndCacheSystemBuildInfo();
+
+// Returns the cached build info.
+BASE_EXPORT const fuchsia::buildinfo::BuildInfo& GetCachedBuildInfo();
+
+// Returns the cached version string.
+BASE_EXPORT StringPiece GetBuildInfoVersion();
+
+// Reset the cached BuildInfo to empty so that FetchAndCacheSystemBuildInfo()
+// can be called again in this process.
+BASE_EXPORT void ClearCachedBuildInfoForTesting();
+
+} // namespace base
+
+#endif // BASE_FUCHSIA_BUILD_INFO_H_