Browse Source

WebDriver: Introduce a `WebDriverEndpoints` class

This holds the Functions used by the WebDriver to not clutter up the
`Tab.h` file.
Tobias Christiansen 2 years ago
parent
commit
c11462f40e

+ 6 - 6
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);
     };
 

+ 5 - 6
Userland/Applications/Browser/Tab.h

@@ -8,6 +8,7 @@
 #pragma once
 
 #include "History.h"
+#include "WebDriverEndpoints.h"
 #include <AK/Optional.h>
 #include <AK/URL.h>
 #include <LibGUI/ActionGroup.h>
@@ -68,12 +69,8 @@ public:
     Function<Vector<Web::Cookie::Cookie>()> on_get_cookies_entries;
     Function<OrderedHashMap<String, String>()> on_get_local_storage_entries;
     Function<OrderedHashMap<String, String>()> on_get_session_storage_entries;
-    Function<Optional<i32>()> on_get_document_element;
-    Function<Optional<Vector<i32>>(i32 start_node_id, String const&)> on_query_selector_all;
-    Function<Optional<String>(i32 element_id, String const&)> on_get_element_attribute;
-    Function<Optional<String>(i32 element_id, String const&)> on_get_element_property;
-    Function<String()> on_get_active_documents_type;
-    Function<String(i32 element_id, String const&)> on_get_computed_value_for_element;
+
+    WebDriverEndpoints& webdriver_endpoints() { return m_webdriver_endpoints; }
 
     enum class InspectorTarget {
         Document,
@@ -141,6 +138,8 @@ private:
 
     Optional<URL> m_navigating_url;
 
+    WebDriverEndpoints m_webdriver_endpoints {};
+
     bool m_loaded { false };
     bool m_is_history_navigation { false };
 };

+ 12 - 12
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 { "" };
 }

+ 28 - 0
Userland/Applications/Browser/WebDriverEndpoints.h

@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Forward.h>
+#include <AK/Function.h>
+#include <LibWeb/Forward.h>
+
+namespace Browser {
+
+class WebDriverEndpoints {
+public:
+    WebDriverEndpoints() = default;
+    ~WebDriverEndpoints() = default;
+
+    Function<Optional<i32>()> on_get_document_element;
+    Function<Optional<Vector<i32>>(i32 start_node_id, String const&)> on_query_selector_all;
+    Function<Optional<String>(i32 element_id, String const&)> on_get_element_attribute;
+    Function<Optional<String>(i32 element_id, String const&)> on_get_element_property;
+    Function<String()> on_get_active_documents_type;
+    Function<String(i32 element_id, String const&)> on_get_computed_value_for_element;
+};
+
+}