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
// 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 {
    anyhow::{Context, Error},
    base64::engine::{general_purpose::STANDARD as BASE64_STANDARD, Engine as _},
    fidl_fuchsia_feedback::{DataProviderMarker, GetSnapshotParameters},
    fuchsia_component::client::connect_to_protocol,
    fuchsia_zircon::DurationNum,
};

/// Facade providing access to feedback interface.
#[derive(Debug)]
pub struct FeedbackDataProviderFacade {}

impl FeedbackDataProviderFacade {
    pub fn new() -> FeedbackDataProviderFacade {
        FeedbackDataProviderFacade {}
    }

    pub async fn get_snapshot(&self) -> Result<serde_json::Value, Error> {
        let data_provider =
            connect_to_protocol::<DataProviderMarker>().context("connect to DataProvider")?;
        let params = GetSnapshotParameters {
            collection_timeout_per_data: Some(2.minutes().into_nanos()),
            ..Default::default()
        };
        let snapshot = data_provider.get_snapshot(params).await.context("get snapshot")?;
        match snapshot.archive {
            Some(archive) => {
                let mut buf = vec![0; archive.value.size as usize];
                archive.value.vmo.read(&mut buf, 0).context("reading vmo")?;
                let result = BASE64_STANDARD.encode(&buf);
                return Ok(serde_json::json!({
                    "zip": result,
                }));
            }
            None => Err(format_err!("No zip file data in the snapshot response")),
        }
    }
}