Skip to main content

runtime_capabilities/fidl/
connector.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::fidl::registry;
6use crate::{Connector, Receiver, WeakInstanceToken};
7use fidl::endpoints::ClientEnd;
8use fidl_fuchsia_component_sandbox as fsandbox;
9use fuchsia_async as fasync;
10use futures::channel::mpsc;
11use std::sync::Arc;
12
13impl Connector {
14    pub(crate) fn new_with_fidl_receiver(
15        receiver_client: ClientEnd<fsandbox::ReceiverMarker>,
16        scope: &fasync::Scope,
17    ) -> Arc<Self> {
18        let (sender, receiver) = mpsc::unbounded();
19        let receiver = Receiver::new(receiver);
20        // Exits when ServerEnd<Receiver> is closed
21        scope.spawn(receiver.handle_receiver(receiver_client.into_proxy()));
22        Self::new_sendable(sender)
23    }
24
25    pub(crate) fn to_fsandbox(self: Arc<Self>) -> fsandbox::Connector {
26        fsandbox::Connector { token: registry::insert_token(self.into()) }
27    }
28}
29
30impl crate::fidl::IntoFsandboxCapability for Arc<Connector> {
31    fn into_fsandbox_capability(self, _token: Arc<WeakInstanceToken>) -> fsandbox::Capability {
32        fsandbox::Capability::Connector(fsandbox::Connector {
33            token: registry::insert_token(self.into()),
34        })
35    }
36}