endpoints.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 event;
  13. #[cfg(os_any)]
  14. mod operating_system;
  15. #[cfg(process_any)]
  16. mod process;
  17. mod window;
  18. /// The context passed to the invoke handler.
  19. pub struct InvokeContext<R: Runtime> {
  20. pub window: Window<R>,
  21. pub config: Arc<Config>,
  22. pub package_info: PackageInfo,
  23. }
  24. #[cfg(test)]
  25. impl<R: Runtime> Clone for InvokeContext<R> {
  26. fn clone(&self) -> Self {
  27. Self {
  28. window: self.window.clone(),
  29. config: self.config.clone(),
  30. package_info: self.package_info.clone(),
  31. }
  32. }
  33. }
  34. /// The response for a JS `invoke` call.
  35. pub struct InvokeResponse {
  36. json: Result<JsonValue>,
  37. }
  38. impl<T: Serialize> From<T> for InvokeResponse {
  39. fn from(value: T) -> Self {
  40. Self {
  41. json: serde_json::to_value(value).map_err(Into::into),
  42. }
  43. }
  44. }
  45. #[derive(Deserialize)]
  46. #[serde(tag = "module", content = "message")]
  47. enum Module {
  48. #[cfg(process_any)]
  49. Process(process::Cmd),
  50. #[cfg(os_any)]
  51. Os(operating_system::Cmd),
  52. Window(Box<window::Cmd>),
  53. Event(event::Cmd),
  54. }
  55. impl Module {
  56. fn run<R: Runtime>(
  57. self,
  58. window: Window<R>,
  59. resolver: InvokeResolver<R>,
  60. config: Arc<Config>,
  61. package_info: PackageInfo,
  62. ) {
  63. let context = InvokeContext {
  64. window,
  65. config,
  66. package_info,
  67. };
  68. match self {
  69. #[cfg(process_any)]
  70. Self::Process(cmd) => resolver.respond_async(async move {
  71. cmd
  72. .run(context)
  73. .and_then(|r| r.json)
  74. .map_err(InvokeError::from_anyhow)
  75. }),
  76. #[cfg(os_any)]
  77. Self::Os(cmd) => resolver.respond_async(async move {
  78. cmd
  79. .run(context)
  80. .and_then(|r| r.json)
  81. .map_err(InvokeError::from_anyhow)
  82. }),
  83. Self::Window(cmd) => resolver.respond_async(async move {
  84. cmd
  85. .run(context)
  86. .await
  87. .and_then(|r| r.json)
  88. .map_err(InvokeError::from_anyhow)
  89. }),
  90. Self::Event(cmd) => resolver.respond_async(async move {
  91. cmd
  92. .run(context)
  93. .and_then(|r| r.json)
  94. .map_err(InvokeError::from_anyhow)
  95. }),
  96. }
  97. }
  98. }
  99. pub(crate) fn handle<R: Runtime>(
  100. module: String,
  101. invoke: Invoke<R>,
  102. config: Arc<Config>,
  103. package_info: &PackageInfo,
  104. ) {
  105. let Invoke { message, resolver } = invoke;
  106. let InvokeMessage {
  107. mut payload,
  108. window,
  109. ..
  110. } = message;
  111. if let JsonValue::Object(ref mut obj) = payload {
  112. obj.insert("module".to_string(), JsonValue::String(module.clone()));
  113. }
  114. match serde_json::from_value::<Module>(payload) {
  115. Ok(module) => module.run(window, resolver, config, package_info.clone()),
  116. Err(e) => {
  117. let message = e.to_string();
  118. if message.starts_with("unknown variant") {
  119. let mut s = message.split('`');
  120. s.next();
  121. if let Some(unknown_variant_name) = s.next() {
  122. if unknown_variant_name == module {
  123. return resolver.reject(format!(
  124. "The `{module}` module is not enabled. You must enable one of its APIs in the allowlist.",
  125. ));
  126. } else if module == "Window" {
  127. return resolver.reject(window::into_allowlist_error(unknown_variant_name).to_string());
  128. }
  129. }
  130. }
  131. resolver.reject(message);
  132. }
  133. }
  134. }