From 422dd5e2a0a03bb1556915c78f110bfab092c874 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 21 Apr 2021 23:31:27 -0300 Subject: [PATCH] fix(core): command name on plugin invoke handler (#1577) --- .changes/fix-plugin-invoke.md | 5 +++++ core/tauri-macros/src/command.rs | 9 ++++++--- core/tauri/src/hooks.rs | 2 +- core/tauri/src/plugin.rs | 22 +++++++++++++--------- core/tauri/src/runtime/manager.rs | 4 ++-- core/tauri/src/runtime/window.rs | 2 +- 6 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 .changes/fix-plugin-invoke.md diff --git a/.changes/fix-plugin-invoke.md b/.changes/fix-plugin-invoke.md new file mode 100644 index 00000000000..c867d2c3fe2 --- /dev/null +++ b/.changes/fix-plugin-invoke.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Fixes the Message `command` name value on plugin invoke handler. diff --git a/core/tauri-macros/src/command.rs b/core/tauri-macros/src/command.rs index de80346b4c8..903d34dabed 100644 --- a/core/tauri-macros/src/command.rs +++ b/core/tauri-macros/src/command.rs @@ -108,7 +108,7 @@ pub fn generate_command(attrs: Vec, function: ItemFn) -> TokenStream Ok(parsed_args) => message.respond_async(async move { #return_value }), - Err(e) => message.reject(::core::result::Result::<(), String>::Err(::tauri::Error::InvalidArgs(#fn_name_str, e).to_string())), + Err(e) => message.reject(::tauri::Error::InvalidArgs(#fn_name_str, e).to_string()), } } } @@ -135,9 +135,12 @@ pub fn generate_handler(item: proc_macro::TokenStream) -> TokenStream { quote! { move |message| { - match message.command() { + let cmd = message.command().to_string(); + match cmd.as_str() { #(stringify!(#fn_names) => #fn_wrappers(message),)* - _ => {}, + _ => { + message.reject(format!("command {} not found", cmd)) + }, } } } diff --git a/core/tauri/src/hooks.rs b/core/tauri/src/hooks.rs index 40ccf4386cb..698f99af4a2 100644 --- a/core/tauri/src/hooks.rs +++ b/core/tauri/src/hooks.rs @@ -48,7 +48,7 @@ pub(crate) struct InvokePayload { /// An invoke message. pub struct InvokeMessage { window: Window, - command: String, + pub(crate) command: String, /// Allow our crate to access the payload without cloning it. pub(crate) payload: InvokePayload, diff --git a/core/tauri/src/plugin.rs b/core/tauri/src/plugin.rs index dda999fd48b..1999b90797e 100644 --- a/core/tauri/src/plugin.rs +++ b/core/tauri/src/plugin.rs @@ -105,16 +105,20 @@ impl PluginStore { .for_each(|plugin| plugin.on_page_load(window.clone(), payload.clone())) } - pub(crate) fn extend_api(&mut self, command: String, message: InvokeMessage) { - let target = command - .replace("plugin:", "") - .split('|') - .next() - .expect("target plugin name empty") - .to_string(); - - if let Some(plugin) = self.store.get_mut(target.as_str()) { + pub(crate) fn extend_api(&mut self, mut message: InvokeMessage) { + let command = message.command.replace("plugin:", ""); + let mut tokens = command.split('|'); + // safe to unwrap: split always has a least one item + let target = tokens.next().unwrap(); + + if let Some(plugin) = self.store.get_mut(target) { + message.command = tokens + .next() + .map(|c| c.to_string()) + .unwrap_or_else(String::new); plugin.extend_api(message); + } else { + message.reject(format!("plugin {} not found", target)); } } } diff --git a/core/tauri/src/runtime/manager.rs b/core/tauri/src/runtime/manager.rs index 952b60a9d3b..2adfe70d598 100644 --- a/core/tauri/src/runtime/manager.rs +++ b/core/tauri/src/runtime/manager.rs @@ -405,13 +405,13 @@ impl WindowManager

{ .expect("poisoned plugin store") .on_page_load(window, payload); } - pub fn extend_api(&self, command: String, message: InvokeMessage

) { + pub fn extend_api(&self, message: InvokeMessage

) { self .inner .plugins .lock() .expect("poisoned plugin store") - .extend_api(command, message); + .extend_api(message); } pub fn initialize_plugins(&self) -> crate::Result<()> { self diff --git a/core/tauri/src/runtime/window.rs b/core/tauri/src/runtime/window.rs index 5df5a593711..3c5da1b9fd0 100644 --- a/core/tauri/src/runtime/window.rs +++ b/core/tauri/src/runtime/window.rs @@ -182,7 +182,7 @@ pub(crate) mod export { let module = module.to_string(); crate::endpoints::handle(module, message, manager.config(), manager.package_info()); } else if command.starts_with("plugin:") { - manager.extend_api(command, message); + manager.extend_api(message); } else { manager.run_invoke_handler(message); }