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