sl4f_lib/feedback_data_provider/
facade.rs

1// Copyright 2020 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 anyhow::{Context, Error};
6use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
7use base64::engine::Engine as _;
8use fidl_fuchsia_feedback::{DataProviderMarker, GetSnapshotParameters};
9use fuchsia_component::client::connect_to_protocol;
10
11/// Facade providing access to feedback interface.
12#[derive(Debug)]
13pub struct FeedbackDataProviderFacade {}
14
15impl FeedbackDataProviderFacade {
16    pub fn new() -> FeedbackDataProviderFacade {
17        FeedbackDataProviderFacade {}
18    }
19
20    pub async fn get_snapshot(&self) -> Result<serde_json::Value, Error> {
21        let data_provider =
22            connect_to_protocol::<DataProviderMarker>().context("connect to DataProvider")?;
23        let params = GetSnapshotParameters {
24            collection_timeout_per_data: Some(zx::MonotonicDuration::from_minutes(2).into_nanos()),
25            ..Default::default()
26        };
27        let snapshot = data_provider.get_snapshot(params).await.context("get snapshot")?;
28        match snapshot.archive {
29            Some(archive) => {
30                let mut buf = vec![0; archive.value.size as usize];
31                archive.value.vmo.read(&mut buf, 0).context("reading vmo")?;
32                let result = BASE64_STANDARD.encode(&buf);
33                return Ok(serde_json::json!({
34                    "zip": result,
35                }));
36            }
37            None => Err(format_err!("No zip file data in the snapshot response")),
38        }
39    }
40}