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