Truly independent web browser
/*
* Copyright (c) 2022, Andreas Kling <[email protected]>
* Copyright (c) 2025, Jelle Raaijmakers <[email protected]>
* Copyright (c) 2023-2025, Aliaksandr Kalenik <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/HashMap.h>
#include <LibGC/RootVector.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Geolocation/GeolocationCoordinates.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/CheckUnloadingCanceledState.h>
#include <LibWeb/HTML/DocumentState.h>
#include <LibWeb/HTML/History.h>
#include <LibWeb/HTML/HistoryExecutor.h>
#include <LibWeb/HTML/LocalTraversableNavigable.h>
#include <LibWeb/HTML/NavigableContainer.h>
#include <LibWeb/HTML/Navigation.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/HTML/RemoteNavigable.h>
#include <LibWeb/HTML/SameDocumentNavigationEntry.h>
#include <LibWeb/HTML/SessionHistoryEntry.h>
#include <LibWeb/HTML/SourceSnapshotParams.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/WebDriver/BiDi/BiDi.h>
#include <LibWebCommon/HTML/NavigationPopulationRequest.h>
namespace Web::HTML {
GC_DEFINE_ALLOCATOR(LocalTraversableNavigable);
LocalTraversableNavigable::LocalTraversableNavigable(GC::Ref<Page> page)
: LocalNavigable(
page,
page->client().is_svg_page_client(),
Web::PagePresentationRegistration::Yes)
{
}
LocalTraversableNavigable::~LocalTraversableNavigable() = default;
// https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-top-level-traversable
GC::Ref<LocalTraversableNavigable> LocalTraversableNavigable::create_a_new_top_level_traversable(GC::Ref<Page> page, GC::Ptr<HTML::BrowsingContext> opener, Optional<SessionHistoryEntryDescriptor> initial_history_entry_from_owner, Optional<CrossProcessId> document_id)
{
auto& vm = Bindings::main_thread_vm();
page->ensure_compositor_host();
// NB: A traversable with no owning process, such as an SVG image's, mints its own entry
auto initial_entry = initial_history_entry_from_owner.has_value()
? initial_history_entry_from_owner.release_value()
: create_initial_session_history_entry_descriptor(page->client().allocate_cross_process_id(),
opener ? Optional<URL::URL> { opener->active_document()->base_url() } : Optional<URL::URL> {}, {});
// 1. Let document be null.
GC::Ptr<DOM::Document> document = nullptr;
// 2. If opener is null, then set document to the second return value of creating a new top-level browsing context and document.
// NB: The UI process holds the group a top-level browsing context is created in.
if (!opener) {
document = BrowsingContext::create_a_new_browsing_context_and_document(page, nullptr, nullptr, {}, initial_entry.document_state.origin, document_id).document;
}
// 3. Otherwise, set document to the second return value of creating a new auxiliary browsing context and document given opener.
else {
document = BrowsingContext::create_a_new_auxiliary_browsing_context_and_document(page, *opener, document_id).document;
}
// 4. Let documentState be a new document state, with
auto document_state = DocumentState::create(initial_entry.document_state.id);
// document: document (now owned by LocalNavigable::m_active_document, not DocumentState)
// initiator origin: null if opener is null; otherwise, document's origin
document_state->set_initiator_origin(opener ? document->origin() : Optional<URL::Origin> {});
// origin: document's origin
document_state->set_origin(document->origin());
// navigable target name: targetName
document_state->set_navigable_target_name(initial_entry.document_state.navigable_target_name);
// about base URL: document's about base URL
document_state->set_about_base_url(document->about_base_url());
// 5. Let traversable be a new traversable navigable.
auto traversable = vm.heap().allocate<LocalTraversableNavigable>(page);
// 6. Initialize the navigable traversable given documentState.
traversable->initialize_navigable(document_state, nullptr, *document, page->system_visibility_state());
// 7. Let initialHistoryEntry be traversable's active session history entry.
auto initial_history_entry = traversable->active_session_history_entry();
VERIFY(initial_history_entry);
// 8. Set initialHistoryEntry's step to 0.
initial_history_entry->set_step(0);
// NB: The owner's copy of this entry and this one must be the same entry, so take its identity
initial_history_entry->set_navigation_api_key(initial_entry.navigation_api_key);
initial_history_entry->set_navigation_api_id(initial_entry.navigation_api_id);
// 9. Append initialHistoryEntry to traversable's session history entries.
// NB: A traversable's owner keeps the canonical session history; this entry is the owner's initial entry.
traversable->set_has_session_history_entry_and_ready_for_navigation();
// 10. If opener is non-null, then legacy-clone a traversable storage shed given opener's top-level traversable and traversable. [STORAGE]
// NB: This is done by the canonical traversable.
// 11. Append traversable to the user agent's top-level traversable set.
// NB: The UI process holds the user agent's top-level traversable set.
// 12. Return traversable.
return traversable;
}
// Creates a new top-level traversable in the page that the UI process created for it, adopting the identity the UI
// process assigned to it.
GC::Ref<LocalTraversableNavigable> LocalTraversableNavigable::create_for_new_web_view(PageClient::NewWebViewResult&& new_web_view, GC::Ptr<BrowsingContext> opener)
{
auto traversable = create_a_new_top_level_traversable(*new_web_view.page, opener, new_web_view.initial_history_entry.release_value(), new_web_view.initial_document_id.release_value());
traversable->active_document()->relevant_settings_object().id = new_web_view.initial_environment_id.release_value();
traversable->active_browsing_context()->set_browsing_context_group_id(new_web_view.browsing_context_group_id);
new_web_view.page->set_top_level_traversable(traversable);
traversable->set_window_handle(Utf16String::from_ascii_without_validation(new_web_view.window_handle.bytes()));
// AD-HOC: WebDriver BiDi creates the realm of the traversable's first document, and runs its preload scripts, once
// the traversable is named by its window handle.
if (WebDriver::BiDi::is_active())
WebDriver::BiDi::document_is_ready_for_scripts(*traversable->active_document());
return traversable;
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#create-a-fresh-top-level-traversable
GC::Ref<LocalTraversableNavigable> LocalTraversableNavigable::create_a_fresh_top_level_traversable(GC::Ref<Page> page, SessionHistoryEntryDescriptor initial_history_entry, CrossProcessId document_id)
{
// 1. Let traversable be the result of creating a new top-level traversable given null and the empty string.
auto traversable = create_a_new_top_level_traversable(page, nullptr, move(initial_history_entry), document_id);
page->set_top_level_traversable(traversable);
// AD-HOC: Mark the about:blank document as finished parsing. This matches the behavior of the window open steps.
auto document = GC::Ref(*traversable->active_document());
auto completion_token = HTML::HTMLParser::parserless_completion_token(document);
Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(traversable->heap(), GC::captures(document, completion_token.parser_generation), [](auto& document, auto& parser_generation) {
// FIXME: We do this other places too when creating a new about:blank document. Perhaps it's worth a spec issue?
HTML::HTMLParser::the_end(document, { parser_generation });
}));
// 2. Navigate traversable to initialNavigationURL using traversable's active document, with documentResource set to initialNavigationPostResource.
// NB: The UI process navigates the canonical traversable.
// 3. Return traversable.
return traversable;
}
GC::Ref<LocalTraversableNavigable> LocalTraversableNavigable::create_stand_in(Badge<Page>, RemoteNavigable& remote_navigable, SessionHistoryEntryDescriptor const& current_history_entry)
{
VERIFY(!remote_navigable.parent());
auto& page = remote_navigable.page();
page.ensure_compositor_host();
// The stand-in's document is a top-level browsing context's, as a fresh traversable's is. The WindowProxy scripts
// hold for the tab's document is its browsing context's.
auto [browsing_context, document] = BrowsingContext::create_a_new_browsing_context_and_document(page, nullptr, nullptr, remote_navigable.window_proxy(), current_history_entry.document_state.origin);
auto traversable = Bindings::main_thread_vm().heap().allocate<LocalTraversableNavigable>(page);
traversable->initialize_stand_in(remote_navigable, current_history_entry, browsing_context, document, VisibilityState::Hidden);
traversable->set_has_session_history_entry_and_ready_for_navigation();
// The stand-in displays the tab until the document it populates does: its document completes as a fresh
// traversable's.
auto completion_token = HTML::HTMLParser::parserless_completion_token(document);
Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(traversable->heap(), GC::captures(GC::Ref { document }, completion_token.parser_generation), [](auto& document, auto& parser_generation) {
HTML::HTMLParser::the_end(document, { parser_generation });
}));
return traversable;
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#top-level-traversable
bool LocalTraversableNavigable::is_top_level_traversable() const
{
// A top-level traversable is a traversable navigable with a null parent.
return parent() == nullptr;
}
Optional<u64> LocalTraversableNavigable::browsing_context_group_id() const
{
auto document = active_document();
if (!document || !document->browsing_context())
return {};
return document->browsing_context()->browsing_context_group_id();
}
// NB: The UI process sends the reset request at its position on the session history traversal queue and holds the
// queue until the retained active entry is returned, so this runs with the ordering the replaced algorithms had.
void LocalTraversableNavigable::reset_session_history_for_testing()
{
auto maybe_active_entry = active_session_history_entry();
VERIFY(maybe_active_entry);
auto active_entry = maybe_active_entry.release_nonnull();
active_entry->set_step(0);
set_active_session_history_entry(active_entry);
set_current_session_history_entry(active_entry);
m_session_history_entry_count = 1;
auto document = active_document();
VERIFY(document);
document->history()->m_index = 0;
document->history()->m_length = 1;
Vector<NonnullRefPtr<SessionHistoryEntry>> entries_for_navigation_api { active_entry };
active_window()->navigation()->initialize_the_navigation_api_entries_for_reconstructed_session_history(entries_for_navigation_api, active_entry);
}
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-window-close
void LocalTraversableNavigable::close_top_level_traversable_from_script()
{
// 1. Let thisTraversable be this's navigable.
auto& this_traversable = *this;
// 2. If thisTraversable is not a top-level traversable, then return.
if (!this_traversable.is_top_level_traversable())
return;
// 3. If thisTraversable's is closing is true, then return.
if (this_traversable.is_closing())
return;
// 5. Let sourceSnapshotParams be the result of snapshotting source snapshot params given the incumbent global object's
// associated Document.
// NB: The requesting process snapshotted it.
// 6. If all the following are true:
// - thisTraversable is script-closable;
// - the incumbent global object's browsing context is familiar with browsingContext; and
// - the incumbent global object's navigable is allowed by sandboxing to navigate thisTraversable, given sourceSnapshotParams,
// NB: The requesting process found these true, and the UI process checked them again.
// then:
// 1. Set thisTraversable's is closing to true.
this_traversable.set_closing(true);
// 2. Queue a task on the DOM manipulation task source to definitely close thisTraversable.
queue_a_task(Task::Source::DOMManipulation, nullptr, nullptr, GC::create_function(heap(), GC::captures(*this), [](auto& self) {
self.definitely_close_top_level_traversable();
}));
}
void LocalTraversableNavigable::close_top_level_traversable(PromptToUnload prompt_to_unload)
{
// 1. If traversable's is closing is true, then return.
// AD-HOC: A forced close must be able to supersede an in-progress prompted close.
if (is_closing() && prompt_to_unload == PromptToUnload::Yes)
return;
// AD-HOC: Set the is closing flag to prevent re-entrant calls from queuing duplicate session history steps.
set_closing(true);
// 2. Definitely close traversable.
definitely_close_top_level_traversable(prompt_to_unload);
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#definitely-close-a-top-level-traversable
void LocalTraversableNavigable::definitely_close_top_level_traversable(PromptToUnload prompt_to_unload)
{
VERIFY(is_top_level_traversable());
auto append_close_steps = GC::create_function(heap(), GC::captures(*this), [](auto& self) {
if (self.m_close_steps_have_been_appended)
return;
self.m_close_steps_have_been_appended = true;
// 3. Append the following session history traversal steps to traversable:
// NB: The UI process runs the traversal steps' unload recursion and delivers the final unload-and-destroy task
// through run_ui_traversable_close_unload_task.
self.page().history_executor().request_history_operation(CloseTopLevelTraversableHistoryOperationParameters {});
});
if (prompt_to_unload == PromptToUnload::No) {
append_close_steps->function()();
return;
}
// 1. Let toUnload be traversable's active document's inclusive descendant navigables.
// 2. If the result of checking if unloading is canceled for toUnload is not "continue", then return.
// NB: The documents of toUnload are hosted by this page and by others. The UI process runs the check in each
// page hosting one, this one included, with the prompt shown at most once, and reports the result.
page().client().page_did_request_unload_check(id(), GC::create_function(heap(), GC::captures(*this, append_close_steps), [](auto& self, auto& append_close_steps, CheckIfUnloadingIsCanceledResult result) {
if (result != CheckIfUnloadingIsCanceledResult::Continue) {
// AD-HOC: Allow a later close attempt if this one was canceled.
if (!self.m_close_steps_have_been_appended)
self.set_closing(false);
return;
}
// 3. Append the following session history traversal steps to traversable:
append_close_steps->function()();
}));
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#definitely-close-a-top-level-traversable
void LocalTraversableNavigable::run_ui_traversable_close_unload_task()
{
// The UI process has already unloaded every descendant subtree.
queue_a_task(Task::Source::NavigationAndTraversal, nullptr, nullptr,
GC::create_function(heap(), GC::captures(*this), [](auto& self) {
// NB: The UI process discards the page of a tab it let go of without waiting for this task, and the page
// goes without the unload then. A tab can also go while this task is queued.
if (self.page().has_been_discarded() || self.has_been_destroyed())
return;
// 2. Unload document, passing along newDocument if it is not null.
if (auto document = self.active_document())
document->unload();
// 3. If afterAllUnloads was given, then run it.
// NB: afterAllUnloads is the close traversal steps' algorithm step which destroys traversable.
self.destroy_top_level_traversable();
}));
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#destroy-a-top-level-traversable
void LocalTraversableNavigable::destroy_top_level_traversable()
{
VERIFY(is_top_level_traversable());
// 1. Let browsingContext be traversable's active browsing context.
// 2. For each historyEntry in traversable's session history entries:
// NOTE: Without bfcache, only the active document is alive, so we only need to destroy it.
if (active_document())
active_document()->destroy_a_document_and_its_descendants();
// 3. Remove browsingContext.
// NB: The UI process removes browsingContext from its group.
// 4. Remove traversable from the user interface (e.g., close or hide its tab in a tabbed browser).
page().client().page_did_close();
// 5. Remove traversable from the user agent's top-level traversable set.
// NB: The UI process holds the user agent's top-level traversable set.
// FIXME: 6. Invoke WebDriver BiDi navigable destroyed with traversable.
// FIXME: Figure out why we need to do this... we shouldn't be leaking Navigables for all time.
// However, without this, we can keep stale destroyed navigables around.
set_has_been_destroyed();
remove_from_all_local_navigables();
}
void LocalTraversableNavigable::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_active_beforeunload_check);
}
}