From c11462f40e4b0ee56c76877cc5d5baf143e987ff Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Thu, 20 Oct 2022 20:07:10 +0200 Subject: [PATCH] WebDriver: Introduce a `WebDriverEndpoints` class This holds the Functions used by the WebDriver to not clutter up the `Tab.h` file. --- .../Applications/Browser/BrowserWindow.cpp | 12 ++++---- Userland/Applications/Browser/Tab.h | 11 ++++---- .../Browser/WebDriverConnection.cpp | 24 ++++++++-------- .../Applications/Browser/WebDriverEndpoints.h | 28 +++++++++++++++++++ 4 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 Userland/Applications/Browser/WebDriverEndpoints.h diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index da874d8c932..1645ca51e4a 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -582,27 +582,27 @@ void BrowserWindow::create_new_tab(URL url, bool activate) return active_tab().view().get_session_storage_entries(); }; - new_tab.on_get_document_element = [this]() { + new_tab.webdriver_endpoints().on_get_document_element = [this]() { return active_tab().view().get_document_element(); }; - new_tab.on_query_selector_all = [this](i32 start_node_id, String const& selector) { + new_tab.webdriver_endpoints().on_query_selector_all = [this](i32 start_node_id, String const& selector) { return active_tab().view().query_selector_all(start_node_id, selector); }; - new_tab.on_get_element_attribute = [this](i32 element_id, String const& name) { + new_tab.webdriver_endpoints().on_get_element_attribute = [this](i32 element_id, String const& name) { return active_tab().view().get_element_attribute(element_id, name); }; - new_tab.on_get_element_property = [this](i32 element_id, String const& name) { + new_tab.webdriver_endpoints().on_get_element_property = [this](i32 element_id, String const& name) { return active_tab().view().get_element_property(element_id, name); }; - new_tab.on_get_active_documents_type = [this]() { + new_tab.webdriver_endpoints().on_get_active_documents_type = [this]() { return active_tab().view().get_active_documents_type(); }; - new_tab.on_get_computed_value_for_element = [this](i32 element_id, String const& property_name) { + new_tab.webdriver_endpoints().on_get_computed_value_for_element = [this](i32 element_id, String const& property_name) { return active_tab().view().get_computed_value_for_element(element_id, property_name); }; diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index 7cb43124a5d..103b0acf957 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -8,6 +8,7 @@ #pragma once #include "History.h" +#include "WebDriverEndpoints.h" #include #include #include @@ -68,12 +69,8 @@ public: Function()> on_get_cookies_entries; Function()> on_get_local_storage_entries; Function()> on_get_session_storage_entries; - Function()> on_get_document_element; - Function>(i32 start_node_id, String const&)> on_query_selector_all; - Function(i32 element_id, String const&)> on_get_element_attribute; - Function(i32 element_id, String const&)> on_get_element_property; - Function on_get_active_documents_type; - Function on_get_computed_value_for_element; + + WebDriverEndpoints& webdriver_endpoints() { return m_webdriver_endpoints; } enum class InspectorTarget { Document, @@ -141,6 +138,8 @@ private: Optional m_navigating_url; + WebDriverEndpoints m_webdriver_endpoints {}; + bool m_loaded { false }; bool m_is_history_navigation { false }; }; diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index c7bf1aa65f6..d5ca2775a84 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -127,8 +127,8 @@ Messages::WebDriverSessionClient::GetDocumentElementResponse WebDriverConnection dbgln("WebDriverConnection: get_document_element"); if (auto browser_window = m_browser_window.strong_ref()) { auto& tab = browser_window->active_tab(); - if (tab.on_get_document_element) - return { tab.on_get_document_element() }; + if (tab.webdriver_endpoints().on_get_document_element) + return { tab.webdriver_endpoints().on_get_document_element() }; } return { {} }; } @@ -138,8 +138,8 @@ Messages::WebDriverSessionClient::QuerySelectorAllResponse WebDriverConnection:: dbgln("WebDriverConnection: query_selector_all"); if (auto browser_window = m_browser_window.strong_ref()) { auto& tab = browser_window->active_tab(); - if (tab.on_query_selector_all) - return { tab.on_query_selector_all(start_node_id, selector) }; + if (tab.webdriver_endpoints().on_query_selector_all) + return { tab.webdriver_endpoints().on_query_selector_all(start_node_id, selector) }; } return { {} }; } @@ -149,8 +149,8 @@ Messages::WebDriverSessionClient::GetElementAttributeResponse WebDriverConnectio dbgln("WebDriverConnection: get_element_attribute"); if (auto browser_window = m_browser_window.strong_ref()) { auto& tab = browser_window->active_tab(); - if (tab.on_get_element_attribute) - return { tab.on_get_element_attribute(element_id, name) }; + if (tab.webdriver_endpoints().on_get_element_attribute) + return { tab.webdriver_endpoints().on_get_element_attribute(element_id, name) }; } return { {} }; } @@ -160,8 +160,8 @@ Messages::WebDriverSessionClient::GetElementPropertyResponse WebDriverConnection dbgln("WebDriverConnection: get_element_property"); if (auto browser_window = m_browser_window.strong_ref()) { auto& tab = browser_window->active_tab(); - if (tab.on_get_element_property) - return { tab.on_get_element_property(element_id, name) }; + if (tab.webdriver_endpoints().on_get_element_property) + return { tab.webdriver_endpoints().on_get_element_property(element_id, name) }; } return { {} }; } @@ -171,8 +171,8 @@ Messages::WebDriverSessionClient::GetActiveDocumentsTypeResponse WebDriverConnec dbgln("WebDriverConnection: get_active_documents_type"); if (auto browser_window = m_browser_window.strong_ref()) { auto& tab = browser_window->active_tab(); - if (tab.on_get_active_documents_type) - return { tab.on_get_active_documents_type() }; + if (tab.webdriver_endpoints().on_get_active_documents_type) + return { tab.webdriver_endpoints().on_get_active_documents_type() }; } return { "" }; } @@ -182,8 +182,8 @@ Messages::WebDriverSessionClient::GetComputedValueForElementResponse WebDriverCo dbgln("WebDriverConnection: get_computed_value_for_element"); if (auto browser_window = m_browser_window.strong_ref()) { auto& tab = browser_window->active_tab(); - if (tab.on_get_computed_value_for_element) - return { tab.on_get_computed_value_for_element(element_id, property_name) }; + if (tab.webdriver_endpoints().on_get_computed_value_for_element) + return { tab.webdriver_endpoints().on_get_computed_value_for_element(element_id, property_name) }; } return { "" }; } diff --git a/Userland/Applications/Browser/WebDriverEndpoints.h b/Userland/Applications/Browser/WebDriverEndpoints.h new file mode 100644 index 00000000000..4af693b9877 --- /dev/null +++ b/Userland/Applications/Browser/WebDriverEndpoints.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022, Tobias Christiansen + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace Browser { + +class WebDriverEndpoints { +public: + WebDriverEndpoints() = default; + ~WebDriverEndpoints() = default; + + Function()> on_get_document_element; + Function>(i32 start_node_id, String const&)> on_query_selector_all; + Function(i32 element_id, String const&)> on_get_element_attribute; + Function(i32 element_id, String const&)> on_get_element_property; + Function on_get_active_documents_type; + Function on_get_computed_value_for_element; +}; + +}