Skip to main content

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::{
56	ApplicationState::State::ApplicationState::ApplicationState,
57	RunTime::ApplicationRunTime::ApplicationRunTime,
58	dev_log,
59};
60
61/// A specific Tauri command handler for a Webview guest to post a message back
62/// to the extension host.
63#[command]
64pub async fn MountainWebviewPostMessageFromGuest(
65	ApplicationHandle:AppHandle,
66
67	Handle:String,
68
69	Message:Value,
70) -> Result<(), String> {
71	let IPC:Arc<dyn IPCProvider> = {
72		let RunTime = ApplicationHandle.state::<Arc<ApplicationRunTime>>().inner().clone();
73
74		RunTime.Environment.Require()
75	};
76
77	let RPCResult = IPC
78		.SendNotificationToSideCar("cocoon-main".into(), "$onDidReceiveMessage".into(), json!([Handle, Message]))
79		.await;
80
81	if let Err(Error) = RPCResult {
82		dev_log!(
83			"ipc",
84			"error: [Track/Webview] Failed to forward webview message to Cocoon: {}",
85			Error
86		);
87
88		return Err(Error.to_string());
89	}
90
91	Ok(())
92}