Browse Source

refactor(core): remove salt APIs (#2426)

Lucas Fernandes Nogueira 4 years ago
parent
commit
e2a0704c6c

+ 5 - 0
.changes/remove-salt.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Remove salt-related APIs (no longer needed after the `__TAURI_INVOKE_KEY__` implementation).

+ 0 - 8
core/tauri/src/endpoints.rs

@@ -22,7 +22,6 @@ mod event;
 mod file_system;
 mod global_shortcut;
 mod http;
-mod internal;
 mod notification;
 mod operating_system;
 mod path;
@@ -54,7 +53,6 @@ enum Module {
   Window(Box<window::Cmd>),
   Shell(shell::Cmd),
   Event(event::Cmd),
-  Internal(internal::Cmd),
   Dialog(dialog::Cmd),
   Cli(cli::Cmd),
   Notification(notification::Cmd),
@@ -113,12 +111,6 @@ impl Module {
           .and_then(|r| r.json)
           .map_err(InvokeError::from)
       }),
-      Self::Internal(cmd) => resolver.respond_async(async move {
-        cmd
-          .run(window)
-          .and_then(|r| r.json)
-          .map_err(InvokeError::from)
-      }),
       // on macOS, the dialog must run on another thread: https://github.com/rust-windowing/winit/issues/1779
       // we do the same on Windows just to stay consistent with `tao` (and it also improves UX because of the event loop)
       #[cfg(not(target_os = "linux"))]

+ 0 - 22
core/tauri/src/endpoints/internal.rs

@@ -1,22 +0,0 @@
-// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
-// SPDX-License-Identifier: Apache-2.0
-// SPDX-License-Identifier: MIT
-
-use super::InvokeResponse;
-use crate::{runtime::Runtime, Window};
-use serde::Deserialize;
-
-/// The API descriptor.
-#[derive(Deserialize)]
-#[serde(tag = "cmd", rename_all = "camelCase")]
-pub enum Cmd {
-  ValidateSalt { salt: String },
-}
-
-impl Cmd {
-  pub fn run<R: Runtime>(self, window: Window<R>) -> crate::Result<InvokeResponse> {
-    match self {
-      Self::ValidateSalt { salt } => Ok(window.verify_salt(salt).into()),
-    }
-  }
-}

+ 9 - 50
core/tauri/src/manager.rs

@@ -44,7 +44,6 @@ use std::{
 };
 use tauri_macros::default_runtime;
 use url::Url;
-use uuid::Uuid;
 
 const WINDOW_RESIZED_EVENT: &str = "tauri://resize";
 const WINDOW_MOVED_EVENT: &str = "tauri://move";
@@ -72,8 +71,6 @@ pub struct InnerWindowManager<R: Runtime> {
   assets: Arc<dyn Assets>,
   default_window_icon: Option<Vec<u8>>,
 
-  /// A list of salts that are valid for the current application.
-  salts: Mutex<HashSet<Uuid>>,
   package_info: PackageInfo,
   /// The webview protocols protocols available to all windows.
   uri_scheme_protocols: HashMap<String, Arc<CustomProtocol>>,
@@ -96,7 +93,6 @@ impl<R: Runtime> fmt::Debug for InnerWindowManager<R> {
       .field("state", &self.state)
       .field("config", &self.config)
       .field("default_window_icon", &self.default_window_icon)
-      .field("salts", &self.salts)
       .field("package_info", &self.package_info);
     {
       w = w
@@ -158,7 +154,6 @@ impl<R: Runtime> WindowManager<R> {
         config: Arc::new(context.config),
         assets: context.assets,
         default_window_icon: context.default_window_icon,
-        salts: Mutex::default(),
         package_info: context.package_info,
         uri_scheme_protocols,
         menu_ids: {
@@ -444,37 +439,24 @@ impl<R: Runtime> WindowManager<R> {
       } else {
         ""
       },
-      event_initialization_script = self.event_initialization_script(key),
+      event_initialization_script = self.event_initialization_script(),
       plugin_initialization_script = plugin_initialization_script
     )
   }
 
-  fn event_initialization_script(&self, key: u32) -> String {
+  fn event_initialization_script(&self) -> String {
     return format!(
       "
-      window['{function}'] = function (eventData, salt) {{
+      window['{function}'] = function (eventData) {{
       const listeners = (window['{listeners}'] && window['{listeners}'][eventData.event]) || []
 
-      if (listeners.length > 0) {{
-        window.__TAURI_INVOKE__('tauri', {{
-          __tauriModule: 'Internal',
-          message: {{
-            cmd: 'validateSalt',
-            salt: salt
-          }}
-        }}, {key}).then(function (flag) {{
-          if (flag) {{
-            for (let i = listeners.length - 1; i >= 0; i--) {{
-              const listener = listeners[i]
-              eventData.id = listener.id
-              listener.handler(eventData)
-            }}
-          }}
-        }})
+      for (let i = listeners.length - 1; i >= 0; i--) {{
+        const listener = listeners[i]
+        eventData.id = listener.id
+        listener.handler(eventData)
       }}
     }}
     ",
-      key = key,
       function = self.inner.listeners.function_name(),
       listeners = self.inner.listeners.listeners_object_name()
     );
@@ -708,37 +690,14 @@ impl<R: Runtime> WindowManager<R> {
   ) -> EventHandler {
     self.inner.listeners.once(event, window, handler)
   }
+
   pub fn event_listeners_object_name(&self) -> String {
     self.inner.listeners.listeners_object_name()
   }
+
   pub fn event_emit_function_name(&self) -> String {
     self.inner.listeners.function_name()
   }
-  pub fn generate_salt(&self) -> Uuid {
-    let salt = Uuid::new_v4();
-    self
-      .inner
-      .salts
-      .lock()
-      .expect("poisoned salt mutex")
-      .insert(salt);
-    salt
-  }
-  pub fn verify_salt(&self, salt: String) -> bool {
-    // flat out ignore any invalid uuids
-    let uuid: Uuid = match salt.parse() {
-      Ok(uuid) => uuid,
-      Err(_) => return false,
-    };
-
-    // HashSet::remove lets us know if the entry was found
-    self
-      .inner
-      .salts
-      .lock()
-      .expect("poisoned salt mutex")
-      .remove(&uuid)
-  }
 
   pub fn get_window(&self, label: &str) -> Option<Window<R>> {
     self.windows_lock().get(label).cloned()

+ 1 - 6
core/tauri/src/window.rs

@@ -239,11 +239,10 @@ impl<R: Runtime> Window<R> {
   /// Emits an event to the current window.
   pub fn emit<S: Serialize>(&self, event: &str, payload: S) -> crate::Result<()> {
     self.eval(&format!(
-      "window['{}']({{event: {}, payload: {}}}, '{}')",
+      "window['{}']({{event: {}, payload: {}}})",
       self.manager.event_emit_function_name(),
       serde_json::to_string(event)?,
       serde_json::to_value(payload)?,
-      self.manager.generate_salt(),
     ))?;
 
     Ok(())
@@ -689,8 +688,4 @@ impl<R: Runtime> Window<R> {
   pub fn start_dragging(&self) -> crate::Result<()> {
     self.window.dispatcher.start_dragging().map_err(Into::into)
   }
-
-  pub(crate) fn verify_salt(&self, salt: String) -> bool {
-    self.manager.verify_salt(salt)
-  }
 }