settings/
event.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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::base::SettingType;
use crate::message::base::Audience;
use crate::service_context::ExternalServiceEvent;
use crate::{config, payload_convert, service};
use std::rc::Rc;

#[derive(Clone, Debug, PartialEq)]
pub enum Payload {
    Event(Event),
}

/// Top level definition of events in the setting service. A new type should
/// be defined for each component (setting, agent, proxy, etc.)
#[derive(Clone, Debug, PartialEq)]
pub enum Event {
    Custom(&'static str),
    CameraUpdate(camera_watcher::Event),
    ConfigLoad(config::base::Event),
    Earcon(earcon::Event),
    MediaButtons(media_buttons::Event),
    Restore(restore::Event),
    ExternalServiceEvent(ExternalServiceEvent),
    Handler(SettingType, handler::Event),
    Source(source::Event),
}

#[derive(PartialEq, Copy, Clone, Debug, Eq, Hash)]
pub enum Address {
    SettingProxy(SettingType),
}

pub(crate) mod camera_watcher {
    #[derive(PartialEq, Clone, Debug, Eq, Hash)]
    pub enum Event {
        // Indicates that the camera's software mute state changed.
        OnSWMuteState(bool),
    }

    impl From<bool> for Event {
        fn from(muted: bool) -> Self {
            Self::OnSWMuteState(muted)
        }
    }
}

pub(crate) mod earcon {
    #[derive(PartialEq, Clone, Debug, Eq, Hash)]
    pub enum Event {
        // Indicates the specified earcon type was not played when it normally
        // would.
        Suppressed(EarconType),
    }

    #[derive(PartialEq, Clone, Debug, Eq, Hash)]
    pub enum EarconType {
        Volume,
    }
}

pub(crate) mod source {
    use crate::job::source::{Error, Id};

    #[derive(PartialEq, Clone, Copy, Debug, Eq, Hash)]
    pub enum Event {
        Start(Id),
        Complete(Id, Result<(), CompleteError>),
    }

    #[derive(PartialEq, Clone, Copy, Debug, Eq, Hash)]
    pub enum CompleteError {
        Unexpected,
        InvalidInput,
        InvalidPolicyInput,
        Unsupported,
    }

    impl From<Error> for CompleteError {
        fn from(err: Error) -> Self {
            match err {
                Error::Unexpected(_) => CompleteError::Unexpected,
                Error::InvalidInput(_) => CompleteError::InvalidInput,
                Error::InvalidPolicyInput(_) => CompleteError::InvalidPolicyInput,
                Error::Unsupported => CompleteError::Unsupported,
            }
        }
    }
}

pub(crate) mod handler {
    use crate::handler::base::Request;
    use crate::handler::setting_handler::ExitResult;

    #[derive(PartialEq, Clone, Debug)]
    pub enum Event {
        Exit(ExitResult),
        Request(Action, Request),
        Teardown,
    }

    /// Possible actions taken on a `Request` by a Setting Handler.
    #[derive(PartialEq, Clone, Debug)]
    pub enum Action {
        Execute,
        Retry,
        Timeout,
        AttemptsExceeded,
    }
}

pub(crate) mod media_buttons {
    use crate::input::MediaButtons;

    #[derive(PartialEq, Clone, Debug)]
    pub enum Event {
        OnButton(MediaButtons),
    }

    impl From<MediaButtons> for Event {
        fn from(button_types: MediaButtons) -> Self {
            Self::OnButton(button_types)
        }
    }
}

pub(crate) mod restore {
    use crate::base::SettingType;

    #[derive(PartialEq, Clone, Debug, Eq, Hash)]
    pub enum Event {
        // Indicates that the setting type does nothing for a call to restore.
        NoOp(SettingType),
    }
}

payload_convert!(Event, Payload);

/// Publisher is a helper for producing logs. It simplifies message creation for
/// each event and associates an address with these messages at construction.
#[derive(Clone, Debug)]
pub struct Publisher {
    messenger: service::message::Messenger,
}

impl Publisher {
    pub(crate) async fn create(
        delegate: &service::message::Delegate,
        messenger_type: service::message::MessengerType,
    ) -> Publisher {
        let (messenger, _) = delegate
            .create(messenger_type)
            .await
            .expect("should be able to retrieve messenger for publisher");

        Publisher { messenger }
    }

    /// Broadcasts event to the message hub.
    pub(crate) fn send_event(&self, event: Event) {
        let _ = self.messenger.message(Payload::Event(event).into(), Audience::EventSink);
    }
}

pub(crate) mod subscriber {
    use super::*;
    use futures::future::LocalBoxFuture;

    pub type BlueprintHandle = Rc<dyn Blueprint>;

    /// The Subscriber Blueprint is used for spawning new subscribers on demand
    /// in the environment.
    pub trait Blueprint {
        fn create(&self, delegate: service::message::Delegate) -> LocalBoxFuture<'static, ()>;
    }
}