/* * Copyright (c) 2022, Florent Castelli * Copyright (c) 2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace WebDriver { class Client final : public Core::Object { C_OBJECT(Client); public: void start(); void close_session(unsigned session_id); private: Client(NonnullOwnPtr, Core::Object* parent); ErrorOr read_body_as_json(HTTP::HttpRequest const&); ErrorOr handle_request(HTTP::HttpRequest const&, JsonValue const& body); ErrorOr send_response(StringView content, HTTP::HttpRequest const&); ErrorOr send_error_response(WebDriverError const& error, HTTP::HttpRequest const&); void die(); void log_response(unsigned code, HTTP::HttpRequest const&); using RouteHandler = ErrorOr (Client::*)(Vector const&, JsonValue const&); struct Route { HTTP::HttpRequest::Method method; Vector path; RouteHandler handler; }; struct RoutingResult { RouteHandler handler; Vector parameters; }; ErrorOr match_route(HTTP::HttpRequest::Method method, String const& resource); ErrorOr handle_new_session(Vector const&, JsonValue const& payload); ErrorOr handle_delete_session(Vector const&, JsonValue const& payload); ErrorOr handle_get_status(Vector const&, JsonValue const& payload); ErrorOr handle_get_timeouts(Vector const&, JsonValue const& payload); ErrorOr handle_set_timeouts(Vector const&, JsonValue const& payload); ErrorOr handle_navigate_to(Vector const&, JsonValue const& payload); ErrorOr handle_get_current_url(Vector const&, JsonValue const& payload); ErrorOr handle_back(Vector const&, JsonValue const& payload); ErrorOr handle_forward(Vector const&, JsonValue const& payload); ErrorOr handle_refresh(Vector const&, JsonValue const& payload); ErrorOr handle_get_title(Vector const&, JsonValue const& payload); ErrorOr handle_get_window_handle(Vector const&, JsonValue const& payload); ErrorOr handle_close_window(Vector const&, JsonValue const& payload); ErrorOr handle_get_window_handles(Vector const&, JsonValue const& payload); ErrorOr handle_get_window_rect(Vector const&, JsonValue const& payload); ErrorOr handle_set_window_rect(Vector const&, JsonValue const& payload); ErrorOr handle_maximize_window(Vector const&, JsonValue const& payload); ErrorOr handle_minimize_window(Vector const&, JsonValue const& payload); ErrorOr handle_find_element(Vector const&, JsonValue const& payload); ErrorOr handle_find_elements(Vector const&, JsonValue const& payload); ErrorOr handle_find_element_from_element(Vector const&, JsonValue const& payload); ErrorOr handle_find_elements_from_element(Vector const&, JsonValue const& payload); ErrorOr handle_get_element_attribute(Vector const&, JsonValue const& payload); ErrorOr handle_get_element_property(Vector const&, JsonValue const& payload); ErrorOr handle_get_element_css_value(Vector const&, JsonValue const& payload); ErrorOr handle_get_element_text(Vector const&, JsonValue const& payload); ErrorOr handle_get_element_tag_name(Vector const&, JsonValue const& payload); ErrorOr handle_get_element_rect(Vector const&, JsonValue const& payload); ErrorOr handle_get_source(Vector const&, JsonValue const& payload); ErrorOr handle_execute_script(Vector const&, JsonValue const& payload); ErrorOr handle_execute_async_script(Vector const&, JsonValue const& payload); ErrorOr handle_get_all_cookies(Vector const&, JsonValue const& payload); ErrorOr handle_get_named_cookie(Vector const&, JsonValue const& payload); ErrorOr handle_add_cookie(Vector const&, JsonValue const& payload); ErrorOr handle_delete_cookie(Vector const&, JsonValue const& payload); ErrorOr handle_delete_all_cookies(Vector const&, JsonValue const& payload); ErrorOr handle_take_screenshot(Vector const&, JsonValue const& payload); ErrorOr find_session_with_id(StringView session_id); JsonValue make_json_value(JsonValue const&); template static ErrorOr unwrap_result(ErrorOr> result) { if (result.is_error()) { Variant error = result.release_error(); if (error.has()) return error.get(); return WebDriverError::from_code(ErrorCode::UnsupportedOperation, error.get().string_literal()); } return result.release_value(); } static ErrorOr unwrap_result(ErrorOr> result) { if (result.is_error()) { Variant error = result.release_error(); if (error.has()) return error.get(); return WebDriverError::from_code(ErrorCode::UnsupportedOperation, error.get().string_literal()); } return {}; } NonnullOwnPtr m_socket; static Vector s_routes; String m_prefix = "/"; static NonnullOwnPtrVector s_sessions; static Atomic s_next_session_id; }; }