Mountain/Environment/WebviewProvider/
messaging.rs

1//! # WebviewProvider - Messaging Operations
2//!
3//! Implementation of webview message passing for
4//! [`MountainEnvironment`]
5//!
6//! Handles secure bidirectional communication between host and webview.
7
8use std::collections::HashMap;
9
10use CommonLibrary::Error::CommonError::CommonError;
11use log::{debug, warn};
12use serde::{Deserialize, Serialize};
13use serde_json::Value;
14use tauri::{Emitter, Manager};
15use uuid::Uuid;
16
17use super::super::MountainEnvironment::MountainEnvironment;
18
19/// Represents a Webview message
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct WebviewMessage {
22	pub MessageIdentifier:String,
23	pub MessageType:String,
24	pub Payload:Value,
25	pub Source:Option<String>,
26}
27
28/// Webview message handler context
29struct WebviewMessageContext {
30	Handle:String,
31	SideCarIdentifier:Option<String>,
32	PendingResponses:HashMap<String, tokio::sync::oneshot::Sender<Value>>,
33}
34
35/// Messaging operations implementation for MountainEnvironment
36pub(super) async fn post_message_to_webview_impl(
37	env:&MountainEnvironment,
38	handle:String,
39	message:Value,
40) -> Result<bool, CommonError> {
41	debug!("[WebviewProvider] Posting message to Webview: {}", handle);
42
43	if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
44		let webview_message = WebviewMessage {
45			MessageIdentifier:Uuid::new_v4().to_string(),
46			MessageType:"request".to_string(),
47			Payload:message,
48			Source:Some("host".to_string()),
49		};
50
51		webview_window
52			.emit::<WebviewMessage>("sky://webview/post-message", webview_message)
53			.map_err(|error| {
54				CommonError::IPCError { Description:format!("Failed to post message to Webview: {}", error) }
55			})?;
56
57		debug!("[WebviewProvider] Message sent successfully to Webview: {}", handle);
58		Ok(true)
59	} else {
60		warn!("[WebviewProvider] Webview not found for message: {}", handle);
61		Ok(false)
62	}
63}
64
65/// Sets up a message listener for a specific Webview.
66pub(super) async fn setup_webview_message_listener_impl(
67	env:&MountainEnvironment,
68	handle:String,
69) -> Result<(), CommonError> {
70	debug!("[WebviewProvider] Setting up message listener for Webview: {}", handle);
71
72	// In a full implementation, this would register an event listener
73	// that forwards Webview messages to the appropriate handler.
74	// For now, we'll just log a message.
75
76	Ok(())
77}
78
79/// Removes a message listener for a specific Webview.
80pub(super) async fn remove_webview_message_listener_impl(_env:&MountainEnvironment, _handle:&str) {
81	// In a full implementation, this would remove the event listener
82	// that forwards Webview messages.
83}