xref: /aosp_15_r20/external/cronet/base/fuchsia/process_lifecycle.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 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/process_lifecycle.h"
6 
7 #include <lib/async/default.h>
8 #include <zircon/process.h>
9 #include <zircon/processargs.h>
10 
11 #include "base/check.h"
12 #include "base/fuchsia/fuchsia_logging.h"
13 
14 namespace base {
15 
ProcessLifecycle(base::OnceClosure on_stop)16 ProcessLifecycle::ProcessLifecycle(base::OnceClosure on_stop)
17     : on_stop_(std::move(on_stop)) {
18   // Sanity-check that an instance was not already created.
19   static bool was_created = false;
20   DCHECK(!was_created);
21   was_created = true;
22 
23   // Under Components Framework v2 the ELF runner provides PA_LIFECYCLE.
24   zx::channel lifecycle_server_channel(zx_take_startup_handle(PA_LIFECYCLE));
25   CHECK(lifecycle_server_channel.is_valid());
26   fidl::ServerEnd<fuchsia_process_lifecycle::Lifecycle> lifecycle_server_end(
27       std::move(lifecycle_server_channel));
28   binding_.emplace(async_get_default_dispatcher(),
29                    std::move(lifecycle_server_end), this,
30                    fidl::kIgnoreBindingClosure);
31 }
32 
33 ProcessLifecycle::~ProcessLifecycle() = default;
34 
Stop(ProcessLifecycle::StopCompleter::Sync & completer)35 void ProcessLifecycle::Stop(ProcessLifecycle::StopCompleter::Sync& completer) {
36   if (on_stop_) {
37     std::move(on_stop_).Run();
38   }
39 }
40 
41 }  // namespace base
42