@@ -1,4 +1,7 @@ |
| 1 | -use std::collections::HashMap; | 1 | +use std::{ |
| | 2 | + collections::HashMap, |
| | 3 | + sync::{Arc, Mutex}, |
| | 4 | +}; |
| 2 | | 5 | |
| 3 | use zbus::{ | 6 | use zbus::{ |
| 4 | blocking::Connection, | 7 | blocking::Connection, |
@@ -7,8 +10,10 @@ use zbus::{ |
| 7 | zvariant::{OwnedObjectPath, OwnedValue}, | 10 | zvariant::{OwnedObjectPath, OwnedValue}, |
| 8 | }; | 11 | }; |
| 9 | | 12 | |
| | 13 | +use crate::config::Config; |
| 10 | use crate::error::{PortalError, map_portal_error}; | 14 | use crate::error::{PortalError, map_portal_error}; |
| 11 | use crate::portal::derive_request_id_from_handle; | 15 | use crate::portal::derive_request_id_from_handle; |
| | 16 | +use crate::portal_dispatch::PortalDispatch; |
| 12 | use crate::portal_options::{ | 17 | use crate::portal_options::{ |
| 13 | parse_app_chooser_options, parse_file_chooser_options, parse_screenshot_options, | 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 | (mapping.code as u32, HashMap::new()) | 32 | (mapping.code as u32, HashMap::new()) |
| 28 | } | 33 | } |
| 29 | | 34 | |
| 30 | -fn request_id_for_call( | 35 | +fn request_identity_for_call( |
| 31 | request_handle: &OwnedObjectPath, | 36 | request_handle: &OwnedObjectPath, |
| 32 | - header: Header<'_>, | 37 | + header: &Header<'_>, |
| 33 | -) -> Result<String, PortalError> { | 38 | +) -> Result<(String, String), PortalError> { |
| 34 | let sender = header | 39 | let sender = header |
| 35 | .sender() | 40 | .sender() |
| 36 | - .map(|sender| sender.as_str()) | 41 | + .map(|sender| sender.as_str().to_string()) |
| 37 | .ok_or(PortalError::UnauthorizedClient)?; | 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 | pub struct SessionNameGuard { | 55 | pub struct SessionNameGuard { |
@@ -45,13 +58,22 @@ pub struct SessionNameGuard { |
| 45 | | 58 | |
| 46 | impl SessionNameGuard { | 59 | impl SessionNameGuard { |
| 47 | pub fn acquire() -> zbus::Result<Self> { | 60 | pub fn acquire() -> zbus::Result<Self> { |
| | 61 | + let dispatch = Arc::new(Mutex::new(PortalDispatch::new( |
| | 62 | + Config::from_env().request_timeout, |
| | 63 | + ))); |
| 48 | let connection = Connection::session()?; | 64 | let connection = Connection::session()?; |
| 49 | connection.request_name(BACKEND_DBUS_NAME)?; | 65 | connection.request_name(BACKEND_DBUS_NAME)?; |
| 50 | { | 66 | { |
| 51 | let object_server = connection.object_server(); | 67 | let object_server = connection.object_server(); |
| 52 | - object_server.at(BACKEND_OBJECT_PATH, ScreenshotPortal)?; | 68 | + object_server.at( |
| 53 | - object_server.at(BACKEND_OBJECT_PATH, FileChooserPortal)?; | 69 | + BACKEND_OBJECT_PATH, |
| 54 | - object_server.at(BACKEND_OBJECT_PATH, AppChooserPortal)?; | 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 | Ok(Self { | 78 | Ok(Self { |
| 57 | _connection: connection, | 79 | _connection: connection, |
@@ -60,7 +82,15 @@ impl SessionNameGuard { |
| 60 | } | 82 | } |
| 61 | | 83 | |
| 62 | #[derive(Debug)] | 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 | #[interface(name = "org.freedesktop.impl.portal.Screenshot")] | 95 | #[interface(name = "org.freedesktop.impl.portal.Screenshot")] |
| 66 | impl ScreenshotPortal { | 96 | impl ScreenshotPortal { |
@@ -75,7 +105,13 @@ impl ScreenshotPortal { |
| 75 | if let Err(error) = parse_screenshot_options(&_options) { | 105 | if let Err(error) = parse_screenshot_options(&_options) { |
| 76 | return failure_response(&error); | 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 | return failure_response(&error); | 115 | return failure_response(&error); |
| 80 | } | 116 | } |
| 81 | failed_response() | 117 | failed_response() |
@@ -92,7 +128,13 @@ impl ScreenshotPortal { |
| 92 | if let Err(error) = parse_screenshot_options(&_options) { | 128 | if let Err(error) = parse_screenshot_options(&_options) { |
| 93 | return failure_response(&error); | 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 | return failure_response(&error); | 138 | return failure_response(&error); |
| 97 | } | 139 | } |
| 98 | failed_response() | 140 | failed_response() |
@@ -105,7 +147,15 @@ impl ScreenshotPortal { |
| 105 | } | 147 | } |
| 106 | | 148 | |
| 107 | #[derive(Debug)] | 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 | #[interface(name = "org.freedesktop.impl.portal.FileChooser")] | 160 | #[interface(name = "org.freedesktop.impl.portal.FileChooser")] |
| 111 | impl FileChooserPortal { | 161 | impl FileChooserPortal { |
@@ -121,7 +171,13 @@ impl FileChooserPortal { |
| 121 | if let Err(error) = parse_file_chooser_options(&_options) { | 171 | if let Err(error) = parse_file_chooser_options(&_options) { |
| 122 | return failure_response(&error); | 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 | return failure_response(&error); | 181 | return failure_response(&error); |
| 126 | } | 182 | } |
| 127 | failed_response() | 183 | failed_response() |
@@ -139,7 +195,13 @@ impl FileChooserPortal { |
| 139 | if let Err(error) = parse_file_chooser_options(&_options) { | 195 | if let Err(error) = parse_file_chooser_options(&_options) { |
| 140 | return failure_response(&error); | 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 | return failure_response(&error); | 205 | return failure_response(&error); |
| 144 | } | 206 | } |
| 145 | failed_response() | 207 | failed_response() |
@@ -157,7 +219,13 @@ impl FileChooserPortal { |
| 157 | if let Err(error) = parse_file_chooser_options(&_options) { | 219 | if let Err(error) = parse_file_chooser_options(&_options) { |
| 158 | return failure_response(&error); | 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 | return failure_response(&error); | 229 | return failure_response(&error); |
| 162 | } | 230 | } |
| 163 | failed_response() | 231 | failed_response() |
@@ -170,7 +238,15 @@ impl FileChooserPortal { |
| 170 | } | 238 | } |
| 171 | | 239 | |
| 172 | #[derive(Debug)] | 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 | #[interface(name = "org.freedesktop.impl.portal.AppChooser")] | 251 | #[interface(name = "org.freedesktop.impl.portal.AppChooser")] |
| 176 | impl AppChooserPortal { | 252 | impl AppChooserPortal { |
@@ -186,7 +262,13 @@ impl AppChooserPortal { |
| 186 | if let Err(error) = parse_app_chooser_options(&_options) { | 262 | if let Err(error) = parse_app_chooser_options(&_options) { |
| 187 | return failure_response(&error); | 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 | return failure_response(&error); | 272 | return failure_response(&error); |
| 191 | } | 273 | } |
| 192 | failed_response() | 274 | failed_response() |
@@ -198,8 +280,13 @@ impl AppChooserPortal { |
| 198 | _choices: Vec<String>, | 280 | _choices: Vec<String>, |
| 199 | #[zbus(header)] header: Header<'_>, | 281 | #[zbus(header)] header: Header<'_>, |
| 200 | ) -> zbus::fdo::Result<()> { | 282 | ) -> zbus::fdo::Result<()> { |
| 201 | - request_id_for_call(&handle, header) | 283 | + let (sender, request_id) = |
| 202 | - .map(|_| ()) | 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 | + }) |
| 203 | .map_err(|error| zbus::fdo::Error::Failed(map_portal_error(&error).reason.to_string())) | 290 | .map_err(|error| zbus::fdo::Error::Failed(map_portal_error(&error).reason.to_string())) |
| 204 | } | 291 | } |
| 205 | | 292 | |
@@ -211,13 +298,18 @@ impl AppChooserPortal { |
| 211 | | 298 | |
| 212 | #[cfg(test)] | 299 | #[cfg(test)] |
| 213 | mod tests { | 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 | use super::{ | 307 | use super::{ |
| 217 | AppChooserPortal, BACKEND_OBJECT_PATH, FileChooserPortal, INTERFACE_VERSION, | 308 | AppChooserPortal, BACKEND_OBJECT_PATH, FileChooserPortal, INTERFACE_VERSION, |
| 218 | ScreenshotPortal, | 309 | ScreenshotPortal, |
| 219 | }; | 310 | }; |
| 220 | use crate::error::PortalResponseCode; | 311 | use crate::error::PortalResponseCode; |
| | 312 | + use crate::portal_dispatch::PortalDispatch; |
| 221 | use zbus::{ | 313 | use zbus::{ |
| 222 | message::{Header, Message}, | 314 | message::{Header, Message}, |
| 223 | zvariant::{OwnedObjectPath, OwnedValue}, | 315 | zvariant::{OwnedObjectPath, OwnedValue}, |
@@ -230,15 +322,25 @@ mod tests { |
| 230 | | 322 | |
| 231 | #[test] | 323 | #[test] |
| 232 | fn portal_interfaces_report_expected_version() { | 324 | fn portal_interfaces_report_expected_version() { |
| 233 | - assert_eq!(ScreenshotPortal.version(), INTERFACE_VERSION); | 325 | + assert_eq!( |
| 234 | - assert_eq!(FileChooserPortal.version(), INTERFACE_VERSION); | 326 | + ScreenshotPortal::new(test_dispatch()).version(), |
| 235 | - assert_eq!(AppChooserPortal.version(), INTERFACE_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 | #[test] | 339 | #[test] |
| 239 | fn screenshot_and_pick_color_return_failed_placeholder() { | 340 | fn screenshot_and_pick_color_return_failed_placeholder() { |
| | 341 | + let portal = ScreenshotPortal::new(test_dispatch()); |
| 240 | let options = HashMap::<String, OwnedValue>::new(); | 342 | let options = HashMap::<String, OwnedValue>::new(); |
| 241 | - let (response, results) = ScreenshotPortal.screenshot( | 343 | + let (response, results) = portal.screenshot( |
| 242 | request_handle_path(), | 344 | request_handle_path(), |
| 243 | "org.test.App", | 345 | "org.test.App", |
| 244 | "x11:0x2a", | 346 | "x11:0x2a", |
@@ -248,7 +350,7 @@ mod tests { |
| 248 | assert_eq!(response, PortalResponseCode::Failed as u32); | 350 | assert_eq!(response, PortalResponseCode::Failed as u32); |
| 249 | assert!(results.is_empty()); | 351 | assert!(results.is_empty()); |
| 250 | | 352 | |
| 251 | - let (response, results) = ScreenshotPortal.pick_color( | 353 | + let (response, results) = portal.pick_color( |
| 252 | request_handle_path(), | 354 | request_handle_path(), |
| 253 | "org.test.App", | 355 | "org.test.App", |
| 254 | "", | 356 | "", |
@@ -261,8 +363,9 @@ mod tests { |
| 261 | | 363 | |
| 262 | #[test] | 364 | #[test] |
| 263 | fn file_chooser_methods_return_failed_placeholder() { | 365 | fn file_chooser_methods_return_failed_placeholder() { |
| | 366 | + let portal = FileChooserPortal::new(test_dispatch()); |
| 264 | let options = HashMap::<String, OwnedValue>::new(); | 367 | let options = HashMap::<String, OwnedValue>::new(); |
| 265 | - let (response, results) = FileChooserPortal.open_file( | 368 | + let (response, results) = portal.open_file( |
| 266 | request_handle_path(), | 369 | request_handle_path(), |
| 267 | "org.test.App", | 370 | "org.test.App", |
| 268 | "", | 371 | "", |
@@ -273,7 +376,7 @@ mod tests { |
| 273 | assert_eq!(response, PortalResponseCode::Failed as u32); | 376 | assert_eq!(response, PortalResponseCode::Failed as u32); |
| 274 | assert!(results.is_empty()); | 377 | assert!(results.is_empty()); |
| 275 | | 378 | |
| 276 | - let (response, results) = FileChooserPortal.save_file( | 379 | + let (response, results) = portal.save_file( |
| 277 | request_handle_path(), | 380 | request_handle_path(), |
| 278 | "org.test.App", | 381 | "org.test.App", |
| 279 | "", | 382 | "", |
@@ -284,7 +387,7 @@ mod tests { |
| 284 | assert_eq!(response, PortalResponseCode::Failed as u32); | 387 | assert_eq!(response, PortalResponseCode::Failed as u32); |
| 285 | assert!(results.is_empty()); | 388 | assert!(results.is_empty()); |
| 286 | | 389 | |
| 287 | - let (response, results) = FileChooserPortal.save_files( | 390 | + let (response, results) = portal.save_files( |
| 288 | request_handle_path(), | 391 | request_handle_path(), |
| 289 | "org.test.App", | 392 | "org.test.App", |
| 290 | "", | 393 | "", |
@@ -298,9 +401,10 @@ mod tests { |
| 298 | | 401 | |
| 299 | #[test] | 402 | #[test] |
| 300 | fn app_chooser_methods_have_stable_placeholders() { | 403 | fn app_chooser_methods_have_stable_placeholders() { |
| | 404 | + let portal = AppChooserPortal::new(test_dispatch()); |
| 301 | let options = HashMap::<String, OwnedValue>::new(); | 405 | let options = HashMap::<String, OwnedValue>::new(); |
| 302 | let choices = vec!["org.test.Viewer".to_string()]; | 406 | let choices = vec!["org.test.Viewer".to_string()]; |
| 303 | - let (response, results) = AppChooserPortal.choose_application( | 407 | + let (response, results) = portal.choose_application( |
| 304 | request_handle_path(), | 408 | request_handle_path(), |
| 305 | "org.test.App", | 409 | "org.test.App", |
| 306 | "", | 410 | "", |
@@ -311,7 +415,7 @@ mod tests { |
| 311 | assert_eq!(response, PortalResponseCode::Failed as u32); | 415 | assert_eq!(response, PortalResponseCode::Failed as u32); |
| 312 | assert!(results.is_empty()); | 416 | assert!(results.is_empty()); |
| 313 | | 417 | |
| 314 | - let error = AppChooserPortal | 418 | + let error = portal |
| 315 | .update_choices(request_handle_path(), choices, test_call_header()) | 419 | .update_choices(request_handle_path(), choices, test_call_header()) |
| 316 | .expect_err("update choices should reject missing sender metadata"); | 420 | .expect_err("update choices should reject missing sender metadata"); |
| 317 | match error { | 421 | match error { |
@@ -334,4 +438,8 @@ mod tests { |
| 334 | .expect("test message"); | 438 | .expect("test message"); |
| 335 | Box::leak(Box::new(call)).header() | 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 | } |