pattern.rs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use std::marker::PhantomData;
  5. #[cfg(feature = "isolation")]
  6. use std::sync::Arc;
  7. use serde::Serialize;
  8. use serialize_to_javascript::{default_template, Template};
  9. use tauri_utils::assets::{Assets, EmbeddedAssets};
  10. /// An application pattern.
  11. #[derive(Debug, Clone)]
  12. pub enum Pattern<A: Assets = EmbeddedAssets> {
  13. /// The brownfield pattern.
  14. Brownfield(PhantomData<A>),
  15. /// Isolation pattern. Recommended for security purposes.
  16. #[cfg(feature = "isolation")]
  17. Isolation {
  18. /// The HTML served on `isolation://index.html`.
  19. assets: Arc<A>,
  20. /// The schema used for the isolation frames.
  21. schema: String,
  22. /// A random string used to ensure that the message went through the isolation frame.
  23. ///
  24. /// This should be regenerated at runtime.
  25. key: String,
  26. /// Cryptographically secure keys
  27. crypto_keys: Box<tauri_utils::pattern::isolation::Keys>,
  28. },
  29. }
  30. /// The shape of the JavaScript Pattern config
  31. #[derive(Debug, Serialize)]
  32. #[serde(rename_all = "lowercase", tag = "pattern")]
  33. pub(crate) enum PatternObject {
  34. /// Brownfield pattern.
  35. Brownfield,
  36. /// Isolation pattern. Recommended for security purposes.
  37. #[cfg(feature = "isolation")]
  38. Isolation {
  39. /// Which `IsolationSide` this `PatternObject` is getting injected into
  40. side: IsolationSide,
  41. },
  42. }
  43. impl From<&Pattern> for PatternObject {
  44. fn from(pattern: &Pattern) -> Self {
  45. match pattern {
  46. Pattern::Brownfield(_) => Self::Brownfield,
  47. #[cfg(feature = "isolation")]
  48. Pattern::Isolation { .. } => Self::Isolation {
  49. side: IsolationSide::default(),
  50. },
  51. }
  52. }
  53. }
  54. /// Where the JavaScript is injected to
  55. #[derive(Debug, Serialize)]
  56. #[serde(rename_all = "lowercase")]
  57. pub(crate) enum IsolationSide {
  58. /// Original frame, the Brownfield application
  59. Original,
  60. /// Secure frame, the isolation security application
  61. #[allow(dead_code)]
  62. Secure,
  63. }
  64. impl Default for IsolationSide {
  65. fn default() -> Self {
  66. Self::Original
  67. }
  68. }
  69. #[derive(Template)]
  70. #[default_template("../scripts/pattern.js")]
  71. pub(crate) struct PatternJavascript {
  72. pub(crate) pattern: PatternObject,
  73. }
  74. #[allow(dead_code)]
  75. pub(crate) fn format_real_schema(schema: &str) -> String {
  76. if cfg!(windows) {
  77. format!("https://{schema}.localhost")
  78. } else {
  79. format!("{schema}://localhost")
  80. }
  81. }