sandbox/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::{ConversionError, Data, Router, RouterResponse, WeakInstanceToken};
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<Data> {
15    fn try_into_directory_entry(
16        self,
17        scope: ExecutionScope,
18        token: WeakInstanceToken,
19    ) -> Result<Arc<dyn DirectoryEntry>, ConversionError> {
20        Ok(self.into_directory_entry(fio::DirentType::Service, scope, token))
21    }
22}
23
24impl TryFrom<RouterResponse<Data>> for fsandbox::DataRouterRouteResponse {
25    type Error = fsandbox::RouterError;
26
27    fn try_from(resp: RouterResponse<Data>) -> Result<Self, Self::Error> {
28        match resp {
29            RouterResponse::<Data>::Capability(c) => {
30                Ok(fsandbox::DataRouterRouteResponse::Data(c.into()))
31            }
32            RouterResponse::<Data>::Unavailable => {
33                Ok(fsandbox::DataRouterRouteResponse::Unavailable(fsandbox::Unit {}))
34            }
35            RouterResponse::<Data>::Debug(_) => Err(fsandbox::RouterError::NotSupported),
36        }
37    }
38}
39
40impl crate::fidl::IntoFsandboxCapability for Router<Data> {
41    fn into_fsandbox_capability(self, token: WeakInstanceToken) -> fsandbox::Capability {
42        let (client_end, sender_stream) =
43            fidl::endpoints::create_request_stream::<fsandbox::DataRouterMarker>();
44        self.serve_and_register(sender_stream, client_end.get_koid().unwrap(), token);
45        fsandbox::Capability::DataRouter(client_end)
46    }
47}
48
49impl Router<Data> {
50    async fn serve_router(
51        self,
52        mut stream: fsandbox::DataRouterRequestStream,
53        token: WeakInstanceToken,
54    ) -> Result<(), fidl::Error> {
55        while let Ok(Some(request)) = stream.try_next().await {
56            match request {
57                fsandbox::DataRouterRequest::Route { payload, responder } => {
58                    responder.send(router::route_from_fidl(&self, payload, token.clone()).await)?;
59                }
60                fsandbox::DataRouterRequest::_UnknownMethod { ordinal, .. } => {
61                    log::warn!(ordinal:%; "Received unknown DataRouter request");
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::DataRouterRequestStream,
72        koid: zx::Koid,
73        token: WeakInstanceToken,
74    ) {
75        let router = self.clone();
76
77        // Move this capability into the registry.
78        crate::fidl::registry::insert(self.into(), koid, async move {
79            router.serve_router(stream, token).await.expect("failed to serve Router");
80        });
81    }
82}