[fuchsia] Support the new Component Framework in StartupContext
Allow StartupContext instances to be created from instances of the new fuchsia.component.runner.ComponentStartInfo. This will be used by Runner implementations for the new framework. Bug: 1065707 Change-Id: I0480de851b4570cf0de8430677fea6049a8b28ca Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3987824 Reviewed-by: Greg Thompson <grt@chromium.org> Commit-Queue: Wez <wez@chromium.org> Auto-Submit: Wez <wez@chromium.org> Cr-Commit-Position: refs/heads/main@{#1065992}
This commit is contained in:
base
@ -1938,6 +1938,7 @@ mixed_component("base") {
|
||||
# by public //base headers, which requires they be on the include path.
|
||||
# TODO(https://crbug.com/841171): Move these back to |deps|.
|
||||
public_deps += [
|
||||
"//third_party/fuchsia-sdk/sdk/fidl/fuchsia.component.runner",
|
||||
"//third_party/fuchsia-sdk/sdk/fidl/fuchsia.intl",
|
||||
"//third_party/fuchsia-sdk/sdk/fidl/fuchsia.io",
|
||||
"//third_party/fuchsia-sdk/sdk/fidl/fuchsia.logger",
|
||||
|
@ -5,11 +5,13 @@
|
||||
#include "base/fuchsia/startup_context.h"
|
||||
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
#include <fuchsia/io/cpp/fidl.h>
|
||||
#include <lib/sys/cpp/outgoing_directory.h>
|
||||
#include <lib/sys/cpp/service_directory.h>
|
||||
|
||||
#include "base/check.h"
|
||||
#include "base/check_op.h"
|
||||
#include "base/fuchsia/file_utils.h"
|
||||
#include "base/fuchsia/fuchsia_logging.h"
|
||||
@ -17,6 +19,36 @@
|
||||
|
||||
namespace base {
|
||||
|
||||
StartupContext::StartupContext(
|
||||
fuchsia::component::runner::ComponentStartInfo start_info) {
|
||||
std::unique_ptr<sys::ServiceDirectory> incoming_services;
|
||||
|
||||
// Component manager generates |flat_namespace|, so things are horribly broken
|
||||
// if |flat_namespace| is malformed.
|
||||
CHECK(start_info.has_ns());
|
||||
|
||||
// Find the /svc directory and wrap it into a sys::ServiceDirectory.
|
||||
auto& namespace_entries = *start_info.mutable_ns();
|
||||
for (auto& entry : namespace_entries) {
|
||||
CHECK(entry.has_path() && entry.has_directory());
|
||||
if (entry.path() == kServiceDirectoryPath) {
|
||||
incoming_services = std::make_unique<sys::ServiceDirectory>(
|
||||
std::move(*entry.mutable_directory()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If there is no service-directory in the namespace then `incoming_services`
|
||||
// may be null, in which case `svc()` will be null.
|
||||
component_context_ =
|
||||
std::make_unique<sys::ComponentContext>(std::move(incoming_services));
|
||||
if (start_info.has_outgoing_dir()) {
|
||||
outgoing_directory_request_ = std::move(*start_info.mutable_outgoing_dir());
|
||||
}
|
||||
}
|
||||
|
||||
StartupContext::~StartupContext() = default;
|
||||
|
||||
StartupContext::StartupContext(fuchsia::sys::StartupInfo startup_info) {
|
||||
std::unique_ptr<sys::ServiceDirectory> incoming_services;
|
||||
|
||||
@ -34,51 +66,6 @@ StartupContext::StartupContext(fuchsia::sys::StartupInfo startup_info) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(https://crbug.com/933834): Remove these workarounds when we migrate to
|
||||
// the new component manager.
|
||||
if (!incoming_services && startup_info.launch_info.flat_namespace) {
|
||||
LOG(WARNING) << "Falling back to LaunchInfo namespace";
|
||||
for (size_t i = 0;
|
||||
i < startup_info.launch_info.flat_namespace->paths.size(); ++i) {
|
||||
if (startup_info.launch_info.flat_namespace->paths[i] ==
|
||||
kServiceDirectoryPath) {
|
||||
incoming_services = std::make_unique<sys::ServiceDirectory>(
|
||||
std::move(startup_info.launch_info.flat_namespace->directories[i]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!incoming_services && startup_info.launch_info.additional_services) {
|
||||
LOG(WARNING) << "Falling back to additional ServiceList services";
|
||||
|
||||
// Construct a OutgoingDirectory and publish the additional services into
|
||||
// it.
|
||||
additional_services_.Bind(
|
||||
std::move(startup_info.launch_info.additional_services->provider));
|
||||
additional_services_directory_ = std::make_unique<sys::OutgoingDirectory>();
|
||||
for (auto& name : startup_info.launch_info.additional_services->names) {
|
||||
zx_status_t status = additional_services_directory_->AddPublicService(
|
||||
std::make_unique<vfs::Service>([this, name](
|
||||
zx::channel channel,
|
||||
async_dispatcher_t* dispatcher) {
|
||||
additional_services_->ConnectToService(name, std::move(channel));
|
||||
}),
|
||||
name);
|
||||
ZX_CHECK(status == ZX_OK, status)
|
||||
<< "AddPublicService(" << name << ") failed";
|
||||
}
|
||||
|
||||
// Publish those services to the caller as |incoming_services|.
|
||||
fidl::InterfaceHandle<fuchsia::io::Directory> incoming_directory;
|
||||
additional_services_directory_->GetOrCreateDirectory("svc")->Serve(
|
||||
fuchsia::io::OpenFlags::RIGHT_READABLE |
|
||||
fuchsia::io::OpenFlags::RIGHT_WRITABLE,
|
||||
incoming_directory.NewRequest().TakeChannel());
|
||||
incoming_services =
|
||||
std::make_unique<sys::ServiceDirectory>(std::move(incoming_directory));
|
||||
}
|
||||
|
||||
if (!incoming_services) {
|
||||
LOG(WARNING) << "Component started without a service directory";
|
||||
|
||||
@ -96,8 +83,6 @@ StartupContext::StartupContext(fuchsia::sys::StartupInfo startup_info) {
|
||||
std::move(startup_info.launch_info.directory_request);
|
||||
}
|
||||
|
||||
StartupContext::~StartupContext() = default;
|
||||
|
||||
void StartupContext::ServeOutgoingDirectory() {
|
||||
DCHECK(outgoing_directory_request_);
|
||||
component_context_->outgoing()->Serve(std::move(outgoing_directory_request_));
|
||||
|
@ -5,15 +5,17 @@
|
||||
#ifndef BASE_FUCHSIA_STARTUP_CONTEXT_H_
|
||||
#define BASE_FUCHSIA_STARTUP_CONTEXT_H_
|
||||
|
||||
#include <fuchsia/component/runner/cpp/fidl.h>
|
||||
#include <fuchsia/io/cpp/fidl.h>
|
||||
#include <fuchsia/sys/cpp/fidl.h>
|
||||
#include <lib/sys/cpp/component_context.h>
|
||||
#include <lib/zx/channel.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "base/base_export.h"
|
||||
|
||||
namespace sys {
|
||||
class ComponentContext;
|
||||
class ServiceDirectory;
|
||||
class OutgoingDirectory;
|
||||
} // namespace sys
|
||||
@ -25,10 +27,15 @@ namespace base {
|
||||
// directories, resolve launch URL etc).
|
||||
// Embedders may derived from StartupContext to e.g. add bound pointers to
|
||||
// embedder-specific services, as required.
|
||||
class BASE_EXPORT StartupContext {
|
||||
class BASE_EXPORT StartupContext final {
|
||||
public:
|
||||
explicit StartupContext(
|
||||
::fuchsia::component::runner::ComponentStartInfo start_info);
|
||||
~StartupContext();
|
||||
|
||||
// TODO(https://crbug.com/1065707): Remove this overload once the CFv1
|
||||
// Runner implementations are removed.
|
||||
explicit StartupContext(::fuchsia::sys::StartupInfo startup_info);
|
||||
virtual ~StartupContext();
|
||||
|
||||
StartupContext(const StartupContext&) = delete;
|
||||
StartupContext& operator=(const StartupContext&) = delete;
|
||||
@ -56,11 +63,6 @@ class BASE_EXPORT StartupContext {
|
||||
}
|
||||
|
||||
private:
|
||||
// TODO(https://crbug.com/933834): Remove these when we migrate to the new
|
||||
// component manager APIs.
|
||||
::fuchsia::sys::ServiceProviderPtr additional_services_;
|
||||
std::unique_ptr<sys::OutgoingDirectory> additional_services_directory_;
|
||||
|
||||
std::unique_ptr<sys::ComponentContext> component_context_;
|
||||
|
||||
// Used to store outgoing directory until ServeOutgoingDirectory() is called.
|
||||
|
Reference in New Issue
Block a user