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