1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:37:45 +00:00

WebDriver: Defer removing closed window handles until no longer needed

WebDriver::Session::close_window may invoke Session::stop, which needs
the WebContent connection to still exist. Do not remove the window's
handle (thus destroying the connection) until it is no longer needed.
This commit is contained in:
Timothy Flynn 2023-03-07 09:16:01 -05:00 committed by Linus Groh
parent ae1611aa08
commit 7be8931ca0
2 changed files with 15 additions and 8 deletions

View file

@ -11,6 +11,7 @@
#include "Session.h" #include "Session.h"
#include "Client.h" #include "Client.h"
#include <AK/JsonObject.h> #include <AK/JsonObject.h>
#include <AK/ScopeGuard.h>
#include <LibCore/LocalServer.h> #include <LibCore/LocalServer.h>
#include <LibCore/StandardPaths.h> #include <LibCore/StandardPaths.h>
#include <LibCore/System.h> #include <LibCore/System.h>
@ -117,13 +118,17 @@ Web::WebDriver::Response Session::stop()
// 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window // 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
Web::WebDriver::Response Session::close_window() Web::WebDriver::Response Session::close_window()
{ {
// 3. Close the current top-level browsing context. {
TRY(web_content_connection().close_window()); // Defer removing the window handle from this session until after we know we are done with its connection.
m_windows.remove(m_current_window_handle); ScopeGuard guard { [this] { m_windows.remove(m_current_window_handle); } };
// 4. If there are no more open top-level browsing contexts, then close the session. // 3. Close the current top-level browsing context.
if (m_windows.is_empty()) TRY(web_content_connection().close_window());
TRY(stop());
// 4. If there are no more open top-level browsing contexts, then close the session.
if (m_windows.size() == 1)
TRY(stop());
}
// 5. Return the result of running the remote end steps for the Get Window Handles command. // 5. Return the result of running the remote end steps for the Get Window Handles command.
return get_window_handles(); return get_window_handles();

View file

@ -36,8 +36,10 @@ public:
WebContentConnection& web_content_connection() const WebContentConnection& web_content_connection() const
{ {
auto const& current_window = m_windows.get(m_current_window_handle).value(); auto current_window = m_windows.get(m_current_window_handle);
return current_window.web_content_connection; VERIFY(current_window.has_value());
return current_window->web_content_connection;
} }
String const& current_window_handle() const String const& current_window_handle() const