use crate::runtime::current_runtime;
use crate::{Error, Operation, Result};
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AppInfo {
pub display_name: String,
pub identifier: String,
pub version: String,
pub build: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Platform {
Android,
Ios,
Test,
Other,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeviceInfo {
pub platform: Platform,
pub os_version: String,
pub model: Option<String>,
pub locale: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Theme {
Light,
Dark,
Unknown,
}
pub fn can_open_url() -> bool {
current_runtime().supports(Operation::OpenUrl)
}
pub fn can_open_settings() -> bool {
current_runtime().supports(Operation::OpenSettings)
}
pub fn has_app_info() -> bool {
current_runtime().supports(Operation::AppInfo)
}
pub fn has_device_info() -> bool {
current_runtime().supports(Operation::DeviceInfo)
}
pub fn has_theme() -> bool {
current_runtime().supports(Operation::Theme)
}
pub fn open_url(url: impl AsRef<str>) -> Result<()> {
let url = Url::parse(url.as_ref()).map_err(|error| Error::invalid("URL", error.to_string()))?;
validate_external_url(&url)?;
let runtime = current_runtime();
runtime.ensure_supported(Operation::OpenUrl)?;
runtime.backend().open_url(&url)
}
fn validate_external_url(url: &Url) -> Result<()> {
match url.scheme() {
"http" | "https" if url.host_str().is_none() => {
Err(Error::invalid("URL", "HTTP(S) URLs must include a host"))
}
"http" | "https" | "mailto" | "tel" | "sms" => Ok(()),
scheme => Err(Error::invalid(
"URL",
format!("scheme `{scheme}` is not allowed; expected http, https, mailto, tel, or sms"),
)),
}
}
pub fn open_settings() -> Result<()> {
let runtime = current_runtime();
runtime.ensure_supported(Operation::OpenSettings)?;
runtime.backend().open_settings()
}
pub fn app_info() -> Result<AppInfo> {
let runtime = current_runtime();
runtime.ensure_supported(Operation::AppInfo)?;
runtime.backend().app_info()
}
pub fn device_info() -> Result<DeviceInfo> {
let runtime = current_runtime();
runtime.ensure_supported(Operation::DeviceInfo)?;
runtime.backend().device_info()
}
pub fn theme() -> Result<Theme> {
let runtime = current_runtime();
runtime.ensure_supported(Operation::Theme)?;
runtime.backend().theme()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::testing::TestRuntime;
#[test]
fn open_url_accepts_only_safe_external_schemes() {
let runtime = TestRuntime::new();
let _guard = runtime.enter();
for value in [
"https://example.test/account",
"http://example.test",
"mailto:hello@example.test",
"tel:+15551234567",
"sms:+15551234567",
] {
open_url(value).unwrap();
}
assert_eq!(runtime.opened_urls().len(), 5);
}
#[test]
fn open_url_rejects_active_content_and_platform_schemes() {
let runtime = TestRuntime::new();
let _guard = runtime.enter();
for value in [
"javascript:alert(1)",
"data:text/html,<script>alert(1)</script>",
"file:///private/data.txt",
"content://org.example.provider/item",
"intent://example.test/#Intent;scheme=https;end",
"custom-app://open",
] {
let error = open_url(value).unwrap_err();
assert!(matches!(error, Error::InvalidInput { field: "URL", .. }));
}
assert!(runtime.opened_urls().is_empty());
}
}