Skip to main content

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 serde::{Deserialize, Serialize};
12use serde_json::Value;
13use tauri::{Emitter, Manager};
14use uuid::Uuid;
15
16use super::super::MountainEnvironment::MountainEnvironment;
17use crate::dev_log;
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	dev_log!("extensions", "[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		dev_log!(
58			"extensions",
59			"[WebviewProvider] Message sent successfully to Webview: {}",
60			handle
61		);
62		Ok(true)
63	} else {
64		dev_log!(
65			"extensions",
66			"warn: [WebviewProvider] Webview not found for message: {}",
67			handle
68		);
69		Ok(false)
70	}
71}
72
73/// Sets up a message listener for a specific Webview.
74pub(super) async fn setup_webview_message_listener_impl(
75	env:&MountainEnvironment,
76	handle:String,
77) -> Result<(), CommonError> {
78	dev_log!(
79		"extensions",
80		"[WebviewProvider] Setting up message listener for Webview: {}",
81		handle
82	);
83
84	// In a full implementation, this would register an event listener
85	// that forwards Webview messages to the appropriate handler.
86	// For now, we'll just log a message.
87
88	Ok(())
89}
90
91/// Removes a message listener for a specific Webview.
92pub(super) async fn remove_webview_message_listener_impl(_env:&MountainEnvironment, _handle:&str) {
93	// In a full implementation, this would remove the event listener
94	// that forwards Webview messages.
95}