Mountain/Track/Webview/MountainWebviewPostMessageFromGuest.rs
1//! # MountainWebviewPostMessageFromGuest (Track)
2//!
3//! ## RESPONSIBILITIES
4//!
5//! This module provides a Tauri command handler for a Webview guest to post
6//! a message back to the extension host.
7//!
8//! ### Core Functions:
9//! - Get IPC provider from runtime
10//! - Forward message to main Cocoon sidecar
11//! - Handle IPC errors gracefully
12//!
13//! ## ARCHITECTURAL ROLE
14//!
15//! MountainWebviewPostMessageFromGuest acts as the **webview message
16//! forwarder** in Track's dispatch layer:
17//!
18//! ```text
19//! Webview (Guest) ──► MountainWebviewPostMessageFromGuest ──► IPC Provider ──► Cocoon (Sidecar)
20//! ```
21//!
22//! ## KEY COMPONENTS
23//!
24//! - **Fn**: Main webview message forwarding function (public async fn Fn)
25//!
26//! ## ERROR HANDLING
27//!
28//! - IPC communication errors are logged and propagated to caller
29//! - Provider requirement failures are propagated
30//!
31//! ## LOGGING
32//!
33//! - Message forwarding failures are logged at error level
34//! - Log format: "[Track/Webview] Forwarding webview message to Cocoon"
35//!
36//! ## PERFORMANCE CONSIDERATIONS
37//!
38//! - Direct IPC provider access without intermediate overhead
39//! - Async IPC operations to avoid blocking
40//!
41//! ## TODO
42//!
43//! - [ ] Add message validation before forwarding
44//! - [ ] Implement message rate limiting
45//! - [ ] Add message metrics and telemetry
46
47use std::sync::Arc;
48
49use CommonLibrary::{Environment::Requires::Requires, IPC::IPCProvider::IPCProvider};
50use log::error;
51use serde_json::{Value, json};
52use tauri::{AppHandle, Manager, command};
53
54use crate::{ApplicationState::ApplicationState, RunTime::ApplicationRunTime::ApplicationRunTime};
55
56/// A specific Tauri command handler for a Webview guest to post a message back
57/// to the extension host.
58#[command]
59pub async fn MountainWebviewPostMessageFromGuest(
60 ApplicationHandle:AppHandle,
61
62 Handle:String,
63
64 Message:Value,
65) -> Result<(), String> {
66 let IPC:Arc<dyn IPCProvider> = {
67 let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
68
69 RunTime.Environment.Require()
70 };
71
72 let RPCResult = IPC
73 .SendNotificationToSideCar("cocoon-main".into(), "$onDidReceiveMessage".into(), json!([Handle, Message]))
74 .await;
75
76 if let Err(Error) = RPCResult {
77 error!("[Track/Webview] Failed to forward webview message to Cocoon: {}", Error);
78
79 return Err(Error.to_string());
80 }
81
82 Ok(())
83}