endpoints.rs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use crate::{
  5. hooks::{InvokeError, InvokeMessage, InvokeResolver},
  6. Config, Invoke, PackageInfo, Runtime, Window,
  7. };
  8. pub use anyhow::Result;
  9. use serde::{Deserialize, Serialize};
  10. use serde_json::Value as JsonValue;
  11. use std::sync::Arc;
  12. mod app;
  13. #[cfg(cli)]
  14. mod cli;
  15. #[cfg(clipboard_any)]
  16. mod clipboard;
  17. #[cfg(dialog_any)]
  18. mod dialog;
  19. mod event;
  20. #[cfg(fs_any)]
  21. mod file_system;
  22. #[cfg(global_shortcut_any)]
  23. mod global_shortcut;
  24. #[cfg(http_any)]
  25. mod http;
  26. mod notification;
  27. #[cfg(os_any)]
  28. mod operating_system;
  29. #[cfg(path_any)]
  30. mod path;
  31. #[cfg(process_any)]
  32. mod process;
  33. #[cfg(shell_any)]
  34. mod shell;
  35. mod window;
  36. /// The context passed to the invoke handler.
  37. pub struct InvokeContext<R: Runtime> {
  38. pub window: Window<R>,
  39. pub config: Arc<Config>,
  40. pub package_info: PackageInfo,
  41. }
  42. #[cfg(test)]
  43. impl<R: Runtime> Clone for InvokeContext<R> {
  44. fn clone(&self) -> Self {
  45. Self {
  46. window: self.window.clone(),
  47. config: self.config.clone(),
  48. package_info: self.package_info.clone(),
  49. }
  50. }
  51. }
  52. /// The response for a JS `invoke` call.
  53. pub struct InvokeResponse {
  54. json: Result<JsonValue>,
  55. }
  56. impl<T: Serialize> From<T> for InvokeResponse {
  57. fn from(value: T) -> Self {
  58. Self {
  59. json: serde_json::to_value(value).map_err(Into::into),
  60. }
  61. }
  62. }
  63. #[derive(Deserialize)]
  64. #[serde(tag = "module", content = "message")]
  65. enum Module {
  66. App(app::Cmd),
  67. #[cfg(process_any)]
  68. Process(process::Cmd),
  69. #[cfg(fs_any)]
  70. Fs(file_system::Cmd),
  71. #[cfg(os_any)]
  72. Os(operating_system::Cmd),
  73. #[cfg(path_any)]
  74. Path(path::Cmd),
  75. Window(Box<window::Cmd>),
  76. #[cfg(shell_any)]
  77. Shell(shell::Cmd),
  78. Event(event::Cmd),
  79. #[cfg(dialog_any)]
  80. Dialog(dialog::Cmd),
  81. #[cfg(cli)]
  82. Cli(cli::Cmd),
  83. Notification(notification::Cmd),
  84. #[cfg(http_any)]
  85. Http(http::Cmd),
  86. #[cfg(global_shortcut_any)]
  87. GlobalShortcut(global_shortcut::Cmd),
  88. #[cfg(clipboard_any)]
  89. Clipboard(clipboard::Cmd),
  90. }
  91. impl Module {
  92. fn run<R: Runtime>(
  93. self,
  94. window: Window<R>,
  95. resolver: InvokeResolver<R>,
  96. config: Arc<Config>,
  97. package_info: PackageInfo,
  98. ) {
  99. let context = InvokeContext {
  100. window,
  101. config,
  102. package_info,
  103. };
  104. match self {
  105. Self::App(cmd) => resolver.respond_async(async move {
  106. cmd
  107. .run(context)
  108. .and_then(|r| r.json)
  109. .map_err(InvokeError::from_anyhow)
  110. }),
  111. #[cfg(process_any)]
  112. Self::Process(cmd) => resolver.respond_async(async move {
  113. cmd
  114. .run(context)
  115. .and_then(|r| r.json)
  116. .map_err(InvokeError::from_anyhow)
  117. }),
  118. #[cfg(fs_any)]
  119. Self::Fs(cmd) => resolver.respond_async(async move {
  120. cmd
  121. .run(context)
  122. .and_then(|r| r.json)
  123. .map_err(InvokeError::from_anyhow)
  124. }),
  125. #[cfg(path_any)]
  126. Self::Path(cmd) => resolver.respond_async(async move {
  127. cmd
  128. .run(context)
  129. .and_then(|r| r.json)
  130. .map_err(InvokeError::from_anyhow)
  131. }),
  132. #[cfg(os_any)]
  133. Self::Os(cmd) => resolver.respond_async(async move {
  134. cmd
  135. .run(context)
  136. .and_then(|r| r.json)
  137. .map_err(InvokeError::from_anyhow)
  138. }),
  139. Self::Window(cmd) => resolver.respond_async(async move {
  140. cmd
  141. .run(context)
  142. .await
  143. .and_then(|r| r.json)
  144. .map_err(InvokeError::from_anyhow)
  145. }),
  146. #[cfg(shell_any)]
  147. Self::Shell(cmd) => resolver.respond_async(async move {
  148. cmd
  149. .run(context)
  150. .and_then(|r| r.json)
  151. .map_err(InvokeError::from_anyhow)
  152. }),
  153. Self::Event(cmd) => resolver.respond_async(async move {
  154. cmd
  155. .run(context)
  156. .and_then(|r| r.json)
  157. .map_err(InvokeError::from_anyhow)
  158. }),
  159. #[cfg(dialog_any)]
  160. Self::Dialog(cmd) => resolver.respond_async(async move {
  161. cmd
  162. .run(context)
  163. .and_then(|r| r.json)
  164. .map_err(InvokeError::from_anyhow)
  165. }),
  166. #[cfg(cli)]
  167. Self::Cli(cmd) => resolver.respond_async(async move {
  168. cmd
  169. .run(context)
  170. .and_then(|r| r.json)
  171. .map_err(InvokeError::from_anyhow)
  172. }),
  173. Self::Notification(cmd) => resolver.respond_async(async move {
  174. cmd
  175. .run(context)
  176. .and_then(|r| r.json)
  177. .map_err(InvokeError::from_anyhow)
  178. }),
  179. #[cfg(http_any)]
  180. Self::Http(cmd) => resolver.respond_async(async move {
  181. cmd
  182. .run(context)
  183. .await
  184. .and_then(|r| r.json)
  185. .map_err(InvokeError::from_anyhow)
  186. }),
  187. #[cfg(global_shortcut_any)]
  188. Self::GlobalShortcut(cmd) => resolver.respond_async(async move {
  189. cmd
  190. .run(context)
  191. .and_then(|r| r.json)
  192. .map_err(InvokeError::from_anyhow)
  193. }),
  194. #[cfg(clipboard_any)]
  195. Self::Clipboard(cmd) => resolver.respond_async(async move {
  196. cmd
  197. .run(context)
  198. .and_then(|r| r.json)
  199. .map_err(InvokeError::from_anyhow)
  200. }),
  201. }
  202. }
  203. }
  204. pub(crate) fn handle<R: Runtime>(
  205. module: String,
  206. invoke: Invoke<R>,
  207. config: Arc<Config>,
  208. package_info: &PackageInfo,
  209. ) {
  210. let Invoke { message, resolver } = invoke;
  211. let InvokeMessage {
  212. mut payload,
  213. window,
  214. ..
  215. } = message;
  216. if let JsonValue::Object(ref mut obj) = payload {
  217. obj.insert("module".to_string(), JsonValue::String(module.clone()));
  218. }
  219. match serde_json::from_value::<Module>(payload) {
  220. Ok(module) => module.run(window, resolver, config, package_info.clone()),
  221. Err(e) => {
  222. let message = e.to_string();
  223. if message.starts_with("unknown variant") {
  224. let mut s = message.split('`');
  225. s.next();
  226. if let Some(unknown_variant_name) = s.next() {
  227. if unknown_variant_name == module {
  228. return resolver.reject(format!(
  229. "The `{module}` module is not enabled. You must enable one of its APIs in the allowlist."
  230. ));
  231. } else if module == "Window" {
  232. return resolver.reject(window::into_allowlist_error(unknown_variant_name).to_string());
  233. }
  234. }
  235. }
  236. resolver.reject(message);
  237. }
  238. }
  239. }