From a8e4b73ae139a6b779bbca8b40df78b283a7337a Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Mon, 16 Sep 2024 11:26:31 -0300 Subject: [PATCH 1/2] refactor(core): remove responder from custom invoke systems the responder can be set directly in the WebviewWindow::on_message call --- .changes/remove-invoke-system-responder.md | 5 + Cargo.lock | 2 +- crates/tauri/src/app.rs | 17 +- crates/tauri/src/ipc/protocol.rs | 174 ++++++++++----------- crates/tauri/src/manager/mod.rs | 5 +- crates/tauri/src/manager/webview.rs | 4 +- crates/tauri/src/webview/mod.rs | 6 - 7 files changed, 97 insertions(+), 116 deletions(-) create mode 100644 .changes/remove-invoke-system-responder.md diff --git a/.changes/remove-invoke-system-responder.md b/.changes/remove-invoke-system-responder.md new file mode 100644 index 000000000000..78f90a2321c2 --- /dev/null +++ b/.changes/remove-invoke-system-responder.md @@ -0,0 +1,5 @@ +--- +"tauri": patch:breaking +--- + +Remove the `responder` part of a custom invoke system now that the responder can be set directly in the `tauri::WebviewWindow::on_message` function. diff --git a/Cargo.lock b/Cargo.lock index bf4eefd5275d..f8d29af73a4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7284,7 +7284,7 @@ checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" [[package]] name = "tauri" -version = "2.0.0-rc.13" +version = "2.0.0-rc.14" dependencies = [ "anyhow", "bytes", diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index 0364f4b5e48a..754c6f33b834 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -5,8 +5,7 @@ use crate::{ image::Image, ipc::{ - channel::ChannelDataIpcQueue, CallbackFn, CommandArg, CommandItem, Invoke, InvokeError, - InvokeHandler, InvokeResponder, InvokeResponse, + channel::ChannelDataIpcQueue, CommandArg, CommandItem, Invoke, InvokeError, InvokeHandler, }, manager::{webview::UriSchemeProtocol, AppManager, Asset}, plugin::{Plugin, PluginStore}, @@ -1169,9 +1168,6 @@ pub struct Builder { /// The JS message handler. invoke_handler: Box>, - /// The JS message responder. - invoke_responder: Option>>, - /// The script that initializes the `window.__TAURI_INTERNALS__.postMessage` function. pub(crate) invoke_initialization_script: String, @@ -1248,7 +1244,6 @@ impl Builder { runtime_any_thread: false, setup: Box::new(|_| Ok(())), invoke_handler: Box::new(|_| false), - invoke_responder: None, invoke_initialization_script: InvokeInitializationScript { process_ipc_message_fn: crate::manager::webview::PROCESS_IPC_MESSAGE_FN, os_name: std::env::consts::OS, @@ -1312,17 +1307,11 @@ impl Builder { /// Defines a custom JS message system. /// - /// The `responder` is a function that will be called when a command has been executed and must send a response to the JS layer. - /// /// The `initialization_script` is a script that initializes `window.__TAURI_INTERNALS__.postMessage`. /// That function must take the `(message: object, options: object)` arguments and send it to the backend. #[must_use] - pub fn invoke_system(mut self, initialization_script: String, responder: F) -> Self - where - F: Fn(&Webview, &str, &InvokeResponse, CallbackFn, CallbackFn) + Send + Sync + 'static, - { + pub fn invoke_system(mut self, initialization_script: String) -> Self { self.invoke_initialization_script = initialization_script; - self.invoke_responder.replace(Arc::new(responder)); self } @@ -1782,7 +1771,7 @@ tauri::Builder::default() self.webview_event_listeners, #[cfg(desktop)] HashMap::new(), - (self.invoke_responder, self.invoke_initialization_script), + self.invoke_initialization_script, self.invoke_key, )); diff --git a/crates/tauri/src/ipc/protocol.rs b/crates/tauri/src/ipc/protocol.rs index 23563251b511..3152e2d8d566 100644 --- a/crates/tauri/src/ipc/protocol.rs +++ b/crates/tauri/src/ipc/protocol.rs @@ -325,7 +325,6 @@ fn handle_ipc_message(request: Request, manager: &AppManager request, Box::new(move |webview, cmd, response, callback, error| { use crate::ipc::Channel; - use crate::sealed::ManagerBase; #[cfg(feature = "tracing")] let _respond_span = tracing::trace_span!( @@ -334,104 +333,101 @@ fn handle_ipc_message(request: Request, manager: &AppManager ) .entered(); - // the channel data command is the only command that uses a custom protocol on Linux - if webview.manager().webview.invoke_responder.is_none() { - fn responder_eval( - webview: &crate::Webview, - js: crate::Result, - error: CallbackFn, - ) { - let eval_js = match js { - Ok(js) => js, - Err(e) => crate::ipc::format_callback::format(error, &e.to_string()) - .expect("unable to serialize response error string to json"), - }; - - let _ = webview.eval(&eval_js); - } + fn responder_eval( + webview: &crate::Webview, + js: crate::Result, + error: CallbackFn, + ) { + let eval_js = match js { + Ok(js) => js, + Err(e) => crate::ipc::format_callback::format(error, &e.to_string()) + .expect("unable to serialize response error string to json"), + }; - let can_use_channel_for_response = cmd - != crate::ipc::channel::FETCH_CHANNEL_DATA_COMMAND - && !options.custom_protocol_ipc_blocked; + let _ = webview.eval(&eval_js); + } - #[cfg(feature = "tracing")] - let mime_type = match &response { - InvokeResponse::Ok(InvokeResponseBody::Json(_)) => mime::APPLICATION_JSON, - InvokeResponse::Ok(InvokeResponseBody::Raw(_)) => mime::APPLICATION_OCTET_STREAM, - InvokeResponse::Err(_) => mime::APPLICATION_JSON, - }; + let can_use_channel_for_response = cmd + != crate::ipc::channel::FETCH_CHANNEL_DATA_COMMAND + && !options.custom_protocol_ipc_blocked; - #[cfg(feature = "tracing")] - let _response_span = match &response { - InvokeResponse::Ok(InvokeResponseBody::Json(v)) => tracing::trace_span!( - "ipc::request::response", - response = v, - mime_type = mime_type.essence_str() - ) - .entered(), - InvokeResponse::Ok(InvokeResponseBody::Raw(v)) => tracing::trace_span!( - "ipc::request::response", - response = format!("{v:?}"), - mime_type = mime_type.essence_str() - ) - .entered(), - InvokeResponse::Err(e) => tracing::trace_span!( - "ipc::request::response", - response = format!("{e:?}"), - mime_type = mime_type.essence_str() - ) - .entered(), - }; + #[cfg(feature = "tracing")] + let mime_type = match &response { + InvokeResponse::Ok(InvokeResponseBody::Json(_)) => mime::APPLICATION_JSON, + InvokeResponse::Ok(InvokeResponseBody::Raw(_)) => mime::APPLICATION_OCTET_STREAM, + InvokeResponse::Err(_) => mime::APPLICATION_JSON, + }; - match response { - InvokeResponse::Ok(InvokeResponseBody::Json(v)) => { - if !(cfg!(target_os = "macos") || cfg!(target_os = "ios")) - && (v.starts_with('{') || v.starts_with('[')) - && can_use_channel_for_response - { - let _ = Channel::from_callback_fn(webview, callback) - .send(InvokeResponseBody::Json(v)); - } else { - responder_eval( - &webview, - crate::ipc::format_callback::format_result_raw( - Result::<_, String>::Ok(v), - callback, - error, - ), + #[cfg(feature = "tracing")] + let _response_span = match &response { + InvokeResponse::Ok(InvokeResponseBody::Json(v)) => tracing::trace_span!( + "ipc::request::response", + response = v, + mime_type = mime_type.essence_str() + ) + .entered(), + InvokeResponse::Ok(InvokeResponseBody::Raw(v)) => tracing::trace_span!( + "ipc::request::response", + response = format!("{v:?}"), + mime_type = mime_type.essence_str() + ) + .entered(), + InvokeResponse::Err(e) => tracing::trace_span!( + "ipc::request::response", + response = format!("{e:?}"), + mime_type = mime_type.essence_str() + ) + .entered(), + }; + + match response { + InvokeResponse::Ok(InvokeResponseBody::Json(v)) => { + if !(cfg!(target_os = "macos") || cfg!(target_os = "ios")) + && (v.starts_with('{') || v.starts_with('[')) + && can_use_channel_for_response + { + let _ = + Channel::from_callback_fn(webview, callback).send(InvokeResponseBody::Json(v)); + } else { + responder_eval( + &webview, + crate::ipc::format_callback::format_result_raw( + Result::<_, String>::Ok(v), + callback, error, - ) - } + ), + error, + ) } - InvokeResponse::Ok(InvokeResponseBody::Raw(v)) => { - if cfg!(target_os = "macos") - || cfg!(target_os = "ios") - || !can_use_channel_for_response - { - responder_eval( - &webview, - crate::ipc::format_callback::format_result( - Result::<_, ()>::Ok(v), - callback, - error, - ), + } + InvokeResponse::Ok(InvokeResponseBody::Raw(v)) => { + if cfg!(target_os = "macos") + || cfg!(target_os = "ios") + || !can_use_channel_for_response + { + responder_eval( + &webview, + crate::ipc::format_callback::format_result( + Result::<_, ()>::Ok(v), + callback, error, - ); - } else { - let _ = Channel::from_callback_fn(webview, callback) - .send(InvokeResponseBody::Raw(v.clone())); - } - } - InvokeResponse::Err(e) => responder_eval( - &webview, - crate::ipc::format_callback::format_result( - Result::<(), _>::Err(&e.0), - callback, + ), error, - ), + ); + } else { + let _ = Channel::from_callback_fn(webview, callback) + .send(InvokeResponseBody::Raw(v.clone())); + } + } + InvokeResponse::Err(e) => responder_eval( + &webview, + crate::ipc::format_callback::format_result( + Result::<(), _>::Err(&e.0), + callback, error, ), - } + error, + ), } }), ); diff --git a/crates/tauri/src/manager/mod.rs b/crates/tauri/src/manager/mod.rs index b095086234bc..21795768b570 100644 --- a/crates/tauri/src/manager/mod.rs +++ b/crates/tauri/src/manager/mod.rs @@ -22,7 +22,7 @@ use tauri_utils::{ use crate::{ app::{AppHandle, GlobalWebviewEventListener, GlobalWindowEventListener, OnPageLoad}, event::{assert_event_name_is_valid, Event, EventId, EventTarget, Listeners}, - ipc::{Invoke, InvokeHandler, InvokeResponder, RuntimeAuthority}, + ipc::{Invoke, InvokeHandler, RuntimeAuthority}, plugin::PluginStore, utils::{config::Config, PackageInfo}, Assets, Context, Pattern, Runtime, StateManager, Window, @@ -254,7 +254,7 @@ impl AppManager { String, crate::app::GlobalMenuEventListener>, >, - (invoke_responder, invoke_initialization_script): (Option>>, String), + invoke_initialization_script: String, invoke_key: String, ) -> Self { // generate a random isolation key at runtime @@ -276,7 +276,6 @@ impl AppManager { on_page_load, uri_scheme_protocols: Mutex::new(uri_scheme_protocols), event_listeners: Arc::new(webiew_event_listeners), - invoke_responder, invoke_initialization_script, invoke_key: invoke_key.clone(), }, diff --git a/crates/tauri/src/manager/webview.rs b/crates/tauri/src/manager/webview.rs index ebb0f3f96393..0ea38e93d002 100644 --- a/crates/tauri/src/manager/webview.rs +++ b/crates/tauri/src/manager/webview.rs @@ -21,7 +21,7 @@ use url::Url; use crate::{ app::{GlobalWebviewEventListener, OnPageLoad, UriSchemeResponder, WebviewEvent}, - ipc::{InvokeHandler, InvokeResponder}, + ipc::InvokeHandler, pattern::PatternJavascript, sealed::ManagerBase, webview::PageLoadPayload, @@ -75,8 +75,6 @@ pub struct WebviewManager { /// Webview event listeners to all webviews. pub event_listeners: Arc>>, - /// Responder for invoke calls. - pub invoke_responder: Option>>, /// The script that initializes the invoke system. pub invoke_initialization_script: String, diff --git a/crates/tauri/src/webview/mod.rs b/crates/tauri/src/webview/mod.rs index ee75d97d1596..4593b9eebf85 100644 --- a/crates/tauri/src/webview/mod.rs +++ b/crates/tauri/src/webview/mod.rs @@ -1138,17 +1138,11 @@ fn main() { return; } - let custom_responder = self.manager().webview.invoke_responder.clone(); - let resolver = InvokeResolver::new( self.clone(), Arc::new(Mutex::new(Some(Box::new( #[allow(unused_variables)] move |webview: Webview, cmd, response, callback, error| { - if let Some(responder) = &custom_responder { - (responder)(&webview, &cmd, &response, callback, error); - } - responder(webview, cmd, response, callback, error); }, )))), From 36c4c9bc9246e1de6787cadd847d64409501b686 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Mon, 16 Sep 2024 11:28:20 -0300 Subject: [PATCH 2/2] fix tests --- crates/tauri/src/ipc/protocol.rs | 4 ++-- crates/tauri/src/manager/mod.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/tauri/src/ipc/protocol.rs b/crates/tauri/src/ipc/protocol.rs index 3152e2d8d566..8252a92577c8 100644 --- a/crates/tauri/src/ipc/protocol.rs +++ b/crates/tauri/src/ipc/protocol.rs @@ -589,7 +589,7 @@ mod tests { Default::default(), Default::default(), Default::default(), - (None, "".into()), + "".into(), crate::generate_invoke_key().unwrap(), ); @@ -702,7 +702,7 @@ mod tests { Default::default(), Default::default(), Default::default(), - (None, "".into()), + "".into(), crate::generate_invoke_key().unwrap(), ); diff --git a/crates/tauri/src/manager/mod.rs b/crates/tauri/src/manager/mod.rs index 21795768b570..00b13fef1077 100644 --- a/crates/tauri/src/manager/mod.rs +++ b/crates/tauri/src/manager/mod.rs @@ -730,7 +730,7 @@ mod test { Default::default(), Default::default(), Default::default(), - (None, "".into()), + "".into(), crate::generate_invoke_key().unwrap(), );