settings/privacy/
privacy_fidl_handler.rs1use crate::base::{SettingInfo, SettingType};
6use crate::handler::base::Request;
7use crate::ingress::{request, watch, Scoped};
8use crate::job::source::{Error as JobError, ErrorResponder};
9use crate::job::Job;
10use fidl::prelude::*;
11use fidl_fuchsia_settings::{
12 PrivacyRequest, PrivacySetResponder, PrivacySetResult, PrivacySettings, PrivacyWatchResponder,
13};
14
15impl ErrorResponder for PrivacySetResponder {
16 fn id(&self) -> &'static str {
17 "Privacy_Set"
18 }
19
20 fn respond(self: Box<Self>, error: fidl_fuchsia_settings::Error) -> Result<(), fidl::Error> {
21 self.send(Err(error))
22 }
23}
24
25impl request::Responder<Scoped<PrivacySetResult>> for PrivacySetResponder {
26 fn respond(self, Scoped(response): Scoped<PrivacySetResult>) {
27 let _ = self.send(response);
28 }
29}
30
31impl watch::Responder<PrivacySettings, zx::Status> for PrivacyWatchResponder {
32 fn respond(self, response: Result<PrivacySettings, zx::Status>) {
33 match response {
34 Ok(settings) => {
35 let _ = self.send(&settings);
36 }
37 Err(error) => {
38 self.control_handle().shutdown_with_epitaph(error);
39 }
40 }
41 }
42}
43
44impl TryFrom<PrivacyRequest> for Job {
45 type Error = JobError;
46
47 fn try_from(item: PrivacyRequest) -> Result<Self, Self::Error> {
48 #[allow(unreachable_patterns)]
49 match item {
50 PrivacyRequest::Set { settings, responder } => {
51 Ok(request::Work::new(SettingType::Privacy, to_request(settings), responder).into())
52 }
53 PrivacyRequest::Watch { responder } => {
54 Ok(watch::Work::new_job(SettingType::Privacy, responder))
55 }
56 _ => {
57 log::warn!("Received a call to an unsupported API: {:?}", item);
58 Err(JobError::Unsupported)
59 }
60 }
61 }
62}
63
64impl From<SettingInfo> for PrivacySettings {
65 fn from(response: SettingInfo) -> Self {
66 if let SettingInfo::Privacy(info) = response {
67 return PrivacySettings {
68 user_data_sharing_consent: info.user_data_sharing_consent,
69 ..Default::default()
70 };
71 }
72
73 panic!("incorrect value sent to privacy");
74 }
75}
76
77fn to_request(settings: PrivacySettings) -> Request {
78 Request::SetUserDataSharingConsent(settings.user_data_sharing_consent)
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84 use crate::job::{execution, work};
85 use assert_matches::assert_matches;
86 use fidl_fuchsia_settings::{PrivacyMarker, PrivacyRequestStream};
87 use futures::StreamExt;
88
89 #[fuchsia::test]
90 fn test_request_from_settings_empty() {
91 let request = to_request(PrivacySettings::default());
92
93 assert_eq!(request, Request::SetUserDataSharingConsent(None));
94 }
95
96 #[fuchsia::test]
97 fn test_request_from_settings() {
98 const USER_DATA_SHARING_CONSENT: bool = true;
99
100 let request = to_request(PrivacySettings {
101 user_data_sharing_consent: Some(USER_DATA_SHARING_CONSENT),
102 ..Default::default()
103 });
104
105 assert_eq!(request, Request::SetUserDataSharingConsent(Some(USER_DATA_SHARING_CONSENT)));
106 }
107
108 #[fuchsia::test(allow_stalls = false)]
109 async fn try_from_set_converts_supplied_params() {
110 let (proxy, server) = fidl::endpoints::create_proxy::<PrivacyMarker>();
111 let _fut = proxy
112 .set(&PrivacySettings { user_data_sharing_consent: Some(true), ..Default::default() });
113 let mut request_stream: PrivacyRequestStream = server.into_stream();
114 let request = request_stream
115 .next()
116 .await
117 .expect("should have on request before stream is closed")
118 .expect("should have gotten a request");
119 let job = Job::try_from(request);
120 let job = job.as_ref();
121 assert_matches!(job.map(|j| j.workload()), Ok(work::Load::Independent(_)));
122 assert_matches!(job.map(|j| j.execution_type()), Ok(execution::Type::Independent));
123 }
124
125 #[fuchsia::test(allow_stalls = false)]
126 async fn try_from_watch_converts_supplied_params() {
127 let (proxy, server) = fidl::endpoints::create_proxy::<PrivacyMarker>();
128 let _fut = proxy.watch();
129 let mut request_stream: PrivacyRequestStream = server.into_stream();
130 let request = request_stream
131 .next()
132 .await
133 .expect("should have on request before stream is closed")
134 .expect("should have gotten a request");
135 let job = Job::try_from(request);
136 let job = job.as_ref();
137 assert_matches!(job.map(|j| j.workload()), Ok(work::Load::Sequential(_, _)));
138 assert_matches!(job.map(|j| j.execution_type()), Ok(execution::Type::Sequential(_)));
139 }
140}