Skip to main content

runtime_capabilities/fidl/
connector_router.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::router;
6use crate::{Connector, ConversionError, Router, WeakInstanceToken};
7use fidl::AsHandleRef;
8use fidl_fuchsia_component_sandbox as fsandbox;
9use fidl_fuchsia_io as fio;
10use futures::TryStreamExt;
11use std::sync::Arc;
12use vfs::directory::entry::DirectoryEntry;
13use vfs::execution_scope::ExecutionScope;
14
15impl crate::RemotableCapability for Router<Connector> {
16    fn try_into_directory_entry(
17        self,
18        scope: ExecutionScope,
19        token: WeakInstanceToken,
20    ) -> Result<Arc<dyn DirectoryEntry>, ConversionError> {
21        Ok(self.into_directory_entry(fio::DirentType::Service, scope, token))
22    }
23}
24
25impl crate::fidl::IntoFsandboxCapability for Router<Connector> {
26    fn into_fsandbox_capability(self, token: WeakInstanceToken) -> fsandbox::Capability {
27        let (client_end, sender_stream) =
28            fidl::endpoints::create_request_stream::<fsandbox::ConnectorRouterMarker>();
29        self.serve_and_register(sender_stream, client_end.as_handle_ref().koid().unwrap(), token);
30        fsandbox::Capability::ConnectorRouter(client_end)
31    }
32}
33
34impl Router<Connector> {
35    async fn serve_router(
36        self,
37        mut stream: fsandbox::ConnectorRouterRequestStream,
38        token: WeakInstanceToken,
39    ) -> Result<(), fidl::Error> {
40        while let Ok(Some(request)) = stream.try_next().await {
41            match request {
42                fsandbox::ConnectorRouterRequest::Route { payload, responder } => {
43                    let resp = match router::route_from_fidl(&self, payload, token.clone()).await {
44                        Ok(Some(c)) => {
45                            Ok(fsandbox::ConnectorRouterRouteResponse::Connector(c.into()))
46                        }
47                        Ok(None) => Ok(fsandbox::ConnectorRouterRouteResponse::Unavailable(
48                            fsandbox::Unit {},
49                        )),
50                        Err(e) => Err(e),
51                    };
52                    responder.send(resp)?;
53                }
54                fsandbox::ConnectorRouterRequest::_UnknownMethod { ordinal, .. } => {
55                    log::warn!(
56                        ordinal:%;
57                        "Received unknown ConnectorRouter request"
58                    );
59                }
60            }
61        }
62        Ok(())
63    }
64
65    /// Serves the `fuchsia.sandbox.Router` protocol and moves ourself into the registry.
66    pub fn serve_and_register(
67        self,
68        stream: fsandbox::ConnectorRouterRequestStream,
69        koid: zx::Koid,
70        token: WeakInstanceToken,
71    ) {
72        let router = self.clone();
73
74        // Move this capability into the registry.
75        crate::fidl::registry::insert(self.into(), koid, async move {
76            router.serve_router(stream, token).await.expect("failed to serve Router");
77        });
78    }
79}