forked from qodo-benchmark/tauri
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.rs
More file actions
93 lines (81 loc) · 2.58 KB
/
pattern.rs
File metadata and controls
93 lines (81 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
#[cfg(feature = "isolation")]
use std::sync::Arc;
use serde::Serialize;
use serialize_to_javascript::{default_template, Template};
/// The domain of the isolation iframe source.
#[cfg(feature = "isolation")]
pub const ISOLATION_IFRAME_SRC_DOMAIN: &str = "localhost";
/// An application pattern.
#[derive(Debug)]
pub enum Pattern {
/// The brownfield pattern.
Brownfield,
/// Isolation pattern. Recommended for security purposes.
#[cfg(feature = "isolation")]
Isolation {
/// The HTML served on `isolation://index.html`.
assets: Arc<tauri_utils::assets::EmbeddedAssets>,
/// The schema used for the isolation frames.
schema: String,
/// A random string used to ensure that the message went through the isolation frame.
///
/// This should be regenerated at runtime.
key: String,
/// Cryptographically secure keys
crypto_keys: Box<tauri_utils::pattern::isolation::Keys>,
},
}
/// The shape of the JavaScript Pattern config
#[derive(Debug, Serialize)]
#[serde(rename_all = "lowercase", tag = "pattern")]
pub(crate) enum PatternObject {
/// Brownfield pattern.
Brownfield,
/// Isolation pattern. Recommended for security purposes.
#[cfg(feature = "isolation")]
Isolation {
/// Which `IsolationSide` this `PatternObject` is getting injected into
side: IsolationSide,
},
}
impl From<&Pattern> for PatternObject {
fn from(pattern: &Pattern) -> Self {
match pattern {
Pattern::Brownfield => Self::Brownfield,
#[cfg(feature = "isolation")]
Pattern::Isolation { .. } => Self::Isolation {
side: IsolationSide::default(),
},
}
}
}
/// Where the JavaScript is injected to
#[cfg(feature = "isolation")]
#[derive(Default, Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub(crate) enum IsolationSide {
/// Original frame, the Brownfield application
#[default]
Original,
/// Secure frame, the isolation security application
#[allow(dead_code)]
Secure,
}
#[cfg(feature = "isolation")]
#[derive(Template)]
#[default_template("../scripts/pattern.js")]
pub(crate) struct PatternJavascript {
pub(crate) pattern: PatternObject,
}
#[cfg(feature = "isolation")]
pub(crate) fn format_real_schema(schema: &str, https: bool) -> String {
if cfg!(windows) || cfg!(target_os = "android") {
let scheme = if https { "https" } else { "http" };
format!("{scheme}://{schema}.{ISOLATION_IFRAME_SRC_DOMAIN}/")
} else {
format!("{schema}://{ISOLATION_IFRAME_SRC_DOMAIN}/")
}
}