#![cfg_attr(
test,
allow(
clippy::disallowed_methods,
clippy::string_slice,
reason = "in a test these are the assertion, not a hazard"
)
)]
use std::sync::{Arc, OnceLock};
mod caps;
#[cfg(unix)]
pub mod chooser_service;
#[cfg(unix)]
pub mod chosen;
#[cfg(any(test, feature = "test-support"))]
pub mod conformance;
pub mod portal;
#[cfg(any(unix, windows))]
pub mod shell;
#[cfg(not(any(unix, windows)))]
#[path = "shell_absent.rs"]
pub mod shell;
#[cfg(not(target_arch = "wasm32"))]
mod unattended;
pub mod web_policy;
#[cfg(target_os = "macos")]
pub mod confined;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
mod web;
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
pub use web::download;
#[cfg(windows)]
mod windows;
#[cfg(all(target_os = "linux", feature = "portal-client"))]
mod xdg;
pub use caps::{Availability, Feature, Unsupported, availability, available};
use portal::{
LaunchChannel, LaunchOutcome, LaunchRequest, MultiChannel, MultiOutcome, OpenFileChannel,
OpenFileOutcome, PickRequest, PortalChannel, PortalOutcome, SaveChannel,
};
pub trait FilePicker: Send + Sync {
fn pick(&self, request: PickRequest) -> PortalChannel;
fn pick_multiple(&self, request: PickRequest) -> MultiChannel;
fn open_handle(&self, request: PickRequest) -> OpenFileChannel {
let _ = request;
answered(OpenFileOutcome::Failed(UNAVAILABLE.to_string()))
}
fn reopen(&self, reference: &std::path::Path, write: bool) -> OpenFileChannel {
let _ = (reference, write);
answered(OpenFileOutcome::Failed(
"This host cannot open the file again once it is chosen.".to_string(),
))
}
fn save_file(&self, request: PickRequest) -> OpenFileChannel {
let _ = request;
let (tx, rx) = std::sync::mpsc::channel();
let _ = tx.send(OpenFileOutcome::Failed(
"this host answers Save with a path; use `PickRequest::send`".to_string(),
));
rx
}
fn launch(&self, request: LaunchRequest) -> LaunchChannel;
fn save_destination(&self, request: PickRequest) -> portal::DestinationChannel {
if request.suggested_name().is_none() {
return portal::DestinationChannel::answered(portal::DestinationOutcome::Failed(
"only a Save chooser chooses where to save".to_owned(),
));
}
portal::DestinationChannel::from_path(self.pick(request))
}
fn save_contents(&self, request: PickRequest, bytes: Vec<u8>) -> SaveChannel {
let destination = request
.suggested_name()
.is_some()
.then(|| self.pick(request));
crate::portal::write_picked(destination, bytes)
}
}
pub trait UriOpener: Send + Sync {
fn open(&self, uri: &str) -> std::io::Result<()>;
}
#[cfg(not(target_arch = "wasm32"))]
pub fn unattended_picker(directory: std::path::PathBuf) -> Option<Arc<dyn FilePicker>> {
unattended::SaveDirectoryPicker::new(directory)
.map(|picker| Arc::new(picker) as Arc<dyn FilePicker>)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn inherited_shell_picker(elsewhere: Arc<dyn FilePicker>) -> Option<Arc<dyn FilePicker>> {
shell::inherited_picker(elsewhere)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn inherited_shell_opener(elsewhere: Arc<dyn UriOpener>) -> Option<Arc<dyn UriOpener>> {
shell::inherited_opener(elsewhere)
}
pub fn system_picker() -> Arc<dyn FilePicker> {
#[cfg(all(target_os = "linux", feature = "portal-client"))]
return Arc::new(xdg::PortalFilePicker);
#[cfg(target_os = "macos")]
return match confined::from_env() {
Some(confined) => confined as Arc<dyn FilePicker>,
None => Arc::new(macos::PanelFilePicker),
};
#[cfg(windows)]
return Arc::new(windows::WindowsFilePicker);
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
return Arc::new(web::PageFiles);
#[allow(
unreachable_code,
reason = "a target without choosers, or an app process whose host holds one"
)]
unavailable_picker()
}
pub fn system_opener() -> Arc<dyn UriOpener> {
Arc::new(SystemOpener)
}
struct SystemOpener;
impl UriOpener for SystemOpener {
fn open(&self, uri: &str) -> std::io::Result<()> {
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
if let Err(error) = url::Url::parse(uri) {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("bad URI: {error}"),
));
}
#[cfg(all(target_os = "linux", feature = "portal-client"))]
return xdg::open_uri(uri);
#[cfg(all(target_os = "linux", not(feature = "portal-client")))]
return open::that_detached(uri);
#[cfg(target_os = "macos")]
return macos::open_uri(uri);
#[cfg(windows)]
return open::that_detached(uri);
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
return web::open_uri(uri);
#[allow(unreachable_code, reason = "a target without a URI handler")]
UnavailableOpener.open(uri)
}
}
pub fn unavailable_picker() -> Arc<dyn FilePicker> {
Arc::new(UnavailableFilePicker)
}
pub fn unavailable_opener() -> Arc<dyn UriOpener> {
Arc::new(UnavailableOpener)
}
const UNAVAILABLE: &str = "Choosing files isn't available here";
struct UnavailableFilePicker;
fn answered<T>(outcome: T) -> std::sync::mpsc::Receiver<T> {
let (tx, rx) = std::sync::mpsc::channel();
let _ = tx.send(outcome);
rx
}
impl FilePicker for UnavailableFilePicker {
fn pick(&self, _request: PickRequest) -> PortalChannel {
answered(PortalOutcome::Failed(UNAVAILABLE.to_string()))
}
fn pick_multiple(&self, _request: PickRequest) -> MultiChannel {
answered(MultiOutcome::Failed(UNAVAILABLE.to_string()))
}
fn launch(&self, _request: LaunchRequest) -> LaunchChannel {
answered(LaunchOutcome::Failed(UNAVAILABLE.to_string()))
}
fn save_destination(&self, _request: PickRequest) -> portal::DestinationChannel {
portal::DestinationChannel::answered(portal::DestinationOutcome::Failed(
UNAVAILABLE.to_string(),
))
}
fn save_contents(&self, _request: PickRequest, _bytes: Vec<u8>) -> SaveChannel {
answered(portal::SaveOutcome::Failed(UNAVAILABLE.to_string()))
}
}
struct UnavailableOpener;
impl UriOpener for UnavailableOpener {
fn open(&self, _uri: &str) -> std::io::Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"Opening links isn't available here",
))
}
}
#[derive(Clone, Copy)]
pub struct Resolver {
pub picker: fn() -> Arc<dyn FilePicker>,
pub opener: fn() -> Arc<dyn UriOpener>,
}
static RESOLVER: OnceLock<Resolver> = OnceLock::new();
pub fn install_resolver(resolver: Resolver) {
let _ = RESOLVER.set(resolver);
}
pub(crate) fn current_picker() -> Arc<dyn FilePicker> {
RESOLVER
.get()
.map_or_else(unavailable_picker, |resolver| (resolver.picker)())
}
pub(crate) fn current_opener() -> Arc<dyn UriOpener> {
RESOLVER
.get()
.map_or_else(unavailable_opener, |resolver| (resolver.opener)())
}
#[cfg(any(
all(target_os = "linux", feature = "portal-client"),
target_os = "macos",
windows
))]
pub(crate) enum PortalFailure {
Cancelled,
Failed(String),
}
#[cfg(any(
all(target_os = "linux", feature = "portal-client"),
target_os = "macos",
windows
))]
impl PortalFailure {
fn into_single(self) -> PortalOutcome {
match self {
PortalFailure::Cancelled => PortalOutcome::Cancelled,
PortalFailure::Failed(e) => PortalOutcome::Failed(e),
}
}
fn into_multi(self) -> MultiOutcome {
match self {
PortalFailure::Cancelled => MultiOutcome::Cancelled,
PortalFailure::Failed(e) => MultiOutcome::Failed(e),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
tilo_test_env::scenario! {
fn without_a_desktop_every_service_answers_instead_of_hanging;
phase unavailable_services_conform;
{
conformance::check_picker_contract(&*unavailable_picker());
conformance::check_opener_contract(&*unavailable_opener());
}
phase the_system_backends_answer_bad_requests_without_a_desktop;
{
conformance::check_picker_contract(&*system_picker());
conformance::check_opener_contract(&*system_opener());
}
phase without_a_resolver_requests_fail_rather_than_hang;
{
let outcome = PickRequest::open("Open", "Open").send().recv().unwrap();
assert!(matches!(outcome, PortalOutcome::Failed(_)));
assert!(portal::open_uri("https://example.invalid").is_err());
}
}
#[cfg(all(target_os = "linux", not(feature = "portal-client")))]
#[test]
fn an_app_process_chooses_only_through_its_session() {
let outcome = system_picker()
.pick(PickRequest::open("Open", "Open"))
.recv()
.unwrap();
assert!(
matches!(outcome, PortalOutcome::Failed(_)),
"an app process holds no portal"
);
}
}