/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #![allow(missing_docs)] use std::collections::HashMap; use base::generic_channel::GenericSender; use base::id::{BrowsingContextId, WebViewId}; use cookie::Cookie; use euclid::default::Rect as UntypedRect; use euclid::{Rect, Size2D}; use hyper_serde::Serde; use ipc_channel::ipc::IpcSender; use keyboard_types::{CompositionEvent, KeyboardEvent}; use pixels::RasterImage; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; use servo_geometry::DeviceIndependentIntRect; use servo_url::ServoUrl; use style_traits::CSSPixel; use webdriver::error::ErrorStatus; use webrender_api::units::DevicePixel; use crate::{JSValue, MouseButton, MouseButtonAction, TraversalId}; #[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)] pub struct WebDriverMessageId(pub usize); #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] pub enum WebDriverUserPrompt { Alert, BeforeUnload, Confirm, Default, File, Prompt, FallbackDefault, } impl WebDriverUserPrompt { pub fn new_from_str(s: &str) -> Option { match s { "alert" => Some(WebDriverUserPrompt::Alert), "beforeUnload" => Some(WebDriverUserPrompt::BeforeUnload), "confirm" => Some(WebDriverUserPrompt::Confirm), "default" => Some(WebDriverUserPrompt::Default), "file" => Some(WebDriverUserPrompt::File), "prompt" => Some(WebDriverUserPrompt::Prompt), "fallbackDefault" => Some(WebDriverUserPrompt::FallbackDefault), _ => None, } } } #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum WebDriverUserPromptAction { Accept, Dismiss, Ignore, } impl WebDriverUserPromptAction { pub fn new_from_str(s: &str) -> Option { match s { "accept" => Some(WebDriverUserPromptAction::Accept), "dismiss" => Some(WebDriverUserPromptAction::Dismiss), "ignore" => Some(WebDriverUserPromptAction::Ignore), _ => None, } } } /// Messages to the constellation originating from the WebDriver server. #[derive(Debug, Deserialize, Serialize)] pub enum WebDriverCommandMsg { /// Used in the initialization of the WebDriver server to set the sender for sending responses /// back to the WebDriver client. It is set to constellation for now SetWebDriverResponseSender(IpcSender), /// Get the window rectangle. GetWindowRect(WebViewId, IpcSender), /// Get the viewport size. GetViewportSize(WebViewId, IpcSender>), /// Load a URL in the top-level browsing context with the given ID. LoadUrl(WebViewId, ServoUrl, GenericSender), /// Refresh the top-level browsing context with the given ID. Refresh(WebViewId, GenericSender), /// Navigate the webview with the given ID to the previous page in the browsing context's history. GoBack(WebViewId, GenericSender), /// Navigate the webview with the given ID to the next page in the browsing context's history. GoForward(WebViewId, GenericSender), /// Pass a webdriver command to the script thread of the current pipeline /// of a browsing context. ScriptCommand(BrowsingContextId, WebDriverScriptCommand), /// Dispatch composition event from element send keys command. DispatchComposition(WebViewId, CompositionEvent), /// Act as if keys were pressed or release in the browsing context with the given ID. KeyboardAction( WebViewId, KeyboardEvent, // Should never be None. Option, ), /// Act as if the mouse was clicked in the browsing context with the given ID. MouseButtonAction( WebViewId, MouseButtonAction, MouseButton, f32, f32, // Should never be None. Option, ), /// Act as if the mouse was moved in the browsing context with the given ID. MouseMoveAction( WebViewId, f32, f32, // None if it's not the last `perform_pointer_move` since we only // expect one response from constellation for each tick actions. Option, ), /// Act as if the mouse wheel is scrolled in the browsing context given the given ID. WheelScrollAction( WebViewId, f64, f64, f64, f64, // None if it's not the last `perform_wheel_scroll` since we only // expect one response from constellation for each tick actions. Option, ), /// Set the outer window rectangle. SetWindowRect( WebViewId, DeviceIndependentIntRect, IpcSender, ), /// Maximize the window. Send back result window rectangle. MaximizeWebView(WebViewId, IpcSender), /// Take a screenshot of the viewport. TakeScreenshot( WebViewId, Option>, IpcSender>, ), /// Create a new webview that loads about:blank. The embedder will use /// the provided channels to return the top level browsing context id /// associated with the new webview, and sets a "load status sender" if provided. NewWebView( IpcSender, Option>, ), /// Close the webview associated with the provided id. CloseWebView(WebViewId, IpcSender<()>), /// Focus the webview associated with the provided id. FocusWebView(WebViewId), /// Get focused webview. For now, this is only used when start new session. GetFocusedWebView(IpcSender>), /// Get webviews state GetAllWebViews(IpcSender>), /// Check whether top-level browsing context is open. IsWebViewOpen(WebViewId, IpcSender), /// Check whether browsing context is open. IsBrowsingContextOpen(BrowsingContextId, IpcSender), CurrentUserPrompt(WebViewId, IpcSender>), HandleUserPrompt( WebViewId, WebDriverUserPromptAction, IpcSender, ()>>, ), GetAlertText(WebViewId, IpcSender>), SendAlertText(WebViewId, String), FocusBrowsingContext(BrowsingContextId), } #[derive(Debug, Deserialize, Serialize)] pub enum WebDriverScriptCommand { AddCookie( #[serde( deserialize_with = "::hyper_serde::deserialize", serialize_with = "::hyper_serde::serialize" )] Cookie<'static>, IpcSender>, ), DeleteCookies(IpcSender>), DeleteCookie(String, IpcSender>), ElementClear(String, IpcSender>), ExecuteScript(String, IpcSender), ExecuteAsyncScript(String, IpcSender), FindElementsCSSSelector(String, IpcSender, ErrorStatus>>), FindElementsLinkText(String, bool, IpcSender, ErrorStatus>>), FindElementsTagName(String, IpcSender, ErrorStatus>>), FindElementsXpathSelector(String, IpcSender, ErrorStatus>>), FindElementElementsCSSSelector(String, String, IpcSender, ErrorStatus>>), FindElementElementsLinkText( String, String, bool, IpcSender, ErrorStatus>>, ), FindElementElementsTagName(String, String, IpcSender, ErrorStatus>>), FindElementElementsXPathSelector(String, String, IpcSender, ErrorStatus>>), FindShadowElementsCSSSelector(String, String, IpcSender, ErrorStatus>>), FindShadowElementsLinkText( String, String, bool, IpcSender, ErrorStatus>>, ), FindShadowElementsTagName(String, String, IpcSender, ErrorStatus>>), FindShadowElementsXPathSelector(String, String, IpcSender, ErrorStatus>>), GetElementShadowRoot(String, IpcSender, ErrorStatus>>), ElementClick(String, IpcSender, ErrorStatus>>), GetKnownElement(String, IpcSender>), GetKnownShadowRoot(String, IpcSender>), GetActiveElement(IpcSender>), GetComputedRole(String, IpcSender, ErrorStatus>>), GetCookie( String, IpcSender>>, ErrorStatus>>, ), GetCookies(IpcSender>>, ErrorStatus>>), GetElementAttribute( String, String, IpcSender, ErrorStatus>>, ), GetElementProperty(String, String, IpcSender>), GetElementCSS(String, String, IpcSender>), GetElementRect(String, IpcSender, ErrorStatus>>), GetElementTagName(String, IpcSender>), GetElementText(String, IpcSender>), GetElementInViewCenterPoint(String, IpcSender, ErrorStatus>>), ScrollAndGetBoundingClientRect(String, IpcSender, ErrorStatus>>), GetBrowsingContextId( WebDriverFrameId, IpcSender>, ), GetParentFrameId(IpcSender>), GetUrl(IpcSender), GetPageSource(IpcSender>), IsEnabled(String, IpcSender>), IsSelected(String, IpcSender>), GetTitle(IpcSender), /// Deal with the case of input element for Element Send Keys, which does not send keys. WillSendKeys(String, String, bool, IpcSender>), AddLoadStatusSender(WebViewId, GenericSender), RemoveLoadStatusSender(WebViewId), } #[derive(Debug, Deserialize, Serialize)] pub enum WebDriverJSError { /// Occurs when handler received an event message for a layout channel that is not /// associated with the current script thread BrowsingContextNotFound, DetachedShadowRoot, JSException(JSValue), JSError, StaleElementReference, Timeout, UnknownType, } pub type WebDriverJSResult = Result; #[derive(Debug, Deserialize, Serialize)] pub enum WebDriverFrameId { Short(u16), Element(String), } #[derive(Debug, Deserialize, Serialize)] pub struct WebDriverCommandResponse { pub id: WebDriverMessageId, } #[derive(Debug, Deserialize, Serialize)] pub enum WebDriverLoadStatus { NavigationStart, // Navigation stops for any reason NavigationStop, // Document ready state is complete Complete, // Load timeout Timeout, // Navigation is blocked by a user prompt Blocked, } /// A collection of [`IpcSender`]s that are used to asynchronously communicate /// to a WebDriver server with information about application state. #[derive(Clone, Default)] pub struct WebDriverSenders { pub load_status_senders: FxHashMap>, pub script_evaluation_interrupt_sender: Option>, pub pending_traversals: HashMap>, }