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, IPC::SkyEvent::SkyEvent};
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
29#[allow(dead_code)]
30struct WebviewMessageContext {
31	Handle:String,
32	SideCarIdentifier:Option<String>,
33	PendingResponses:HashMap<String, tokio::sync::oneshot::Sender<Value>>,
34}
35
36/// Messaging operations implementation for MountainEnvironment
37pub(super) async fn post_message_to_webview_impl(
38	env:&MountainEnvironment,
39	handle:String,
40	message:Value,
41) -> Result<bool, CommonError> {
42	dev_log!("extensions", "[WebviewProvider] Posting message to Webview: {}", handle);
43
44	if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
45		let webview_message = WebviewMessage {
46			MessageIdentifier:Uuid::new_v4().to_string(),
47			MessageType:"request".to_string(),
48			Payload:message,
49			Source:Some("host".to_string()),
50		};
51
52		webview_window
53			.emit::<WebviewMessage>(SkyEvent::WebviewPostMessage.AsStr(), webview_message)
54			.map_err(|error| {
55				CommonError::IPCError { Description:format!("Failed to post message to Webview: {}", error) }
56			})?;
57
58		dev_log!(
59			"extensions",
60			"[WebviewProvider] Message sent successfully to Webview: {}",
61			handle
62		);
63		Ok(true)
64	} else {
65		dev_log!(
66			"extensions",
67			"warn: [WebviewProvider] Webview not found for message: {}",
68			handle
69		);
70		Ok(false)
71	}
72}
73
74/// Sets up a message listener for a specific Webview.
75pub(super) async fn setup_webview_message_listener_impl(
76	env:&MountainEnvironment,
77	handle:String,
78) -> Result<(), CommonError> {
79	dev_log!(
80		"extensions",
81		"[WebviewProvider] Setting up message listener for Webview: {}",
82		handle
83	);
84
85	// In a full implementation, this would register an event listener
86	// that forwards Webview messages to the appropriate handler.
87	// For now, we'll just log a message.
88
89	Ok(())
90}
91
92/// Removes a message listener for a specific Webview.
93pub(super) async fn remove_webview_message_listener_impl(_env:&MountainEnvironment, _handle:&str) {
94	// In a full implementation, this would remove the event listener
95	// that forwards Webview messages.
96}