simplify persisted-scopes impl (remove protocol-asset feature)

pull/355/head
Lucas Nogueira 2 years ago
parent 7e85e09ab9
commit 0425d569ce
No known key found for this signature in database
GPG Key ID: 7C32FCA95C8C95D7

14
Cargo.lock generated

@ -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",

@ -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<R: Runtime>(
if let Some(files) = &files {
for file in files {
window.fs_scope().allow_file(&file.path)?;
window
.state::<tauri::scope::Scopes>()
.allow_file(&file.path)?;
}
}
OpenResponse::Files(files)
@ -143,6 +146,9 @@ pub(crate) async fn open<R: Runtime>(
let file = dialog_builder.blocking_pick_file();
if let Some(file) = &file {
window.fs_scope().allow_file(&file.path)?;
window
.state::<tauri::scope::Scopes>()
.allow_file(&file.path)?;
}
OpenResponse::File(file)
};
@ -181,6 +187,7 @@ pub(crate) async fn save<R: Runtime>(
if let Some(s) = window.try_fs_scope() {
s.allow_file(p)?;
}
window.state::<tauri::scope::Scopes>().allow_file(p)?;
}
Ok(path)

@ -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" ]

@ -87,18 +87,18 @@ fn save_scopes<R: Runtime>(app: &AppHandle<R>, app_dir: &Path, scope_state_path:
pub fn init<R: Runtime>() -> TauriPlugin<R> {
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::<tauri::scope::Scopes>();
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<R: Runtime>() -> TauriPlugin<R> {
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<R: Runtime>() -> TauriPlugin<R> {
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(())
})

Loading…
Cancel
Save