Skip to main content

runtime_capabilities/fidl/
data_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::{Data, 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<Data>> {
14    fn into_fsandbox_capability(self, token: Arc<WeakInstanceToken>) -> fsandbox::Capability {
15        fsandbox::Capability::DataRouter(self.into_fsandbox_router(token))
16    }
17}
18
19impl Router<Data> {
20    fn into_fsandbox_router(
21        self: Arc<Self>,
22        token: Arc<WeakInstanceToken>,
23    ) -> ClientEnd<fsandbox::DataRouterMarker> {
24        let (client_end, sender_stream) =
25            fidl::endpoints::create_request_stream::<fsandbox::DataRouterMarker>();
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::DataRouterRequestStream,
33        token: Arc<WeakInstanceToken>,
34    ) -> Result<(), fidl::Error> {
35        while let Ok(Some(request)) = stream.try_next().await {
36            match request {
37                fsandbox::DataRouterRequest::Route { payload, responder } => {
38                    let resp = match router::route_from_fidl(&self, payload, token.clone()).await {
39                        Ok(Some(c)) => {
40                            let data = c.to_fsandbox();
41                            Ok(fsandbox::DataRouterRouteResponse::Data(data))
42                        }
43                        Ok(None) => {
44                            Ok(fsandbox::DataRouterRouteResponse::Unavailable(fsandbox::Unit {}))
45                        }
46                        Err(e) => Err(e),
47                    };
48                    responder.send(resp)?;
49                }
50                fsandbox::DataRouterRequest::_UnknownMethod { ordinal, .. } => {
51                    log::warn!(ordinal:%; "Received unknown DataRouter request");
52                }
53            }
54        }
55        Ok(())
56    }
57
58    /// Serves the `fuchsia.sandbox.Router` protocol and moves ourself into the registry.
59    pub fn serve_and_register(
60        self: Arc<Self>,
61        stream: fsandbox::DataRouterRequestStream,
62        koid: zx::Koid,
63        token: Arc<WeakInstanceToken>,
64    ) {
65        let router = self.clone();
66
67        // Move this capability into the registry.
68        crate::fidl::registry::insert(self.into(), koid, async move {
69            router.serve_router(stream, token).await.expect("failed to serve Router");
70        });
71    }
72}