tilo_process_services/location.rs
1//! Location, as an app's process holds it: the device's answer, which the host asks through the
2//! device's own action (`devices/<device>/location#locate`) and installs here.
3//!
4//! The platform's location service belongs to the device, not to an app: a sandboxed process
5//! speaks to no D-Bus and reaches no portal. A host that can ask the device installs a
6//! [`LocationProvider`] with [`crate::process::ProcessServices::with_location`]; without one the
7//! process answers that location is not available here, never a made-up position.
8
9use std::sync::mpsc::Receiver;
10use std::sync::{Arc, OnceLock};
11
12/// What one request for the device's position answered.
13#[derive(Clone, Debug, PartialEq)]
14pub enum LocationOutcome {
15 Located {
16 lat: f64,
17 lon: f64,
18 /// Reported horizontal accuracy in metres (0 when unknown).
19 accuracy_m: f64,
20 },
21 /// No position could be obtained: the service was missing, permission was
22 /// denied, or no fix arrived. The reason is for the UI to show.
23 Unavailable(String),
24}
25
26/// The answer a request for a fix arrives on.
27pub type LocationChannel = Receiver<LocationOutcome>;
28
29/// Where the device's position comes from in this process: the host's provider, or nothing.
30pub trait LocationProvider: Send + Sync {
31 fn locate(&self) -> LocationChannel;
32}
33
34/// Ask the current process's location service for a fix.
35pub fn request_location() -> LocationChannel {
36 crate::process::current().location.locate()
37}
38
39/// A provider that answers nothing: a process its host gave no way to reach the device's
40/// location.
41#[must_use]
42pub fn unavailable() -> Arc<dyn LocationProvider> {
43 static NOWHERE: OnceLock<Arc<dyn LocationProvider>> = OnceLock::new();
44 Arc::clone(NOWHERE.get_or_init(|| Arc::new(Refusing("Location isn't available here"))))
45}
46
47struct Refusing(&'static str);
48
49impl LocationProvider for Refusing {
50 fn locate(&self) -> LocationChannel {
51 answered(LocationOutcome::Unavailable(self.0.to_owned()))
52 }
53}
54
55fn answered(outcome: LocationOutcome) -> LocationChannel {
56 let (tx, rx) = std::sync::mpsc::channel();
57 let _ = tx.send(outcome);
58 rx
59}