sandbox/fidl/
dir_entry_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, DirEntry, 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<DirEntry> {
15    fn try_into_directory_entry(
16        self,
17        scope: ExecutionScope,
18    ) -> Result<Arc<dyn DirectoryEntry>, ConversionError> {
19        Ok(self.into_directory_entry(
20            // TODO(https://fxbug.dev/340891837): This assumes the DirEntry type is Service.
21            // Unfortunately, with the current API there is no good way to get the DirEntry type in
22            // advance. This problem should go away once we revamp or remove DirEntry.
23            fio::DirentType::Service,
24            scope,
25        ))
26    }
27}
28
29impl From<Router<DirEntry>> for fsandbox::Capability {
30    fn from(router: Router<DirEntry>) -> Self {
31        let (client_end, sender_stream) =
32            fidl::endpoints::create_request_stream::<fsandbox::DirEntryRouterMarker>();
33        router.serve_and_register(sender_stream, client_end.get_koid().unwrap());
34        fsandbox::Capability::DirEntryRouter(client_end)
35    }
36}
37
38impl TryFrom<RouterResponse<DirEntry>> for fsandbox::DirEntryRouterRouteResponse {
39    type Error = fsandbox::RouterError;
40
41    fn try_from(resp: RouterResponse<DirEntry>) -> Result<Self, Self::Error> {
42        match resp {
43            RouterResponse::<DirEntry>::Capability(c) => {
44                Ok(fsandbox::DirEntryRouterRouteResponse::DirEntry(c.into()))
45            }
46            RouterResponse::<DirEntry>::Unavailable => {
47                Ok(fsandbox::DirEntryRouterRouteResponse::Unavailable(fsandbox::Unit {}))
48            }
49            RouterResponse::<DirEntry>::Debug(_) => Err(fsandbox::RouterError::NotSupported),
50        }
51    }
52}
53
54impl Router<DirEntry> {
55    async fn serve_router(
56        self,
57        mut stream: fsandbox::DirEntryRouterRequestStream,
58    ) -> Result<(), fidl::Error> {
59        while let Ok(Some(request)) = stream.try_next().await {
60            match request {
61                fsandbox::DirEntryRouterRequest::Route { payload, responder } => {
62                    responder.send(router::route_from_fidl(&self, payload).await)?;
63                }
64                fsandbox::DirEntryRouterRequest::_UnknownMethod { ordinal, .. } => {
65                    log::warn!(
66                        ordinal:%; "Received unknown DirEntryRouter request"
67                    );
68                }
69            }
70        }
71        Ok(())
72    }
73
74    /// Serves the `fuchsia.sandbox.Router` protocol and moves ourself into the registry.
75    pub fn serve_and_register(self, stream: fsandbox::DirEntryRouterRequestStream, koid: zx::Koid) {
76        let router = self.clone();
77
78        // Move this capability into the registry.
79        crate::fidl::registry::insert(self.into(), koid, async move {
80            router.serve_router(stream).await.expect("failed to serve Router");
81        });
82    }
83}