Skip to main content

Mountain/Environment/WebviewProvider/
Lifecycle.rs

1//! # WebviewProvider - Lifecycle Operations
2//!
3//! Implementation of webview panel lifecycle methods for
4//! [`MountainEnvironment`]
5//!
6//! Handles creation, disposal, and visibility management of webview panels.
7
8use CommonLibrary::{
9	Error::CommonError::CommonError,
10	IPC::SkyEvent::SkyEvent,
11	Webview::DTO::WebviewContentOptionsDTO::WebviewContentOptionsDTO,
12};
13use serde_json::{Value, json};
14use tauri::{Emitter, Manager, WebviewWindowBuilder};
15use uuid::Uuid;
16
17use super::super::{MountainEnvironment::MountainEnvironment, Utility};
18use crate::{ApplicationState::DTO::WebviewStateDTO::WebviewStateDTO, dev_log};
19
20/// Lifecycle operations implementation for MountainEnvironment
21pub(super) async fn create_webview_panel_impl(
22	env:&MountainEnvironment,
23
24	extension_data_value:Value,
25
26	view_type:String,
27
28	title:String,
29
30	_show_options_value:Value,
31
32	panel_options_value:Value,
33
34	content_options_value:Value,
35) -> Result<String, CommonError> {
36	let handle = Uuid::new_v4().to_string();
37
38	dev_log!(
39		"extensions",
40		"[WebviewProvider] Creating WebviewPanel with handle: {}, viewType: {}",
41		handle,
42		view_type
43	);
44
45	// Parse content options to ensure security settings
46	let content_options:WebviewContentOptionsDTO =
47		serde_json::from_value(content_options_value.clone()).map_err(|error| {
48			CommonError::InvalidArgument { ArgumentName:"ContentOptions".into(), Reason:error.to_string() }
49		})?;
50
51	let state = WebviewStateDTO {
52		Handle:handle.clone(),
53
54		ViewType:view_type.clone(),
55
56		Title:title.clone(),
57
58		ContentOptions:content_options,
59
60		PanelOptions:panel_options_value.clone(),
61
62		SideCarIdentifier:"cocoon-main".to_string(),
63
64		ExtensionIdentifier:extension_data_value
65			.get("id")
66			.and_then(|v| v.as_str())
67			.unwrap_or_default()
68			.to_string(),
69
70		IsActive:true,
71
72		IsVisible:true,
73	};
74
75	// Store the initial state with lifecycle state
76	{
77		let mut webview_guard = env
78			.ApplicationState
79			.Feature
80			.Webviews
81			.ActiveWebviews
82			.lock()
83			.map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?;
84
85		webview_guard.insert(handle.clone(), state);
86	}
87
88	// Create a new Tauri window for this webview with security settings.
89	// Use the localhost plugin URL (http://localhost:PORT/Mountain/WebviewHost.html)
90	// so the webview's scripts are served by the same localhost plugin as the
91	// main window. WebviewUrl::App("WebviewHost.html") routes through
92	// tauri::manager which has no assets when frontendDist is null.
93	let title_clone = title.clone();
94
95	let WebviewHostUrl = crate::IPC::WindServiceHandlers::Utilities::LocalhostUrl::Get::Fn()
96		.map(|Base| format!("{}/Mountain/WebviewHost", Base))
97		.unwrap_or_else(|| "http://localhost:15536/Mountain/WebviewHost".to_string());
98
99	let WebviewUrlParsed = WebviewHostUrl
100		.parse::<url::Url>()
101		.map(tauri::WebviewUrl::External)
102		.unwrap_or_else(|_| tauri::WebviewUrl::App("WebviewHost.html".into()));
103
104	let _webview_window = WebviewWindowBuilder::new(&env.ApplicationHandle, &handle, WebviewUrlParsed)
105		.title(title)
106		.initialization_script(&format!(
107			"window.__WEBVIEW_INITIAL_STATE__ = {};",
108			json!({
109				"Handle": handle,
110				"ViewType": view_type,
111				"Title": title_clone
112			})
113		))
114		.build()
115		.map_err(|error| {
116			dev_log!(
117				"extensions",
118				"error: [WebviewProvider] Failed to create Webview window: {}",
119				error
120			);
121			CommonError::UserInterfaceInteraction { Reason:error.to_string() }
122		})?;
123
124	// Setup message listener for this Webview
125	crate::Environment::WebviewProvider::Messaging::setup_webview_message_listener_impl(env, handle.clone()).await?;
126
127	// Notify frontend about Webview creation
128	env.ApplicationHandle
129		.emit::<Value>(
130			SkyEvent::WebviewCreated.AsStr(),
131			json!({ "Handle": handle.clone(), "ViewType": view_type.clone(), "Title": title_clone }),
132		)
133		.map_err(|error| {
134			CommonError::IPCError { Description:format!("Failed to emit Webview creation event: {}", error) }
135		})?;
136
137	Ok(handle)
138}
139
140/// Disposes a Webview panel and cleans up all associated resources.
141pub(super) async fn dispose_webview_panel_impl(env:&MountainEnvironment, handle:String) -> Result<(), CommonError> {
142	dev_log!("extensions", "[WebviewProvider] Disposing WebviewPanel: {}", handle);
143
144	// Remove message listener
145	let _ = crate::Environment::WebviewProvider::Messaging::remove_webview_message_listener_impl(env, &handle).await;
146
147	// Close the window
148	if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
149		if let Err(error) = webview_window.close() {
150			dev_log!(
151				"extensions",
152				"warn: [WebviewProvider] Failed to close Webview window: {}",
153				error
154			);
155		}
156	}
157
158	// Remove state
159	env.ApplicationState
160		.Feature
161		.Webviews
162		.ActiveWebviews
163		.lock()
164		.map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?
165		.remove(&handle);
166
167	// Notify frontend about Webview disposal
168	env.ApplicationHandle
169		.emit::<Value>(SkyEvent::WebviewDisposed.AsStr(), json!({ "Handle": handle }))
170		.map_err(|error| {
171			CommonError::IPCError { Description:format!("Failed to emit Webview disposal event: {}", error) }
172		})?;
173
174	Ok(())
175}
176
177/// Reveals (shows and focuses) a Webview panel.
178pub(super) async fn reveal_webview_panel_impl(
179	env:&MountainEnvironment,
180
181	handle:String,
182
183	_show_options_value:Value,
184) -> Result<(), CommonError> {
185	dev_log!("extensions", "[WebviewProvider] Revealing WebviewPanel: {}", handle);
186
187	if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
188		webview_window.show().map_err(|error| {
189			CommonError::UserInterfaceInteraction { Reason:format!("Failed to show Webview window: {}", error) }
190		})?;
191
192		webview_window.set_focus().map_err(|error| {
193			CommonError::UserInterfaceInteraction { Reason:format!("Failed to focus Webview window: {}", error) }
194		})?;
195
196		// Update visibility state
197		{
198			let mut webview_guard = env
199				.ApplicationState
200				.Feature
201				.Webviews
202				.ActiveWebviews
203				.lock()
204				.map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?;
205
206			if let Some(state) = webview_guard.get_mut(&handle) {
207				state.IsVisible = true;
208			}
209		}
210
211		// Emit visibility event
212		env.ApplicationHandle
213			.emit::<Value>(SkyEvent::WebviewRevealed.AsStr(), json!({ "Handle": handle }))
214			.map_err(|error| {
215				CommonError::IPCError { Description:format!("Failed to emit Webview revealed event: {}", error) }
216			})?;
217	}
218
219	Ok(())
220}