omaha_client/state_machine/
observer.rs

1// Copyright 2019 The Fuchsia Authors
2//
3// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to
7// those terms.
8
9use crate::{
10    common::{ProtocolState, UpdateCheckSchedule},
11    installer::ProgressObserver,
12    protocol::response::Response,
13    state_machine::{update_check, State, UpdateCheckError},
14};
15use futures::{channel::mpsc, future::BoxFuture, prelude::*};
16
17/// Events emitted by the state machine.
18#[derive(Debug)]
19pub enum StateMachineEvent {
20    StateChange(State),
21    ScheduleChange(UpdateCheckSchedule),
22    ProtocolStateChange(ProtocolState),
23    UpdateCheckResult(Result<update_check::Response, UpdateCheckError>),
24    InstallProgressChange(InstallProgress),
25    OmahaServerResponse(Response),
26    InstallerError(Option<Box<dyn std::error::Error + Send + 'static>>),
27}
28
29#[derive(Debug)]
30pub struct InstallProgress {
31    pub progress: f32,
32}
33
34pub(super) struct StateMachineProgressObserver(pub(super) mpsc::Sender<InstallProgress>);
35
36impl ProgressObserver for StateMachineProgressObserver {
37    fn receive_progress(
38        &self,
39        _operation: Option<&str>,
40        progress: f32,
41        _total_size: Option<u64>,
42        _size_so_far: Option<u64>,
43    ) -> BoxFuture<'_, ()> {
44        async move {
45            let _ = self.0.clone().send(InstallProgress { progress }).await;
46        }
47        .boxed()
48    }
49}