WebContent: Separately store the WebDriver parent and top-level BCs

We are currently trying to access the current parent and top-level
browsing contexts from the current BC itself. However, if the current BC
is closed, its association to the parent and top-level BCs is lost, and
we are no longer able to handle WebDriver endpoints involving those BCs.

Instead, let's store the parent and top-level BCs separately, and update
them in accordance with the spec.

(cherry picked from commit ab3cfd9bbb3d572d2e1ce4337e112d10ac0e94a3)
This commit is contained in:
Timothy Flynn authored and Nico Weber committed 2024-11-24 21:30:22 -05:00
1 parent ec4fd77063
commit 8da4c4db65
2 files changed
+46 -26

No files matched your search

@@ -190,8 +190,8 @@ ErrorOr<NonnullRefPtr<WebDriverConnection>> WebDriverConnection::connect(Web::Pa
WebDriverConnection::WebDriverConnection(NonnullOwnPtr<Core::LocalSocket> socket, Web::PageClient& page_client)
: IPC::ConnectionToServer<WebDriverClientEndpoint, WebDriverServerEndpoint>(*this, move(socket))
, m_current_browsing_context(page_client.page().top_level_browsing_context())
{
set_current_top_level_browsing_context(page_client.page().top_level_browsing_context());
}
// https://w3c.github.io/webdriver/#dfn-close-the-session
@@ -284,7 +284,7 @@ Messages::WebDriverClient::NavigateToResponse WebDriverConnection::navigate_to(J
}
// 9. Set the current browsing context with the current top-level browsing context.
m_current_browsing_context = *current_top_level_browsing_context();
set_current_browsing_context(*current_top_level_browsing_context());
// 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.
@@ -365,7 +365,7 @@ Messages::WebDriverClient::RefreshResponse WebDriverConnection::refresh()
// FIXME: 2. Try to run the post-navigation checks.
// 5. Set the current browsing context with current top-level browsing context.
m_current_browsing_context = *current_top_level_browsing_context();
set_current_browsing_context(*current_top_level_browsing_context());
// 6. Return success with data null.
return JsonValue {};
@@ -423,7 +423,7 @@ Messages::WebDriverClient::SwitchToWindowResponse WebDriverConnection::switch_to
continue;
if (handle == traversable->window_handle()) {
m_current_browsing_context = traversable->active_browsing_context();
set_current_top_level_browsing_context(*traversable->active_browsing_context());
found_matching_context = true;
break;
}
@@ -512,7 +512,7 @@ Messages::WebDriverClient::SwitchToFrameResponse WebDriverConnection::switch_to_
TRY(handle_any_user_prompts());
// 3. Set the current browsing context with session and session's current top-level browsing context.
m_current_browsing_context = current_top_level_browsing_context();
set_current_browsing_context(*current_top_level_browsing_context());
}
// -> id is a Number object
@@ -550,10 +550,10 @@ Messages::WebDriverClient::SwitchToFrameResponse WebDriverConnection::switch_to_
// 5. Set the current browsing context with session and element's content navigable's active browsing context.
if (is_frame) {
// FIXME: Should HTMLFrameElement also be a NavigableContainer?
m_current_browsing_context = *element->navigable()->active_browsing_context();
set_current_browsing_context(*element->navigable()->active_browsing_context());
} else {
auto& navigable_container = static_cast<Web::HTML::NavigableContainer&>(*element);
m_current_browsing_context = navigable_container.content_navigable()->active_browsing_context();
set_current_browsing_context(*navigable_container.content_navigable()->active_browsing_context());
}
}
@@ -586,7 +586,7 @@ Messages::WebDriverClient::SwitchToParentFrameResponse WebDriverConnection::swit
// 4. If session's current parent browsing context is not null, set the current browsing context with session and
// current parent browsing context.
if (parent_browsing_context)
m_current_browsing_context = *parent_browsing_context;
set_current_browsing_context(*current_parent_browsing_context());
// FIXME: 5. Update any implementation-specific state that would result from the user selecting session's current browsing context for interaction, without altering OS-level focus.
@@ -2124,28 +2124,39 @@ Messages::WebDriverClient::PrintPageResponse WebDriverConnection::print_page(Jso
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::UnsupportedOperation, "Print not implemented"sv);
}
// https://w3c.github.io/webdriver/#dfn-set-the-current-browsing-context
void WebDriverConnection::set_current_browsing_context(Web::HTML::BrowsingContext& browsing_context)
{
// 1. Set session's current browsing context to context.
m_current_browsing_context = browsing_context;
// 2. Set the session's current parent browsing context to the parent browsing context of context, if that context
// exists, or null otherwise.
if (auto navigable = browsing_context.active_document()->navigable(); navigable && navigable->parent())
m_current_parent_browsing_context = navigable->parent()->active_browsing_context();
else
m_current_parent_browsing_context = nullptr;
}
// https://w3c.github.io/webdriver/#dfn-set-the-current-browsing-context
void WebDriverConnection::set_current_top_level_browsing_context(Web::HTML::BrowsingContext& browsing_context)
{
// 1. Assert: context is a top-level browsing context.
VERIFY(browsing_context.is_top_level());
// 2. Set session's current top-level browsing context to context.
m_current_top_level_browsing_context = browsing_context;
// 3. Set the current browsing context with session and context.
set_current_browsing_context(browsing_context);
}
Messages::WebDriverClient::EnsureTopLevelBrowsingContextIsOpenResponse WebDriverConnection::ensure_top_level_browsing_context_is_open()
{
TRY(ensure_current_top_level_browsing_context_is_open());
return JsonValue {};
}
// https://w3c.github.io/webdriver/#dfn-current-parent-browsing-context
JS::GCPtr<Web::HTML::BrowsingContext> WebDriverConnection::current_parent_browsing_context()
{
auto current_navigable = current_browsing_context().active_document()->navigable();
if (!current_navigable || !current_navigable->parent())
return {};
return current_navigable->parent()->active_browsing_context();
}
// https://w3c.github.io/webdriver/#dfn-current-top-level-browsing-context
JS::GCPtr<Web::HTML::BrowsingContext> WebDriverConnection::current_top_level_browsing_context()
{
return current_browsing_context().top_level_browsing_context();
}
ErrorOr<void, Web::WebDriver::Error> WebDriverConnection::ensure_current_browsing_context_is_open()
{
return ensure_browsing_context_is_open(current_browsing_context());
@@ -104,9 +104,12 @@ private:
virtual Messages::WebDriverClient::PrintPageResponse print_page(JsonValue const& payload) override;
virtual Messages::WebDriverClient::EnsureTopLevelBrowsingContextIsOpenResponse ensure_top_level_browsing_context_is_open() override;
void set_current_browsing_context(Web::HTML::BrowsingContext&);
Web::HTML::BrowsingContext& current_browsing_context() { return *m_current_browsing_context; }
JS::GCPtr<Web::HTML::BrowsingContext> current_parent_browsing_context();
JS::GCPtr<Web::HTML::BrowsingContext> current_top_level_browsing_context();
JS::GCPtr<Web::HTML::BrowsingContext> current_parent_browsing_context() { return m_current_parent_browsing_context.ptr(); }
void set_current_top_level_browsing_context(Web::HTML::BrowsingContext&);
JS::GCPtr<Web::HTML::BrowsingContext> current_top_level_browsing_context() { return m_current_top_level_browsing_context.ptr(); }
ErrorOr<void, Web::WebDriver::Error> ensure_current_browsing_context_is_open();
ErrorOr<void, Web::WebDriver::Error> ensure_current_top_level_browsing_context_is_open();
@@ -146,6 +149,12 @@ private:
// https://w3c.github.io/webdriver/#dfn-current-browsing-context
JS::Handle<Web::HTML::BrowsingContext> m_current_browsing_context;
// https://w3c.github.io/webdriver/#dfn-current-parent-browsing-context
JS::Handle<Web::HTML::BrowsingContext> m_current_parent_browsing_context;
// https://w3c.github.io/webdriver/#dfn-current-top-level-browsing-context
JS::Handle<Web::HTML::BrowsingContext> m_current_top_level_browsing_context;
JS::Handle<JS::Cell> m_action_executor;
};