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