Skip to content

Commit

Permalink
WebContent+WebDriver: Handle when new windows are immediately closed
Browse files Browse the repository at this point in the history
This happens when window.close() is invoked immediately upon a window
being opened.

(cherry picked from commit ce11a907263670a326ccb4a9625bfeaa65dac937)
  • Loading branch information
trflynn89 authored and nico committed Nov 25, 2024
1 parent 6690c5d commit 9651fe1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Userland/Services/WebContent/WebDriverClient.ipc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ endpoint WebDriverClient {
forward() => (Web::WebDriver::Response response)
refresh() => (Web::WebDriver::Response response)
get_title() => (Web::WebDriver::Response response)
get_window_handle() => (String handle)
get_window_handle() => (Web::WebDriver::Response response)
close_window() => (Web::WebDriver::Response response)
switch_to_window(String handle) => (Web::WebDriver::Response response)
new_window(JsonValue payload) => (Web::WebDriver::Response response)
Expand Down
6 changes: 5 additions & 1 deletion Userland/Services/WebContent/WebDriverConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,11 @@ Messages::WebDriverClient::GetTitleResponse WebDriverConnection::get_title()
// 11.1 Get Window Handle, https://w3c.github.io/webdriver/#get-window-handle
Messages::WebDriverClient::GetWindowHandleResponse WebDriverConnection::get_window_handle()
{
return current_top_level_browsing_context()->top_level_traversable()->window_handle();
// 1. If session's current top-level browsing context is no longer open, return error with error code no such window.
TRY(ensure_current_top_level_browsing_context_is_open());

// 2. Return success with data being the window handle associated with session's current top-level browsing context.
return JsonValue { current_top_level_browsing_context()->top_level_traversable()->window_handle() };
}

// 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
Expand Down
9 changes: 8 additions & 1 deletion Userland/Services/WebDriver/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(NonnullRefPtr<S
dbgln("WebDriver is connected to WebContent socket");
auto web_content_connection = maybe_connection.release_value();

auto window_handle = web_content_connection->get_window_handle();
auto maybe_window_handle = web_content_connection->get_window_handle();
if (maybe_window_handle.is_error()) {
promise->reject(Error::from_string_literal("Window was closed immediately"));
return;
}

auto window_handle = MUST(String::from_byte_string(maybe_window_handle.value().as_string()));

web_content_connection->on_close = [this, window_handle]() {
dbgln_if(WEBDRIVER_DEBUG, "Window {} was closed remotely.", window_handle);
m_windows.remove(window_handle);
Expand Down

0 comments on commit 9651fe1

Please sign in to comment.