carnelian/app/strategies/
flatland.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::app::strategies::base::AppStrategy;
use crate::app::{InternalSender, MessageInternal};
use crate::view::strategies::base::{FlatlandParams, ViewStrategyParams, ViewStrategyPtr};
use crate::view::strategies::flatland::FlatlandViewStrategy;
use crate::view::ViewKey;
use anyhow::{bail, Context as _, Error};
use async_trait::async_trait;
use fidl_fuchsia_ui_app::{CreateView2Args, ViewProviderRequest, ViewProviderRequestStream};
use fuchsia_async::{self as fasync};
use fuchsia_component::server::{ServiceFs, ServiceObjLocal};
use fuchsia_scenic::flatland::ViewCreationTokenPair;
use futures::channel::mpsc::UnboundedSender;
use futures::{TryFutureExt, TryStreamExt};

pub(crate) struct FlatlandAppStrategy;

#[async_trait(?Send)]
impl AppStrategy for FlatlandAppStrategy {
    async fn create_view_strategy(
        &mut self,
        key: ViewKey,
        app_sender: UnboundedSender<MessageInternal>,
        strategy_params: ViewStrategyParams,
    ) -> Result<ViewStrategyPtr, Error> {
        match strategy_params {
            ViewStrategyParams::Flatland(flatland_params) => {
                Ok(FlatlandViewStrategy::new(key, flatland_params, app_sender.clone()).await?)
            }
            _ => bail!(
                "Incorrect ViewStrategyParams passed to create_view_strategy for Scenic (Flatland)"
            ),
        }
    }

    fn create_view_strategy_params_for_additional_view(
        &mut self,
        _view_key: ViewKey,
    ) -> ViewStrategyParams {
        todo!("Additional views not yet supported on Scenic (Flatland)");
    }

    fn create_view_for_testing(
        &self,
        app_sender: &UnboundedSender<MessageInternal>,
    ) -> Result<(), Error> {
        let tokens = ViewCreationTokenPair::new().context("ViewCreationTokenPair::new")?;
        app_sender
            .unbounded_send(MessageInternal::CreateView(ViewStrategyParams::Flatland(
                FlatlandParams {
                    args: CreateView2Args {
                        view_creation_token: Some(tokens.view_creation_token),
                        ..Default::default()
                    },
                    debug_name: Some("Test View".to_string()),
                },
            )))
            .expect("send");
        Ok(())
    }

    fn supports_scenic(&self) -> bool {
        return true;
    }

    fn start_services<'a, 'b>(
        &self,
        app_sender: UnboundedSender<MessageInternal>,
        fs: &'a mut ServiceFs<ServiceObjLocal<'b, ()>>,
    ) -> Result<(), Error> {
        let mut public = fs.dir("svc");

        let sender = app_sender;
        let f = move |stream: ViewProviderRequestStream| {
            let sender = sender.clone();
            fasync::Task::local(
                stream
                    .try_for_each(move |req| {
                        #[allow(unreachable_patterns)]
                        match req {
                            ViewProviderRequest::CreateView2 { args, .. } => {
                                sender
                                    .unbounded_send(MessageInternal::CreateView(
                                        ViewStrategyParams::Flatland(FlatlandParams {
                                            args,
                                            debug_name: Some("Carnelian View".to_string()),
                                        }),
                                    ))
                                    .expect("unbounded_send");
                            }

                            _ => {}
                        };
                        futures::future::ready(Ok(()))
                    })
                    .unwrap_or_else(|e| eprintln!("error running ViewProvider server: {:?}", e)),
            )
            .detach()
        };
        public.add_fidl_service(f);

        Ok(())
    }

    async fn post_setup(&mut self, _internal_sender: &InternalSender) -> Result<(), Error> {
        Ok(())
    }
}