@@ -1,4 +1,7 @@ |
| 1 | | -use std::collections::HashMap; |
| 1 | +use std::{ |
| 2 | + collections::HashMap, |
| 3 | + sync::{Arc, Mutex}, |
| 4 | +}; |
| 2 | 5 | |
| 3 | 6 | use zbus::{ |
| 4 | 7 | blocking::Connection, |
@@ -7,8 +10,10 @@ use zbus::{ |
| 7 | 10 | zvariant::{OwnedObjectPath, OwnedValue}, |
| 8 | 11 | }; |
| 9 | 12 | |
| 13 | +use crate::config::Config; |
| 10 | 14 | use crate::error::{PortalError, map_portal_error}; |
| 11 | 15 | use crate::portal::derive_request_id_from_handle; |
| 16 | +use crate::portal_dispatch::PortalDispatch; |
| 12 | 17 | use crate::portal_options::{ |
| 13 | 18 | parse_app_chooser_options, parse_file_chooser_options, parse_screenshot_options, |
| 14 | 19 | }; |
@@ -27,16 +32,24 @@ fn failure_response(error: &PortalError) -> PortalMethodReply { |
| 27 | 32 | (mapping.code as u32, HashMap::new()) |
| 28 | 33 | } |
| 29 | 34 | |
| 30 | | -fn request_id_for_call( |
| 35 | +fn request_identity_for_call( |
| 31 | 36 | request_handle: &OwnedObjectPath, |
| 32 | | - header: Header<'_>, |
| 33 | | -) -> Result<String, PortalError> { |
| 37 | + header: &Header<'_>, |
| 38 | +) -> Result<(String, String), PortalError> { |
| 34 | 39 | let sender = header |
| 35 | 40 | .sender() |
| 36 | | - .map(|sender| sender.as_str()) |
| 41 | + .map(|sender| sender.as_str().to_string()) |
| 37 | 42 | .ok_or(PortalError::UnauthorizedClient)?; |
| 43 | + let request_id = derive_request_id_from_handle(&sender, request_handle.as_str())?; |
| 44 | + Ok((sender, request_id)) |
| 45 | +} |
| 38 | 46 | |
| 39 | | - derive_request_id_from_handle(sender, request_handle.as_str()) |
| 47 | +fn with_dispatch<T>( |
| 48 | + dispatch: &Arc<Mutex<PortalDispatch>>, |
| 49 | + callback: impl FnOnce(&mut PortalDispatch) -> Result<T, PortalError>, |
| 50 | +) -> Result<T, PortalError> { |
| 51 | + let mut dispatch = dispatch.lock().map_err(|_| PortalError::InternalFailure)?; |
| 52 | + callback(&mut dispatch) |
| 40 | 53 | } |
| 41 | 54 | |
| 42 | 55 | pub struct SessionNameGuard { |
@@ -45,13 +58,22 @@ pub struct SessionNameGuard { |
| 45 | 58 | |
| 46 | 59 | impl SessionNameGuard { |
| 47 | 60 | pub fn acquire() -> zbus::Result<Self> { |
| 61 | + let dispatch = Arc::new(Mutex::new(PortalDispatch::new( |
| 62 | + Config::from_env().request_timeout, |
| 63 | + ))); |
| 48 | 64 | let connection = Connection::session()?; |
| 49 | 65 | connection.request_name(BACKEND_DBUS_NAME)?; |
| 50 | 66 | { |
| 51 | 67 | let object_server = connection.object_server(); |
| 52 | | - object_server.at(BACKEND_OBJECT_PATH, ScreenshotPortal)?; |
| 53 | | - object_server.at(BACKEND_OBJECT_PATH, FileChooserPortal)?; |
| 54 | | - object_server.at(BACKEND_OBJECT_PATH, AppChooserPortal)?; |
| 68 | + object_server.at( |
| 69 | + BACKEND_OBJECT_PATH, |
| 70 | + ScreenshotPortal::new(Arc::clone(&dispatch)), |
| 71 | + )?; |
| 72 | + object_server.at( |
| 73 | + BACKEND_OBJECT_PATH, |
| 74 | + FileChooserPortal::new(Arc::clone(&dispatch)), |
| 75 | + )?; |
| 76 | + object_server.at(BACKEND_OBJECT_PATH, AppChooserPortal::new(dispatch))?; |
| 55 | 77 | } |
| 56 | 78 | Ok(Self { |
| 57 | 79 | _connection: connection, |
@@ -60,7 +82,15 @@ impl SessionNameGuard { |
| 60 | 82 | } |
| 61 | 83 | |
| 62 | 84 | #[derive(Debug)] |
| 63 | | -struct ScreenshotPortal; |
| 85 | +struct ScreenshotPortal { |
| 86 | + dispatch: Arc<Mutex<PortalDispatch>>, |
| 87 | +} |
| 88 | + |
| 89 | +impl ScreenshotPortal { |
| 90 | + fn new(dispatch: Arc<Mutex<PortalDispatch>>) -> Self { |
| 91 | + Self { dispatch } |
| 92 | + } |
| 93 | +} |
| 64 | 94 | |
| 65 | 95 | #[interface(name = "org.freedesktop.impl.portal.Screenshot")] |
| 66 | 96 | impl ScreenshotPortal { |
@@ -75,7 +105,13 @@ impl ScreenshotPortal { |
| 75 | 105 | if let Err(error) = parse_screenshot_options(&_options) { |
| 76 | 106 | return failure_response(&error); |
| 77 | 107 | } |
| 78 | | - if let Err(error) = request_id_for_call(&handle, header) { |
| 108 | + let (sender, request_id) = match request_identity_for_call(&handle, &header) { |
| 109 | + Ok(identity) => identity, |
| 110 | + Err(error) => return failure_response(&error), |
| 111 | + }; |
| 112 | + if let Err(error) = with_dispatch(&self.dispatch, |dispatch| { |
| 113 | + dispatch.register_unimplemented_call(&request_id, &sender, _app_id, _parent_window) |
| 114 | + }) { |
| 79 | 115 | return failure_response(&error); |
| 80 | 116 | } |
| 81 | 117 | failed_response() |
@@ -92,7 +128,13 @@ impl ScreenshotPortal { |
| 92 | 128 | if let Err(error) = parse_screenshot_options(&_options) { |
| 93 | 129 | return failure_response(&error); |
| 94 | 130 | } |
| 95 | | - if let Err(error) = request_id_for_call(&handle, header) { |
| 131 | + let (sender, request_id) = match request_identity_for_call(&handle, &header) { |
| 132 | + Ok(identity) => identity, |
| 133 | + Err(error) => return failure_response(&error), |
| 134 | + }; |
| 135 | + if let Err(error) = with_dispatch(&self.dispatch, |dispatch| { |
| 136 | + dispatch.register_unimplemented_call(&request_id, &sender, _app_id, _parent_window) |
| 137 | + }) { |
| 96 | 138 | return failure_response(&error); |
| 97 | 139 | } |
| 98 | 140 | failed_response() |
@@ -105,7 +147,15 @@ impl ScreenshotPortal { |
| 105 | 147 | } |
| 106 | 148 | |
| 107 | 149 | #[derive(Debug)] |
| 108 | | -struct FileChooserPortal; |
| 150 | +struct FileChooserPortal { |
| 151 | + dispatch: Arc<Mutex<PortalDispatch>>, |
| 152 | +} |
| 153 | + |
| 154 | +impl FileChooserPortal { |
| 155 | + fn new(dispatch: Arc<Mutex<PortalDispatch>>) -> Self { |
| 156 | + Self { dispatch } |
| 157 | + } |
| 158 | +} |
| 109 | 159 | |
| 110 | 160 | #[interface(name = "org.freedesktop.impl.portal.FileChooser")] |
| 111 | 161 | impl FileChooserPortal { |
@@ -121,7 +171,13 @@ impl FileChooserPortal { |
| 121 | 171 | if let Err(error) = parse_file_chooser_options(&_options) { |
| 122 | 172 | return failure_response(&error); |
| 123 | 173 | } |
| 124 | | - if let Err(error) = request_id_for_call(&handle, header) { |
| 174 | + let (sender, request_id) = match request_identity_for_call(&handle, &header) { |
| 175 | + Ok(identity) => identity, |
| 176 | + Err(error) => return failure_response(&error), |
| 177 | + }; |
| 178 | + if let Err(error) = with_dispatch(&self.dispatch, |dispatch| { |
| 179 | + dispatch.register_unimplemented_call(&request_id, &sender, _app_id, _parent_window) |
| 180 | + }) { |
| 125 | 181 | return failure_response(&error); |
| 126 | 182 | } |
| 127 | 183 | failed_response() |
@@ -139,7 +195,13 @@ impl FileChooserPortal { |
| 139 | 195 | if let Err(error) = parse_file_chooser_options(&_options) { |
| 140 | 196 | return failure_response(&error); |
| 141 | 197 | } |
| 142 | | - if let Err(error) = request_id_for_call(&handle, header) { |
| 198 | + let (sender, request_id) = match request_identity_for_call(&handle, &header) { |
| 199 | + Ok(identity) => identity, |
| 200 | + Err(error) => return failure_response(&error), |
| 201 | + }; |
| 202 | + if let Err(error) = with_dispatch(&self.dispatch, |dispatch| { |
| 203 | + dispatch.register_unimplemented_call(&request_id, &sender, _app_id, _parent_window) |
| 204 | + }) { |
| 143 | 205 | return failure_response(&error); |
| 144 | 206 | } |
| 145 | 207 | failed_response() |
@@ -157,7 +219,13 @@ impl FileChooserPortal { |
| 157 | 219 | if let Err(error) = parse_file_chooser_options(&_options) { |
| 158 | 220 | return failure_response(&error); |
| 159 | 221 | } |
| 160 | | - if let Err(error) = request_id_for_call(&handle, header) { |
| 222 | + let (sender, request_id) = match request_identity_for_call(&handle, &header) { |
| 223 | + Ok(identity) => identity, |
| 224 | + Err(error) => return failure_response(&error), |
| 225 | + }; |
| 226 | + if let Err(error) = with_dispatch(&self.dispatch, |dispatch| { |
| 227 | + dispatch.register_unimplemented_call(&request_id, &sender, _app_id, _parent_window) |
| 228 | + }) { |
| 161 | 229 | return failure_response(&error); |
| 162 | 230 | } |
| 163 | 231 | failed_response() |
@@ -170,7 +238,15 @@ impl FileChooserPortal { |
| 170 | 238 | } |
| 171 | 239 | |
| 172 | 240 | #[derive(Debug)] |
| 173 | | -struct AppChooserPortal; |
| 241 | +struct AppChooserPortal { |
| 242 | + dispatch: Arc<Mutex<PortalDispatch>>, |
| 243 | +} |
| 244 | + |
| 245 | +impl AppChooserPortal { |
| 246 | + fn new(dispatch: Arc<Mutex<PortalDispatch>>) -> Self { |
| 247 | + Self { dispatch } |
| 248 | + } |
| 249 | +} |
| 174 | 250 | |
| 175 | 251 | #[interface(name = "org.freedesktop.impl.portal.AppChooser")] |
| 176 | 252 | impl AppChooserPortal { |
@@ -186,7 +262,13 @@ impl AppChooserPortal { |
| 186 | 262 | if let Err(error) = parse_app_chooser_options(&_options) { |
| 187 | 263 | return failure_response(&error); |
| 188 | 264 | } |
| 189 | | - if let Err(error) = request_id_for_call(&handle, header) { |
| 265 | + let (sender, request_id) = match request_identity_for_call(&handle, &header) { |
| 266 | + Ok(identity) => identity, |
| 267 | + Err(error) => return failure_response(&error), |
| 268 | + }; |
| 269 | + if let Err(error) = with_dispatch(&self.dispatch, |dispatch| { |
| 270 | + dispatch.register_unimplemented_call(&request_id, &sender, _app_id, _parent_window) |
| 271 | + }) { |
| 190 | 272 | return failure_response(&error); |
| 191 | 273 | } |
| 192 | 274 | failed_response() |
@@ -198,9 +280,14 @@ impl AppChooserPortal { |
| 198 | 280 | _choices: Vec<String>, |
| 199 | 281 | #[zbus(header)] header: Header<'_>, |
| 200 | 282 | ) -> zbus::fdo::Result<()> { |
| 201 | | - request_id_for_call(&handle, header) |
| 202 | | - .map(|_| ()) |
| 203 | | - .map_err(|error| zbus::fdo::Error::Failed(map_portal_error(&error).reason.to_string())) |
| 283 | + let (sender, request_id) = |
| 284 | + request_identity_for_call(&handle, &header).map_err(|error| { |
| 285 | + zbus::fdo::Error::Failed(map_portal_error(&error).reason.to_string()) |
| 286 | + })?; |
| 287 | + with_dispatch(&self.dispatch, |dispatch| { |
| 288 | + dispatch.validate_update_choices(&request_id, &sender) |
| 289 | + }) |
| 290 | + .map_err(|error| zbus::fdo::Error::Failed(map_portal_error(&error).reason.to_string())) |
| 204 | 291 | } |
| 205 | 292 | |
| 206 | 293 | #[zbus(property)] |
@@ -211,13 +298,18 @@ impl AppChooserPortal { |
| 211 | 298 | |
| 212 | 299 | #[cfg(test)] |
| 213 | 300 | mod tests { |
| 214 | | - use std::collections::HashMap; |
| 301 | + use std::{ |
| 302 | + collections::HashMap, |
| 303 | + sync::{Arc, Mutex}, |
| 304 | + time::Duration, |
| 305 | + }; |
| 215 | 306 | |
| 216 | 307 | use super::{ |
| 217 | 308 | AppChooserPortal, BACKEND_OBJECT_PATH, FileChooserPortal, INTERFACE_VERSION, |
| 218 | 309 | ScreenshotPortal, |
| 219 | 310 | }; |
| 220 | 311 | use crate::error::PortalResponseCode; |
| 312 | + use crate::portal_dispatch::PortalDispatch; |
| 221 | 313 | use zbus::{ |
| 222 | 314 | message::{Header, Message}, |
| 223 | 315 | zvariant::{OwnedObjectPath, OwnedValue}, |
@@ -230,15 +322,25 @@ mod tests { |
| 230 | 322 | |
| 231 | 323 | #[test] |
| 232 | 324 | fn portal_interfaces_report_expected_version() { |
| 233 | | - assert_eq!(ScreenshotPortal.version(), INTERFACE_VERSION); |
| 234 | | - assert_eq!(FileChooserPortal.version(), INTERFACE_VERSION); |
| 235 | | - assert_eq!(AppChooserPortal.version(), INTERFACE_VERSION); |
| 325 | + assert_eq!( |
| 326 | + ScreenshotPortal::new(test_dispatch()).version(), |
| 327 | + INTERFACE_VERSION |
| 328 | + ); |
| 329 | + assert_eq!( |
| 330 | + FileChooserPortal::new(test_dispatch()).version(), |
| 331 | + INTERFACE_VERSION |
| 332 | + ); |
| 333 | + assert_eq!( |
| 334 | + AppChooserPortal::new(test_dispatch()).version(), |
| 335 | + INTERFACE_VERSION |
| 336 | + ); |
| 236 | 337 | } |
| 237 | 338 | |
| 238 | 339 | #[test] |
| 239 | 340 | fn screenshot_and_pick_color_return_failed_placeholder() { |
| 341 | + let portal = ScreenshotPortal::new(test_dispatch()); |
| 240 | 342 | let options = HashMap::<String, OwnedValue>::new(); |
| 241 | | - let (response, results) = ScreenshotPortal.screenshot( |
| 343 | + let (response, results) = portal.screenshot( |
| 242 | 344 | request_handle_path(), |
| 243 | 345 | "org.test.App", |
| 244 | 346 | "x11:0x2a", |
@@ -248,7 +350,7 @@ mod tests { |
| 248 | 350 | assert_eq!(response, PortalResponseCode::Failed as u32); |
| 249 | 351 | assert!(results.is_empty()); |
| 250 | 352 | |
| 251 | | - let (response, results) = ScreenshotPortal.pick_color( |
| 353 | + let (response, results) = portal.pick_color( |
| 252 | 354 | request_handle_path(), |
| 253 | 355 | "org.test.App", |
| 254 | 356 | "", |
@@ -261,8 +363,9 @@ mod tests { |
| 261 | 363 | |
| 262 | 364 | #[test] |
| 263 | 365 | fn file_chooser_methods_return_failed_placeholder() { |
| 366 | + let portal = FileChooserPortal::new(test_dispatch()); |
| 264 | 367 | let options = HashMap::<String, OwnedValue>::new(); |
| 265 | | - let (response, results) = FileChooserPortal.open_file( |
| 368 | + let (response, results) = portal.open_file( |
| 266 | 369 | request_handle_path(), |
| 267 | 370 | "org.test.App", |
| 268 | 371 | "", |
@@ -273,7 +376,7 @@ mod tests { |
| 273 | 376 | assert_eq!(response, PortalResponseCode::Failed as u32); |
| 274 | 377 | assert!(results.is_empty()); |
| 275 | 378 | |
| 276 | | - let (response, results) = FileChooserPortal.save_file( |
| 379 | + let (response, results) = portal.save_file( |
| 277 | 380 | request_handle_path(), |
| 278 | 381 | "org.test.App", |
| 279 | 382 | "", |
@@ -284,7 +387,7 @@ mod tests { |
| 284 | 387 | assert_eq!(response, PortalResponseCode::Failed as u32); |
| 285 | 388 | assert!(results.is_empty()); |
| 286 | 389 | |
| 287 | | - let (response, results) = FileChooserPortal.save_files( |
| 390 | + let (response, results) = portal.save_files( |
| 288 | 391 | request_handle_path(), |
| 289 | 392 | "org.test.App", |
| 290 | 393 | "", |
@@ -298,9 +401,10 @@ mod tests { |
| 298 | 401 | |
| 299 | 402 | #[test] |
| 300 | 403 | fn app_chooser_methods_have_stable_placeholders() { |
| 404 | + let portal = AppChooserPortal::new(test_dispatch()); |
| 301 | 405 | let options = HashMap::<String, OwnedValue>::new(); |
| 302 | 406 | let choices = vec!["org.test.Viewer".to_string()]; |
| 303 | | - let (response, results) = AppChooserPortal.choose_application( |
| 407 | + let (response, results) = portal.choose_application( |
| 304 | 408 | request_handle_path(), |
| 305 | 409 | "org.test.App", |
| 306 | 410 | "", |
@@ -311,7 +415,7 @@ mod tests { |
| 311 | 415 | assert_eq!(response, PortalResponseCode::Failed as u32); |
| 312 | 416 | assert!(results.is_empty()); |
| 313 | 417 | |
| 314 | | - let error = AppChooserPortal |
| 418 | + let error = portal |
| 315 | 419 | .update_choices(request_handle_path(), choices, test_call_header()) |
| 316 | 420 | .expect_err("update choices should reject missing sender metadata"); |
| 317 | 421 | match error { |
@@ -334,4 +438,8 @@ mod tests { |
| 334 | 438 | .expect("test message"); |
| 335 | 439 | Box::leak(Box::new(call)).header() |
| 336 | 440 | } |
| 441 | + |
| 442 | + fn test_dispatch() -> Arc<Mutex<PortalDispatch>> { |
| 443 | + Arc::new(Mutex::new(PortalDispatch::new(Duration::from_secs(30)))) |
| 444 | + } |
| 337 | 445 | } |