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

Browser+WebContent+WebDriver: Move Get/Set Window Rect to WebContent

This also lets us more fully implement the "restore the window" method,
which requires we block until the document reaches the "visible" state.
This commit is contained in:
Timothy Flynn 2022-11-09 09:56:20 -05:00 committed by Linus Groh
parent 9505928fdb
commit b57d7e4332
10 changed files with 144 additions and 114 deletions

View file

@ -311,90 +311,6 @@ static JsonValue serialize_rect(Gfx::IntRect const& rect)
return serialized_rect;
}
// 11.8.1 Get Window Rect, https://w3c.github.io/webdriver/#dfn-get-window-rect
Web::WebDriver::Response Session::get_window_rect()
{
// 1. If the current top-level browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 2. Handle any user prompts and return its value if it is an error.
// 3. Return success with data set to the WindowRect object for the current top-level browsing context.
return serialize_rect(m_browser_connection->get_window_rect());
}
// 11.8.2 Set Window Rect, https://w3c.github.io/webdriver/#dfn-set-window-rect
Web::WebDriver::Response Session::set_window_rect(JsonValue const& payload)
{
if (!payload.is_object())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload is not a JSON object");
auto const& properties = payload.as_object();
auto resolve_property = [](auto name, auto const* property, auto min, auto max) -> ErrorOr<Optional<i32>, Web::WebDriver::Error> {
if (!property)
return Optional<i32> {};
if (!property->is_number())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' is not a Number", name));
auto number = property->template to_number<i64>();
if (number < min)
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' value {} exceeds the minimum allowed value {}", name, number, min));
if (number > max)
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' value {} exceeds the maximum allowed value {}", name, number, max));
return static_cast<i32>(number);
};
// 1. Let width be the result of getting a property named width from the parameters argument, else let it be null.
auto const* width_property = properties.get_ptr("width"sv);
// 2. Let height be the result of getting a property named height from the parameters argument, else let it be null.
auto const* height_property = properties.get_ptr("height"sv);
// 3. Let x be the result of getting a property named x from the parameters argument, else let it be null.
auto const* x_property = properties.get_ptr("x"sv);
// 4. Let y be the result of getting a property named y from the parameters argument, else let it be null.
auto const* y_property = properties.get_ptr("y"sv);
// 5. If width or height is neither null nor a Number from 0 to 2^31 1, return error with error code invalid argument.
auto width = TRY(resolve_property("width"sv, width_property, 0, NumericLimits<i32>::max()));
auto height = TRY(resolve_property("height"sv, height_property, 0, NumericLimits<i32>::max()));
// 6. If x or y is neither null nor a Number from (2^31) to 2^31 1, return error with error code invalid argument.
auto x = TRY(resolve_property("x"sv, x_property, NumericLimits<i32>::min(), NumericLimits<i32>::max()));
auto y = TRY(resolve_property("y"sv, y_property, NumericLimits<i32>::min(), NumericLimits<i32>::max()));
// 7. If the remote end does not support the Set Window Rect command for the current top-level browsing context for any reason, return error with error code unsupported operation.
// 8. If the current top-level browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 9. Handle any user prompts and return its value if it is an error.
// FIXME: 10. Fully exit fullscreen.
// 11. Restore the window.
m_browser_connection->async_restore_window();
// 11. If width and height are not null:
if (width.has_value() && height.has_value()) {
// a. Set the width, in CSS pixels, of the operating system window containing the current top-level browsing context, including any browser chrome and externally drawn window decorations to a value that is as close as possible to width.
// b. Set the height, in CSS pixels, of the operating system window containing the current top-level browsing context, including any browser chrome and externally drawn window decorations to a value that is as close as possible to height.
m_browser_connection->async_set_window_size(Gfx::IntSize { *width, *height });
}
// 12. If x and y are not null:
if (x.has_value() && y.has_value()) {
// a. Run the implementation-specific steps to set the position of the operating system level window containing the current top-level browsing context to the position given by the x and y coordinates.
m_browser_connection->async_set_window_position(Gfx::IntPoint { *x, *y });
}
// 14. Return success with data set to the WindowRect object for the current top-level browsing context.
return serialize_rect(m_browser_connection->get_window_rect());
}
// 11.8.3 Maximize Window, https://w3c.github.io/webdriver/#dfn-maximize-window
Web::WebDriver::Response Session::maximize_window()
{