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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//! The questions a program outside Tilo asks the person at the device, as it hands them to the
//! session.
//!
//! A program that needs a person's answer (which file to open, whether an app may record the
//! screen, an administrator's password, a network's secret) asks it on an [`AskSender`] and
//! waits ([`AskSender::ask`]). The sender keeps nothing: each question goes to the session's
//! [`AskReceiver`], which raises it as an asked record the person answers like any other, and
//! hands their answer back through [`AskReceiver::answer`]: what they typed reaches the asking
//! program in memory and nowhere else.
//!
//! A question is gone once it is answered, declined, or withdrawn by its program (the app
//! closed the request, polkit cancelled it): the future [`Asked`] withdraws its question when
//! dropped, so a program never leaves one behind, and a receiver that is gone answers every
//! question no.
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tilo_lock::MutexRecover as _;
use tokio::sync::{mpsc, oneshot};
use zeroize::Zeroizing;
/// One question a service asks.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Question {
/// Names the question while it is pending; asking again under the same
/// handle replaces it. Services keep theirs apart by a prefix of their
/// own.
pub handle: String,
/// What is asked, as the service names it (`portal.file-chooser`,
/// `polkit.authenticate`): what the sheet reads [`Self::details`] as.
pub kind: String,
/// The app the question is asked for, when one asks.
pub app_id: Option<String>,
/// What the question is listed as: the asking app, the network, the
/// administrator action. Data from the asker, never the service's own
/// words, which the sheet supplies in the person's language.
pub label: String,
/// Everything else the sheet needs, in the shape of [`Self::kind`].
pub details: serde_json::Value,
}
/// The person's answer to a question.
#[derive(Clone, Default, PartialEq, Eq)]
pub struct Answer {
/// The choice they made, counted from zero, when the question offered
/// choices.
pub choice: Option<usize>,
/// The files or the place they chose.
pub uris: Vec<String>,
/// Whether they asked for the answer to be remembered.
pub remember: bool,
/// What they typed, one value per field the question asked for.
pub values: Vec<Zeroizing<String>>,
}
impl std::fmt::Debug for Answer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// What was typed is never written out.
f.debug_struct("Answer")
.field("choice", &self.choice)
.field("uris", &self.uris)
.field("remember", &self.remember)
.field("values", &self.values.len())
.finish()
}
}
struct Pending {
/// Tells this asking apart from another under the same handle.
id: u64,
question: Question,
reply: oneshot::Sender<Answer>,
}
/// What a sender hands its receiver.
enum Sent {
Ask(Pending),
/// What pending question `handle` shows changed to these details.
Update {
handle: String,
details: Value,
},
/// The program no longer asks `handle`: the asking `id` alone, or whatever asks it.
Withdraw {
handle: String,
id: Option<u64>,
},
}
type Wake = Arc<dyn Fn() + Send + Sync>;
/// Who is woken when a question arrives or goes: the receiver names it once.
type Waking = Arc<Mutex<Option<Wake>>>;
/// The asking that tells one question from another under the same handle.
static ASKED: AtomicU64 = AtomicU64::new(1);
/// A sender and the receiver its questions reach.
#[must_use]
pub fn channel() -> (AskSender, AskReceiver) {
let (sent, received) = mpsc::unbounded_channel();
let waking = Waking::default();
(
AskSender {
sent,
waking: Arc::clone(&waking),
},
AskReceiver {
received: Arc::new(Mutex::new(Received {
sent: received,
pending: Vec::new(),
})),
waking,
},
)
}
/// Where a program asks the person: every question goes to the [`AskReceiver`] of its
/// [`channel`], and the sender keeps none of them.
#[derive(Clone)]
pub struct AskSender {
sent: mpsc::UnboundedSender<Sent>,
waking: Waking,
}
impl std::fmt::Debug for AskSender {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AskSender")
.field("received", &!self.sent.is_closed())
.finish_non_exhaustive()
}
}
impl AskSender {
fn send(sent: &mpsc::UnboundedSender<Sent>, waking: &Waking, what: Sent) -> bool {
let reached = sent.send(what).is_ok();
let wake = waking.lock_recover().clone();
if reached && let Some(wake) = wake {
wake();
}
reached
}
/// Ask `question`; the answer is `None` when it was declined or replaced, or nobody receives
/// questions any more. Asking again under the same handle replaces the question.
pub fn ask(&self, question: Question) -> Asked {
let (reply, answer) = oneshot::channel();
let handle = question.handle.clone();
let id = ASKED.fetch_add(1, Ordering::Relaxed);
Self::send(
&self.sent,
&self.waking,
Sent::Ask(Pending {
id,
question,
reply,
}),
);
Asked {
id,
handle,
answer,
sent: self.sent.clone(),
waking: Arc::clone(&self.waking),
}
}
/// Change what pending question `handle` shows to `details` (an app offering other choices
/// while it waits), keeping its asker waiting.
pub fn update(&self, handle: &str, details: Value) {
Self::send(
&self.sent,
&self.waking,
Sent::Update {
handle: handle.to_owned(),
details,
},
);
}
/// The program no longer asks question `handle`, whoever asked it under that handle.
pub fn withdraw(&self, handle: &str) {
Self::send(
&self.sent,
&self.waking,
Sent::Withdraw {
handle: handle.to_owned(),
id: None,
},
);
}
}
struct Received {
sent: mpsc::UnboundedReceiver<Sent>,
pending: Vec<Pending>,
}
impl Received {
/// Take in what the senders handed over since.
fn take_in(&mut self) {
while let Ok(sent) = self.sent.try_recv() {
match sent {
Sent::Ask(asked) => {
// Dropping the replaced question's sender tells its asker.
self.pending
.retain(|pending| pending.question.handle != asked.question.handle);
self.pending.push(asked);
}
Sent::Update { handle, details } => {
if let Some(pending) = self
.pending
.iter_mut()
.find(|pending| pending.question.handle == handle)
{
pending.question.details = details;
}
}
Sent::Withdraw { handle, id } => self.pending.retain(|pending| {
pending.question.handle != handle || id.is_some_and(|id| id != pending.id)
}),
}
}
}
fn take(&mut self, handle: &str) -> Option<Pending> {
self.take_in();
let index = self
.pending
.iter()
.position(|pending| pending.question.handle == handle)?;
Some(self.pending.remove(index))
}
}
/// The session's side of a [`channel`]: the questions programs ask now, which it puts to the
/// person, and where their answers go back.
#[derive(Clone)]
pub struct AskReceiver {
received: Arc<Mutex<Received>>,
waking: Waking,
}
impl std::fmt::Debug for AskReceiver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AskReceiver")
.field("pending", &self.pending())
.finish()
}
}
impl AskReceiver {
/// Call `wake` whenever a question arrives or goes, from whichever thread asked it.
pub fn on_change(&self, wake: impl Fn() + Send + Sync + 'static) {
*self.waking.lock_recover() = Some(Arc::new(wake));
}
/// The questions pending now, oldest first.
#[must_use]
pub fn pending(&self) -> Vec<Question> {
let mut received = self.received.lock_recover();
received.take_in();
received
.pending
.iter()
.map(|pending| pending.question.clone())
.collect()
}
/// Pending question `handle`.
#[must_use]
pub fn question(&self, handle: &str) -> Option<Question> {
self.pending()
.into_iter()
.find(|question| question.handle == handle)
}
/// Answer question `handle`; `false` when it is no longer pending, or its program stopped
/// waiting for it.
pub fn answer(&self, handle: &str, answer: Answer) -> bool {
self.received
.lock_recover()
.take(handle)
.is_some_and(|pending| pending.reply.send(answer).is_ok())
}
/// Decline question `handle`; `false` when it is no longer pending.
pub fn decline(&self, handle: &str) -> bool {
self.received.lock_recover().take(handle).is_some()
}
}
/// A question asked and not yet answered: resolves to the answer, `None`
/// when it was declined or replaced. Dropping it withdraws the question.
pub struct Asked {
id: u64,
handle: String,
answer: oneshot::Receiver<Answer>,
sent: mpsc::UnboundedSender<Sent>,
waking: Waking,
}
impl Asked {
/// The question's handle.
#[must_use]
pub fn handle(&self) -> &str {
&self.handle
}
/// Wait on this thread for the person's answer, at most until
/// `deadline` (`None`: until the person answers or the question goes):
/// `Some` of the answer, which is `None` when the question was declined
/// or replaced, or `None` when the question is still up at the deadline,
/// in which case the caller keeps it pending and asks again later.
///
/// The session answers on its own loop, so a thread waiting here must
/// not block the loop: this parks on a waker the answer wakes, and never
/// on a timer that polls the loop.
pub fn until(&mut self, deadline: Option<tilo_clock::time::Instant>) -> Option<Option<Answer>> {
struct Park(std::thread::Thread);
impl std::task::Wake for Park {
fn wake(self: std::sync::Arc<Self>) {
self.0.unpark();
}
fn wake_by_ref(self: &std::sync::Arc<Self>) {
self.0.unpark();
}
}
let waker = std::task::Waker::from(std::sync::Arc::new(Park(std::thread::current())));
let mut cx = Context::from_waker(&waker);
loop {
if let Poll::Ready(answer) = Pin::new(&mut *self).poll(&mut cx) {
return Some(answer);
}
match deadline {
Some(deadline) => {
let now = tilo_clock::now();
if now >= deadline {
// The deadline has come: the question is still up.
return None;
}
std::thread::park_timeout(deadline.saturating_duration_since(now));
}
None => std::thread::park(),
}
}
}
}
impl Future for Asked {
type Output = Option<Answer>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.answer).poll(cx).map(Result::ok)
}
}
impl Drop for Asked {
fn drop(&mut self) {
// Only this asker's own question: a replacement under the same handle belongs to
// whoever asked it. One answered already is gone, and the receiver finds nothing.
if !matches!(
self.answer.try_recv(),
Ok(_) | Err(oneshot::error::TryRecvError::Closed)
) {
AskSender::send(
&self.sent,
&self.waking,
Sent::Withdraw {
handle: std::mem::take(&mut self.handle),
id: Some(self.id),
},
);
}
}
}
#[cfg(test)]
mod tests;