Skip to main content

lingxia_webview/
lib.rs

1//! Cross-platform WebView hosting layer for LingXia.
2//!
3//! This crate is strictly *generic* webview hosting: webview creation and
4//! lifecycle, navigation/scheme/event plumbing, and minimal native surface
5//! ownership required by each platform WebView runtime. It contains no
6//! product UI.
7//!
8//! On Windows, host-window grouping, chrome, panels, and app layout live in
9//! `lingxia-windows-sdk`; this crate only provides the WebView2 surface.
10
11use thiserror::Error;
12
13/// WebView-specific error types
14#[derive(Error, Debug, Clone, PartialEq, Eq)]
15pub enum WebViewError {
16    #[error("WebView error: {0}")]
17    WebView(String),
18
19    #[error("Invalid WebView create options: {0}")]
20    InvalidCreateOptions(String),
21}
22
23#[derive(Error, Debug, Clone, PartialEq, Eq)]
24pub enum WebViewScriptError {
25    #[error("JavaScript error: {0}")]
26    Js(String),
27
28    #[error("JavaScript evaluation timed out")]
29    Timeout,
30
31    #[error("JavaScript evaluation unsupported: {0}")]
32    Unsupported(&'static str),
33
34    #[error("WebView destroyed during JavaScript evaluation")]
35    Destroyed,
36
37    #[error("Navigation changed during JavaScript evaluation")]
38    NavigationChanged,
39
40    #[error("Platform JavaScript evaluation error: {0}")]
41    Platform(String),
42}
43
44#[derive(Error, Debug, Clone, PartialEq, Eq)]
45pub enum WebViewInputError {
46    #[error(transparent)]
47    Script(#[from] WebViewScriptError),
48
49    #[error("Element not found: {0}")]
50    ElementNotFound(String),
51
52    #[error("Element not interactable: {0}")]
53    ElementNotInteractable(String),
54
55    #[error("Input unsupported: {0}")]
56    Unsupported(&'static str),
57
58    #[error("WebView destroyed during input handling")]
59    Destroyed,
60
61    #[error("Navigation changed during input handling")]
62    NavigationChanged,
63
64    #[error("Platform input error: {0}")]
65    Platform(String),
66}
67
68/// Log levels for WebView logging
69#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70pub enum LogLevel {
71    Verbose,
72    Debug,
73    Info,
74    Warn,
75    Error,
76}
77
78mod input_helper;
79mod traits;
80mod webview;
81
82#[cfg(target_os = "android")]
83mod android;
84
85#[cfg(any(target_os = "ios", target_os = "macos"))]
86mod apple;
87
88#[cfg(all(target_os = "linux", target_env = "ohos"))]
89mod harmony;
90
91#[cfg(target_os = "windows")]
92mod windows;
93
94// Public exports
95// WebViewError and LogLevel are defined above
96pub use traits::{
97    ClickOptions, DownloadRequest, FileChooserFile, FileChooserRequest, FileChooserResponse,
98    FillOptions, LoadDataRequest, LoadError, LoadErrorKind, NavigationPolicy, NewWindowPolicy,
99    PressOptions, SchemeOutcome, ScrollOptions, SystemPipeReader, TypeOptions, WebResourceBody,
100    WebResourceResponse, WebViewController, WebViewCookie, WebViewCookieSameSite,
101    WebViewCookieSetRequest, WebViewDelegate, WebViewInputController,
102};
103pub use webview::{
104    BrowserWebViewBuilder, ProxyActivation, ProxyApplyReport, ProxyApplyStatus, ProxyConfig,
105    StrictWebViewBuilder, WebTag, WebView, WebViewBuilder, WebViewCreateStage, WebViewEvent,
106    WebViewEventSubscription, WebViewSession,
107};
108
109/// Runtime-scoped APIs (instance lookup/destruction, proxy state).
110pub mod runtime {
111    use std::sync::Arc;
112
113    use crate::webview;
114    use crate::{ProxyApplyReport, ProxyConfig, WebTag, WebView, WebViewError};
115
116    pub fn find_webview(webtag: &WebTag) -> Option<Arc<WebView>> {
117        webview::find_webview(webtag)
118    }
119
120    pub fn list_webviews() -> Vec<WebTag> {
121        webview::list_webviews()
122    }
123
124    pub fn destroy_webview(webtag: &WebTag) {
125        webview::destroy_webview(webtag);
126    }
127
128    pub fn configure_proxy_for_new_webviews(
129        config: Option<ProxyConfig>,
130    ) -> Result<(), WebViewError> {
131        webview::configure_proxy_for_new_webviews(config)
132    }
133
134    pub fn apply_proxy_to_current_runtime(
135        config: Option<ProxyConfig>,
136    ) -> Result<ProxyApplyReport, WebViewError> {
137        webview::apply_proxy_to_current_runtime(config)
138    }
139
140    pub fn configured_proxy_for_new_webviews() -> Option<ProxyConfig> {
141        webview::configured_proxy_for_new_webviews()
142    }
143}
144
145/// Platform-specific APIs used by SDK/FFI integration layers.
146pub mod platform {
147    #[cfg(target_os = "android")]
148    pub mod android {
149        pub use crate::android::{initialize_jni, with_env};
150    }
151
152    #[cfg(any(target_os = "ios", target_os = "macos"))]
153    pub mod apple {
154        pub use crate::apple::BRIDGE_DOWNSTREAM_CSP_SOURCE;
155        pub use crate::apple::BRIDGE_DOWNSTREAM_URL;
156        #[cfg(target_os = "macos")]
157        pub use crate::apple::toggle_webview_devtools_by_swift_ptr;
158    }
159
160    #[cfg(all(target_os = "linux", target_env = "ohos"))]
161    pub mod harmony {
162        pub use crate::harmony::{
163            check_navigation_policy, complete_pending_screenshot_request,
164            on_file_chooser_requested, schemehandler::register_custom_schemes, tsfn,
165            webview_controller_created, webview_controller_destroyed,
166        };
167
168        #[doc(hidden)]
169        pub fn on_load_error(webtag: &str, url: &str, error_code: i32, description: &str) {
170            crate::harmony::on_load_error(webtag, url, error_code, description);
171        }
172
173        #[doc(hidden)]
174        pub fn on_download_start(
175            webtag_str: &str,
176            url: &str,
177            user_agent: &str,
178            content_disposition: &str,
179            mime_type: &str,
180            content_length: i64,
181        ) -> bool {
182            crate::harmony::on_download_start(
183                webtag_str,
184                url,
185                user_agent,
186                content_disposition,
187                mime_type,
188                content_length,
189            )
190        }
191    }
192
193    #[cfg(target_os = "windows")]
194    pub mod windows {
195        pub use crate::windows::{
196            WindowsWebViewHandler, WindowsWebViewNativeView, WindowsWebViewNativeViewHost,
197            find_webview_handler, set_webview_devtools_enabled, set_webview_native_view_host,
198            set_webview_user_data_dir, set_windows_context_menu_refresh_provider,
199        };
200    }
201}