xref: /aosp_15_r20/external/cronet/base/fuchsia/startup_context.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/fuchsia/startup_context.h"
6 
7 #include <tuple>
8 #include <utility>
9 
10 #include <fuchsia/io/cpp/fidl.h>
11 #include <lib/sys/cpp/outgoing_directory.h>
12 #include <lib/sys/cpp/service_directory.h>
13 
14 #include "base/check.h"
15 #include "base/check_op.h"
16 #include "base/fuchsia/file_utils.h"
17 #include "base/fuchsia/fuchsia_logging.h"
18 #include "base/logging.h"
19 
20 namespace base {
21 
StartupContext(fuchsia::component::runner::ComponentStartInfo start_info)22 StartupContext::StartupContext(
23     fuchsia::component::runner::ComponentStartInfo start_info) {
24   std::unique_ptr<sys::ServiceDirectory> incoming_services;
25 
26   // Component manager generates |flat_namespace|, so things are horribly broken
27   // if |flat_namespace| is malformed.
28   CHECK(start_info.has_ns());
29 
30   // Find the /svc directory and wrap it into a sys::ServiceDirectory.
31   auto& namespace_entries = *start_info.mutable_ns();
32   for (auto& entry : namespace_entries) {
33     CHECK(entry.has_path() && entry.has_directory());
34     if (entry.path() == kServiceDirectoryPath) {
35       incoming_services = std::make_unique<sys::ServiceDirectory>(
36           std::move(*entry.mutable_directory()));
37       break;
38     }
39   }
40 
41   // If there is no service-directory in the namespace then `incoming_services`
42   // may be null, in which case `svc()` will be null.
43   component_context_ =
44       std::make_unique<sys::ComponentContext>(std::move(incoming_services));
45   if (start_info.has_outgoing_dir()) {
46     outgoing_directory_request_ = std::move(*start_info.mutable_outgoing_dir());
47   }
48 }
49 
50 StartupContext::~StartupContext() = default;
51 
ServeOutgoingDirectory()52 void StartupContext::ServeOutgoingDirectory() {
53   DCHECK(outgoing_directory_request_);
54   component_context_->outgoing()->Serve(std::move(outgoing_directory_request_));
55 }
56 
57 }  // namespace base
58