diff --git a/.changes/custom-protocol-remove-window-parameter.md b/.changes/custom-protocol-remove-window-parameter.md new file mode 100644 index 000000000..6c52e8d77 --- /dev/null +++ b/.changes/custom-protocol-remove-window-parameter.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +Custom Protocol handlers no longer take a `&Window` parameter. \ No newline at end of file diff --git a/bench/tests/src/cpu_intensive.rs b/bench/tests/src/cpu_intensive.rs index bc043eb81..d91df3615 100644 --- a/bench/tests/src/cpu_intensive.rs +++ b/bench/tests/src/cpu_intensive.rs @@ -25,7 +25,7 @@ fn main() -> wry::Result<()> { }; let webview = WebViewBuilder::new(window) .unwrap() - .with_custom_protocol("wry.bench".into(), move |_, requested_asset_path| { + .with_custom_protocol("wry.bench".into(), move |requested_asset_path| { let requested_asset_path = requested_asset_path.replace("wry.bench://", ""); match requested_asset_path.as_str() { "/index.css" => Ok((include_bytes!("static/index.css").to_vec(), "text/css".into())), diff --git a/bench/tests/src/custom_protocol.rs b/bench/tests/src/custom_protocol.rs index 1077dcba7..cb89e2ff1 100644 --- a/bench/tests/src/custom_protocol.rs +++ b/bench/tests/src/custom_protocol.rs @@ -32,7 +32,7 @@ fn main() -> wry::Result<()> { let webview = WebViewBuilder::new(window) .unwrap() .with_rpc_handler(handler) - .with_custom_protocol("wry.bench".into(), move |_, _| { + .with_custom_protocol("wry.bench".into(), move |_uri| { let index_html = r#" diff --git a/examples/custom_protocol.rs b/examples/custom_protocol.rs index 54262823f..3db5acf27 100644 --- a/examples/custom_protocol.rs +++ b/examples/custom_protocol.rs @@ -22,7 +22,7 @@ fn main() -> wry::Result<()> { let _webview = WebViewBuilder::new(window) .unwrap() - .with_custom_protocol("wry".into(), move |_, requested_asset_path| { + .with_custom_protocol("wry".into(), move |requested_asset_path| { // Remove url scheme let path = requested_asset_path.replace("wry://", ""); // Read the file content from file path diff --git a/examples/system_tray.rs b/examples/system_tray.rs index 997353c62..099b45e62 100644 --- a/examples/system_tray.rs +++ b/examples/system_tray.rs @@ -129,7 +129,7 @@ fn main() -> wry::Result<()> { let webview = WebViewBuilder::new(window) .unwrap() - .with_custom_protocol("wry.dev".into(), move |_, _| { + .with_custom_protocol("wry.dev".into(), move |_uri| { Ok((index_html.as_bytes().into(), "text/html".into())) }) .with_url("wry.dev://") diff --git a/examples/system_tray_no_menu.rs b/examples/system_tray_no_menu.rs index 0a034601f..b2ba2fee3 100644 --- a/examples/system_tray_no_menu.rs +++ b/examples/system_tray_no_menu.rs @@ -134,7 +134,7 @@ fn main() -> wry::Result<()> { let webview = WebViewBuilder::new(window) .unwrap() - .with_custom_protocol("wry.dev".into(), move |_, _| { + .with_custom_protocol("wry.dev".into(), move |_uri| { Ok((index_html.as_bytes().into(), "text/html".into())) }) .with_url("wry.dev://") diff --git a/src/webview/mod.rs b/src/webview/mod.rs index a5f2ac53f..251b4fa09 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -58,12 +58,9 @@ pub struct WebViewAttributes { /// Register custom file loading protocols with pairs of scheme uri string and a handling /// closure. /// - /// The closure takes the `Window` and a url string slice as parameters, and returns a tuple of a - /// vector of bytes which is the content and a mimetype string of the content. - pub custom_protocols: Vec<( - String, - Box Result<(Vec, String)>>, - )>, + /// The closure takes a url string slice, and returns a two item tuple of a vector of + /// bytes which is the content and a mimetype string of the content. + pub custom_protocols: Vec<(String, Box Result<(Vec, String)>>)>, /// Set the RPC handler to Communicate between the host Rust code and Javascript on webview. /// /// The communication is done via [JSON-RPC](https://www.jsonrpc.org). Users can use this to register an incoming @@ -149,12 +146,12 @@ impl<'a> WebViewBuilder<'a> { /// Register custom file loading protocols with pairs of scheme uri string and a handling /// closure. /// - /// The closure takes the `Window` and a url string slice as parameters, and returns a tuple of a + /// The closure takes a url string slice, and returns a two item tuple of a /// vector of bytes which is the content and a mimetype string of the content. #[cfg(feature = "protocol")] pub fn with_custom_protocol(mut self, name: String, handler: F) -> Self where - F: Fn(&Window, &str) -> Result<(Vec, String)> + 'static, + F: Fn(&str) -> Result<(Vec, String)> + 'static, { self .webview diff --git a/src/webview/web_context.rs b/src/webview/web_context.rs index 12926389b..6cb7dc70f 100644 --- a/src/webview/web_context.rs +++ b/src/webview/web_context.rs @@ -104,7 +104,7 @@ use self::unix::WebContextImpl; pub mod unix { //! Unix platform extensions for [`WebContext`](super::WebContext). - use crate::{application::window::Window, Error}; + use crate::Error; use glib::FileError; use std::{ collections::{HashSet, VecDeque}, @@ -211,27 +211,17 @@ pub mod unix { /// When duplicate schemes are registered, the duplicate handler will still be submitted and the /// `Err(Error::DuplicateCustomProtocol)` will be returned. It is safe to ignore if you are /// relying on the platform's implementation to properly handle duplicated scheme handlers. - fn register_uri_scheme( - &mut self, - name: &str, - handler: F, - window: Rc, - ) -> crate::Result<()> + fn register_uri_scheme(&mut self, name: &str, handler: F) -> crate::Result<()> where - F: Fn(&Window, &str) -> crate::Result<(Vec, String)> + 'static; + F: Fn(&str) -> crate::Result<(Vec, String)> + 'static; /// Register a custom protocol to the web context, only if it is not a duplicate scheme. /// /// If a duplicate scheme has been passed, its handler will **NOT** be registered and the /// function will return `Err(Error::DuplicateCustomProtocol)`. - fn try_register_uri_scheme( - &mut self, - name: &str, - handler: F, - window: Rc, - ) -> crate::Result<()> + fn try_register_uri_scheme(&mut self, name: &str, handler: F) -> crate::Result<()> where - F: Fn(&Window, &str) -> crate::Result<(Vec, String)> + 'static; + F: Fn(&str) -> crate::Result<(Vec, String)> + 'static; /// Add a [`WebView`] to the queue waiting to be opened. /// @@ -258,16 +248,11 @@ pub mod unix { &self.os.manager } - fn register_uri_scheme( - &mut self, - name: &str, - handler: F, - window: Rc, - ) -> crate::Result<()> + fn register_uri_scheme(&mut self, name: &str, handler: F) -> crate::Result<()> where - F: Fn(&Window, &str) -> crate::Result<(Vec, String)> + 'static, + F: Fn(&str) -> crate::Result<(Vec, String)> + 'static, { - actually_register_uri_scheme(self, name, handler, window)?; + actually_register_uri_scheme(self, name, handler)?; if self.os.registered_protocols.insert(name.to_string()) { Ok(()) } else { @@ -275,17 +260,12 @@ pub mod unix { } } - fn try_register_uri_scheme( - &mut self, - name: &str, - handler: F, - window: Rc, - ) -> crate::Result<()> + fn try_register_uri_scheme(&mut self, name: &str, handler: F) -> crate::Result<()> where - F: Fn(&Window, &str) -> crate::Result<(Vec, String)> + 'static, + F: Fn(&str) -> crate::Result<(Vec, String)> + 'static, { if self.os.registered_protocols.insert(name.to_string()) { - actually_register_uri_scheme(self, name, handler, window) + actually_register_uri_scheme(self, name, handler) } else { Err(Error::DuplicateCustomProtocol(name.to_string())) } @@ -308,10 +288,9 @@ pub mod unix { context: &mut super::WebContext, name: &str, handler: F, - window: Rc, ) -> crate::Result<()> where - F: Fn(&Window, &str) -> crate::Result<(Vec, String)> + 'static, + F: Fn(&str) -> crate::Result<(Vec, String)> + 'static, { let context = &context.os.context; context @@ -323,7 +302,7 @@ pub mod unix { if let Some(uri) = request.get_uri() { let uri = uri.as_str(); - match handler(&window, uri) { + match handler(uri) { Ok((buffer, mime)) => { let input = gio::MemoryInputStream::from_bytes(&glib::Bytes::from(&buffer)); request.finish(&input, buffer.len() as i64, Some(&mime)) diff --git a/src/webview/webkitgtk/mod.rs b/src/webview/webkitgtk/mod.rs index 08c461f4f..66a4a53cf 100644 --- a/src/webview/webkitgtk/mod.rs +++ b/src/webview/webkitgtk/mod.rs @@ -171,7 +171,7 @@ impl InnerWebView { // File drop handling if let Some(file_drop_handler) = attributes.file_drop_handler { - file_drop::connect_drag_event(webview.clone(), window_rc.clone(), file_drop_handler); + file_drop::connect_drag_event(webview.clone(), window_rc, file_drop_handler); } if window.get_visible() { @@ -193,8 +193,7 @@ impl InnerWebView { } for (name, handler) in attributes.custom_protocols { - let w = window_rc.clone(); - if let Err(e) = web_context.register_uri_scheme(&name, handler, w) { + if let Err(e) = web_context.register_uri_scheme(&name, handler) { if let Error::DuplicateCustomProtocol(_) = e { // Swallow duplicate scheme errors to preserve current behavior. // FIXME: we should log this error in the future diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index 13acf1269..7d4a26bda 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -196,7 +196,6 @@ impl InnerWebView { let custom_protocols = attributes.custom_protocols; let env_clone = env_.clone(); - let window_ = window.clone(); w.add_web_resource_requested(move |_, args| { let uri = args.get_request()?.get_uri()?; // Undo the protocol workaround when giving path to resolver @@ -209,7 +208,7 @@ impl InnerWebView { .iter() .find(|(name, _)| name == &scheme) .unwrap() - .1)(&window_, &path) + .1)(&path) { Ok((content, mime)) => { let stream = webview2::Stream::from_bytes(&content); diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index e48c177c4..b59fab813 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -52,10 +52,7 @@ pub struct InnerWebView { ), #[cfg(target_os = "macos")] file_drop_ptr: *mut (Box bool>, Rc), - protocol_ptrs: Vec<*mut ( - Box Result<(Vec, String)>>, - Rc, - )>, + protocol_ptrs: Vec<*mut Box Result<(Vec, String)>>>, } impl InnerWebView { @@ -163,8 +160,7 @@ impl InnerWebView { None => Class::get(&scheme_name).expect("Failed to get the class definition"), }; let handler: id = msg_send![cls, new]; - let w = window.clone(); - let function = Box::into_raw(Box::new((function, w))); + let function = Box::into_raw(Box::new(function)); protocol_ptrs.push(function); (*handler).set_ivar("function", function as *mut _ as *mut c_void);