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	let title_clone = title.clone();
90
91	let _webview_window = WebviewWindowBuilder::new(
92		&env.ApplicationHandle,
93		&handle,
94		tauri::WebviewUrl::App("WebviewHost.html".into()),
95	)
96	.title(title)
97	.initialization_script(&format!(
98		"window.__WEBVIEW_INITIAL_STATE__ = {};",
99		json!({
100			"Handle": handle,
101			"ViewType": view_type,
102			"Title": title_clone
103		})
104	))
105	.build()
106	.map_err(|error| {
107		dev_log!(
108			"extensions",
109			"error: [WebviewProvider] Failed to create Webview window: {}",
110			error
111		);
112		CommonError::UserInterfaceInteraction { Reason:error.to_string() }
113	})?;
114
115	// Setup message listener for this Webview
116	crate::Environment::WebviewProvider::Messaging::setup_webview_message_listener_impl(env, handle.clone()).await?;
117
118	// Notify frontend about Webview creation
119	env.ApplicationHandle
120		.emit::<Value>(
121			SkyEvent::WebviewCreated.AsStr(),
122			json!({ "Handle": handle.clone(), "ViewType": view_type.clone(), "Title": title_clone }),
123		)
124		.map_err(|error| {
125			CommonError::IPCError { Description:format!("Failed to emit Webview creation event: {}", error) }
126		})?;
127
128	Ok(handle)
129}
130
131/// Disposes a Webview panel and cleans up all associated resources.
132pub(super) async fn dispose_webview_panel_impl(env:&MountainEnvironment, handle:String) -> Result<(), CommonError> {
133	dev_log!("extensions", "[WebviewProvider] Disposing WebviewPanel: {}", handle);
134
135	// Remove message listener
136	let _ = crate::Environment::WebviewProvider::Messaging::remove_webview_message_listener_impl(env, &handle).await;
137
138	// Close the window
139	if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
140		if let Err(error) = webview_window.close() {
141			dev_log!(
142				"extensions",
143				"warn: [WebviewProvider] Failed to close Webview window: {}",
144				error
145			);
146		}
147	}
148
149	// Remove state
150	env.ApplicationState
151		.Feature
152		.Webviews
153		.ActiveWebviews
154		.lock()
155		.map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?
156		.remove(&handle);
157
158	// Notify frontend about Webview disposal
159	env.ApplicationHandle
160		.emit::<Value>(SkyEvent::WebviewDisposed.AsStr(), json!({ "Handle": handle }))
161		.map_err(|error| {
162			CommonError::IPCError { Description:format!("Failed to emit Webview disposal event: {}", error) }
163		})?;
164
165	Ok(())
166}
167
168/// Reveals (shows and focuses) a Webview panel.
169pub(super) async fn reveal_webview_panel_impl(
170	env:&MountainEnvironment,
171
172	handle:String,
173
174	_show_options_value:Value,
175) -> Result<(), CommonError> {
176	dev_log!("extensions", "[WebviewProvider] Revealing WebviewPanel: {}", handle);
177
178	if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
179		webview_window.show().map_err(|error| {
180			CommonError::UserInterfaceInteraction { Reason:format!("Failed to show Webview window: {}", error) }
181		})?;
182
183		webview_window.set_focus().map_err(|error| {
184			CommonError::UserInterfaceInteraction { Reason:format!("Failed to focus Webview window: {}", error) }
185		})?;
186
187		// Update visibility state
188		{
189			let mut webview_guard = env
190				.ApplicationState
191				.Feature
192				.Webviews
193				.ActiveWebviews
194				.lock()
195				.map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?;
196
197			if let Some(state) = webview_guard.get_mut(&handle) {
198				state.IsVisible = true;
199			}
200		}
201
202		// Emit visibility event
203		env.ApplicationHandle
204			.emit::<Value>(SkyEvent::WebviewRevealed.AsStr(), json!({ "Handle": handle }))
205			.map_err(|error| {
206				CommonError::IPCError { Description:format!("Failed to emit Webview revealed event: {}", error) }
207			})?;
208	}
209
210	Ok(())
211}