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, ConversionError, Message, Receiver, WeakInstanceToken};
7use fidl::endpoints::ClientEnd;
8use fidl::handle::Channel;
9use fidl_fuchsia_component_sandbox as fsandbox;
10use fuchsia_async as fasync;
11use futures::channel::mpsc;
12use std::sync::Arc;
13use vfs::directory::entry::DirectoryEntry;
14use vfs::execution_scope::ExecutionScope;
15
16impl Connector {
17    pub(crate) fn send_channel(&self, channel: Channel) -> Result<(), ()> {
18        self.send(Message { channel })
19    }
20
21    pub(crate) fn new_with_fidl_receiver(
22        receiver_client: ClientEnd<fsandbox::ReceiverMarker>,
23        scope: &fasync::Scope,
24    ) -> Self {
25        let (sender, receiver) = mpsc::unbounded();
26        let receiver = Receiver::new(receiver);
27        // Exits when ServerEnd<Receiver> is closed
28        scope.spawn(receiver.handle_receiver(receiver_client.into_proxy()));
29        Self::new_sendable(sender)
30    }
31}
32
33impl crate::RemotableCapability for Connector {
34    fn try_into_directory_entry(
35        self,
36        _scope: ExecutionScope,
37        _token: WeakInstanceToken,
38    ) -> Result<Arc<dyn DirectoryEntry>, ConversionError> {
39        Ok(vfs::service::endpoint(move |_scope, server_end| {
40            let _ = self.send_channel(server_end.into_zx_channel().into());
41        }))
42    }
43}
44
45impl From<Connector> for fsandbox::Connector {
46    fn from(value: Connector) -> Self {
47        fsandbox::Connector { token: registry::insert_token(value.into()) }
48    }
49}
50
51impl crate::fidl::IntoFsandboxCapability for Connector {
52    fn into_fsandbox_capability(self, _token: WeakInstanceToken) -> fsandbox::Capability {
53        fsandbox::Capability::Connector(self.into())
54    }
55}