Skip to main content

tauri_plugin_topcoat/
trace.rs

1//! What this plugin reports, and the only place its `tracing` feature is
2//! `cfg`ed.
3//!
4//! The token is never reported. Nothing here takes one.
5
6pub(crate) fn record_status(status: http::StatusCode) {
7    #[cfg(feature = "tracing")]
8    tracing::Span::current().record("status", status.as_u16());
9    #[cfg(not(feature = "tracing"))]
10    let _ = status;
11}
12
13/// Reaches stderr without the feature. A window that silently refuses to
14/// navigate is the one failure where saying nothing misleads.
15pub(crate) fn blocked_navigation(webview: &str, url: &str) {
16    #[cfg(feature = "tracing")]
17    tracing::warn!(webview, url, "blocked a navigation off this origin");
18    #[cfg(not(feature = "tracing"))]
19    eprintln!("tauri-plugin-topcoat: blocked navigation to {url} in webview {webview}");
20}
21
22/// Not a refusal - confinement is off and the application asked for this -
23/// but it explains the anonymous requests that follow.
24pub(crate) fn left_our_origin(webview: &str, url: &str) {
25    #[cfg(feature = "tracing")]
26    tracing::debug!(
27        webview,
28        url,
29        "a webview is showing another origin; it will not be handed a session"
30    );
31    #[cfg(not(feature = "tracing"))]
32    let _ = (webview, url);
33}
34
35pub(crate) fn served_before_ready() {
36    #[cfg(feature = "tracing")]
37    tracing::error!("a request arrived before the plugin finished starting");
38}
39
40/// Which rule in `Webviews::read` declined. An enum rather than a string, so a
41/// new rule has to name itself here before it compiles.
42#[cfg(feature = "session")]
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub(crate) enum Withheld {
45    UnknownWebview,
46    ShowingAnotherOrigin,
47    NoToken,
48    Expired,
49}
50
51#[cfg(all(feature = "session", feature = "tracing"))]
52impl Withheld {
53    const fn reason(self) -> &'static str {
54        match self {
55            Withheld::UnknownWebview => "unknown webview",
56            Withheld::ShowingAnotherOrigin => "showing another origin",
57            Withheld::NoToken => "no token held",
58            Withheld::Expired => "token expired",
59        }
60    }
61}
62
63#[cfg(feature = "session")]
64pub(crate) fn session_withheld(webview: &str, withheld: Withheld) {
65    #[cfg(feature = "tracing")]
66    tracing::debug!(
67        webview,
68        reason = withheld.reason(),
69        "withheld the session token"
70    );
71    #[cfg(not(feature = "tracing"))]
72    let _ = (webview, withheld);
73}
74
75#[cfg(feature = "session")]
76pub(crate) fn session_presented(webview: &str) {
77    #[cfg(feature = "tracing")]
78    tracing::trace!(webview, "presented the session token");
79    #[cfg(not(feature = "tracing"))]
80    let _ = webview;
81}
82
83#[cfg(feature = "session")]
84pub(crate) fn session_issued(webview: &str) {
85    #[cfg(feature = "tracing")]
86    tracing::debug!(webview, "issued a session token");
87    #[cfg(not(feature = "tracing"))]
88    let _ = webview;
89}
90
91#[cfg(feature = "session")]
92pub(crate) fn session_cleared(webview: &str) {
93    #[cfg(feature = "tracing")]
94    tracing::debug!(webview, "discarded the session token");
95    #[cfg(not(feature = "tracing"))]
96    let _ = webview;
97}