Common/WebView/
WebViewProvider.rs

1// File: Common/Source/WebView/WebViewProvider.rs
2// Role: Defines the abstract service trait for creating and managing WebViews.
3// Responsibilities:
4//   - Provide a contract for creating, disposing, and revealing WebView panels.
5//   - Define methods for setting a WebView's content (HTML) and options (title,
6
7//     icon).
8//   - Define a method for posting messages to a WebView's content script.
9
10//! # WebViewProvider Trait
11//!
12//! Defines the abstract service trait for creating and managing WebViews.
13
14use async_trait::async_trait;
15use serde_json::Value;
16
17use crate::{Environment::Environment::Environment, Error::CommonError::CommonError};
18
19/// An abstract service contract for an environment component that can manage
20/// WebView panels.
21///
22/// This trait defines all the operations necessary for creating WebView-based
23/// UI, setting their content, and managing their lifecycle, abstracting away
24/// the specific UI framework (e.g., Tauri, Electron) being used by the host.
25#[async_trait]
26pub trait WebViewProvider: Environment + Send + Sync {
27	/// Creates a new WebView panel.
28	///
29	/// # Parameters
30	/// * `ExtensionDataValue`: DTO containing information about the extension
31	///   creating the panel.
32	/// * `ViewType`: A unique string identifying the type of the WebView.
33	/// * `Title`: The initial title for the WebView panel.
34	/// * `ShowOptionsValue`: DTO specifying the view column to show the panel
35	///   in.
36	/// * `PanelOptionsValue`: DTO specifying behavior options for the panel
37	///   (e.g., enable scripts).
38	/// * `ContentOptionsValue`: DTO specifying content options (e.g., local
39	///   resource roots).
40	///
41	/// # Returns
42	/// A `Result` containing a unique handle (string) for the new WebView, or
43	/// a `CommonError` on failure.
44	async fn CreateWebViewPanel(
45		&self,
46
47		// DTO: WebViewExtensionDescriptionDTO
48		ExtensionDataValue:Value,
49
50		ViewType:String,
51
52		Title:String,
53
54		// DTO: WebViewShowOptionsDTO
55		ShowOptionsValue:Value,
56
57		// DTO: WebViewPanelOptionsDTO
58		PanelOptionsValue:Value,
59
60		// DTO: WebViewContentOptionsDTO
61		ContentOptionsValue:Value,
62	) -> Result<String, CommonError>;
63
64	/// Disposes of a WebView panel, removing it from the UI.
65	///
66	/// # Parameters
67	/// * `Handle`: The unique handle of the WebView panel to dispose.
68	async fn DisposeWebViewPanel(&self, Handle:String) -> Result<(), CommonError>;
69
70	/// Reveals an existing WebView panel, bringing it to the front.
71	///
72	/// # Parameters
73	/// * `Handle`: The unique handle of the WebView panel to reveal.
74	/// * `ShowOptionsValue`: DTO specifying the view column to show the panel
75	///   in.
76	async fn RevealWebViewPanel(
77		&self,
78
79		Handle:String,
80
81		// DTO: WebViewShowOptionsDTO
82		ShowOptionsValue:Value,
83	) -> Result<(), CommonError>;
84
85	/// Sets various options for a WebView panel, such as its title and icon
86	/// path.
87	///
88	/// # Parameters
89	/// * `Handle`: The unique handle of the WebView panel to update.
90	/// * `OptionsValue`: A DTO (`WebviewPanelOptionsUpdateDTO`) containing the
91	///   options to set.
92	async fn SetWebViewOptions(&self, Handle:String, OptionsValue:Value) -> Result<(), CommonError>;
93
94	/// Sets the HTML content of a WebView panel.
95	// # Parameters
96	/// * `Handle`: The unique handle of the WebView panel.
97	/// * `HTML`: The HTML string to set as the content.
98	async fn SetWebViewHTML(&self, Handle:String, HTML:String) -> Result<(), CommonError>;
99
100	/// Posts a message from the extension host to the WebView content script.
101	///
102	/// # Parameters
103	/// * `Handle`: The unique handle of the target WebView panel.
104	/// * `Message`: The JSON-serializable message to post.
105	///
106	/// # Returns
107	/// `Ok(true)` if the message was posted successfully.
108	async fn PostMessageToWebView(&self, Handle:String, Message:Value) -> Result<bool, CommonError>;
109}