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 #[serde(default)]
137 pub can_go_back: bool,
138 #[serde(default)]
139 pub can_go_forward: bool,
140}
141
142#[derive(Debug, Clone, Serialize, Deserialize)]
143pub struct BrowserRect {
144 pub left: f64,
145 pub top: f64,
146 pub width: f64,
147 pub height: f64,
148 pub right: f64,
149 pub bottom: f64,
150 pub center_x: f64,
151 pub center_y: f64,
152 pub viewport_width: f64,
153 pub viewport_height: f64,
154}
155
156#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct BrowserElementInfo {
158 pub exists: bool,
159 pub visible: bool,
160 pub enabled: bool,
161 pub editable: bool,
162 #[serde(default, skip_serializing_if = "Option::is_none")]
163 pub text: Option<String>,
164 #[serde(default, skip_serializing_if = "is_false")]
165 pub text_truncated: bool,
166 #[serde(default, skip_serializing_if = "Option::is_none")]
167 pub value: Option<String>,
168 #[serde(default, skip_serializing_if = "is_false")]
169 pub value_truncated: bool,
170 #[serde(default, skip_serializing_if = "Option::is_none")]
171 pub rect: Option<BrowserRect>,
172}
173
174#[derive(Debug, Clone, Serialize, Deserialize)]
175#[serde(tag = "kind", rename_all = "snake_case")]
176pub enum BrowserWaitCondition {
177 Loaded,
178 SelectorExists {
179 selector: String,
180 },
181 SelectorVisible {
182 selector: String,
183 },
184 SelectorHidden {
185 selector: String,
186 },
187 SelectorEditable {
188 selector: String,
189 },
190 JsTrue {
191 js: String,
192 },
193 UrlEquals {
194 url: String,
195 },
196 UrlContains {
197 text: String,
198 },
199 Navigation {
200 #[serde(default, skip_serializing_if = "Option::is_none")]
201 initial_url: Option<String>,
202 #[serde(default)]
203 wait_until_complete: bool,
204 },
205}
206
207#[derive(Debug, Clone, Serialize, Deserialize)]
208pub struct BrowserWaitResult {
209 pub elapsed_ms: u64,
210 #[serde(default, skip_serializing_if = "Option::is_none")]
211 pub current_url: Option<String>,
212 #[serde(default, skip_serializing_if = "Option::is_none")]
213 pub element: Option<BrowserElementInfo>,
214 #[serde(default, skip_serializing_if = "Option::is_none")]
215 pub value: Option<serde_json::Value>,
216}
217
218pub trait BrowserNativeInputHost: Send + Sync {
219 fn prepare_for_input(&self, tab_id: &str) -> Result<(), String>;
220}
221
222#[derive(Debug, thiserror::Error)]
223pub enum BrowserAutomationError {
224 #[error("browser tab not found: {0}")]
225 TabNotFound(String),
226 #[error("browser tab webview not found: {0}")]
227 WebViewNotFound(String),
228 #[error(transparent)]
229 Script(#[from] WebViewScriptError),
230 #[error(transparent)]
231 Input(#[from] WebViewInputError),
232 #[error(transparent)]
233 WebView(#[from] WebViewError),
234 #[error("native input host is not registered")]
235 NativeInputHostMissing,
236 #[error("native input error: {0}")]
237 NativeInput(String),
238 #[error("timed out after {timeout_ms}ms waiting for {condition}")]
239 WaitTimeout { condition: String, timeout_ms: u64 },
240}
241
242fn default_true() -> bool {
243 true
244}
245
246fn is_false(value: &bool) -> bool {
247 !*value
248}