From 3aee787539eb083c3cdbb3e5a52a185e4d49f5b5 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Sat, 3 Feb 2024 09:09:33 -0700 Subject: [PATCH] LibWeb+WebContent: Convert WebDriver to choose a navigable AO Also use the TraversableNavigable's window_handle instead of the BrowsingContext's. --- Userland/Libraries/LibWeb/HTML/Navigable.cpp | 6 +++--- Userland/Libraries/LibWeb/WebDriver/Contexts.cpp | 12 ++++++++++-- .../Services/WebContent/ConnectionFromClient.cpp | 4 ++-- Userland/Services/WebContent/WebDriverConnection.cpp | 7 +++---- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/Navigable.cpp b/Userland/Libraries/LibWeb/HTML/Navigable.cpp index e9465a5e33..b2bae8ebcd 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigable.cpp +++ b/Userland/Libraries/LibWeb/HTML/Navigable.cpp @@ -305,7 +305,7 @@ void Navigable::set_ongoing_navigation(Variant ongoing } // https://html.spec.whatwg.org/multipage/document-sequences.html#the-rules-for-choosing-a-navigable -Navigable::ChosenNavigable Navigable::choose_a_navigable(StringView name, TokenizedFeature::NoOpener no_opener, ActivateTab) +Navigable::ChosenNavigable Navigable::choose_a_navigable(StringView name, TokenizedFeature::NoOpener no_opener, ActivateTab activate_tab) { // NOTE: Implementation for step 7 here. JS::GCPtr same_name_navigable = nullptr; @@ -412,13 +412,13 @@ Navigable::ChosenNavigable Navigable::choose_a_navigable(StringView name, Tokeni if (!Infra::is_ascii_case_insensitive_match(name, "_blank"sv)) target_name = MUST(String::from_utf8(name)); - auto create_new_traversable_closure = [this, window_type, no_opener, target_name](JS::GCPtr opener) -> JS::NonnullGCPtr { + auto create_new_traversable_closure = [this, window_type, no_opener, target_name, activate_tab](JS::GCPtr opener) -> JS::NonnullGCPtr { // FIXME: The popup state for window.open is calculated after this call (somehow?) // Probably want to deviate from the spec and pass the popup state in here auto hints = WebViewHints { .popup = window_type != WindowType::ExistingOrNone, }; - auto [page, window_handle] = traversable_navigable()->page().client().page_did_request_new_web_view(ActivateTab::Yes, hints, no_opener); + auto [page, window_handle] = traversable_navigable()->page().client().page_did_request_new_web_view(activate_tab, hints, no_opener); auto traversable = TraversableNavigable::create_a_new_top_level_traversable(*page, opener, target_name).release_value_but_fixme_should_propagate_errors(); page->set_top_level_traversable(traversable); traversable->set_window_handle(window_handle); diff --git a/Userland/Libraries/LibWeb/WebDriver/Contexts.cpp b/Userland/Libraries/LibWeb/WebDriver/Contexts.cpp index b20171c411..1d4a37399d 100644 --- a/Userland/Libraries/LibWeb/WebDriver/Contexts.cpp +++ b/Userland/Libraries/LibWeb/WebDriver/Contexts.cpp @@ -5,7 +5,9 @@ */ #include +#include #include +#include #include #include @@ -16,7 +18,13 @@ JsonObject window_proxy_reference_object(HTML::WindowProxy const& window) { // 1. Let identifier be the web window identifier if the associated browsing context of window is a top-level browsing context. // Otherwise let it be the web frame identifier. - auto identifier = window.associated_browsing_context()->is_top_level() + + // NOTE: We look at the active browsing context's active document's node navigable instead. + // Because a Browsing context's top-level traversable is this navigable's top level traversable. + // Ref: https://html.spec.whatwg.org/multipage/document-sequences.html#bc-traversable + auto traversable_navigable = window.associated_browsing_context()->active_document()->navigable()->traversable_navigable(); + + auto identifier = traversable_navigable->is_top_level_traversable() ? WEB_WINDOW_IDENTIFIER : WEB_FRAME_IDENTIFIER; @@ -25,7 +33,7 @@ JsonObject window_proxy_reference_object(HTML::WindowProxy const& window) // identifier // Associated window handle of the window’s browsing context. - object.set(identifier, window.associated_browsing_context()->window_handle().to_byte_string()); + object.set(identifier, traversable_navigable->window_handle().to_byte_string()); return object; } diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 356c6c5ca0..68029a4c8a 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -77,12 +77,12 @@ PageClient const& ConnectionFromClient::page(u64 index) const Messages::WebContentServer::GetWindowHandleResponse ConnectionFromClient::get_window_handle(u64 page_id) { - return page(page_id).page().top_level_browsing_context().window_handle(); + return page(page_id).page().top_level_traversable()->window_handle(); } void ConnectionFromClient::set_window_handle(u64 page_id, String const& handle) { - page(page_id).page().top_level_browsing_context().set_window_handle(handle); + page(page_id).page().top_level_traversable()->set_window_handle(handle); } void ConnectionFromClient::connect_to_webdriver(u64 page_id, ByteString const& webdriver_ipc_path) diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index 9981711f04..ebcd219cc6 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -529,7 +529,7 @@ 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 m_page_client.page().top_level_browsing_context().window_handle(); + return m_page_client.page().top_level_traversable()->window_handle(); } // 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window @@ -578,11 +578,10 @@ Messages::WebDriverClient::NewWindowResponse WebDriverConnection::new_window(Jso // is "window", and the implementation supports multiple browsing contexts in separate OS windows, the // created browsing context should be in a new OS window. In all other cases the details of how the browsing // context is presented to the user are implementation defined. - // FIXME: Reuse code of window.open() instead of calling choose_a_browsing_context - auto [browsing_context, window_type] = m_page_client.page().top_level_browsing_context().choose_a_browsing_context("_blank"sv, Web::HTML::TokenizedFeature::NoOpener::Yes, Web::HTML::ActivateTab::No); + auto [navigable, window_type] = m_page_client.page().top_level_traversable()->choose_a_navigable("_blank"sv, Web::HTML::TokenizedFeature::NoOpener::Yes, Web::HTML::ActivateTab::No); // 6. Let handle be the associated window handle of the newly created window. - auto handle = browsing_context->window_handle(); + auto handle = navigable->traversable_navigable()->window_handle(); // 7. Let type be "tab" if the newly created window shares an OS-level window with the current browsing context, or "window" otherwise. auto type = "tab"sv;