Skip to main content

lingxia_webview/
lib.rs

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