settings/
event.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 crate::base::SettingType;
6use crate::message::base::Audience;
7use crate::{payload_convert, service};
8use settings_common::config;
9use settings_common::service_context::ExternalServiceEvent;
10use std::rc::Rc;
11
12#[derive(Clone, Debug, PartialEq)]
13pub enum Payload {
14    Event(Event),
15}
16
17/// Top level definition of events in the setting service. A new type should
18/// be defined for each component (setting, agent, proxy, etc.)
19#[derive(Clone, Debug, PartialEq)]
20pub enum Event {
21    Custom(&'static str),
22    CameraUpdate(camera_watcher::Event),
23    ConfigLoad(config::Event),
24    Earcon(earcon::Event),
25    MediaButtons(settings_media_buttons::Event),
26    Restore(restore::Event),
27    ExternalServiceEvent(ExternalServiceEvent),
28    Handler(SettingType, handler::Event),
29    Source(source::Event),
30}
31
32#[derive(PartialEq, Copy, Clone, Debug, Eq, Hash)]
33pub enum Address {
34    SettingProxy(SettingType),
35}
36
37pub(crate) mod camera_watcher {
38    #[derive(PartialEq, Clone, Debug, Eq, Hash)]
39    pub enum Event {
40        // Indicates that the camera's software mute state changed.
41        OnSWMuteState(bool),
42    }
43
44    impl From<bool> for Event {
45        fn from(muted: bool) -> Self {
46            Self::OnSWMuteState(muted)
47        }
48    }
49}
50
51pub(crate) mod earcon {
52    #[derive(PartialEq, Clone, Debug, Eq, Hash)]
53    pub enum Event {
54        // Indicates the specified earcon type was not played when it normally
55        // would.
56        Suppressed(EarconType),
57    }
58
59    #[derive(PartialEq, Clone, Debug, Eq, Hash)]
60    pub enum EarconType {
61        Volume,
62    }
63}
64
65pub(crate) mod source {
66    use crate::job::source::{Error, Id};
67
68    #[derive(PartialEq, Clone, Copy, Debug, Eq, Hash)]
69    pub enum Event {
70        Start(Id),
71        Complete(Id, Result<(), CompleteError>),
72    }
73
74    #[derive(PartialEq, Clone, Copy, Debug, Eq, Hash)]
75    pub enum CompleteError {
76        Unexpected,
77        InvalidInput,
78        InvalidPolicyInput,
79        Unsupported,
80    }
81
82    impl From<Error> for CompleteError {
83        fn from(err: Error) -> Self {
84            match err {
85                Error::Unexpected(_) => CompleteError::Unexpected,
86                Error::InvalidInput(_) => CompleteError::InvalidInput,
87                Error::InvalidPolicyInput(_) => CompleteError::InvalidPolicyInput,
88                Error::Unsupported => CompleteError::Unsupported,
89            }
90        }
91    }
92}
93
94pub(crate) mod handler {
95    use crate::handler::base::Request;
96    use crate::handler::setting_handler::ExitResult;
97
98    #[derive(PartialEq, Clone, Debug)]
99    pub enum Event {
100        Exit(ExitResult),
101        Request(Action, Request),
102        Teardown,
103    }
104
105    /// Possible actions taken on a `Request` by a Setting Handler.
106    #[derive(PartialEq, Clone, Debug)]
107    pub enum Action {
108        Execute,
109        Retry,
110        Timeout,
111        AttemptsExceeded,
112    }
113}
114
115pub(crate) mod restore {
116    use crate::base::SettingType;
117
118    #[derive(PartialEq, Clone, Debug, Eq, Hash)]
119    pub enum Event {
120        // Indicates that the setting type does nothing for a call to restore.
121        NoOp(SettingType),
122    }
123}
124
125payload_convert!(Event, Payload);
126
127/// Publisher is a helper for producing logs. It simplifies message creation for
128/// each event and associates an address with these messages at construction.
129#[derive(Clone, Debug)]
130pub struct Publisher {
131    messenger: service::message::Messenger,
132}
133
134impl Publisher {
135    pub(crate) async fn create(
136        delegate: &service::message::Delegate,
137        messenger_type: service::message::MessengerType,
138    ) -> Publisher {
139        let (messenger, _) = delegate
140            .create(messenger_type)
141            .await
142            .expect("should be able to retrieve messenger for publisher");
143
144        Publisher { messenger }
145    }
146
147    /// Broadcasts event to the message hub.
148    pub(crate) fn send_event(&self, event: Event) {
149        let _ = self.messenger.message(Payload::Event(event).into(), Audience::EventSink);
150    }
151}
152
153pub(crate) mod subscriber {
154    use super::*;
155    use futures::future::LocalBoxFuture;
156
157    pub type BlueprintHandle = Rc<dyn Blueprint>;
158
159    /// The Subscriber Blueprint is used for spawning new subscribers on demand
160    /// in the environment.
161    pub trait Blueprint {
162        fn create(&self, delegate: service::message::Delegate) -> LocalBoxFuture<'static, ()>;
163    }
164}