Skip to main content

cbf_chrome/data/
download.rs

1//! Chrome-specific download lifecycle types.
2
3use cbf::data::download::{
4    DownloadId, DownloadOutcome, DownloadPromptActionHint, DownloadPromptResult, DownloadState,
5};
6
7use crate::data::ids::TabId;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub struct ChromeDownloadId(u64);
11
12impl ChromeDownloadId {
13    pub const fn new(value: u64) -> Self {
14        Self(value)
15    }
16
17    pub const fn get(self) -> u64 {
18        self.0
19    }
20
21    pub const fn to_generic(self) -> DownloadId {
22        DownloadId::new(self.get())
23    }
24}
25
26impl From<DownloadId> for ChromeDownloadId {
27    fn from(value: DownloadId) -> Self {
28        Self::new(value.get())
29    }
30}
31
32impl From<ChromeDownloadId> for DownloadId {
33    fn from(value: ChromeDownloadId) -> Self {
34        value.to_generic()
35    }
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum ChromeDownloadPromptResult {
40    Allowed,
41    Denied,
42    Aborted,
43}
44
45impl From<ChromeDownloadPromptResult> for DownloadPromptResult {
46    fn from(value: ChromeDownloadPromptResult) -> Self {
47        match value {
48            ChromeDownloadPromptResult::Allowed => Self::Allowed,
49            ChromeDownloadPromptResult::Denied => Self::Denied,
50            ChromeDownloadPromptResult::Aborted => Self::Aborted,
51        }
52    }
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub enum ChromeDownloadPromptReason {
57    None,
58    Unexpected,
59    SaveAs,
60    Preference,
61    NameTooLong,
62    TargetConflict,
63    TargetPathNotWriteable,
64    TargetNoSpace,
65    DlpBlocked,
66    Unknown,
67}
68
69impl From<ChromeDownloadPromptReason> for DownloadPromptActionHint {
70    fn from(value: ChromeDownloadPromptReason) -> Self {
71        match value {
72            ChromeDownloadPromptReason::None => Self::AutoSave,
73            ChromeDownloadPromptReason::Unexpected
74            | ChromeDownloadPromptReason::SaveAs
75            | ChromeDownloadPromptReason::Preference
76            | ChromeDownloadPromptReason::NameTooLong
77            | ChromeDownloadPromptReason::TargetConflict
78            | ChromeDownloadPromptReason::TargetPathNotWriteable
79            | ChromeDownloadPromptReason::TargetNoSpace => Self::SelectDestination,
80            ChromeDownloadPromptReason::DlpBlocked => Self::Deny,
81            ChromeDownloadPromptReason::Unknown => Self::Unknown,
82        }
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use cbf::data::download::DownloadPromptActionHint;
89
90    use super::ChromeDownloadPromptReason;
91
92    #[test]
93    fn chrome_download_prompt_reason_maps_to_action_hint() {
94        assert_eq!(
95            DownloadPromptActionHint::from(ChromeDownloadPromptReason::None),
96            DownloadPromptActionHint::AutoSave
97        );
98        assert_eq!(
99            DownloadPromptActionHint::from(ChromeDownloadPromptReason::SaveAs),
100            DownloadPromptActionHint::SelectDestination
101        );
102        assert_eq!(
103            DownloadPromptActionHint::from(ChromeDownloadPromptReason::Preference),
104            DownloadPromptActionHint::SelectDestination
105        );
106        assert_eq!(
107            DownloadPromptActionHint::from(ChromeDownloadPromptReason::NameTooLong),
108            DownloadPromptActionHint::SelectDestination
109        );
110        assert_eq!(
111            DownloadPromptActionHint::from(ChromeDownloadPromptReason::TargetConflict),
112            DownloadPromptActionHint::SelectDestination
113        );
114        assert_eq!(
115            DownloadPromptActionHint::from(ChromeDownloadPromptReason::TargetPathNotWriteable),
116            DownloadPromptActionHint::SelectDestination
117        );
118        assert_eq!(
119            DownloadPromptActionHint::from(ChromeDownloadPromptReason::TargetNoSpace),
120            DownloadPromptActionHint::SelectDestination
121        );
122        assert_eq!(
123            DownloadPromptActionHint::from(ChromeDownloadPromptReason::Unexpected),
124            DownloadPromptActionHint::SelectDestination
125        );
126        assert_eq!(
127            DownloadPromptActionHint::from(ChromeDownloadPromptReason::DlpBlocked),
128            DownloadPromptActionHint::Deny
129        );
130    }
131
132    #[test]
133    fn chrome_download_prompt_reason_unknown_maps_to_unknown_action_hint() {
134        assert_eq!(
135            DownloadPromptActionHint::from(ChromeDownloadPromptReason::Unknown),
136            DownloadPromptActionHint::Unknown
137        );
138    }
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq)]
142pub enum ChromeDownloadState {
143    InProgress,
144    Paused,
145    Completed,
146    Cancelled,
147    Interrupted,
148    Unknown,
149}
150
151impl From<ChromeDownloadState> for DownloadState {
152    fn from(value: ChromeDownloadState) -> Self {
153        match value {
154            ChromeDownloadState::InProgress => Self::InProgress,
155            ChromeDownloadState::Paused => Self::Paused,
156            ChromeDownloadState::Completed => Self::Completed,
157            ChromeDownloadState::Cancelled => Self::Cancelled,
158            ChromeDownloadState::Interrupted => Self::Interrupted,
159            ChromeDownloadState::Unknown => Self::Unknown,
160        }
161    }
162}
163
164#[derive(Debug, Clone, Copy, PartialEq, Eq)]
165pub enum ChromeDownloadOutcome {
166    Succeeded,
167    Cancelled,
168    Interrupted,
169    Unknown,
170}
171
172impl From<ChromeDownloadOutcome> for DownloadOutcome {
173    fn from(value: ChromeDownloadOutcome) -> Self {
174        match value {
175            ChromeDownloadOutcome::Succeeded => Self::Succeeded,
176            ChromeDownloadOutcome::Cancelled => Self::Cancelled,
177            ChromeDownloadOutcome::Interrupted => Self::Interrupted,
178            ChromeDownloadOutcome::Unknown => Self::Unknown,
179        }
180    }
181}
182
183#[derive(Debug, Clone, PartialEq, Eq)]
184pub struct ChromeDownloadSnapshot {
185    pub download_id: ChromeDownloadId,
186    pub source_tab_id: Option<TabId>,
187    pub file_name: String,
188    pub total_bytes: Option<u64>,
189    pub target_path: Option<String>,
190}
191
192#[derive(Debug, Clone, PartialEq, Eq)]
193pub struct ChromeDownloadProgress {
194    pub download_id: ChromeDownloadId,
195    pub source_tab_id: Option<TabId>,
196    pub state: ChromeDownloadState,
197    pub file_name: String,
198    pub received_bytes: u64,
199    pub total_bytes: Option<u64>,
200    pub target_path: Option<String>,
201    pub can_resume: bool,
202    pub is_paused: bool,
203}
204
205#[derive(Debug, Clone, PartialEq, Eq)]
206pub struct ChromeDownloadCompletion {
207    pub download_id: ChromeDownloadId,
208    pub source_tab_id: Option<TabId>,
209    pub outcome: ChromeDownloadOutcome,
210    pub file_name: String,
211    pub received_bytes: u64,
212    pub total_bytes: Option<u64>,
213    pub target_path: Option<String>,
214}