Skip to main content

mobiler_core/
clipboard.rs

1//! A copy with options (ADR-0052): `cx.copy_with` / `cx.copy_sensitive`. The request goes to the built-in
2//! `clipboard_with` capability, not `clipboard`, so a shell that predates it answers `ok: false` instead of copying a
3//! secret in plain text.
4
5use std::time::Duration;
6
7/// How [`Cx::copy_with`](crate::Cx::copy_with) copies. [`CopyOptions::new`] is a plain copy (like `cx.copy`, but with
8/// a reply); [`CopyOptions::sensitive`] is the preset for a password, a recovery code or a TOTP secret.
9///
10/// What each platform honours:
11/// - `mark_sensitive`: Android hides the text in the clipboard preview (Android 13+) and asks keyboards not to keep it.
12///   iOS has no equivalent. The web can't.
13/// - `expires_after`: iOS removes the item itself. On Android it is **best effort**: the shell clears the clipboard
14///   when the time is up, but only while it still holds this copy. Android 10+ lets only the app in front read the
15///   clipboard, so after the user switches away (say, to paste the secret into another app) it clears within about
16///   5 s of the app coming back to the front, gives up an hour after the deadline, and never clears if the process dies first.
17///   Android 13+ clears the clipboard itself after about an hour. The hidden preview is Android's dependable
18///   protection. The web can't expire a copy.
19/// - `local_only`: iOS keeps the item out of Universal Clipboard. No effect on Android (an OEM or companion-app
20///   clipboard sync may still carry it) or the web.
21///
22/// A shell that predates `clipboard_with` answers `ok: false`. Don't fall back to `cx.copy` then: that is the plain
23/// copy this exists to avoid.
24#[derive(Clone, Debug, PartialEq)]
25pub struct CopyOptions {
26    sensitive: bool,
27    expires_after: Option<Duration>,
28    local_only: bool,
29}
30
31impl Default for CopyOptions {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37impl CopyOptions {
38    /// A plain copy: not sensitive, never expires, may sync to the user's other devices.
39    #[must_use]
40    pub fn new() -> Self {
41        Self { sensitive: false, expires_after: None, local_only: false }
42    }
43
44    /// Sensitive, expires after 2 minutes, kept off other devices (iOS). Every field can still be changed.
45    #[must_use]
46    pub fn sensitive() -> Self {
47        Self { sensitive: true, expires_after: Some(Duration::from_secs(120)), local_only: true }
48    }
49
50    /// Hide the text in the clipboard preview and ask keyboards not to keep it (Android).
51    #[must_use]
52    pub fn mark_sensitive(mut self, on: bool) -> Self {
53        self.sensitive = on;
54        self
55    }
56
57    /// Remove the copy after `d` (at least 1 s); best effort on Android, see [`CopyOptions`].
58    #[must_use]
59    pub fn expires_after(mut self, d: Duration) -> Self {
60        self.expires_after = Some(d.max(Duration::from_secs(1)));
61        self
62    }
63
64    /// Leave the copy on the clipboard until something replaces it.
65    #[must_use]
66    pub fn never_expires(mut self) -> Self {
67        self.expires_after = None;
68        self
69    }
70
71    /// Keep the copy on this device (iOS: out of Universal Clipboard).
72    #[must_use]
73    pub fn local_only(mut self, on: bool) -> Self {
74        self.local_only = on;
75        self
76    }
77
78    /// The `clipboard_with` request's input: every key always present (ADR-0052).
79    pub(crate) fn to_input(&self, text: &str) -> String {
80        let expires_ms = self.expires_after.map(|d| u64::try_from(d.as_millis()).unwrap_or(u64::MAX));
81        serde_json::json!({
82            "text": text,
83            "sensitive": self.sensitive,
84            "expires_ms": expires_ms,
85            "local_only": self.local_only,
86        })
87        .to_string()
88    }
89}