1#![allow(missing_docs)]
6
7use std::collections::HashMap;
8
9use base::generic_channel::GenericSender;
10use base::id::{BrowsingContextId, WebViewId};
11use cookie::Cookie;
12use crossbeam_channel::Sender;
13use euclid::default::Rect as UntypedRect;
14use euclid::{Rect, Size2D};
15use hyper_serde::Serde;
16use image::RgbaImage;
17use ipc_channel::ipc::IpcSender;
18use rustc_hash::FxHashMap;
19use serde::{Deserialize, Serialize};
20use servo_geometry::DeviceIndependentIntRect;
21use servo_url::ServoUrl;
22use style_traits::CSSPixel;
23use webdriver::error::ErrorStatus;
24use webrender_api::units::DevicePixel;
25
26use crate::{InputEvent, JSValue, JavaScriptEvaluationError, ScreenshotCaptureError, TraversalId};
27
28#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
29pub enum WebDriverUserPrompt {
30 Alert,
31 BeforeUnload,
32 Confirm,
33 Default,
34 File,
35 Prompt,
36 FallbackDefault,
37}
38
39impl WebDriverUserPrompt {
40 pub fn new_from_str(s: &str) -> Option<Self> {
41 match s {
42 "alert" => Some(WebDriverUserPrompt::Alert),
43 "beforeUnload" => Some(WebDriverUserPrompt::BeforeUnload),
44 "confirm" => Some(WebDriverUserPrompt::Confirm),
45 "default" => Some(WebDriverUserPrompt::Default),
46 "file" => Some(WebDriverUserPrompt::File),
47 "prompt" => Some(WebDriverUserPrompt::Prompt),
48 "fallbackDefault" => Some(WebDriverUserPrompt::FallbackDefault),
49 _ => None,
50 }
51 }
52}
53
54#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
55pub enum WebDriverUserPromptAction {
56 Accept,
57 Dismiss,
58 Ignore,
59}
60
61impl WebDriverUserPromptAction {
62 pub fn new_from_str(s: &str) -> Option<Self> {
63 match s {
64 "accept" => Some(WebDriverUserPromptAction::Accept),
65 "dismiss" => Some(WebDriverUserPromptAction::Dismiss),
66 "ignore" => Some(WebDriverUserPromptAction::Ignore),
67 _ => None,
68 }
69 }
70}
71
72#[derive(Debug)]
74pub enum WebDriverCommandMsg {
75 GetWindowRect(WebViewId, IpcSender<DeviceIndependentIntRect>),
77 GetViewportSize(WebViewId, IpcSender<Size2D<u32, DevicePixel>>),
79 LoadUrl(WebViewId, ServoUrl, GenericSender<WebDriverLoadStatus>),
81 Refresh(WebViewId, GenericSender<WebDriverLoadStatus>),
83 GoBack(WebViewId, GenericSender<WebDriverLoadStatus>),
85 GoForward(WebViewId, GenericSender<WebDriverLoadStatus>),
87 ScriptCommand(BrowsingContextId, WebDriverScriptCommand),
90 InputEvent(WebViewId, InputEvent, Option<Sender<()>>),
94 SetWindowRect(
96 WebViewId,
97 DeviceIndependentIntRect,
98 IpcSender<DeviceIndependentIntRect>,
99 ),
100 MaximizeWebView(WebViewId, IpcSender<DeviceIndependentIntRect>),
102 TakeScreenshot(
104 WebViewId,
105 Option<Rect<f32, CSSPixel>>,
106 Sender<Result<RgbaImage, ScreenshotCaptureError>>,
107 ),
108 NewWebView(
112 IpcSender<WebViewId>,
113 Option<GenericSender<WebDriverLoadStatus>>,
114 ),
115 CloseWebView(WebViewId, IpcSender<()>),
117 FocusWebView(WebViewId),
119 GetFocusedWebView(IpcSender<Option<WebViewId>>),
121 GetAllWebViews(IpcSender<Vec<WebViewId>>),
123 IsWebViewOpen(WebViewId, IpcSender<bool>),
125 IsBrowsingContextOpen(BrowsingContextId, IpcSender<bool>),
127 CurrentUserPrompt(WebViewId, IpcSender<Option<WebDriverUserPrompt>>),
128 HandleUserPrompt(
129 WebViewId,
130 WebDriverUserPromptAction,
131 IpcSender<Result<Option<String>, ()>>,
132 ),
133 GetAlertText(WebViewId, IpcSender<Result<String, ()>>),
134 SendAlertText(WebViewId, String),
135 FocusBrowsingContext(BrowsingContextId),
136}
137
138#[derive(Debug, Deserialize, Serialize)]
139pub enum WebDriverScriptCommand {
140 AddCookie(
141 #[serde(
142 deserialize_with = "::hyper_serde::deserialize",
143 serialize_with = "::hyper_serde::serialize"
144 )]
145 Cookie<'static>,
146 IpcSender<Result<(), ErrorStatus>>,
147 ),
148 DeleteCookies(IpcSender<Result<(), ErrorStatus>>),
149 DeleteCookie(String, IpcSender<Result<(), ErrorStatus>>),
150 ElementClear(String, IpcSender<Result<(), ErrorStatus>>),
151 ExecuteScript(String, IpcSender<WebDriverJSResult>),
152 ExecuteAsyncScript(String, IpcSender<WebDriverJSResult>),
153 FindElementsCSSSelector(String, IpcSender<Result<Vec<String>, ErrorStatus>>),
154 FindElementsLinkText(String, bool, IpcSender<Result<Vec<String>, ErrorStatus>>),
155 FindElementsTagName(String, IpcSender<Result<Vec<String>, ErrorStatus>>),
156 FindElementsXpathSelector(String, IpcSender<Result<Vec<String>, ErrorStatus>>),
157 FindElementElementsCSSSelector(String, String, IpcSender<Result<Vec<String>, ErrorStatus>>),
158 FindElementElementsLinkText(
159 String,
160 String,
161 bool,
162 IpcSender<Result<Vec<String>, ErrorStatus>>,
163 ),
164 FindElementElementsTagName(String, String, IpcSender<Result<Vec<String>, ErrorStatus>>),
165 FindElementElementsXPathSelector(String, String, IpcSender<Result<Vec<String>, ErrorStatus>>),
166 FindShadowElementsCSSSelector(String, String, IpcSender<Result<Vec<String>, ErrorStatus>>),
167 FindShadowElementsLinkText(
168 String,
169 String,
170 bool,
171 IpcSender<Result<Vec<String>, ErrorStatus>>,
172 ),
173 FindShadowElementsTagName(String, String, IpcSender<Result<Vec<String>, ErrorStatus>>),
174 FindShadowElementsXPathSelector(String, String, IpcSender<Result<Vec<String>, ErrorStatus>>),
175 GetElementShadowRoot(String, IpcSender<Result<Option<String>, ErrorStatus>>),
176 ElementClick(String, IpcSender<Result<Option<String>, ErrorStatus>>),
177 GetKnownElement(String, IpcSender<Result<(), ErrorStatus>>),
178 GetKnownShadowRoot(String, IpcSender<Result<(), ErrorStatus>>),
179 GetKnownWindow(String, IpcSender<Result<(), ErrorStatus>>),
180 GetActiveElement(IpcSender<Option<String>>),
181 GetComputedRole(String, IpcSender<Result<Option<String>, ErrorStatus>>),
182 GetCookie(
183 String,
184 IpcSender<Result<Vec<Serde<Cookie<'static>>>, ErrorStatus>>,
185 ),
186 GetCookies(IpcSender<Result<Vec<Serde<Cookie<'static>>>, ErrorStatus>>),
187 GetElementAttribute(
188 String,
189 String,
190 IpcSender<Result<Option<String>, ErrorStatus>>,
191 ),
192 GetElementProperty(String, String, IpcSender<Result<JSValue, ErrorStatus>>),
193 GetElementCSS(String, String, IpcSender<Result<String, ErrorStatus>>),
194 GetElementRect(String, IpcSender<Result<UntypedRect<f64>, ErrorStatus>>),
195 GetElementTagName(String, IpcSender<Result<String, ErrorStatus>>),
196 GetElementText(String, IpcSender<Result<String, ErrorStatus>>),
197 GetElementInViewCenterPoint(String, IpcSender<Result<Option<(i64, i64)>, ErrorStatus>>),
198 ScrollAndGetBoundingClientRect(String, IpcSender<Result<UntypedRect<f32>, ErrorStatus>>),
199 GetBrowsingContextId(
200 WebDriverFrameId,
201 IpcSender<Result<BrowsingContextId, ErrorStatus>>,
202 ),
203 GetParentFrameId(IpcSender<Result<BrowsingContextId, ErrorStatus>>),
204 GetUrl(IpcSender<ServoUrl>),
205 GetPageSource(IpcSender<Result<String, ErrorStatus>>),
206 IsEnabled(String, IpcSender<Result<bool, ErrorStatus>>),
207 IsSelected(String, IpcSender<Result<bool, ErrorStatus>>),
208 GetTitle(IpcSender<String>),
209 WillSendKeys(String, String, bool, IpcSender<Result<bool, ErrorStatus>>),
211 AddLoadStatusSender(WebViewId, GenericSender<WebDriverLoadStatus>),
212 RemoveLoadStatusSender(WebViewId),
213}
214
215pub type WebDriverJSResult = Result<JSValue, JavaScriptEvaluationError>;
216
217#[derive(Debug, Deserialize, Serialize)]
218pub enum WebDriverFrameId {
219 Short(u16),
220 Element(String),
221}
222
223#[derive(Debug, Deserialize, Serialize)]
224pub enum WebDriverLoadStatus {
225 NavigationStart,
226 NavigationStop,
228 Complete,
230 Timeout,
232 Blocked,
234}
235
236#[derive(Clone, Default)]
239pub struct WebDriverSenders {
240 pub load_status_senders: FxHashMap<WebViewId, GenericSender<WebDriverLoadStatus>>,
241 pub script_evaluation_interrupt_sender: Option<IpcSender<WebDriverJSResult>>,
242 pub pending_traversals: HashMap<TraversalId, GenericSender<WebDriverLoadStatus>>,
243}