Переглянути джерело

WebDriver: Implement `POST /session/{id}/refresh` endpoint

Tobias Christiansen 2 роки тому
батько
коміт
1179d951f6

+ 7 - 0
Userland/Applications/Browser/WebDriverConnection.cpp

@@ -46,4 +46,11 @@ Messages::WebDriverSessionClient::GetTitleResponse WebDriverConnection::get_titl
     return { "" };
     return { "" };
 }
 }
 
 
+void WebDriverConnection::refresh()
+{
+    dbgln("WebDriverConnection: refresh");
+    if (auto browser_window = m_browser_window.strong_ref())
+        browser_window->active_tab().reload();
+}
+
 }
 }

+ 1 - 0
Userland/Applications/Browser/WebDriverConnection.h

@@ -39,6 +39,7 @@ public:
     virtual Messages::WebDriverSessionClient::GetUrlResponse get_url() override;
     virtual Messages::WebDriverSessionClient::GetUrlResponse get_url() override;
     virtual void set_url(AK::URL const& url) override;
     virtual void set_url(AK::URL const& url) override;
     virtual Messages::WebDriverSessionClient::GetTitleResponse get_title() override;
     virtual Messages::WebDriverSessionClient::GetTitleResponse get_title() override;
+    virtual void refresh() override;
 
 
 private:
 private:
     WebDriverConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, NonnullRefPtr<BrowserWindow> browser_window);
     WebDriverConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, NonnullRefPtr<BrowserWindow> browser_window);

+ 1 - 0
Userland/Applications/Browser/WebDriverSessionClient.ipc

@@ -6,4 +6,5 @@ endpoint WebDriverSessionClient {
     get_url() => (URL url)
     get_url() => (URL url)
     set_url(URL url) =|
     set_url(URL url) =|
     get_title() => (String title)
     get_title() => (String title)
+    refresh() =|
 }
 }

+ 12 - 0
Userland/Services/WebDriver/Client.cpp

@@ -26,6 +26,7 @@ Vector<Client::Route> Client::s_routes = {
     { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "url" }, &Client::handle_get_url },
     { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "url" }, &Client::handle_get_url },
     { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "title" }, &Client::handle_get_title },
     { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "title" }, &Client::handle_get_title },
     { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "window" }, &Client::handle_delete_window },
     { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "window" }, &Client::handle_delete_window },
+    { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "refresh" }, &Client::handle_refresh },
 };
 };
 
 
 Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent)
 Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent)
@@ -451,4 +452,15 @@ ErrorOr<JsonValue, HttpError> Client::handle_delete_window(Vector<StringView> pa
     return make_json_value(JsonValue());
     return make_json_value(JsonValue());
 }
 }
 
 
+// POST /session/{session id}/refresh https://w3c.github.io/webdriver/#dfn-refresh
+ErrorOr<JsonValue, HttpError> Client::handle_refresh(Vector<StringView> parameters, JsonValue const&)
+{
+    dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session/<session_id>/refresh");
+    Session* session = TRY(find_session_with_id(parameters[0]));
+
+    // NOTE: Spec steps handled in Session::refresh().
+    auto result = TRY(session->refresh());
+    return make_json_value(result);
+}
+
 }
 }

+ 1 - 0
Userland/Services/WebDriver/Client.h

@@ -53,6 +53,7 @@ private:
     ErrorOr<JsonValue, HttpError> handle_get_url(Vector<StringView>, JsonValue const& payload);
     ErrorOr<JsonValue, HttpError> handle_get_url(Vector<StringView>, JsonValue const& payload);
     ErrorOr<JsonValue, HttpError> handle_get_title(Vector<StringView>, JsonValue const& payload);
     ErrorOr<JsonValue, HttpError> handle_get_title(Vector<StringView>, JsonValue const& payload);
     ErrorOr<JsonValue, HttpError> handle_delete_window(Vector<StringView>, JsonValue const& payload);
     ErrorOr<JsonValue, HttpError> handle_delete_window(Vector<StringView>, JsonValue const& payload);
+    ErrorOr<JsonValue, HttpError> handle_refresh(Vector<StringView>, JsonValue const& payload);
 
 
     ErrorOr<Session*, HttpError> find_session_with_id(StringView session_id);
     ErrorOr<Session*, HttpError> find_session_with_id(StringView session_id);
     JsonValue make_json_value(JsonValue const&);
     JsonValue make_json_value(JsonValue const&);

+ 25 - 0
Userland/Services/WebDriver/Session.cpp

@@ -158,4 +158,29 @@ ErrorOr<JsonValue, HttpError> Session::get_title()
     return JsonValue(m_browser_connection->get_title());
     return JsonValue(m_browser_connection->get_title());
 }
 }
 
 
+// POST /session/{session id}/refresh https://w3c.github.io/webdriver/#dfn-refresh
+ErrorOr<JsonValue, HttpError> Session::refresh()
+{
+    // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
+    auto current_window = get_window_object();
+    if (!current_window.has_value())
+        return HttpError { 404, "no such window", "Window not found" };
+
+    // FIXME: 2. Handle any user prompts and return its value if it is an error.
+
+    // 3. Initiate an overridden reload of the current top-level browsing context’s active document.
+    m_browser_connection->async_refresh();
+
+    // FIXME: 4. If url is special except for file:
+
+    // FIXME:     1. Try to wait for navigation to complete.
+
+    // FIXME:     2. Try to run the post-navigation checks.
+
+    // FIXME: 5. Set the current browsing context with current top-level browsing context.
+
+    // 6. Return success with data null.
+    return JsonValue();
+}
+
 }
 }

+ 1 - 0
Userland/Services/WebDriver/Session.h

@@ -37,6 +37,7 @@ public:
     ErrorOr<JsonValue, HttpError> post_url(JsonValue const& url);
     ErrorOr<JsonValue, HttpError> post_url(JsonValue const& url);
     ErrorOr<JsonValue, HttpError> get_url();
     ErrorOr<JsonValue, HttpError> get_url();
     ErrorOr<JsonValue, HttpError> get_title();
     ErrorOr<JsonValue, HttpError> get_title();
+    ErrorOr<JsonValue, HttpError> refresh();
 
 
 private:
 private:
     NonnullRefPtr<Client> m_client;
     NonnullRefPtr<Client> m_client;