Commit b4e699d8 authored by Sharon Yang's avatar Sharon Yang Committed by Commit Bot

[fuchsia] Report component data to the inspect API

Add ComponentInspectorForProcess to manage a singleton per component.
Add static functions to inspect.* as a helper to publish version and
last revision.

Test: Added InspectTest to cr_fuchsia_base_unittests
Bug: 1061245
Change-Id: I0918b183fecedcd9a4c70685f7b7a166b2d82732
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2173238
Commit-Queue: Sharon Yang <yangsharon@chromium.org>
Reviewed-by: default avatarDavid Dorwin <ddorwin@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774917}
parent c15a2e73
...@@ -1524,6 +1524,8 @@ jumbo_component("base") { ...@@ -1524,6 +1524,8 @@ jumbo_component("base") {
"fuchsia/fuchsia_logging.h", "fuchsia/fuchsia_logging.h",
"fuchsia/intl_profile_watcher.cc", "fuchsia/intl_profile_watcher.cc",
"fuchsia/intl_profile_watcher.h", "fuchsia/intl_profile_watcher.h",
"fuchsia/process_context.cc",
"fuchsia/process_context.h",
"fuchsia/scoped_service_binding.cc", "fuchsia/scoped_service_binding.cc",
"fuchsia/scoped_service_binding.h", "fuchsia/scoped_service_binding.h",
"fuchsia/service_provider_impl.cc", "fuchsia/service_provider_impl.cc",
...@@ -1592,6 +1594,7 @@ jumbo_component("base") { ...@@ -1592,6 +1594,7 @@ jumbo_component("base") {
"//third_party/fuchsia-sdk/sdk/pkg/async-loop-cpp", "//third_party/fuchsia-sdk/sdk/pkg/async-loop-cpp",
"//third_party/fuchsia-sdk/sdk/pkg/async-loop-default", "//third_party/fuchsia-sdk/sdk/pkg/async-loop-default",
"//third_party/fuchsia-sdk/sdk/pkg/fidl", "//third_party/fuchsia-sdk/sdk/pkg/fidl",
"//third_party/fuchsia-sdk/sdk/pkg/sys_inspect_cpp",
"//third_party/fuchsia-sdk/sdk/pkg/syslog", "//third_party/fuchsia-sdk/sdk/pkg/syslog",
"//third_party/fuchsia-sdk/sdk/pkg/vfs_cpp", "//third_party/fuchsia-sdk/sdk/pkg/vfs_cpp",
"//third_party/icu", "//third_party/icu",
......
...@@ -16,8 +16,11 @@ class ComponentContext; ...@@ -16,8 +16,11 @@ class ComponentContext;
namespace base { namespace base {
namespace fuchsia { namespace fuchsia {
// TODO(https://crbug.com/1090364): Move this to process_context.h.
// Returns default sys::ComponentContext for the current process. // Returns default sys::ComponentContext for the current process.
BASE_EXPORT sys::ComponentContext* ComponentContextForCurrentProcess(); BASE_EXPORT sys::ComponentContext* ComponentContextForCurrentProcess();
} // namespace fuchsia } // namespace fuchsia
// Replaces the default sys::ComponentContext for the current process, and // Replaces the default sys::ComponentContext for the current process, and
......
// Copyright 2020 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.
#include "base/fuchsia/process_context.h"
#include <lib/sys/inspect/cpp/component.h>
#include "base/fuchsia/default_context.h"
#include "base/no_destructor.h"
namespace base {
sys::ComponentInspector* ComponentInspectorForProcess() {
static base::NoDestructor<sys::ComponentInspector> value(
fuchsia::ComponentContextForCurrentProcess());
return value.get();
}
} // namespace base
// Copyright 2020 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_PROCESS_CONTEXT_H_
#define BASE_FUCHSIA_PROCESS_CONTEXT_H_
#include "base/base_export.h"
namespace sys {
class ComponentInspector;
} // namespace sys
namespace base {
// Returns sys::ComponentInspector for the current process.
BASE_EXPORT sys::ComponentInspector* ComponentInspectorForProcess();
} // namespace base
#endif // BASE_FUCHSIA_PROCESS_CONTEXT_H_
...@@ -15,6 +15,7 @@ source_set("base") { ...@@ -15,6 +15,7 @@ source_set("base") {
"config_reader.cc", "config_reader.cc",
"fuchsia_dir_scheme.cc", "fuchsia_dir_scheme.cc",
"init_logging.cc", "init_logging.cc",
"inspect.cc",
"mem_buffer_util.cc", "mem_buffer_util.cc",
"string_util.cc", "string_util.cc",
] ]
...@@ -22,12 +23,16 @@ source_set("base") { ...@@ -22,12 +23,16 @@ source_set("base") {
"config_reader.h", "config_reader.h",
"fuchsia_dir_scheme.h", "fuchsia_dir_scheme.h",
"init_logging.h", "init_logging.h",
"inspect.h",
"mem_buffer_util.h", "mem_buffer_util.h",
"string_util.h", "string_util.h",
] ]
deps = [ deps = [
"//base", "//base",
"//components/version_info",
"//third_party/fuchsia-sdk/sdk/fidl/fuchsia.mem", "//third_party/fuchsia-sdk/sdk/fidl/fuchsia.mem",
"//third_party/fuchsia-sdk/sdk/pkg/fdio",
"//third_party/fuchsia-sdk/sdk/pkg/sys_inspect_cpp",
"//url", "//url",
] ]
} }
...@@ -120,6 +125,7 @@ source_set("test_support") { ...@@ -120,6 +125,7 @@ source_set("test_support") {
test("cr_fuchsia_base_unittests") { test("cr_fuchsia_base_unittests") {
sources = [ sources = [
"agent_impl_unittests.cc", "agent_impl_unittests.cc",
"inspect_unittest.cc",
"legacymetrics_client_unittest.cc", "legacymetrics_client_unittest.cc",
"legacymetrics_histogram_flattener_unittest.cc", "legacymetrics_histogram_flattener_unittest.cc",
"legacymetrics_user_event_recorder_unittest.cc", "legacymetrics_user_event_recorder_unittest.cc",
...@@ -132,6 +138,9 @@ test("cr_fuchsia_base_unittests") { ...@@ -132,6 +138,9 @@ test("cr_fuchsia_base_unittests") {
"//base:testfidl", "//base:testfidl",
"//base/test:run_all_unittests", "//base/test:run_all_unittests",
"//base/test:test_support", "//base/test:test_support",
"//components/version_info",
"//testing/gtest", "//testing/gtest",
"//third_party/fuchsia-sdk/sdk/pkg/sys_cpp",
"//third_party/fuchsia-sdk/sdk/pkg/sys_inspect_cpp",
] ]
} }
include_rules = [ include_rules = [
"+components/version_info",
"+mojo/public", "+mojo/public",
"+third_party/blink/public/common/messaging", "+third_party/blink/public/common/messaging",
"+third_party/blink/public/mojom/messaging", "+third_party/blink/public/mojom/messaging",
......
// Copyright 2020 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.
#include "fuchsia/base/inspect.h"
#include <lib/sys/inspect/cpp/component.h>
#include "components/version_info/version_info.h"
namespace cr_fuchsia {
namespace {
const char kVersion[] = "version";
const char kLastChange[] = "last_change_revision";
} // namespace
void PublishVersionInfoToInspect(sys::ComponentInspector* inspector) {
// These values are managed by the inspector, since they won't be updated over
// the lifetime of the component.
// TODO(https://crbug.com/1077428): Add release channel.
inspector->root().CreateString(kVersion, version_info::GetVersionNumber(),
inspector);
inspector->root().CreateString(kLastChange, version_info::GetLastChange(),
inspector);
}
} // namespace cr_fuchsia
// Copyright 2020 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 FUCHSIA_BASE_INSPECT_H_
#define FUCHSIA_BASE_INSPECT_H_
namespace sys {
class ComponentInspector;
} // namespace sys
namespace cr_fuchsia {
// Publish the Chromium version via the Inspect API. The lifetime of
// |inspector| has to be the same as the component it belongs to.
void PublishVersionInfoToInspect(sys::ComponentInspector* inspector);
} // namespace cr_fuchsia
#endif // FUCHSIA_BASE_INSPECT_H_
\ No newline at end of file
// Copyright 2020 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.
#include "fuchsia/base/inspect.h"
#include <lib/fdio/directory.h>
#include <lib/inspect/cpp/hierarchy.h>
#include <lib/inspect/cpp/reader.h>
#include <lib/inspect/service/cpp/reader.h>
#include <lib/sys/cpp/component_context.h>
#include <lib/sys/inspect/cpp/component.h>
#include <cstdint>
#include <memory>
#include "base/task/single_thread_task_executor.h"
#include "base/test/task_environment.h"
#include "components/version_info/version_info.h"
#include "fuchsia/base/mem_buffer_util.h"
#include "fuchsia/base/string_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cr_fuchsia {
namespace {
const char kVersion[] = "version";
const char kLastChange[] = "last_change_revision";
class InspectTest : public ::testing::Test {
public:
InspectTest() {
fidl::InterfaceHandle<fuchsia::io::Directory> incoming_directory;
auto incoming_services =
std::make_shared<sys::ServiceDirectory>(std::move(incoming_directory));
context_ = std::make_unique<sys::ComponentContext>(
std::move(incoming_services),
published_root_directory_.NewRequest().TakeChannel());
inspector_ = std::make_unique<sys::ComponentInspector>(context_.get());
base::RunLoop().RunUntilIdle();
}
InspectTest(const InspectTest&) = delete;
InspectTest& operator=(const InspectTest&) = delete;
protected:
base::test::SingleThreadTaskEnvironment task_environment_{
base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
std::unique_ptr<sys::ComponentContext> context_;
fidl::InterfaceHandle<fuchsia::io::Directory> published_root_directory_;
std::unique_ptr<sys::ComponentInspector> inspector_;
};
} // namespace
TEST_F(InspectTest, PublishVersionInfoToInspect) {
cr_fuchsia::PublishVersionInfoToInspect(inspector_.get());
fidl::InterfaceHandle<fuchsia::io::Directory> directory;
zx_status_t status = fdio_service_connect_at(
published_root_directory_.channel().get(), "diagnostics",
directory.NewRequest().TakeChannel().release());
ASSERT_EQ(ZX_OK, status);
std::unique_ptr<sys::ServiceDirectory> diagnostics =
std::make_unique<sys::ServiceDirectory>(std::move(directory));
// Access the inspect::Tree where the data is served. |tree| is in the
// directory created for the test, not the diagnostics directory for the test
// component.
fuchsia::inspect::TreePtr tree;
diagnostics->Connect(tree.NewRequest());
fuchsia::inspect::TreeContent content;
base::RunLoop run_loop;
tree->GetContent([&content, &run_loop](fuchsia::inspect::TreeContent c) {
content = std::move(c);
run_loop.Quit();
});
run_loop.Run();
// Parse the data as an inspect::Hierarchy.
ASSERT_TRUE(content.has_buffer());
std::string buffer_data;
cr_fuchsia::StringFromMemBuffer(content.buffer(), &buffer_data);
inspect::Hierarchy hierarchy =
inspect::ReadFromBuffer(cr_fuchsia::StringToBytes(buffer_data))
.take_value();
auto* property =
hierarchy.node().get_property<inspect::StringPropertyValue>(kVersion);
EXPECT_EQ(property->value(), version_info::GetVersionNumber());
property =
hierarchy.node().get_property<inspect::StringPropertyValue>(kLastChange);
EXPECT_EQ(property->value(), version_info::GetLastChange());
}
} // namespace cr_fuchsia
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/fuchsia/default_context.h" #include "base/fuchsia/default_context.h"
#include "base/fuchsia/process_context.h"
#include "base/fuchsia/scoped_service_binding.h" #include "base/fuchsia/scoped_service_binding.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/message_loop/message_pump_type.h" #include "base/message_loop/message_pump_type.h"
...@@ -17,6 +18,7 @@ ...@@ -17,6 +18,7 @@
#include "base/task/single_thread_task_executor.h" #include "base/task/single_thread_task_executor.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
#include "fuchsia/base/init_logging.h" #include "fuchsia/base/init_logging.h"
#include "fuchsia/base/inspect.h"
#include "fuchsia/base/lifecycle_impl.h" #include "fuchsia/base/lifecycle_impl.h"
#include "fuchsia/engine/context_provider_impl.h" #include "fuchsia/engine/context_provider_impl.h"
...@@ -68,6 +70,9 @@ int ContextProviderMain() { ...@@ -68,6 +70,9 @@ int ContextProviderMain() {
base::fuchsia::ScopedServiceBinding<fuchsia::web::Debug> debug_binding( base::fuchsia::ScopedServiceBinding<fuchsia::web::Debug> debug_binding(
directory->debug_dir(), &context_provider); directory->debug_dir(), &context_provider);
// Publish version information for this component to Inspect.
cr_fuchsia::PublishVersionInfoToInspect(base::ComponentInspectorForProcess());
// Publish the Lifecycle service, used by the framework to request that the // Publish the Lifecycle service, used by the framework to request that the
// service terminate. // service terminate.
base::RunLoop run_loop; base::RunLoop run_loop;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/fuchsia/default_context.h" #include "base/fuchsia/default_context.h"
#include "base/fuchsia/process_context.h"
#include "base/fuchsia/scoped_service_binding.h" #include "base/fuchsia/scoped_service_binding.h"
#include "base/message_loop/message_pump_type.h" #include "base/message_loop/message_pump_type.h"
#include "base/optional.h" #include "base/optional.h"
...@@ -15,6 +16,7 @@ ...@@ -15,6 +16,7 @@
#include "fuchsia/base/config_reader.h" #include "fuchsia/base/config_reader.h"
#include "fuchsia/base/fuchsia_dir_scheme.h" #include "fuchsia/base/fuchsia_dir_scheme.h"
#include "fuchsia/base/init_logging.h" #include "fuchsia/base/init_logging.h"
#include "fuchsia/base/inspect.h"
#include "fuchsia/runners/cast/cast_runner.h" #include "fuchsia/runners/cast/cast_runner.h"
namespace { namespace {
...@@ -53,6 +55,9 @@ int main(int argc, char** argv) { ...@@ -53,6 +55,9 @@ int main(int argc, char** argv) {
->outgoing() ->outgoing()
->ServeFromStartupInfo(); ->ServeFromStartupInfo();
// Publish version information for this component to Inspect.
cr_fuchsia::PublishVersionInfoToInspect(base::ComponentInspectorForProcess());
// Run until there are no Components, or the last service client channel is // Run until there are no Components, or the last service client channel is
// closed. // closed.
// TODO(https://crbug.com/952560): Implement Components v2 graceful exit. // TODO(https://crbug.com/952560): Implement Components v2 graceful exit.
......
...@@ -7,12 +7,14 @@ ...@@ -7,12 +7,14 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/fuchsia/default_context.h" #include "base/fuchsia/default_context.h"
#include "base/fuchsia/file_utils.h" #include "base/fuchsia/file_utils.h"
#include "base/fuchsia/process_context.h"
#include "base/fuchsia/scoped_service_binding.h" #include "base/fuchsia/scoped_service_binding.h"
#include "base/message_loop/message_pump_type.h" #include "base/message_loop/message_pump_type.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/task/single_thread_task_executor.h" #include "base/task/single_thread_task_executor.h"
#include "fuchsia/base/fuchsia_dir_scheme.h" #include "fuchsia/base/fuchsia_dir_scheme.h"
#include "fuchsia/base/init_logging.h" #include "fuchsia/base/init_logging.h"
#include "fuchsia/base/inspect.h"
#include "fuchsia/runners/buildflags.h" #include "fuchsia/runners/buildflags.h"
#include "fuchsia/runners/common/web_content_runner.h" #include "fuchsia/runners/common/web_content_runner.h"
...@@ -67,6 +69,9 @@ int main(int argc, char** argv) { ...@@ -67,6 +69,9 @@ int main(int argc, char** argv) {
->outgoing() ->outgoing()
->ServeFromStartupInfo(); ->ServeFromStartupInfo();
// Publish version information for this component to Inspect.
cr_fuchsia::PublishVersionInfoToInspect(base::ComponentInspectorForProcess());
// Run until there are no Components, or the last service client channel is // Run until there are no Components, or the last service client channel is
// closed. // closed.
// TODO(https://crbug.com/952560): Implement Components v2 graceful exit. // TODO(https://crbug.com/952560): Implement Components v2 graceful exit.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment