1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:27:35 +00:00

LibWeb+WebContentr+WebDriver: Move driver response wrapping to LibWeb

Success responses are meant to be wrapped in a JSON object with a single
"value" key. Instead of doing this in both WebContent and WebDriver, do
it once in LibWeb.
This commit is contained in:
Timothy Flynn 2022-11-13 00:52:44 -05:00 committed by Linus Groh
parent 09c59ee7c0
commit 5385cb1287
4 changed files with 43 additions and 55 deletions

View file

@ -150,6 +150,13 @@ static ErrorOr<MatchedRoute, Error> match_route(HTTP::HttpRequest const& request
return Error::from_code(ErrorCode::UnknownCommand, "The command was not recognized."); return Error::from_code(ErrorCode::UnknownCommand, "The command was not recognized.");
} }
static JsonValue make_success_response(JsonValue value)
{
JsonObject result;
result.set("value", move(value));
return result;
}
Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent) Client::Client(NonnullOwnPtr<Core::Stream::BufferedTCPSocket> socket, Core::Object* parent)
: Core::Object(parent) : Core::Object(parent)
, m_socket(move(socket)) , m_socket(move(socket))
@ -244,6 +251,7 @@ ErrorOr<void, Client::WrappedError> Client::handle_request(JsonValue body)
ErrorOr<void, Client::WrappedError> Client::send_success_response(JsonValue result) ErrorOr<void, Client::WrappedError> Client::send_success_response(JsonValue result)
{ {
result = make_success_response(move(result));
auto content = result.serialized<StringBuilder>(); auto content = result.serialized<StringBuilder>();
StringBuilder builder; StringBuilder builder;

View file

@ -35,13 +35,6 @@
namespace WebContent { namespace WebContent {
static JsonValue make_success_response(JsonValue value)
{
JsonObject result;
result.set("value", move(value));
return result;
}
// https://w3c.github.io/webdriver/#dfn-serialized-cookie // https://w3c.github.io/webdriver/#dfn-serialized-cookie
static JsonValue serialize_cookie(Web::Cookie::Cookie const& cookie) static JsonValue serialize_cookie(Web::Cookie::Cookie const& cookie)
{ {
@ -66,7 +59,7 @@ static JsonValue serialize_rect(Gfx::IntRect const& rect)
serialized_rect.set("width", rect.width()); serialized_rect.set("width", rect.width());
serialized_rect.set("height", rect.height()); serialized_rect.set("height", rect.height());
return make_success_response(move(serialized_rect)); return serialized_rect;
} }
static Gfx::IntRect compute_window_rect(Web::Page const& page) static Gfx::IntRect compute_window_rect(Web::Page const& page)
@ -293,7 +286,7 @@ Messages::WebDriverClient::NavigateToResponse WebDriverConnection::navigate_to(J
// FIXME: 10. If the current top-level browsing context contains a refresh state pragma directive of time 1 second or less, wait until the refresh timeout has elapsed, a new navigate has begun, and return to the first step of this algorithm. // FIXME: 10. If the current top-level browsing context contains a refresh state pragma directive of time 1 second or less, wait until the refresh timeout has elapsed, a new navigate has begun, and return to the first step of this algorithm.
// 11. Return success with data null. // 11. Return success with data null.
return make_success_response({}); return JsonValue {};
} }
// 10.2 Get Current URL, https://w3c.github.io/webdriver/#get-current-url // 10.2 Get Current URL, https://w3c.github.io/webdriver/#get-current-url
@ -310,7 +303,7 @@ Messages::WebDriverClient::GetCurrentUrlResponse WebDriverConnection::get_curren
auto url = m_page_host.page().top_level_browsing_context().active_document()->url().to_string(); auto url = m_page_host.page().top_level_browsing_context().active_document()->url().to_string();
// 4. Return success with data url. // 4. Return success with data url.
return make_success_response(url); return url;
} }
// 10.3 Back, https://w3c.github.io/webdriver/#dfn-back // 10.3 Back, https://w3c.github.io/webdriver/#dfn-back
@ -328,7 +321,7 @@ Messages::WebDriverClient::BackResponse WebDriverConnection::back()
// FIXME: 5. If the previous step completed by the session page load timeout being reached, and user prompts have been handled, return error with error code timeout. // FIXME: 5. If the previous step completed by the session page load timeout being reached, and user prompts have been handled, return error with error code timeout.
// 6. Return success with data null. // 6. Return success with data null.
return make_success_response({}); return JsonValue {};
} }
// 10.4 Forward, https://w3c.github.io/webdriver/#dfn-forward // 10.4 Forward, https://w3c.github.io/webdriver/#dfn-forward
@ -346,7 +339,7 @@ Messages::WebDriverClient::ForwardResponse WebDriverConnection::forward()
// FIXME: 5. If the previous step completed by the session page load timeout being reached, and user prompts have been handled, return error with error code timeout. // FIXME: 5. If the previous step completed by the session page load timeout being reached, and user prompts have been handled, return error with error code timeout.
// 6. Return success with data null. // 6. Return success with data null.
return make_success_response({}); return JsonValue {};
} }
// 10.5 Refresh, https://w3c.github.io/webdriver/#dfn-refresh // 10.5 Refresh, https://w3c.github.io/webdriver/#dfn-refresh
@ -366,7 +359,7 @@ Messages::WebDriverClient::RefreshResponse WebDriverConnection::refresh()
// FIXME: 5. Set the current browsing context with current top-level browsing context. // FIXME: 5. Set the current browsing context with current top-level browsing context.
// 6. Return success with data null. // 6. Return success with data null.
return make_success_response({}); return JsonValue {};
} }
// 10.6 Get Title, https://w3c.github.io/webdriver/#dfn-get-title // 10.6 Get Title, https://w3c.github.io/webdriver/#dfn-get-title
@ -381,7 +374,7 @@ Messages::WebDriverClient::GetTitleResponse WebDriverConnection::get_title()
auto title = m_page_host.page().top_level_browsing_context().active_document()->title(); auto title = m_page_host.page().top_level_browsing_context().active_document()->title();
// 4. Return success with data title. // 4. Return success with data title.
return make_success_response(move(title)); return title;
} }
// 11.8.1 Get Window Rect, https://w3c.github.io/webdriver/#dfn-get-window-rect // 11.8.1 Get Window Rect, https://w3c.github.io/webdriver/#dfn-get-window-rect
@ -571,7 +564,7 @@ Messages::WebDriverClient::FindElementResponse WebDriverConnection::find_element
if (result.is_empty()) if (result.is_empty())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "The requested element does not exist"sv); return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "The requested element does not exist"sv);
return make_success_response(result.at(0)); return result.take(0);
} }
// 12.3.3 Find Elements, https://w3c.github.io/webdriver/#dfn-find-elements // 12.3.3 Find Elements, https://w3c.github.io/webdriver/#dfn-find-elements
@ -602,8 +595,7 @@ Messages::WebDriverClient::FindElementsResponse WebDriverConnection::find_elemen
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "document element does not exist"sv); return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "document element does not exist"sv);
// 9. Return the result of trying to Find with start node, location strategy, and selector. // 9. Return the result of trying to Find with start node, location strategy, and selector.
auto result = TRY(find(*start_node, *location_strategy, selector)); return TRY(find(*start_node, *location_strategy, selector));
return make_success_response(move(result));
} }
// 12.3.4 Find Element From Element, https://w3c.github.io/webdriver/#dfn-find-element-from-element // 12.3.4 Find Element From Element, https://w3c.github.io/webdriver/#dfn-find-element-from-element
@ -636,7 +628,7 @@ Messages::WebDriverClient::FindElementFromElementResponse WebDriverConnection::f
if (result.is_empty()) if (result.is_empty())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "The requested element does not exist"sv); return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "The requested element does not exist"sv);
return make_success_response(result.at(0)); return result.take(0);
} }
// 12.3.5 Find Elements From Element, https://w3c.github.io/webdriver/#dfn-find-elements-from-element // 12.3.5 Find Elements From Element, https://w3c.github.io/webdriver/#dfn-find-elements-from-element
@ -663,8 +655,7 @@ Messages::WebDriverClient::FindElementsFromElementResponse WebDriverConnection::
auto* start_node = TRY(get_known_connected_element(element_id)); auto* start_node = TRY(get_known_connected_element(element_id));
// 8. Return the result of trying to Find with start node, location strategy, and selector. // 8. Return the result of trying to Find with start node, location strategy, and selector.
auto result = TRY(find(*start_node, *location_strategy, selector)); return TRY(find(*start_node, *location_strategy, selector));
return make_success_response(move(result));
} }
// 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected // 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected
@ -699,7 +690,7 @@ Messages::WebDriverClient::IsElementSelectedResponse WebDriverConnection::is_ele
// -> False. // -> False.
// 5. Return success with data selected. // 5. Return success with data selected.
return make_success_response(selected); return selected;
} }
// 12.4.2 Get Element Attribute, https://w3c.github.io/webdriver/#dfn-get-element-attribute // 12.4.2 Get Element Attribute, https://w3c.github.io/webdriver/#dfn-get-element-attribute
@ -730,8 +721,8 @@ Messages::WebDriverClient::GetElementAttributeResponse WebDriverConnection::get_
// 5. Return success with data result. // 5. Return success with data result.
if (result.has_value()) if (result.has_value())
return make_success_response(result.release_value()); return result.release_value();
return make_success_response({}); return JsonValue {};
} }
// 12.4.3 Get Element Property, https://w3c.github.io/webdriver/#dfn-get-element-property // 12.4.3 Get Element Property, https://w3c.github.io/webdriver/#dfn-get-element-property
@ -760,8 +751,8 @@ Messages::WebDriverClient::GetElementPropertyResponse WebDriverConnection::get_e
// 6. Return success with data result. // 6. Return success with data result.
if (result.has_value()) if (result.has_value())
return make_success_response(result.release_value()); return result.release_value();
return make_success_response({}); return JsonValue {};
} }
// 12.4.4 Get Element CSS Value, https://w3c.github.io/webdriver/#dfn-get-element-css-value // 12.4.4 Get Element CSS Value, https://w3c.github.io/webdriver/#dfn-get-element-css-value
@ -793,7 +784,7 @@ Messages::WebDriverClient::GetElementCssValueResponse WebDriverConnection::get_e
} }
// 5. Return success with data computed value. // 5. Return success with data computed value.
return make_success_response(move(computed_value)); return computed_value;
} }
// 12.4.5 Get Element Text, https://w3c.github.io/webdriver/#dfn-get-element-text // 12.4.5 Get Element Text, https://w3c.github.io/webdriver/#dfn-get-element-text
@ -811,7 +802,7 @@ Messages::WebDriverClient::GetElementTextResponse WebDriverConnection::get_eleme
auto rendered_text = element->text_content(); auto rendered_text = element->text_content();
// 5. Return success with data rendered text. // 5. Return success with data rendered text.
return make_success_response(move(rendered_text)); return rendered_text;
} }
// 12.4.6 Get Element Tag Name, https://w3c.github.io/webdriver/#dfn-get-element-tag-name // 12.4.6 Get Element Tag Name, https://w3c.github.io/webdriver/#dfn-get-element-tag-name
@ -829,7 +820,7 @@ Messages::WebDriverClient::GetElementTagNameResponse WebDriverConnection::get_el
auto qualified_name = element->tag_name(); auto qualified_name = element->tag_name();
// 5. Return success with data qualified name. // 5. Return success with data qualified name.
return make_success_response(move(qualified_name)); return qualified_name;
} }
// 12.4.7 Get Element Rect, https://w3c.github.io/webdriver/#dfn-get-element-rect // 12.4.7 Get Element Rect, https://w3c.github.io/webdriver/#dfn-get-element-rect
@ -884,7 +875,7 @@ Messages::WebDriverClient::IsElementEnabledResponse WebDriverConnection::is_elem
} }
// 7. Return success with data enabled. // 7. Return success with data enabled.
return make_success_response(enabled); return enabled;
} }
// 13.1 Get Page Source, https://w3c.github.io/webdriver/#dfn-get-page-source // 13.1 Get Page Source, https://w3c.github.io/webdriver/#dfn-get-page-source
@ -907,7 +898,7 @@ Messages::WebDriverClient::GetSourceResponse WebDriverConnection::get_source()
source = MUST(document->serialize_fragment(Web::DOMParsing::RequireWellFormed::No)); source = MUST(document->serialize_fragment(Web::DOMParsing::RequireWellFormed::No));
// 5. Return success with data source. // 5. Return success with data source.
return make_success_response(source.release_value()); return source.release_value();
} }
// 13.2.1 Execute Script, https://w3c.github.io/webdriver/#dfn-execute-script // 13.2.1 Execute Script, https://w3c.github.io/webdriver/#dfn-execute-script
@ -931,7 +922,7 @@ Messages::WebDriverClient::ExecuteScriptResponse WebDriverConnection::execute_sc
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::ScriptTimeoutError, "Script timed out"); return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::ScriptTimeoutError, "Script timed out");
// 7. Upon fulfillment of promise with value v, let result be a JSON clone of v, and return success with data result. // 7. Upon fulfillment of promise with value v, let result be a JSON clone of v, and return success with data result.
case Web::WebDriver::ExecuteScriptResultType::PromiseResolved: case Web::WebDriver::ExecuteScriptResultType::PromiseResolved:
return make_success_response(move(result.value)); return move(result.value);
// 8. Upon rejection of promise with reason r, let result be a JSON clone of r, and return error with error code javascript error and data result. // 8. Upon rejection of promise with reason r, let result be a JSON clone of r, and return error with error code javascript error and data result.
case Web::WebDriver::ExecuteScriptResultType::PromiseRejected: case Web::WebDriver::ExecuteScriptResultType::PromiseRejected:
case Web::WebDriver::ExecuteScriptResultType::JavaScriptError: case Web::WebDriver::ExecuteScriptResultType::JavaScriptError:
@ -962,7 +953,7 @@ Messages::WebDriverClient::ExecuteAsyncScriptResponse WebDriverConnection::execu
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::ScriptTimeoutError, "Script timed out"); return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::ScriptTimeoutError, "Script timed out");
// 7. Upon fulfillment of promise with value v, let result be a JSON clone of v, and return success with data result. // 7. Upon fulfillment of promise with value v, let result be a JSON clone of v, and return success with data result.
case Web::WebDriver::ExecuteScriptResultType::PromiseResolved: case Web::WebDriver::ExecuteScriptResultType::PromiseResolved:
return make_success_response(move(result.value)); return move(result.value);
// 8. Upon rejection of promise with reason r, let result be a JSON clone of r, and return error with error code javascript error and data result. // 8. Upon rejection of promise with reason r, let result be a JSON clone of r, and return error with error code javascript error and data result.
case Web::WebDriver::ExecuteScriptResultType::PromiseRejected: case Web::WebDriver::ExecuteScriptResultType::PromiseRejected:
case Web::WebDriver::ExecuteScriptResultType::JavaScriptError: case Web::WebDriver::ExecuteScriptResultType::JavaScriptError:
@ -995,7 +986,7 @@ Messages::WebDriverClient::GetAllCookiesResponse WebDriverConnection::get_all_co
} }
// 5. Return success with data cookies. // 5. Return success with data cookies.
return make_success_response(move(cookies)); return cookies;
} }
// 14.2 Get Named Cookie, https://w3c.github.io/webdriver/#dfn-get-named-cookie // 14.2 Get Named Cookie, https://w3c.github.io/webdriver/#dfn-get-named-cookie
@ -1011,7 +1002,7 @@ Messages::WebDriverClient::GetNamedCookieResponse WebDriverConnection::get_named
if (auto cookie = m_web_content_client.did_request_named_cookie(document->url(), name); cookie.has_value()) { if (auto cookie = m_web_content_client.did_request_named_cookie(document->url(), name); cookie.has_value()) {
auto serialized_cookie = serialize_cookie(*cookie); auto serialized_cookie = serialize_cookie(*cookie);
return make_success_response(move(serialized_cookie)); return serialized_cookie;
} }
// 4. Otherwise, return error with error code no such cookie. // 4. Otherwise, return error with error code no such cookie.
@ -1086,7 +1077,7 @@ Messages::WebDriverClient::AddCookieResponse WebDriverConnection::add_cookie(Jso
// NOTE: This probably should only apply to the actual setting of the cookie in the Browser, which cannot fail in our case. // NOTE: This probably should only apply to the actual setting of the cookie in the Browser, which cannot fail in our case.
// 8. Return success with data null. // 8. Return success with data null.
return make_success_response({}); return JsonValue {};
} }
// 14.4 Delete Cookie, https://w3c.github.io/webdriver/#dfn-delete-cookie // 14.4 Delete Cookie, https://w3c.github.io/webdriver/#dfn-delete-cookie
@ -1101,7 +1092,7 @@ Messages::WebDriverClient::DeleteCookieResponse WebDriverConnection::delete_cook
delete_cookies(name); delete_cookies(name);
// 4. Return success with data null. // 4. Return success with data null.
return make_success_response({}); return JsonValue {};
} }
// 14.5 Delete All Cookies, https://w3c.github.io/webdriver/#dfn-delete-all-cookies // 14.5 Delete All Cookies, https://w3c.github.io/webdriver/#dfn-delete-all-cookies
@ -1116,7 +1107,7 @@ Messages::WebDriverClient::DeleteAllCookiesResponse WebDriverConnection::delete_
delete_cookies(); delete_cookies();
// 4. Return success with data null. // 4. Return success with data null.
return make_success_response({}); return JsonValue {};
} }
// 17.1 Take Screenshot, https://w3c.github.io/webdriver/#take-screenshot // 17.1 Take Screenshot, https://w3c.github.io/webdriver/#take-screenshot
@ -1141,7 +1132,7 @@ Messages::WebDriverClient::TakeScreenshotResponse WebDriverConnection::take_scre
root_rect)); root_rect));
// 3. Return success with data encoded string. // 3. Return success with data encoded string.
return make_success_response(move(encoded_string)); return encoded_string;
} }
// 17.2 Take Element Screenshot, https://w3c.github.io/webdriver/#dfn-take-element-screenshot // 17.2 Take Element Screenshot, https://w3c.github.io/webdriver/#dfn-take-element-screenshot
@ -1173,7 +1164,7 @@ Messages::WebDriverClient::TakeElementScreenshotResponse WebDriverConnection::ta
element_rect)); element_rect));
// 6. Return success with data encoded string. // 6. Return success with data encoded string.
return make_success_response(move(encoded_string)); return encoded_string;
} }
// https://w3c.github.io/webdriver/#dfn-no-longer-open // https://w3c.github.io/webdriver/#dfn-no-longer-open

View file

@ -71,13 +71,6 @@ void Client::close_session(unsigned session_id)
dbgln_if(WEBDRIVER_DEBUG, "Unable to shut down session {}: Not found", session_id); dbgln_if(WEBDRIVER_DEBUG, "Unable to shut down session {}: Not found", session_id);
} }
JsonValue Client::make_json_value(JsonValue const& value)
{
JsonObject result;
result.set("value", value);
return result;
}
// 8.1 New Session, https://w3c.github.io/webdriver/#dfn-new-sessions // 8.1 New Session, https://w3c.github.io/webdriver/#dfn-new-sessions
// POST /session // POST /session
Web::WebDriver::Response Client::new_session(Web::WebDriver::Parameters, JsonValue) Web::WebDriver::Response Client::new_session(Web::WebDriver::Parameters, JsonValue)
@ -144,7 +137,7 @@ Web::WebDriver::Response Client::new_session(Web::WebDriver::Parameters, JsonVal
// FIXME: 15. Set the request queue to a new queue. // FIXME: 15. Set the request queue to a new queue.
// 16. Return success with data body. // 16. Return success with data body.
return make_json_value(body); return JsonValue { move(body) };
} }
// 8.2 Delete Session, https://w3c.github.io/webdriver/#dfn-delete-session // 8.2 Delete Session, https://w3c.github.io/webdriver/#dfn-delete-session
@ -158,7 +151,7 @@ Web::WebDriver::Response Client::delete_session(Web::WebDriver::Parameters param
TRY(session->stop()); TRY(session->stop());
// 2. Return success with data null. // 2. Return success with data null.
return make_json_value(JsonValue()); return JsonValue {};
} }
// 8.3 Status, https://w3c.github.io/webdriver/#dfn-status // 8.3 Status, https://w3c.github.io/webdriver/#dfn-status
@ -259,8 +252,7 @@ Web::WebDriver::Response Client::get_window_handle(Web::WebDriver::Parameters pa
{ {
dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/window"); dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/window");
auto* session = TRY(find_session_with_id(parameters[0])); auto* session = TRY(find_session_with_id(parameters[0]));
auto result = TRY(session->get_window_handle()); return session->get_window_handle();
return make_json_value(result);
} }
// 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window // 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
@ -269,8 +261,7 @@ Web::WebDriver::Response Client::close_window(Web::WebDriver::Parameters paramet
{ {
dbgln_if(WEBDRIVER_DEBUG, "Handling DELETE /session/<session_id>/window"); dbgln_if(WEBDRIVER_DEBUG, "Handling DELETE /session/<session_id>/window");
auto* session = TRY(find_session_with_id(parameters[0])); auto* session = TRY(find_session_with_id(parameters[0]));
auto result = TRY(session->close_window()); return session->close_window();
return make_json_value(result);
} }
// 11.4 Get Window Handles, https://w3c.github.io/webdriver/#dfn-get-window-handles // 11.4 Get Window Handles, https://w3c.github.io/webdriver/#dfn-get-window-handles
@ -279,8 +270,7 @@ Web::WebDriver::Response Client::get_window_handles(Web::WebDriver::Parameters p
{ {
dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/window/handles"); dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/window/handles");
auto* session = TRY(find_session_with_id(parameters[0])); auto* session = TRY(find_session_with_id(parameters[0]));
auto result = TRY(session->get_window_handles()); return session->get_window_handles();
return make_json_value(result);
} }
// 11.8.1 Get Window Rect, https://w3c.github.io/webdriver/#dfn-get-window-rect // 11.8.1 Get Window Rect, https://w3c.github.io/webdriver/#dfn-get-window-rect

View file

@ -32,7 +32,6 @@ private:
ErrorOr<Session*, Web::WebDriver::Error> find_session_with_id(StringView session_id); ErrorOr<Session*, Web::WebDriver::Error> find_session_with_id(StringView session_id);
ErrorOr<NonnullOwnPtr<Session>, Web::WebDriver::Error> take_session_with_id(StringView session_id); ErrorOr<NonnullOwnPtr<Session>, Web::WebDriver::Error> take_session_with_id(StringView session_id);
JsonValue make_json_value(JsonValue const&);
virtual Web::WebDriver::Response new_session(Web::WebDriver::Parameters parameters, JsonValue payload) override; virtual Web::WebDriver::Response new_session(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response delete_session(Web::WebDriver::Parameters parameters, JsonValue payload) override; virtual Web::WebDriver::Response delete_session(Web::WebDriver::Parameters parameters, JsonValue payload) override;