1use cbf::data::{
4 auxiliary_window::AuxiliaryWindowResponse,
5 extension::{ExtensionInfo, IconData},
6};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum ChromeAuxiliaryWindowResponse {
10 ExtensionInstallPrompt {
11 proceed: bool,
12 },
13 ExtensionUninstallPrompt {
14 proceed: bool,
15 report_abuse: bool,
16 },
17 PermissionPrompt {
18 allow: bool,
19 },
20 DownloadPrompt {
21 allow: bool,
22 destination_path: Option<String>,
23 },
24 Unknown,
25}
26
27impl From<AuxiliaryWindowResponse> for ChromeAuxiliaryWindowResponse {
28 fn from(value: AuxiliaryWindowResponse) -> Self {
29 match value {
30 AuxiliaryWindowResponse::ExtensionInstallPrompt { proceed } => {
31 Self::ExtensionInstallPrompt { proceed }
32 }
33 AuxiliaryWindowResponse::ExtensionUninstallPrompt {
34 proceed,
35 report_abuse,
36 } => Self::ExtensionUninstallPrompt {
37 proceed,
38 report_abuse,
39 },
40 AuxiliaryWindowResponse::PermissionPrompt { allow } => Self::PermissionPrompt { allow },
41 AuxiliaryWindowResponse::DownloadPrompt {
42 allow,
43 destination_path,
44 } => Self::DownloadPrompt {
45 allow,
46 destination_path,
47 },
48 AuxiliaryWindowResponse::Unknown => Self::Unknown,
49 }
50 }
51}
52
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub enum ChromeIconData {
55 Url(String),
56 Png(Vec<u8>),
57 Binary {
58 media_type: Option<String>,
59 bytes: Vec<u8>,
60 },
61}
62
63impl From<ChromeIconData> for IconData {
64 fn from(value: ChromeIconData) -> Self {
65 match value {
66 ChromeIconData::Url(url) => Self::Url(url),
67 ChromeIconData::Png(bytes) => Self::Png(bytes),
68 ChromeIconData::Binary { media_type, bytes } => Self::Binary { media_type, bytes },
69 }
70 }
71}
72
73impl From<IconData> for ChromeIconData {
74 fn from(value: IconData) -> Self {
75 match value {
76 IconData::Url(url) => Self::Url(url),
77 IconData::Png(bytes) => Self::Png(bytes),
78 IconData::Binary { media_type, bytes } => Self::Binary { media_type, bytes },
79 }
80 }
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
84pub struct ChromeExtensionInfo {
85 pub id: String,
86 pub name: String,
87 pub version: String,
88 pub enabled: bool,
89 pub permission_names: Vec<String>,
90 pub icon: Option<ChromeIconData>,
91}
92
93impl From<ChromeExtensionInfo> for ExtensionInfo {
94 fn from(value: ChromeExtensionInfo) -> Self {
95 Self {
96 id: value.id,
97 name: value.name,
98 version: value.version,
99 enabled: value.enabled,
100 permission_names: value.permission_names,
101 icon: value.icon.map(Into::into),
102 }
103 }
104}
105
106impl From<ExtensionInfo> for ChromeExtensionInfo {
107 fn from(value: ExtensionInfo) -> Self {
108 Self {
109 id: value.id,
110 name: value.name,
111 version: value.version,
112 enabled: value.enabled,
113 permission_names: value.permission_names,
114 icon: value.icon.map(Into::into),
115 }
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use cbf::data::extension::IconData;
122
123 use super::{ChromeExtensionInfo, ChromeIconData};
124
125 #[test]
126 fn converts_png_icon_to_generic() {
127 let chrome = ChromeExtensionInfo {
128 id: "ext".to_string(),
129 name: "Example".to_string(),
130 version: "1.0.0".to_string(),
131 enabled: true,
132 permission_names: vec!["tabs".to_string()],
133 icon: Some(ChromeIconData::Png(vec![1, 2, 3])),
134 };
135
136 let generic: cbf::data::extension::ExtensionInfo = chrome.into();
137 assert_eq!(generic.icon, Some(IconData::Png(vec![1, 2, 3])));
138 }
139
140 #[test]
141 fn converts_binary_icon_from_generic() {
142 let generic = cbf::data::extension::ExtensionInfo {
143 id: "ext".to_string(),
144 name: "Example".to_string(),
145 version: "1.0.0".to_string(),
146 enabled: false,
147 permission_names: vec![],
148 icon: Some(IconData::Binary {
149 media_type: Some("image/webp".to_string()),
150 bytes: vec![9, 8, 7],
151 }),
152 };
153
154 let chrome: ChromeExtensionInfo = generic.into();
155 assert_eq!(
156 chrome.icon,
157 Some(ChromeIconData::Binary {
158 media_type: Some("image/webp".to_string()),
159 bytes: vec![9, 8, 7],
160 })
161 );
162 }
163}