DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Environment/WebviewProvider.rs
1//! # WebviewProvider (Environment)
2//!
3//! Implements the `WebviewProvider` trait for `MountainEnvironment`, providing
4//! the core logic for creating, managing, and securing Webview panels.
5//!
6//! ## Architecture
7//!
8//! ```text
9//! Extension → CreateWebviewPanel → WebviewProvider → Tauri WebviewWindow
10//! │ │
11//! └→ IPC → Cocoon ◄───────────┘
12//! ```
13//!
14//! ## Webview types
15//!
16//! - **Panel** - sidebar or panel webview (non-floating)
17//! - **Editor** - webview as custom editor (full editor area)
18//! - **Modal** - modal dialog webview (blocks interaction)
19//! - **Widget** - small embedded webview (e.g., diff viewer)
20//!
21//! ## Lifecycle
22//!
23//! 1. `CreateWebviewPanel` - build Tauri `WebviewWindow`, set up event
24//! handlers, record in `ApplicationState.Feature.Webviews`.
25//! 2. `SetWebviewHTML` / `SetWebviewOptions` - configure content and title.
26//! 3. `RevealWebviewPanel` - show and focus.
27//! 4. `PostMessageToWebview` - bidirectional IPC between host and webview.
28//! 5. `DisposeWebviewPanel` - close window and clean up state.
29//!
30//! ## Security
31//!
32//! Webview runs in a sandboxed process (no Node.js). All `postMessage` calls
33//! are validated; origins are checked to prevent XSS.
34//! Memory footprint is ~50-100 MB per webview - reuse panels when possible.
35//!
36//! ## VS Code reference
37//!
38//! - `vs/workbench/contrib/webview/browser/webviewService.ts`
39//! - `vs/workbench/api/browser/mainThreadWebview.ts`
40
41use std::collections::HashMap;
42
43use CommonLibrary::{Error::CommonError::CommonError, Webview::WebviewProvider::WebviewProvider};
44use async_trait::async_trait;
45use serde_json::Value;
46
47use super::MountainEnvironment::MountainEnvironment;
48
49// Atomic public DTOs (one export per file).
50pub mod WebviewLifecycleState;
51
52pub mod WebviewMessage;
53
54// Private submodules - implementation only, accessed through the
55// trait impl below.
56#[path = "WebviewProvider/Configuration.rs"]
57mod Configuration;
58
59#[path = "WebviewProvider/Lifecycle.rs"]
60mod Lifecycle;
61
62#[path = "WebviewProvider/Messaging.rs"]
63mod Messaging;
64
65// TODO: content caching for faster reloads, theming (dark/light auto),
66// custom protocols, screenshot/thumbnail generation, performance monitoring
67// (CPU/memory), webview clustering, state snapshots for debugging,
68// accessibility audit, pause-when-hidden, resource preloading, telemetry,
69// offline mode (service workers), session migration, debugging tools.
70
71/// Webview message handler context. Private - only the dispatch
72/// machinery in `Messaging.rs` consumes it.
73struct WebviewMessageContext {
74 Handle:String,
75
76 SideCarIdentifier:Option<String>,
77
78 PendingResponses:HashMap<String, tokio::sync::oneshot::Sender<Value>>,
79}
80
81#[async_trait]
82impl WebviewProvider for MountainEnvironment {
83 /// Creates a new Webview panel with proper security isolation.
84 async fn CreateWebviewPanel(
85 &self,
86
87 extension_data_value:Value,
88
89 view_type:String,
90
91 title:String,
92
93 _show_options_value:Value,
94
95 panel_options_value:Value,
96
97 content_options_value:Value,
98 ) -> Result<String, CommonError> {
99 Lifecycle::create_webview_panel_impl(
100 self,
101 extension_data_value,
102 view_type,
103 title,
104 _show_options_value,
105 panel_options_value,
106 content_options_value,
107 )
108 .await
109 }
110
111 /// Disposes a Webview panel and cleans up all associated resources.
112 async fn DisposeWebviewPanel(&self, handle:String) -> Result<(), CommonError> {
113 Lifecycle::dispose_webview_panel_impl(self, handle).await
114 }
115
116 /// Reveals (shows and focuses) a Webview panel.
117 async fn RevealWebviewPanel(&self, handle:String, _show_options_value:Value) -> Result<(), CommonError> {
118 Lifecycle::reveal_webview_panel_impl(self, handle, _show_options_value).await
119 }
120
121 /// Sets Webview options (title, icon, etc.).
122 async fn SetWebviewOptions(&self, handle:String, options_value:Value) -> Result<(), CommonError> {
123 Configuration::set_webview_options_impl(self, handle, options_value).await
124 }
125
126 /// Sets the HTML content of a Webview.
127 async fn SetWebviewHTML(&self, handle:String, html:String) -> Result<(), CommonError> {
128 Configuration::set_webview_html_impl(self, handle, html).await
129 }
130
131 /// Posts a message to a Webview with proper error handling.
132 async fn PostMessageToWebview(&self, handle:String, message:Value) -> Result<bool, CommonError> {
133 Messaging::post_message_to_webview_impl(self, handle, message).await
134 }
135}