Skip to main content

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