Skip to main content

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