1use lingxia_webview::{WebViewError, WebViewInputError, WebViewScriptError};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
8#[serde(rename_all = "snake_case")]
9pub enum BrowserAddressInputTrigger {
10 Edit,
11 #[default]
12 Submit,
13}
14
15#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
16#[serde(rename_all = "snake_case")]
17pub enum BrowserAddressAction {
18 Navigate,
19 Suggest,
20 Reject,
21}
22
23#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
24#[serde(rename_all = "snake_case")]
25pub enum BrowserAddressValueKind {
26 Empty,
27 Url,
28 SearchQuery,
29 Invalid,
30}
31
32#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
33#[serde(rename_all = "snake_case")]
34pub enum BrowserNavigationTarget {
35 CurrentTab,
36 NewTab,
37}
38
39#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
40#[serde(rename_all = "snake_case")]
41pub enum BrowserNavigationPolicyDecision {
42 InWebview,
43 OpenExternal,
44 Deny,
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct BrowserNavigationPolicyRequest {
49 pub raw_url: String,
50 #[serde(default)]
51 pub has_user_gesture: bool,
52 #[serde(default = "default_true")]
53 pub is_main_frame: bool,
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct BrowserNavigationPolicyResponse {
58 pub decision: BrowserNavigationPolicyDecision,
59 #[serde(default, skip_serializing_if = "Option::is_none")]
60 pub reason: Option<String>,
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize, Default)]
64pub struct BrowserAddressInputContext {
65 #[serde(default)]
66 pub preferred_scheme: Option<String>,
67 #[serde(default)]
68 pub current_url: Option<String>,
69 #[serde(default)]
70 pub tab_id: Option<String>,
71 #[serde(default)]
72 pub allow_search_fallback: bool,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct BrowserAddressInputRequest {
77 pub raw_input: String,
78 #[serde(default)]
79 pub trigger: BrowserAddressInputTrigger,
80 #[serde(default)]
81 pub context: BrowserAddressInputContext,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct BrowserAddressState {
86 pub raw_input: String,
87 pub normalized_input: String,
88 pub display_text: String,
89 pub value_kind: BrowserAddressValueKind,
90 pub canonical_url: Option<String>,
91 pub inferred_scheme: Option<String>,
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct BrowserAddressNavigation {
96 pub url: String,
97 pub target: BrowserNavigationTarget,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct BrowserAddressSuggestion {
102 pub kind: String,
103 pub title: String,
104 pub subtitle: Option<String>,
105 pub fill_text: String,
106 pub navigation: Option<BrowserAddressNavigation>,
107}
108
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct BrowserAddressInputError {
111 pub code: String,
112 pub message: String,
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct BrowserAddressInputResponse {
117 pub action: BrowserAddressAction,
118 pub state: BrowserAddressState,
119 pub navigation: Option<BrowserAddressNavigation>,
120 #[serde(default, skip_serializing_if = "Option::is_none")]
121 pub suggestions: Option<Vec<BrowserAddressSuggestion>>,
122 pub error: Option<BrowserAddressInputError>,
123}
124
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct BrowserTabInfo {
127 pub tab_id: String,
128 pub path: String,
129 pub session_id: u64,
130 #[serde(default, skip_serializing_if = "Option::is_none")]
131 pub current_url: Option<String>,
132 #[serde(default, skip_serializing_if = "Option::is_none")]
133 pub title: Option<String>,
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize)]
137pub struct BrowserRect {
138 pub left: f64,
139 pub top: f64,
140 pub width: f64,
141 pub height: f64,
142 pub right: f64,
143 pub bottom: f64,
144 pub center_x: f64,
145 pub center_y: f64,
146 pub viewport_width: f64,
147 pub viewport_height: f64,
148}
149
150#[derive(Debug, Clone, Serialize, Deserialize)]
151pub struct BrowserElementInfo {
152 pub exists: bool,
153 pub visible: bool,
154 pub enabled: bool,
155 pub editable: bool,
156 #[serde(default, skip_serializing_if = "Option::is_none")]
157 pub text: Option<String>,
158 #[serde(default, skip_serializing_if = "is_false")]
159 pub text_truncated: bool,
160 #[serde(default, skip_serializing_if = "Option::is_none")]
161 pub value: Option<String>,
162 #[serde(default, skip_serializing_if = "is_false")]
163 pub value_truncated: bool,
164 #[serde(default, skip_serializing_if = "Option::is_none")]
165 pub rect: Option<BrowserRect>,
166}
167
168#[derive(Debug, Clone, Serialize, Deserialize)]
169#[serde(tag = "kind", rename_all = "snake_case")]
170pub enum BrowserWaitCondition {
171 Loaded,
172 SelectorExists {
173 selector: String,
174 },
175 SelectorVisible {
176 selector: String,
177 },
178 SelectorHidden {
179 selector: String,
180 },
181 SelectorEditable {
182 selector: String,
183 },
184 JsTrue {
185 js: String,
186 },
187 UrlEquals {
188 url: String,
189 },
190 UrlContains {
191 text: String,
192 },
193 Navigation {
194 #[serde(default, skip_serializing_if = "Option::is_none")]
195 initial_url: Option<String>,
196 #[serde(default)]
197 wait_until_complete: bool,
198 },
199}
200
201#[derive(Debug, Clone, Serialize, Deserialize)]
202pub struct BrowserWaitResult {
203 pub elapsed_ms: u64,
204 #[serde(default, skip_serializing_if = "Option::is_none")]
205 pub current_url: Option<String>,
206 #[serde(default, skip_serializing_if = "Option::is_none")]
207 pub element: Option<BrowserElementInfo>,
208 #[serde(default, skip_serializing_if = "Option::is_none")]
209 pub value: Option<serde_json::Value>,
210}
211
212pub trait BrowserNativeInputHost: Send + Sync {
213 fn prepare_for_input(&self, tab_id: &str) -> Result<(), String>;
214}
215
216#[derive(Debug, thiserror::Error)]
217pub enum BrowserAutomationError {
218 #[error("browser tab not found: {0}")]
219 TabNotFound(String),
220 #[error("browser tab webview not found: {0}")]
221 WebViewNotFound(String),
222 #[error(transparent)]
223 Script(#[from] WebViewScriptError),
224 #[error(transparent)]
225 Input(#[from] WebViewInputError),
226 #[error(transparent)]
227 WebView(#[from] WebViewError),
228 #[error("native input host is not registered")]
229 NativeInputHostMissing,
230 #[error("native input error: {0}")]
231 NativeInput(String),
232 #[error("timed out after {timeout_ms}ms waiting for {condition}")]
233 WaitTimeout { condition: String, timeout_ms: u64 },
234}
235
236fn default_true() -> bool {
237 true
238}
239
240fn is_false(value: &bool) -> bool {
241 !*value
242}