From 0425d569ce31a1a00f75ac4f34ac98f8a6ff9a30 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sat, 13 May 2023 01:07:39 -0300 Subject: [PATCH] simplify persisted-scopes impl (remove protocol-asset feature) --- Cargo.lock | 14 +++++------ plugins/dialog/src/commands.rs | 9 ++++++- plugins/persisted-scope/Cargo.toml | 3 --- plugins/persisted-scope/src/lib.rs | 38 +++++++++++++++++------------- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9bdc25cf..d590011c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4960,7 +4960,7 @@ dependencies = [ [[package]] name = "tauri" version = "2.0.0-alpha.8" -source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#2bbd957a3b1b1072487f5890071317593481f118" +source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#04735516d5b459421e49755d4821a0773afb6969" dependencies = [ "anyhow", "bytes 1.4.0", @@ -5010,7 +5010,7 @@ dependencies = [ [[package]] name = "tauri-build" version = "2.0.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#2bbd957a3b1b1072487f5890071317593481f118" +source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#04735516d5b459421e49755d4821a0773afb6969" dependencies = [ "anyhow", "cargo_toml", @@ -5030,7 +5030,7 @@ dependencies = [ [[package]] name = "tauri-codegen" version = "2.0.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#2bbd957a3b1b1072487f5890071317593481f118" +source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#04735516d5b459421e49755d4821a0773afb6969" dependencies = [ "base64 0.21.0", "brotli", @@ -5055,7 +5055,7 @@ dependencies = [ [[package]] name = "tauri-macros" version = "2.0.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#2bbd957a3b1b1072487f5890071317593481f118" +source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#04735516d5b459421e49755d4821a0773afb6969" dependencies = [ "heck 0.4.1", "proc-macro2", @@ -5449,7 +5449,7 @@ dependencies = [ [[package]] name = "tauri-runtime" version = "0.13.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#2bbd957a3b1b1072487f5890071317593481f118" +source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#04735516d5b459421e49755d4821a0773afb6969" dependencies = [ "gtk", "http", @@ -5469,7 +5469,7 @@ dependencies = [ [[package]] name = "tauri-runtime-wry" version = "0.13.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#2bbd957a3b1b1072487f5890071317593481f118" +source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#04735516d5b459421e49755d4821a0773afb6969" dependencies = [ "cocoa", "gtk", @@ -5489,7 +5489,7 @@ dependencies = [ [[package]] name = "tauri-utils" version = "2.0.0-alpha.4" -source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#2bbd957a3b1b1072487f5890071317593481f118" +source = "git+https://github.com/tauri-apps/tauri?branch=refactor/cleanup#04735516d5b459421e49755d4821a0773afb6969" dependencies = [ "aes-gcm 0.10.1", "brotli", diff --git a/plugins/dialog/src/commands.rs b/plugins/dialog/src/commands.rs index d2a1697a..faf553a4 100644 --- a/plugins/dialog/src/commands.rs +++ b/plugins/dialog/src/commands.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; use serde::{Deserialize, Serialize}; -use tauri::{command, Runtime, State, Window}; +use tauri::{command, Manager, Runtime, State, Window}; use tauri_plugin_fs::FsExt; use crate::{Dialog, FileDialogBuilder, FileResponse, MessageDialogKind, Result}; @@ -136,6 +136,9 @@ pub(crate) async fn open( if let Some(files) = &files { for file in files { window.fs_scope().allow_file(&file.path)?; + window + .state::() + .allow_file(&file.path)?; } } OpenResponse::Files(files) @@ -143,6 +146,9 @@ pub(crate) async fn open( let file = dialog_builder.blocking_pick_file(); if let Some(file) = &file { window.fs_scope().allow_file(&file.path)?; + window + .state::() + .allow_file(&file.path)?; } OpenResponse::File(file) }; @@ -181,6 +187,7 @@ pub(crate) async fn save( if let Some(s) = window.try_fs_scope() { s.allow_file(p)?; } + window.state::().allow_file(p)?; } Ok(path) diff --git a/plugins/persisted-scope/Cargo.toml b/plugins/persisted-scope/Cargo.toml index b8e25427..841fc5c1 100644 --- a/plugins/persisted-scope/Cargo.toml +++ b/plugins/persisted-scope/Cargo.toml @@ -18,6 +18,3 @@ thiserror.workspace = true aho-corasick = "1.0" bincode = "1" tauri-plugin-fs = { path = "../fs", version = "0.0.0" } - -[features] -protocol-asset = [ "tauri/protocol-asset" ] \ No newline at end of file diff --git a/plugins/persisted-scope/src/lib.rs b/plugins/persisted-scope/src/lib.rs index 7c594e71..6fb9d0f9 100644 --- a/plugins/persisted-scope/src/lib.rs +++ b/plugins/persisted-scope/src/lib.rs @@ -87,18 +87,18 @@ fn save_scopes(app: &AppHandle, app_dir: &Path, scope_state_path: pub fn init() -> TauriPlugin { Builder::new("persisted-scope") .setup(|app, _api| { - let fs_scope = app.fs_scope(); - #[cfg(feature = "protocol-asset")] - let asset_protocol_scope = app.asset_protocol_scope(); + let fs_scope = app.try_fs_scope(); + let core_scopes = app.state::(); let app = app.clone(); let app_dir = app.path().app_data_dir(); if let Ok(app_dir) = app_dir { let scope_state_path = app_dir.join(SCOPE_STATE_FILENAME); - let _ = fs_scope.forbid_file(&scope_state_path); - #[cfg(feature = "protocol-asset")] - let _ = asset_protocol_scope.forbid_file(&scope_state_path); + if let Some(s) = fs_scope { + let _ = s.forbid_file(&scope_state_path); + } + let _ = core_scopes.forbid_file(&scope_state_path); // We're trying to fix broken .persisted-scope files seamlessly, so we'll be running this on the values read on the saved file. // We will still save some semi-broken values because the scope events are quite spammy and we don't want to reduce runtime performance any further. @@ -112,16 +112,18 @@ pub fn init() -> TauriPlugin { for allowed in &scope.allowed_paths { let allowed = fix_pattern(&ac, allowed); - let _ = fs_scope.allow_file(&allowed); - #[cfg(feature = "protocol-asset")] - let _ = asset_protocol_scope.allow_file(&allowed); + if let Some(s) = fs_scope { + let _ = s.allow_file(&allowed); + } + let _ = core_scopes.allow_file(&allowed); } for forbidden in &scope.forbidden_patterns { let forbidden = fix_pattern(&ac, forbidden); - let _ = fs_scope.forbid_file(&forbidden); - #[cfg(feature = "protocol-asset")] - let _ = asset_protocol_scope.forbid_file(&forbidden); + if let Some(s) = fs_scope { + let _ = s.forbid_file(&forbidden); + } + let _ = core_scopes.forbid_file(&forbidden); } // Manually save the fixed scopes to disk once. @@ -129,11 +131,13 @@ pub fn init() -> TauriPlugin { save_scopes(&app, &app_dir, &scope_state_path); } - fs_scope.listen(move |event| { - if let FsScopeEvent::PathAllowed(_) = event { - save_scopes(&app, &app_dir, &scope_state_path); - } - }); + if let Some(s) = fs_scope { + s.listen(move |event| { + if let FsScopeEvent::PathAllowed(_) = event { + save_scopes(&app, &app_dir, &scope_state_path); + } + }); + } } Ok(()) })