use crate::agent::{
AgentError, Context as AgentContext, Invocation, InvocationResult, Lifespan, Payload,
};
use crate::base::SettingType;
use crate::event::{media_buttons, Event, Publisher};
use crate::handler::base::{Payload as HandlerPayload, Request};
use crate::input::{monitor_media_buttons, MediaButtons};
use crate::message::base::Audience;
use crate::service_context::ServiceContext;
use crate::{service, trace_guard};
use fidl_fuchsia_ui_input::MediaButtonsEvent;
use futures::StreamExt;
use std::collections::HashSet;
use std::rc::Rc;
use {fuchsia_async as fasync, fuchsia_trace as ftrace};
fn get_event_setting_types() -> HashSet<SettingType> {
vec![SettingType::Audio, SettingType::Light, SettingType::Input].into_iter().collect()
}
pub(crate) struct MediaButtonsAgent {
publisher: Publisher,
messenger: service::message::Messenger,
recipient_settings: HashSet<SettingType>,
}
impl MediaButtonsAgent {
pub(crate) async fn create(context: AgentContext) {
let mut agent = MediaButtonsAgent {
publisher: context.get_publisher(),
messenger: context.create_messenger().await.expect("media button messenger created"),
recipient_settings: context
.available_components
.intersection(&get_event_setting_types())
.cloned()
.collect::<HashSet<SettingType>>(),
};
let mut receptor = context.receptor;
fasync::Task::local(async move {
while let Ok((Payload::Invocation(invocation), client)) =
receptor.next_of::<Payload>().await
{
let _ = client.reply(Payload::Complete(agent.handle(invocation).await).into());
}
tracing::info!("Media buttons agent done processing requests");
})
.detach()
}
async fn handle(&mut self, invocation: Invocation) -> InvocationResult {
match invocation.lifespan {
Lifespan::Initialization => Err(AgentError::UnhandledLifespan),
Lifespan::Service => self.handle_service_lifespan(invocation.service_context).await,
}
}
async fn handle_service_lifespan(
&mut self,
service_context: Rc<ServiceContext>,
) -> InvocationResult {
let (input_tx, mut input_rx) = futures::channel::mpsc::unbounded::<MediaButtonsEvent>();
if let Err(e) = monitor_media_buttons(service_context, input_tx).await {
tracing::error!("Unable to monitor media buttons: {:?}", e);
return Err(AgentError::UnexpectedError);
}
let event_handler = EventHandler {
publisher: self.publisher.clone(),
messenger: self.messenger.clone(),
recipient_settings: self.recipient_settings.clone(),
};
fasync::Task::local(async move {
while let Some(event) = input_rx.next().await {
let id = ftrace::Id::new();
event_handler.handle_event(event, id);
}
})
.detach();
Ok(())
}
}
struct EventHandler {
publisher: Publisher,
messenger: service::message::Messenger,
recipient_settings: HashSet<SettingType>,
}
impl EventHandler {
fn handle_event(&self, event: MediaButtonsEvent, id: ftrace::Id) {
if event.mic_mute.is_some() || event.camera_disable.is_some() {
let media_buttons: MediaButtons = event.into();
self.send_event(media_buttons, id);
}
}
fn send_event<E>(&self, event: E, id: ftrace::Id)
where
E: Copy + Into<media_buttons::Event> + Into<Request> + std::fmt::Debug,
{
self.publisher.send_event(Event::MediaButtons(event.into()));
let setting_request: Request = event.into();
for setting_type in self.recipient_settings.iter() {
let guard = trace_guard!(
id,
c"media buttons send event",
"setting_type" => format!("{setting_type:?}").as_str()
);
let mut receptor = self.messenger.message(
HandlerPayload::Request(setting_request.clone()).into(),
Audience::Address(service::Address::Handler(*setting_type)),
);
fasync::Task::local(async move {
let _ = receptor.next_payload().await;
drop(guard);
})
.detach();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::event;
use crate::input::common::MediaButtonsEventBuilder;
use crate::message::base::MessageEvent;
use crate::message::receptor::Receptor;
use crate::tests::fakes::service_registry::ServiceRegistry;
use crate::tests::helpers::{
create_messenger_and_publisher, create_messenger_and_publisher_from_hub,
create_receptor_for_setting_type,
};
#[fuchsia::test(allow_stalls = false)]
async fn initialization_lifespan_is_unhandled() {
let (messenger, publisher) = create_messenger_and_publisher().await;
let mut agent =
MediaButtonsAgent { publisher, messenger, recipient_settings: HashSet::new() };
let result = agent
.handle(Invocation {
lifespan: Lifespan::Initialization,
service_context: Rc::new(ServiceContext::new(None, None)),
})
.await;
assert!(matches!(result, Err(AgentError::UnhandledLifespan)));
}
#[fuchsia::test(allow_stalls = false)]
async fn when_media_buttons_inaccessible_returns_err() {
let (messenger, publisher) = create_messenger_and_publisher().await;
let mut agent =
MediaButtonsAgent { publisher, messenger, recipient_settings: HashSet::new() };
let service_context = Rc::new(ServiceContext::new(
Some(ServiceRegistry::serve(ServiceRegistry::create())),
None,
));
let result =
agent.handle(Invocation { lifespan: Lifespan::Service, service_context }).await;
assert!(matches!(result, Err(AgentError::UnexpectedError)));
}
#[fuchsia::test(allow_stalls = false)]
async fn event_handler_proxies_event() {
let service_message_hub = service::MessageHub::create_hub();
let (messenger, publisher) =
create_messenger_and_publisher_from_hub(&service_message_hub).await;
let target_setting_type = SettingType::Unknown;
let event_receptor = service::build_event_listener(&service_message_hub).await;
let handler_receptor: Receptor =
create_receptor_for_setting_type(&service_message_hub, target_setting_type).await;
let event_handler = EventHandler {
publisher,
messenger,
recipient_settings: vec![target_setting_type].into_iter().collect(),
};
event_handler.handle_event(
MediaButtonsEventBuilder::new().set_mic_mute(true).set_camera_disable(true).build(),
0.into(),
);
service_message_hub.delete(handler_receptor.get_signature());
service_message_hub.delete(event_receptor.get_signature());
let mut agent_received_media_buttons = false;
let mut received_events: usize = 0;
let fused_event = event_receptor.fuse();
let fused_handler = handler_receptor.fuse();
futures::pin_mut!(fused_event, fused_handler);
loop {
futures::select! {
message = fused_event.select_next_some() => {
if let MessageEvent::Message(
service::Payload::Event(event::Payload::Event(
event::Event::MediaButtons(event))), _) = message
{
match event {
event::media_buttons::Event::OnButton(
MediaButtons{..}
) => {
agent_received_media_buttons = true;
}
}
}
},
message = fused_handler.select_next_some() => {
if let MessageEvent::Message(
service::Payload::Setting(HandlerPayload::Request(
Request::OnButton(_button),
)),
_,
) = message
{
received_events += 1;
}
}
complete => break,
}
}
assert!(agent_received_media_buttons);
assert_eq!(received_events, 1);
}
#[fuchsia::test(allow_stalls = false)]
async fn event_handler_sends_no_events_if_no_settings_available() {
let service_message_hub = service::MessageHub::create_hub();
let (messenger, publisher) =
create_messenger_and_publisher_from_hub(&service_message_hub).await;
let mut handler_receptor: Receptor =
create_receptor_for_setting_type(&service_message_hub, SettingType::Unknown).await;
let event_handler =
EventHandler { publisher, messenger, recipient_settings: HashSet::new() };
event_handler.handle_event(
MediaButtonsEventBuilder::new().set_mic_mute(true).set_camera_disable(true).build(),
0.into(),
);
let mut received_events: usize = 0;
service_message_hub.delete(handler_receptor.get_signature());
while let Ok((HandlerPayload::Request(_), _)) =
handler_receptor.next_of::<HandlerPayload>().await
{
received_events += 1;
}
assert_eq!(received_events, 0);
}
}