2022-12-12 12:01:09 +01:00
/*
2024-10-04 13:19:50 +02:00
* Copyright ( c ) 2022 , Andreas Kling < andreas @ ladybird . org >
2022-12-12 12:01:09 +01:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2023-07-09 11:40:17 +03:30
# include <AK/QuickSort.h>
2024-09-19 22:34:53 +02:00
# include <LibGfx/SkiaBackendContext.h>
2022-12-17 14:26:48 +01:00
# include <LibWeb/Bindings/MainThreadVM.h>
# include <LibWeb/DOM/Document.h>
# include <LibWeb/HTML/BrowsingContextGroup.h>
# include <LibWeb/HTML/DocumentState.h>
2023-09-27 22:59:57 -06:00
# include <LibWeb/HTML/Navigation.h>
2023-04-13 10:23:59 +03:00
# include <LibWeb/HTML/NavigationParams.h>
2024-02-07 09:03:47 -07:00
# include <LibWeb/HTML/Parser/HTMLParser.h>
2022-12-12 12:01:09 +01:00
# include <LibWeb/HTML/SessionHistoryEntry.h>
# include <LibWeb/HTML/TraversableNavigable.h>
2023-10-08 11:59:40 +02:00
# include <LibWeb/HTML/Window.h>
2023-09-19 19:16:50 +02:00
# include <LibWeb/Page/Page.h>
2024-08-12 00:49:32 +02:00
# include <LibWeb/Painting/ViewportPaintable.h>
2023-04-13 10:23:59 +03:00
# include <LibWeb/Platform/EventLoopPlugin.h>
2022-12-12 12:01:09 +01:00
namespace Web : : HTML {
2023-11-19 19:47:52 +01:00
JS_DEFINE_ALLOCATOR ( TraversableNavigable ) ;
2023-12-03 16:56:04 +13:00
TraversableNavigable : : TraversableNavigable ( JS : : NonnullGCPtr < Page > page )
2024-04-26 14:55:54 +02:00
: Navigable ( page )
2024-11-14 06:13:46 +13:00
, m_session_history_traversal_queue ( vm ( ) . heap ( ) . allocate < SessionHistoryTraversalQueue > ( ) )
2023-08-22 15:42:56 +02:00
{
2024-06-26 17:56:55 +02:00
# ifdef AK_OS_MACOS
2024-07-04 23:57:00 +02:00
auto display_list_player_type = page - > client ( ) . display_list_player_type ( ) ;
2024-07-17 14:13:32 +03:00
if ( display_list_player_type = = DisplayListPlayerType : : SkiaGPUIfAvailable ) {
2024-09-26 17:01:07 +02:00
m_metal_context = Gfx : : get_metal_context ( ) ;
2024-09-19 22:34:53 +02:00
m_skia_backend_context = Gfx : : SkiaBackendContext : : create_metal_context ( * m_metal_context ) ;
2024-07-04 23:57:00 +02:00
}
2024-06-26 17:56:55 +02:00
# endif
2024-07-03 19:23:27 +02:00
# ifdef USE_VULKAN
auto display_list_player_type = page - > client ( ) . display_list_player_type ( ) ;
2024-07-17 14:13:32 +03:00
if ( display_list_player_type = = DisplayListPlayerType : : SkiaGPUIfAvailable ) {
2024-09-26 17:27:58 +02:00
auto maybe_vulkan_context = Gfx : : create_vulkan_context ( ) ;
2024-07-03 19:23:27 +02:00
if ( ! maybe_vulkan_context . is_error ( ) ) {
auto vulkan_context = maybe_vulkan_context . release_value ( ) ;
2024-09-19 22:34:53 +02:00
m_skia_backend_context = Gfx : : SkiaBackendContext : : create_vulkan_context ( vulkan_context ) ;
2024-07-03 19:23:27 +02:00
} else {
dbgln ( " Vulkan context creation failed: {} " , maybe_vulkan_context . error ( ) ) ;
}
}
# endif
2023-08-22 15:42:56 +02:00
}
2022-12-12 12:01:09 +01:00
TraversableNavigable : : ~ TraversableNavigable ( ) = default ;
void TraversableNavigable : : visit_edges ( Cell : : Visitor & visitor )
{
Base : : visit_edges ( visitor ) ;
2024-04-15 13:58:21 +02:00
visitor . visit ( m_session_history_entries ) ;
2024-04-10 04:43:51 +02:00
visitor . visit ( m_session_history_traversal_queue ) ;
2022-12-12 12:01:09 +01:00
}
2022-12-17 14:26:48 +01:00
static OrderedHashTable < TraversableNavigable * > & user_agent_top_level_traversable_set ( )
{
static OrderedHashTable < TraversableNavigable * > set ;
return set ;
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-top-level-browsing-context
2023-12-03 16:56:04 +13:00
WebIDL : : ExceptionOr < BrowsingContextAndDocument > create_a_new_top_level_browsing_context_and_document ( JS : : NonnullGCPtr < Page > page )
2022-12-17 14:26:48 +01:00
{
// 1. Let group and document be the result of creating a new browsing context group and document.
auto [ group , document ] = TRY ( BrowsingContextGroup : : create_a_new_browsing_context_group_and_document ( page ) ) ;
// 2. Return group's browsing context set[0] and document.
return BrowsingContextAndDocument { * * group - > browsing_context_set ( ) . begin ( ) , document } ;
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#creating-a-new-top-level-traversable
2023-12-03 16:56:04 +13:00
WebIDL : : ExceptionOr < JS : : NonnullGCPtr < TraversableNavigable > > TraversableNavigable : : create_a_new_top_level_traversable ( JS : : NonnullGCPtr < Page > page , JS : : GCPtr < HTML : : BrowsingContext > opener , String target_name )
2022-12-17 14:26:48 +01:00
{
auto & vm = Bindings : : main_thread_vm ( ) ;
// 1. Let document be null.
JS : : GCPtr < 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.
if ( ! opener ) {
document = TRY ( create_a_new_top_level_browsing_context_and_document ( page ) ) . document ;
}
// 3. Otherwise, set document to the second return value of creating a new auxiliary browsing context and document given opener.
else {
document = TRY ( BrowsingContext : : create_a_new_auxiliary_browsing_context_and_document ( page , * opener ) ) . document ;
}
// 4. Let documentState be a new document state, with
2024-11-14 06:13:46 +13:00
auto document_state = vm . heap ( ) . allocate < DocumentState > ( ) ;
2022-12-17 14:26:48 +01:00
// document: document
document_state - > set_document ( document ) ;
2023-09-22 17:31:25 -06:00
// initiator origin: null if opener is null; otherwise, document's origin
2024-10-05 15:33:34 +13:00
document_state - > set_initiator_origin ( opener ? Optional < URL : : Origin > { } : document - > origin ( ) ) ;
2023-09-22 17:31:25 -06:00
// origin: document's origin
document_state - > set_origin ( document - > origin ( ) ) ;
2022-12-17 14:26:48 +01:00
// navigable target name: targetName
document_state - > set_navigable_target_name ( target_name ) ;
2023-09-22 17:31:25 -06:00
// about base URL: document's about base URL
document_state - > set_about_base_url ( document - > about_base_url ( ) ) ;
2022-12-17 14:26:48 +01:00
// 5. Let traversable be a new traversable navigable.
2024-11-14 06:13:46 +13:00
auto traversable = vm . heap ( ) . allocate < TraversableNavigable > ( page ) ;
2022-12-17 14:26:48 +01:00
// 6. Initialize the navigable traversable given documentState.
TRY_OR_THROW_OOM ( vm , traversable - > initialize_navigable ( document_state , nullptr ) ) ;
// 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.
2024-03-27 15:59:12 +01:00
initial_history_entry - > set_step ( 0 ) ;
2022-12-17 14:26:48 +01:00
// 9. Append initialHistoryEntry to traversable's session history entries.
traversable - > m_session_history_entries . append ( * initial_history_entry ) ;
// FIXME: 10. If opener is non-null, then legacy-clone a traversable storage shed given opener's top-level traversable and traversable. [STORAGE]
// 11. Append traversable to the user agent's top-level traversable set.
user_agent_top_level_traversable_set ( ) . set ( traversable ) ;
// 12. Return traversable.
return traversable ;
}
// https://html.spec.whatwg.org/multipage/document-sequences.html#create-a-fresh-top-level-traversable
2024-03-18 16:22:27 +13:00
WebIDL : : ExceptionOr < JS : : NonnullGCPtr < TraversableNavigable > > TraversableNavigable : : create_a_fresh_top_level_traversable ( JS : : NonnullGCPtr < Page > page , URL : : URL const & initial_navigation_url , Variant < Empty , String , POSTResource > initial_navigation_post_resource )
2022-12-17 14:26:48 +01:00
{
// 1. Let traversable be the result of creating a new top-level traversable given null and the empty string.
auto traversable = TRY ( create_a_new_top_level_traversable ( page , nullptr , { } ) ) ;
2024-02-07 09:03:47 -07:00
page - > set_top_level_traversable ( traversable ) ;
2022-12-17 14:26:48 +01:00
2024-02-07 09:03:47 -07:00
// AD-HOC: Mark the about:blank document as finished parsing if we're only going to about:blank
// Skip the initial navigation as well. This matches the behavior of the window open steps.
if ( url_matches_about_blank ( initial_navigation_url ) ) {
2024-10-31 02:39:29 +13:00
Platform : : EventLoopPlugin : : the ( ) . deferred_invoke ( JS : : create_heap_function ( traversable - > heap ( ) , [ traversable , initial_navigation_url ] {
2024-02-07 09:03:47 -07:00
// 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 ( * traversable - > active_document ( ) ) ;
// FIXME: If we perform the URL and history update steps here, we start hanging tests and the UI process will
// try to load() the initial URLs passed on the command line before we finish processing the events here.
// However, because we call this before the PageClient is fully initialized... that gets awkward.
2024-10-31 02:39:29 +13:00
} ) ) ;
2024-02-07 09:03:47 -07:00
}
else {
// 2. Navigate traversable to initialNavigationURL using traversable's active document, with documentResource set to initialNavigationPostResource.
TRY ( traversable - > navigate ( { . url = initial_navigation_url ,
. source_document = * traversable - > active_document ( ) ,
. document_resource = initial_navigation_post_resource } ) ) ;
}
2022-12-17 14:26:48 +01:00
// 3. Return traversable.
return traversable ;
}
2022-12-12 12:01:09 +01:00
// https://html.spec.whatwg.org/multipage/document-sequences.html#top-level-traversable
bool TraversableNavigable : : is_top_level_traversable ( ) const
{
// A top-level traversable is a traversable navigable with a null parent.
return parent ( ) = = nullptr ;
}
2023-04-13 10:11:31 +03:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-all-used-history-steps
Vector < int > TraversableNavigable : : get_all_used_history_steps ( ) const
{
// FIXME: 1. Assert: this is running within traversable's session history traversal queue.
// 2. Let steps be an empty ordered set of non-negative integers.
OrderedHashTable < int > steps ;
// 3. Let entryLists be the ordered set « traversable's session history entries ».
Vector < Vector < JS : : NonnullGCPtr < SessionHistoryEntry > > > entry_lists { session_history_entries ( ) } ;
// 4. For each entryList of entryLists:
while ( ! entry_lists . is_empty ( ) ) {
auto entry_list = entry_lists . take_first ( ) ;
// 1. For each entry of entryList:
for ( auto & entry : entry_list ) {
// 1. Append entry's step to steps.
2024-03-27 15:59:12 +01:00
steps . set ( entry - > step ( ) . get < int > ( ) ) ;
2023-04-13 10:11:31 +03:00
// 2. For each nestedHistory of entry's document state's nested histories, append nestedHistory's entries list to entryLists.
2024-03-27 15:59:12 +01:00
for ( auto & nested_history : entry - > document_state ( ) - > nested_histories ( ) )
2023-04-13 10:11:31 +03:00
entry_lists . append ( nested_history . entries ) ;
}
}
// 5. Return steps, sorted.
auto sorted_steps = steps . values ( ) ;
quick_sort ( sorted_steps ) ;
return sorted_steps ;
}
2023-04-13 10:18:46 +03:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-the-history-object-length-and-index
TraversableNavigable : : HistoryObjectLengthAndIndex TraversableNavigable : : get_the_history_object_length_and_index ( int step ) const
{
// 1. Let steps be the result of getting all used history steps within traversable.
auto steps = get_all_used_history_steps ( ) ;
// 2. Let scriptHistoryLength be the size of steps.
auto script_history_length = steps . size ( ) ;
// 3. Assert: steps contains step.
VERIFY ( steps . contains_slow ( step ) ) ;
// 4. Let scriptHistoryIndex be the index of step in steps.
auto script_history_index = * steps . find_first_index ( step ) ;
// 5. Return (scriptHistoryLength, scriptHistoryIndex).
return HistoryObjectLengthAndIndex {
. script_history_length = script_history_length ,
. script_history_index = script_history_index
} ;
}
2023-06-19 21:08:26 +03:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-the-used-step
int TraversableNavigable : : get_the_used_step ( int step ) const
{
// 1. Let steps be the result of getting all used history steps within traversable.
auto steps = get_all_used_history_steps ( ) ;
// 2. Return the greatest item in steps that is less than or equal to step.
VERIFY ( ! steps . is_empty ( ) ) ;
Optional < int > result ;
for ( size_t i = 0 ; i < steps . size ( ) ; i + + ) {
if ( steps [ i ] < = step ) {
if ( ! result . has_value ( ) | | ( result . value ( ) < steps [ i ] ) ) {
result = steps [ i ] ;
}
}
}
return result . value ( ) ;
}
2023-06-19 21:11:30 +03:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#get-all-navigables-whose-current-session-history-entry-will-change-or-reload
Vector < JS : : Handle < Navigable > > TraversableNavigable : : get_all_navigables_whose_current_session_history_entry_will_change_or_reload ( int target_step ) const
{
// 1. Let results be an empty list.
Vector < JS : : Handle < Navigable > > results ;
// 2. Let navigablesToCheck be « traversable ».
Vector < JS : : Handle < Navigable > > navigables_to_check ;
navigables_to_check . append ( const_cast < TraversableNavigable & > ( * this ) ) ;
// 3. For each navigable of navigablesToCheck:
while ( ! navigables_to_check . is_empty ( ) ) {
auto navigable = navigables_to_check . take_first ( ) ;
// 1. Let targetEntry be the result of getting the target history entry given navigable and targetStep.
auto target_entry = navigable - > get_the_target_history_entry ( target_step ) ;
// 2. If targetEntry is not navigable's current session history entry or targetEntry's document state's reload pending is true, then append navigable to results.
2024-03-27 15:59:12 +01:00
if ( target_entry ! = navigable - > current_session_history_entry ( ) | | target_entry - > document_state ( ) - > reload_pending ( ) ) {
2023-06-19 21:11:30 +03:00
results . append ( * navigable ) ;
}
// 3. If targetEntry's document is navigable's document, and targetEntry's document state's reload pending is false, then extend navigablesToCheck with the child navigables of navigable.
2024-03-27 16:14:52 +01:00
if ( target_entry - > document ( ) = = navigable - > active_document ( ) & & ! target_entry - > document_state ( ) - > reload_pending ( ) ) {
2023-06-19 21:11:30 +03:00
navigables_to_check . extend ( navigable - > child_navigables ( ) ) ;
}
}
// 4. Return results.
return results ;
}
2023-09-27 22:59:57 -06:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-all-navigables-that-only-need-history-object-length/index-update
Vector < JS : : Handle < Navigable > > TraversableNavigable : : get_all_navigables_that_only_need_history_object_length_index_update ( int target_step ) const
{
// NOTE: Other navigables might not be impacted by the traversal. For example, if the response is a 204, the currently active document will remain.
// Additionally, going 'back' after a 204 will change the current session history entry, but the active session history entry will already be correct.
// 1. Let results be an empty list.
Vector < JS : : Handle < Navigable > > results ;
// 2. Let navigablesToCheck be « traversable ».
Vector < JS : : Handle < Navigable > > navigables_to_check ;
navigables_to_check . append ( const_cast < TraversableNavigable & > ( * this ) ) ;
// 3. For each navigable of navigablesToCheck:
while ( ! navigables_to_check . is_empty ( ) ) {
auto navigable = navigables_to_check . take_first ( ) ;
// 1. Let targetEntry be the result of getting the target history entry given navigable and targetStep.
auto target_entry = navigable - > get_the_target_history_entry ( target_step ) ;
// 2. If targetEntry is navigable's current session history entry and targetEntry's document state's reload pending is false, then:
2024-03-27 15:59:12 +01:00
if ( target_entry = = navigable - > current_session_history_entry ( ) & & ! target_entry - > document_state ( ) - > reload_pending ( ) ) {
2023-09-27 22:59:57 -06:00
// 1. Append navigable to results.
results . append ( navigable ) ;
// 2. Extend navigablesToCheck with navigable's child navigables.
navigables_to_check . extend ( navigable - > child_navigables ( ) ) ;
}
}
// 4. Return results.
return results ;
}
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-all-navigables-that-might-experience-a-cross-document-traversal
Vector < JS : : Handle < Navigable > > TraversableNavigable : : get_all_navigables_that_might_experience_a_cross_document_traversal ( int target_step ) const
{
// NOTE: From traversable's session history traversal queue's perspective, these documents are candidates for going cross-document during the
// traversal described by targetStep. They will not experience a cross-document traversal if the status code for their target document is
// HTTP 204 No Content.
// Note that if a given navigable might experience a cross-document traversal, this algorithm will return navigable but not its child navigables.
// Those would end up unloaded, not traversed.
// 1. Let results be an empty list.
Vector < JS : : Handle < Navigable > > results ;
// 2. Let navigablesToCheck be « traversable ».
Vector < JS : : Handle < Navigable > > navigables_to_check ;
navigables_to_check . append ( const_cast < TraversableNavigable & > ( * this ) ) ;
// 3. For each navigable of navigablesToCheck:
while ( ! navigables_to_check . is_empty ( ) ) {
auto navigable = navigables_to_check . take_first ( ) ;
// 1. Let targetEntry be the result of getting the target history entry given navigable and targetStep.
auto target_entry = navigable - > get_the_target_history_entry ( target_step ) ;
// 2. If targetEntry's document is not navigable's document or targetEntry's document state's reload pending is true, then append navigable to results.
// NOTE: Although navigable's active history entry can change synchronously, the new entry will always have the same Document,
// so accessing navigable's document is reliable.
2024-03-27 16:14:52 +01:00
if ( target_entry - > document ( ) ! = navigable - > active_document ( ) | | target_entry - > document_state ( ) - > reload_pending ( ) ) {
2023-09-27 22:59:57 -06:00
results . append ( navigable ) ;
}
// 3. Otherwise, extend navigablesToCheck with navigable's child navigables.
// Adding child navigables to navigablesToCheck means those navigables will also be checked by this loop.
// Child navigables are only checked if the navigable's active document will not change as part of this traversal.
else {
navigables_to_check . extend ( navigable - > child_navigables ( ) ) ;
}
}
// 4. Return results.
return results ;
}
2024-03-31 23:42:51 +02:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#deactivate-a-document-for-a-cross-document-navigation
2024-04-16 22:04:01 +02:00
static void deactivate_a_document_for_cross_document_navigation ( JS : : NonnullGCPtr < DOM : : Document > displayed_document , Optional < UserNavigationInvolvement > , JS : : NonnullGCPtr < SessionHistoryEntry > target_entry , JS : : NonnullGCPtr < JS : : HeapFunction < void ( ) > > after_potential_unloads )
2024-03-31 23:42:51 +02:00
{
// 1. Let navigable be displayedDocument's node navigable.
auto navigable = displayed_document - > navigable ( ) ;
// 2. Let potentiallyTriggerViewTransition be false.
auto potentially_trigger_view_transition = false ;
// FIXME: 3. Let isBrowserUINavigation be true if userNavigationInvolvement is "browser UI"; otherwise false.
// FIXME: 4. Set potentiallyTriggerViewTransition to the result of calling can navigation trigger a cross-document
// view-transition? given displayedDocument, targetEntry's document, navigationType, and isBrowserUINavigation.
// 5. If potentiallyTriggerViewTransition is false, then:
if ( ! potentially_trigger_view_transition ) {
// FIXME 1. Let firePageSwapBeforeUnload be the following step
// 1. Fire the pageswap event given displayedDocument, targetEntry, navigationType, and null.
// 2. Set the ongoing navigation for navigable to null.
navigable - > set_ongoing_navigation ( { } ) ;
// 3. Unload a document and its descendants given displayedDocument, targetEntry's document, afterPotentialUnloads, and firePageSwapBeforeUnload.
2024-04-16 22:04:01 +02:00
displayed_document - > unload_a_document_and_its_descendants ( target_entry - > document ( ) , after_potential_unloads ) ;
2024-03-31 23:42:51 +02:00
}
// FIXME: 6. Otherwise, queue a global task on the navigation and traversal task source given navigable's active window to run the steps:
else {
// FIXME: 1. Let proceedWithNavigationAfterViewTransitionCapture be the following step:
// 1. Append the following session history traversal steps to navigable's traversable navigable:
// 1. Set the ongoing navigation for navigable to null.
// 2. Unload a document and its descendants given displayedDocument, targetEntry's document, and afterPotentialUnloads.
// FIXME: 2. Let viewTransition be the result of setting up a cross-document view-transition given displayedDocument,
// targetEntry's document, navigationType, and proceedWithNavigationAfterViewTransitionCapture.
// FIXME: 3. Fire the pageswap event given displayedDocument, targetEntry, navigationType, and viewTransition.
// FIXME: 4. If viewTransition is null, then run proceedWithNavigationAfterViewTransitionCapture.
TODO ( ) ;
}
}
2024-04-21 15:30:58 +02:00
struct ChangingNavigableContinuationState : public JS : : Cell {
JS_CELL ( ChangingNavigableContinuationState , JS : : Cell ) ;
JS_DECLARE_ALLOCATOR ( ChangingNavigableContinuationState ) ;
JS : : GCPtr < DOM : : Document > displayed_document ;
JS : : GCPtr < SessionHistoryEntry > target_entry ;
JS : : GCPtr < Navigable > navigable ;
bool update_only = false ;
JS : : GCPtr < SessionHistoryEntry > populated_target_entry ;
bool populated_cloned_target_session_history_entry = false ;
virtual void visit_edges ( Cell : : Visitor & visitor ) override
{
Base : : visit_edges ( visitor ) ;
visitor . visit ( displayed_document ) ;
visitor . visit ( target_entry ) ;
visitor . visit ( navigable ) ;
visitor . visit ( populated_target_entry ) ;
}
} ;
JS_DEFINE_ALLOCATOR ( ChangingNavigableContinuationState ) ;
2023-04-13 10:23:59 +03:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#apply-the-history-step
2023-09-27 22:59:57 -06:00
TraversableNavigable : : HistoryStepResult TraversableNavigable : : apply_the_history_step (
int step ,
bool check_for_cancelation ,
2024-04-07 16:32:33 -07:00
IGNORE_USE_IN_ESCAPING_LAMBDA Optional < SourceSnapshotParams > source_snapshot_params ,
2023-09-27 22:59:57 -06:00
JS : : GCPtr < Navigable > initiator_to_check ,
2024-03-28 12:19:57 +01:00
Optional < UserNavigationInvolvement > user_involvement_for_navigate_events ,
2024-04-07 16:32:33 -07:00
IGNORE_USE_IN_ESCAPING_LAMBDA Optional < Bindings : : NavigationType > navigation_type ,
IGNORE_USE_IN_ESCAPING_LAMBDA SynchronousNavigation synchronous_navigation )
2023-04-13 10:23:59 +03:00
{
2023-09-27 22:59:57 -06:00
auto & vm = this - > vm ( ) ;
2023-04-13 10:23:59 +03:00
// FIXME: 1. Assert: This is running within traversable's session history traversal queue.
// 2. Let targetStep be the result of getting the used step given traversable and step.
auto target_step = get_the_used_step ( step ) ;
2023-09-27 22:59:57 -06:00
// Note: Calling this early so we can re-use the same list in 3.2 and 6.
auto change_or_reload_navigables = get_all_navigables_whose_current_session_history_entry_will_change_or_reload ( target_step ) ;
2023-04-13 10:23:59 +03:00
2023-09-27 22:59:57 -06:00
// 3. If initiatorToCheck is not null, then:
if ( initiator_to_check ! = nullptr ) {
// 1. Assert: sourceSnapshotParams is not null.
VERIFY ( source_snapshot_params . has_value ( ) ) ;
2023-04-13 10:23:59 +03:00
2023-09-27 22:59:57 -06:00
// 2. For each navigable of get all navigables whose current session history entry will change or reload:
// if initiatorToCheck is not allowed by sandboxing to navigate navigable given sourceSnapshotParams, then return "initiator-disallowed".
for ( auto const & navigable : change_or_reload_navigables ) {
if ( ! initiator_to_check - > allowed_by_sandboxing_to_navigate ( * navigable , * source_snapshot_params ) )
return HistoryStepResult : : InitiatorDisallowed ;
}
}
// 4. Let navigablesCrossingDocuments be the result of getting all navigables that might experience a cross-document traversal given traversable and targetStep.
2024-10-02 23:56:26 +01:00
auto navigables_crossing_documents = get_all_navigables_that_might_experience_a_cross_document_traversal ( target_step ) ;
// 5. If checkForCancelation is true, and the result of checking if unloading is canceled given navigablesCrossingDocuments, traversable, targetStep,
// and userInvolvementForNavigateEvents is not "continue", then return that result.
if ( check_for_cancelation ) {
auto result = check_if_unloading_is_canceled ( navigables_crossing_documents , * this , target_step , user_involvement_for_navigate_events ) ;
if ( result = = CheckIfUnloadingIsCanceledResult : : CanceledByBeforeUnload )
return HistoryStepResult : : CanceledByBeforeUnload ;
if ( result = = CheckIfUnloadingIsCanceledResult : : CanceledByNavigate )
return HistoryStepResult : : CanceledByNavigate ;
}
2023-04-13 10:23:59 +03:00
// 6. Let changingNavigables be the result of get all navigables whose current session history entry will change or reload given traversable and targetStep.
2023-09-27 22:59:57 -06:00
auto changing_navigables = move ( change_or_reload_navigables ) ;
2023-04-13 10:23:59 +03:00
2023-09-27 22:59:57 -06:00
// 7. Let nonchangingNavigablesThatStillNeedUpdates be the result of getting all navigables that only need history object length/index update given traversable and targetStep.
auto non_changing_navigables_that_still_need_updates = get_all_navigables_that_only_need_history_object_length_index_update ( target_step ) ;
2023-04-13 10:23:59 +03:00
// 8. For each navigable of changingNavigables:
for ( auto & navigable : changing_navigables ) {
// 1. Let targetEntry be the result of getting the target history entry given navigable and targetStep.
auto target_entry = navigable - > get_the_target_history_entry ( target_step ) ;
// 2. Set navigable's current session history entry to targetEntry.
navigable - > set_current_session_history_entry ( target_entry ) ;
// 3. Set navigable's ongoing navigation to "traversal".
2023-09-03 17:42:00 +02:00
navigable - > set_ongoing_navigation ( Traversal : : Tag ) ;
2023-04-13 10:23:59 +03:00
}
// 9. Let totalChangeJobs be the size of changingNavigables.
auto total_change_jobs = changing_navigables . size ( ) ;
// 10. Let completedChangeJobs be 0.
2024-04-07 16:32:33 -07:00
IGNORE_USE_IN_ESCAPING_LAMBDA size_t completed_change_jobs = 0 ;
2023-04-13 10:23:59 +03:00
// 11. Let changingNavigableContinuations be an empty queue of changing navigable continuation states.
2023-09-27 22:59:57 -06:00
// NOTE: This queue is used to split the operations on changingNavigables into two parts. Specifically, changingNavigableContinuations holds data for the second part.
2024-04-21 15:30:58 +02:00
IGNORE_USE_IN_ESCAPING_LAMBDA Queue < JS : : Handle < ChangingNavigableContinuationState > > changing_navigable_continuations ;
2023-04-13 10:23:59 +03:00
// 12. For each navigable of changingNavigables, queue a global task on the navigation and traversal task source of navigable's active window to run the steps:
for ( auto & navigable : changing_navigables ) {
2024-10-05 15:16:48 +02:00
if ( ! navigable - > active_window ( ) )
continue ;
2024-04-16 22:04:01 +02:00
queue_global_task ( Task : : Source : : NavigationAndTraversal , * navigable - > active_window ( ) , JS : : create_heap_function ( heap ( ) , [ & ] {
2023-09-05 23:36:20 +02:00
// NOTE: This check is not in the spec but we should not continue navigation if navigable has been destroyed.
2024-04-05 02:46:32 +02:00
if ( navigable - > has_been_destroyed ( ) ) {
completed_change_jobs + + ;
2023-09-05 23:36:20 +02:00
return ;
2024-04-05 02:46:32 +02:00
}
2023-09-05 23:36:20 +02:00
2023-04-13 10:23:59 +03:00
// 1. Let displayedEntry be navigable's active session history entry.
auto displayed_entry = navigable - > active_session_history_entry ( ) ;
// 2. Let targetEntry be navigable's current session history entry.
auto target_entry = navigable - > current_session_history_entry ( ) ;
// 3. Let changingNavigableContinuation be a changing navigable continuation state with:
2024-11-14 06:13:46 +13:00
auto changing_navigable_continuation = vm . heap ( ) . allocate < ChangingNavigableContinuationState > ( ) ;
2024-04-21 15:30:58 +02:00
changing_navigable_continuation - > displayed_document = displayed_entry - > document ( ) ;
changing_navigable_continuation - > target_entry = target_entry ;
changing_navigable_continuation - > navigable = navigable ;
changing_navigable_continuation - > update_only = false ;
changing_navigable_continuation - > populated_target_entry = nullptr ;
changing_navigable_continuation - > populated_cloned_target_session_history_entry = false ;
2023-04-13 10:23:59 +03:00
// 4. If displayedEntry is targetEntry and targetEntry's document state's reload pending is false, then:
2024-04-05 16:02:52 +02:00
if ( synchronous_navigation = = SynchronousNavigation : : Yes & & ! target_entry - > document_state ( ) - > reload_pending ( ) ) {
2023-04-13 10:23:59 +03:00
// 1. Set changingNavigableContinuation's update-only to true.
2024-04-21 15:30:58 +02:00
changing_navigable_continuation - > update_only = true ;
2023-04-13 10:23:59 +03:00
// 2. Enqueue changingNavigableContinuation on changingNavigableContinuations.
changing_navigable_continuations . enqueue ( move ( changing_navigable_continuation ) ) ;
// 3. Abort these steps.
return ;
}
2024-03-28 12:19:57 +01:00
// 5. Switch on navigationType:
if ( navigation_type . has_value ( ) ) {
switch ( navigation_type . value ( ) ) {
case Bindings : : NavigationType : : Reload :
// - "reload": Assert: targetEntry's document state's reload pending is true.
VERIFY ( target_entry - > document_state ( ) - > reload_pending ( ) ) ;
break ;
case Bindings : : NavigationType : : Traverse :
// - "traverse": Assert: targetEntry's document state's ever populated is true.
VERIFY ( target_entry - > document_state ( ) - > ever_populated ( ) ) ;
break ;
case Bindings : : NavigationType : : Replace :
// FIXME: Add ever populated check
// - "replace": Assert: targetEntry's step is displayedEntry's step and targetEntry's document state's ever populated is false.
VERIFY ( target_entry - > step ( ) = = displayed_entry - > step ( ) ) ;
break ;
case Bindings : : NavigationType : : Push :
// FIXME: Add ever populated check, and fix the bug where top level traversable's step is not updated when a child navigable navigates
// - "push": Assert: targetEntry's step is displayedEntry's step + 1 and targetEntry's document state's ever populated is false.
VERIFY ( target_entry - > step ( ) . get < int > ( ) > displayed_entry - > step ( ) . get < int > ( ) ) ;
break ;
}
}
// 6. Let oldOrigin be targetEntry's document state's origin.
2024-03-27 15:59:12 +01:00
auto old_origin = target_entry - > document_state ( ) - > origin ( ) ;
2023-04-13 10:23:59 +03:00
2024-10-13 15:03:40 +13:00
// 7. If all of the following are true:
// * navigable is not traversable;
// * targetEntry is not navigable's current session history entry; and
// * oldOrigin is the same as navigable's current session history entry's document state's origin,
// then:
if ( ! navigable - > is_traversable ( )
& & target_entry ! = navigable - > current_session_history_entry ( )
& & old_origin = = navigable - > current_session_history_entry ( ) - > document_state ( ) - > origin ( ) ) {
// 1. Assert: userInvolvementForNavigateEvents is not null.
VERIFY ( user_involvement_for_navigate_events . has_value ( ) ) ;
// 2. Let navigation be navigable's active window's navigation API.
auto navigation = active_window ( ) - > navigation ( ) ;
// 3. Fire a traverse navigate event at navigation given targetEntry and userInvolvementForNavigateEvents.
navigation - > fire_a_traverse_navigate_event ( * target_entry , * user_involvement_for_navigate_events ) ;
}
2024-03-28 12:19:57 +01:00
2024-04-12 20:13:54 +02:00
auto after_document_populated = [ old_origin , changing_navigable_continuation , & changing_navigable_continuations , & vm , & navigable ] ( bool populated_cloned_target_she , JS : : NonnullGCPtr < SessionHistoryEntry > target_entry ) mutable {
2024-04-21 15:30:58 +02:00
changing_navigable_continuation - > populated_target_entry = target_entry ;
changing_navigable_continuation - > populated_cloned_target_session_history_entry = populated_cloned_target_she ;
2024-04-12 20:13:54 +02:00
2023-04-13 10:23:59 +03:00
// 1. If targetEntry's document is null, then set changingNavigableContinuation's update-only to true.
2024-03-27 16:14:52 +01:00
if ( ! target_entry - > document ( ) ) {
2024-04-21 15:30:58 +02:00
changing_navigable_continuation - > update_only = true ;
2023-04-13 10:23:59 +03:00
}
2023-09-27 22:59:57 -06:00
else {
// 2. If targetEntry's document's origin is not oldOrigin, then set targetEntry's classic history API state to StructuredSerializeForStorage(null).
2024-03-27 16:14:52 +01:00
if ( target_entry - > document ( ) - > origin ( ) ! = old_origin ) {
2024-03-27 15:59:12 +01:00
target_entry - > set_classic_history_api_state ( MUST ( structured_serialize_for_storage ( vm , JS : : js_null ( ) ) ) ) ;
2023-09-27 22:59:57 -06:00
}
2023-04-13 10:23:59 +03:00
2023-09-27 22:59:57 -06:00
// 3. If all of the following are true:
// - navigable's parent is null;
// - targetEntry's document's browsing context is not an auxiliary browsing context whose opener browsing context is non-null; and
// - targetEntry's document's origin is not oldOrigin,
// then set targetEntry's document state's navigable target name to the empty string.
2024-10-15 00:03:34 +13:00
if ( navigable - > parent ( ) = = nullptr
& & ! ( target_entry - > document ( ) - > browsing_context ( ) - > is_auxiliary ( ) & & target_entry - > document ( ) - > browsing_context ( ) - > opener_browsing_context ( ) ! = nullptr )
2024-03-27 15:59:12 +01:00
& & target_entry - > document_state ( ) - > origin ( ) ! = old_origin ) {
target_entry - > document_state ( ) - > set_navigable_target_name ( String { } ) ;
2023-09-27 22:59:57 -06:00
}
}
2023-04-13 10:23:59 +03:00
// 4. Enqueue changingNavigableContinuation on changingNavigableContinuations.
changing_navigable_continuations . enqueue ( move ( changing_navigable_continuation ) ) ;
} ;
2024-03-28 12:19:57 +01:00
// 8. If targetEntry's document is null, or targetEntry's document state's reload pending is true, then:
2024-03-27 16:14:52 +01:00
if ( ! target_entry - > document ( ) | | target_entry - > document_state ( ) - > reload_pending ( ) ) {
2023-04-13 10:23:59 +03:00
// FIXME: 1. Let navTimingType be "back_forward" if targetEntry's document is null; otherwise "reload".
2023-09-21 13:47:19 -06:00
// 2. Let targetSnapshotParams be the result of snapshotting target snapshot params given navigable.
auto target_snapshot_params = navigable - > snapshot_target_snapshot_params ( ) ;
2023-04-13 10:23:59 +03:00
// 3. Let potentiallyTargetSpecificSourceSnapshotParams be sourceSnapshotParams.
Optional < SourceSnapshotParams > potentially_target_specific_source_snapshot_params = source_snapshot_params ;
2023-08-25 20:34:50 -06:00
// 4. If potentiallyTargetSpecificSourceSnapshotParams is null, then set it to the result of snapshotting source snapshot params given navigable's active document.
2023-04-13 10:23:59 +03:00
if ( ! potentially_target_specific_source_snapshot_params . has_value ( ) ) {
2023-08-25 20:34:50 -06:00
potentially_target_specific_source_snapshot_params = navigable - > active_document ( ) - > snapshot_source_snapshot_params ( ) ;
2023-04-13 10:23:59 +03:00
}
// 5. Set targetEntry's document state's reload pending to false.
2024-03-27 15:59:12 +01:00
target_entry - > document_state ( ) - > set_reload_pending ( false ) ;
2023-04-13 10:23:59 +03:00
2023-08-14 15:07:57 +02:00
// 6. Let allowPOST be targetEntry's document state's reload pending.
2024-03-27 15:59:12 +01:00
auto allow_POST = target_entry - > document_state ( ) - > reload_pending ( ) ;
2023-04-13 10:23:59 +03:00
2024-04-12 20:13:54 +02:00
// https://github.com/whatwg/html/issues/9869
// Reloading requires population of the active session history entry, making it inactive.
// This results in a situation where tasks that unload the previous document and activate a new
// document cannot run. To resolve this, the target entry is cloned before it is populated.
// After the unloading of the previous document is completed, all fields potentially affected by the
// population are copied from the cloned target entry to the actual target entry.
auto populated_target_entry = target_entry - > clone ( ) ;
2023-04-13 10:23:59 +03:00
// 7. In parallel, attempt to populate the history entry's document for targetEntry, given navigable, potentiallyTargetSpecificSourceSnapshotParams,
// targetSnapshotParams, with allowPOST set to allowPOST and completionSteps set to queue a global task on the navigation and traversal task source given
// navigable's active window to run afterDocumentPopulated.
2024-10-31 02:39:29 +13:00
Platform : : EventLoopPlugin : : the ( ) . deferred_invoke ( JS : : create_heap_function ( this - > heap ( ) , [ populated_target_entry , potentially_target_specific_source_snapshot_params , target_snapshot_params , this , allow_POST , navigable , after_document_populated = JS : : create_heap_function ( this - > heap ( ) , move ( after_document_populated ) ) ] {
2024-08-18 15:41:50 +12:00
navigable - > populate_session_history_entry_document ( populated_target_entry , * potentially_target_specific_source_snapshot_params , target_snapshot_params , { } , Empty { } , CSPNavigationType : : Other , allow_POST , JS : : create_heap_function ( this - > heap ( ) , [ this , after_document_populated , populated_target_entry ] ( ) mutable {
2024-10-05 15:16:48 +02:00
VERIFY ( active_window ( ) ) ;
2024-04-16 22:04:01 +02:00
queue_global_task ( Task : : Source : : NavigationAndTraversal , * active_window ( ) , JS : : create_heap_function ( this - > heap ( ) , [ after_document_populated , populated_target_entry ] ( ) mutable {
2024-04-24 00:38:28 +02:00
after_document_populated - > function ( ) ( true , populated_target_entry ) ;
2024-04-16 22:04:01 +02:00
} ) ) ;
2024-08-18 15:41:50 +12:00
} ) )
2023-11-03 19:18:18 -06:00
. release_value_but_fixme_should_propagate_errors ( ) ;
2024-10-31 02:39:29 +13:00
} ) ) ;
2023-04-13 10:23:59 +03:00
}
// Otherwise, run afterDocumentPopulated immediately.
else {
2024-04-12 20:13:54 +02:00
after_document_populated ( false , * target_entry ) ;
2023-04-13 10:23:59 +03:00
}
2024-04-16 22:04:01 +02:00
} ) ) ;
2023-04-13 10:23:59 +03:00
}
2024-10-31 05:23:43 +13:00
auto check_if_document_population_tasks_completed = JS : : create_heap_function ( heap ( ) , [ & ] {
2024-04-10 18:24:18 +02:00
return changing_navigable_continuations . size ( ) + completed_change_jobs = = total_change_jobs ;
} ) ;
2024-04-10 19:10:10 +02:00
if ( synchronous_navigation = = SynchronousNavigation : : Yes ) {
// NOTE: Synchronous navigation should never require document population, so it is safe to process only NavigationAndTraversal source.
2024-10-31 05:23:43 +13:00
main_thread_event_loop ( ) . spin_processing_tasks_with_source_until ( Task : : Source : : NavigationAndTraversal , check_if_document_population_tasks_completed ) ;
2024-04-10 19:10:10 +02:00
} else {
// NOTE: Process all task sources while waiting because reloading or back/forward navigation might require fetching to populate a document.
2024-10-31 05:23:43 +13:00
main_thread_event_loop ( ) . spin_until ( check_if_document_population_tasks_completed ) ;
2024-04-10 19:10:10 +02:00
}
2023-11-03 19:18:18 -06:00
// 13. Let navigablesThatMustWaitBeforeHandlingSyncNavigation be an empty set.
2024-08-06 14:35:28 +02:00
HashTable < JS : : NonnullGCPtr < Navigable > > navigables_that_must_wait_before_handling_sync_navigation ;
2023-04-13 10:23:59 +03:00
2023-11-03 19:18:18 -06:00
// 14. While completedChangeJobs does not equal totalChangeJobs:
2024-04-10 18:24:18 +02:00
while ( ! changing_navigable_continuations . is_empty ( ) ) {
2023-11-03 19:18:18 -06:00
// NOTE: Synchronous navigations that are intended to take place before this traversal jump the queue at this point,
// so they can be added to the correct place in traversable's session history entries before this traversal
// potentially unloads their document. More details can be found here (https://html.spec.whatwg.org/multipage/browsing-the-web.html#sync-navigation-steps-queue-jumping-examples)
// 1. If traversable's running nested apply history step is false, then:
if ( ! m_running_nested_apply_history_step ) {
// 1. While traversable's session history traversal queue's algorithm set contains one or more synchronous
// navigation steps with a target navigable not contained in navigablesThatMustWaitBeforeHandlingSyncNavigation:
// 1. Let steps be the first item in traversable's session history traversal queue's algorithm set
// that is synchronous navigation steps with a target navigable not contained in navigablesThatMustWaitBeforeHandlingSyncNavigation.
// 2. Remove steps from traversable's session history traversal queue's algorithm set.
2024-04-10 04:43:51 +02:00
for ( auto entry = m_session_history_traversal_queue - > first_synchronous_navigation_steps_with_target_navigable_not_contained_in ( navigables_that_must_wait_before_handling_sync_navigation ) ;
entry ;
entry = m_session_history_traversal_queue - > first_synchronous_navigation_steps_with_target_navigable_not_contained_in ( navigables_that_must_wait_before_handling_sync_navigation ) ) {
2023-11-03 19:18:18 -06:00
// 3. Set traversable's running nested apply history step to true.
m_running_nested_apply_history_step = true ;
// 4. Run steps.
2024-04-10 04:43:51 +02:00
entry - > execute_steps ( ) ;
2023-11-03 19:18:18 -06:00
// 5. Set traversable's running nested apply history step to false.
m_running_nested_apply_history_step = false ;
}
}
2023-04-13 10:23:59 +03:00
// 2. Let changingNavigableContinuation be the result of dequeuing from changingNavigableContinuations.
auto changing_navigable_continuation = changing_navigable_continuations . dequeue ( ) ;
// 3. If changingNavigableContinuation is nothing, then continue.
// 4. Let displayedDocument be changingNavigableContinuation's displayed document.
2024-04-21 15:30:58 +02:00
auto displayed_document = changing_navigable_continuation - > displayed_document ;
2023-04-13 10:23:59 +03:00
// 5. Let targetEntry be changingNavigableContinuation's target entry.
2024-04-21 15:30:58 +02:00
JS : : GCPtr < SessionHistoryEntry > const populated_target_entry = changing_navigable_continuation - > populated_target_entry ;
2023-04-13 10:23:59 +03:00
// 6. Let navigable be changingNavigableContinuation's navigable.
2024-04-21 15:30:58 +02:00
auto navigable = changing_navigable_continuation - > navigable ;
2023-04-13 10:23:59 +03:00
2023-09-05 23:36:20 +02:00
// NOTE: This check is not in the spec but we should not continue navigation if navigable has been destroyed.
if ( navigable - > has_been_destroyed ( ) )
continue ;
2024-10-14 07:45:19 +02:00
// AD-HOC: We re-compute targetStep here, since it might have changed since the last time we computed it.
// This can happen if navigables are destroyed while we wait for tasks to complete.
target_step = get_the_used_step ( step ) ;
2024-03-31 23:42:51 +02:00
// 7. Let (scriptHistoryLength, scriptHistoryIndex) be the result of getting the history object length and index given traversable and targetStep.
2023-09-23 22:59:27 +02:00
auto history_object_length_and_index = get_the_history_object_length_and_index ( target_step ) ;
auto script_history_length = history_object_length_and_index . script_history_length ;
auto script_history_index = history_object_length_and_index . script_history_index ;
2023-04-13 10:23:59 +03:00
2024-03-31 23:42:51 +02:00
// 8. Append navigable to navigablesThatMustWaitBeforeHandlingSyncNavigation.
2024-08-06 14:35:28 +02:00
navigables_that_must_wait_before_handling_sync_navigation . set ( * navigable ) ;
2023-04-13 10:23:59 +03:00
2024-03-31 23:42:51 +02:00
// 9. Let entriesForNavigationAPI be the result of getting session history entries for the navigation API given navigable and targetStep.
2023-09-27 22:59:57 -06:00
auto entries_for_navigation_api = get_session_history_entries_for_the_navigation_api ( * navigable , target_step ) ;
2024-11-08 11:06:33 +00:00
// NOTE: Steps 10 and 11 come after step 12.
2024-03-31 23:42:51 +02:00
// 12. In both cases, let afterPotentialUnloads be the following steps:
2024-04-21 15:30:58 +02:00
bool const update_only = changing_navigable_continuation - > update_only ;
JS : : GCPtr < SessionHistoryEntry > const target_entry = changing_navigable_continuation - > target_entry ;
bool const populated_cloned_target_session_history_entry = changing_navigable_continuation - > populated_cloned_target_session_history_entry ;
2024-07-21 23:09:20 +01:00
auto after_potential_unload = JS : : create_heap_function ( this - > heap ( ) , [ navigable , update_only , target_entry , populated_target_entry , populated_cloned_target_session_history_entry , displayed_document , & completed_change_jobs , script_history_length , script_history_index , entries_for_navigation_api = move ( entries_for_navigation_api ) , & heap = this - > heap ( ) , navigation_type ] {
2024-04-21 12:49:58 +02:00
if ( populated_cloned_target_session_history_entry ) {
target_entry - > set_document_state ( populated_target_entry - > document_state ( ) ) ;
target_entry - > set_url ( populated_target_entry - > url ( ) ) ;
target_entry - > set_classic_history_api_state ( populated_target_entry - > classic_history_api_state ( ) ) ;
2024-04-12 20:13:54 +02:00
}
2024-11-08 11:06:33 +00:00
// 1. Let previousEntry be navigable's active session history entry.
JS : : GCPtr < SessionHistoryEntry > const previous_entry = navigable - > active_session_history_entry ( ) ;
// 2. If changingNavigableContinuation's update-only is false, then activate history entry targetEntry for navigable.
2024-04-21 12:49:58 +02:00
if ( ! update_only )
navigable - > activate_history_entry ( * target_entry ) ;
2024-03-31 23:42:51 +02:00
2024-11-08 11:06:33 +00:00
// 3. Let updateDocument be an algorithm step which performs update document for history step application given
2024-03-31 23:42:51 +02:00
// targetEntry's document, targetEntry, changingNavigableContinuation's update-only, scriptHistoryLength,
2024-11-08 11:06:33 +00:00
// scriptHistoryIndex, navigationType, entriesForNavigationAPI, and previousEntry.
auto update_document = [ script_history_length , script_history_index , entries_for_navigation_api = move ( entries_for_navigation_api ) , target_entry , update_only , navigation_type , previous_entry ] {
target_entry - > document ( ) - > update_for_history_step_application ( * target_entry , update_only , script_history_length , script_history_index , navigation_type , entries_for_navigation_api , previous_entry ) ;
2024-04-21 15:07:59 +02:00
} ;
2023-04-13 10:23:59 +03:00
2024-03-31 23:42:51 +02:00
// 3. If targetEntry's document is equal to displayedDocument, then perform updateDocument.
if ( target_entry - > document ( ) . ptr ( ) = = displayed_document . ptr ( ) ) {
2023-09-27 22:59:57 -06:00
update_document ( ) ;
}
// 5. Otherwise, queue a global task on the navigation and traversal task source given targetEntry's document's relevant global object to perform updateDocument
else {
2024-04-21 15:07:59 +02:00
queue_global_task ( Task : : Source : : NavigationAndTraversal , relevant_global_object ( * target_entry - > document ( ) ) , JS : : create_heap_function ( heap , move ( update_document ) ) ) ;
2023-09-27 22:59:57 -06:00
}
2023-04-13 10:23:59 +03:00
2023-09-27 22:59:57 -06:00
// 6. Increment completedChangeJobs.
2023-04-13 10:23:59 +03:00
completed_change_jobs + + ;
} ) ;
2024-03-31 23:42:51 +02:00
// 10. If changingNavigableContinuation's update-only is true, or targetEntry's document is displayedDocument, then:
2024-04-21 15:30:58 +02:00
if ( changing_navigable_continuation - > update_only | | populated_target_entry - > document ( ) . ptr ( ) = = displayed_document . ptr ( ) ) {
2024-03-31 23:42:51 +02:00
// 1. Set the ongoing navigation for navigable to null.
navigable - > set_ongoing_navigation ( { } ) ;
// 2. Queue a global task on the navigation and traversal task source given navigable's active window to perform afterPotentialUnloads.
2024-10-05 15:16:48 +02:00
VERIFY ( navigable - > active_window ( ) ) ;
2024-04-16 22:04:01 +02:00
queue_global_task ( Task : : Source : : NavigationAndTraversal , * navigable - > active_window ( ) , after_potential_unload ) ;
2024-03-31 23:42:51 +02:00
}
// 11. Otherwise:
else {
// 1. Assert: navigationType is not null.
VERIFY ( navigation_type . has_value ( ) ) ;
// 2. Deactivate displayedDocument, given userNavigationInvolvement, targetEntry, navigationType, and afterPotentialUnloads.
2024-04-16 22:04:01 +02:00
deactivate_a_document_for_cross_document_navigation ( * displayed_document , user_involvement_for_navigate_events , * populated_target_entry , after_potential_unload ) ;
2024-03-31 23:42:51 +02:00
}
2023-04-13 10:23:59 +03:00
}
2024-10-31 05:23:43 +13:00
main_thread_event_loop ( ) . spin_processing_tasks_with_source_until ( Task : : Source : : NavigationAndTraversal , JS : : create_heap_function ( heap ( ) , [ & ] {
2024-04-10 18:24:18 +02:00
return completed_change_jobs = = total_change_jobs ;
2024-10-31 05:23:43 +13:00
} ) ) ;
2024-04-10 18:24:18 +02:00
2023-11-03 19:18:18 -06:00
// 15. Let totalNonchangingJobs be the size of nonchangingNavigablesThatStillNeedUpdates.
auto total_non_changing_jobs = non_changing_navigables_that_still_need_updates . size ( ) ;
// 16. Let completedNonchangingJobs be 0.
2024-04-07 16:32:33 -07:00
IGNORE_USE_IN_ESCAPING_LAMBDA auto completed_non_changing_jobs = 0u ;
2023-11-03 19:18:18 -06:00
2024-11-03 11:22:22 +01:00
// AD-HOC: We re-compute targetStep here, since it might have changed since the last time we computed it.
// This can happen if navigables are destroyed while we wait for tasks to complete.
target_step = get_the_used_step ( step ) ;
2023-11-03 19:18:18 -06:00
// 17. Let (scriptHistoryLength, scriptHistoryIndex) be the result of getting the history object length and index given traversable and targetStep.
auto length_and_index = get_the_history_object_length_and_index ( target_step ) ;
2024-04-07 16:32:33 -07:00
IGNORE_USE_IN_ESCAPING_LAMBDA auto script_history_length = length_and_index . script_history_length ;
IGNORE_USE_IN_ESCAPING_LAMBDA auto script_history_index = length_and_index . script_history_index ;
2023-11-03 19:18:18 -06:00
// 18. For each navigable of nonchangingNavigablesThatStillNeedUpdates, queue a global task on the navigation and traversal task source given navigable's active window to run the steps:
for ( auto & navigable : non_changing_navigables_that_still_need_updates ) {
2024-03-27 14:53:13 +01:00
if ( navigable - > has_been_destroyed ( ) ) {
+ + completed_non_changing_jobs ;
continue ;
}
2024-10-05 15:16:48 +02:00
VERIFY ( navigable - > active_window ( ) ) ;
2024-04-16 22:04:01 +02:00
queue_global_task ( Task : : Source : : NavigationAndTraversal , * navigable - > active_window ( ) , JS : : create_heap_function ( heap ( ) , [ & ] {
2023-11-03 19:18:18 -06:00
// NOTE: This check is not in the spec but we should not continue navigation if navigable has been destroyed.
if ( navigable - > has_been_destroyed ( ) ) {
+ + completed_non_changing_jobs ;
return ;
}
2023-04-13 10:23:59 +03:00
2023-11-03 19:18:18 -06:00
// 1. Let document be navigable's active document.
auto document = navigable - > active_document ( ) ;
2023-04-13 10:23:59 +03:00
2023-11-03 19:18:18 -06:00
// 2. Set document's history object's index to scriptHistoryIndex.
document - > history ( ) - > m_index = script_history_index ;
2023-04-13 10:23:59 +03:00
2023-11-03 19:18:18 -06:00
// 3. Set document's history object's length to scriptHistoryLength.
document - > history ( ) - > m_length = script_history_length ;
// 4. Increment completedNonchangingJobs.
+ + completed_non_changing_jobs ;
2024-04-16 22:04:01 +02:00
} ) ) ;
2023-11-03 19:18:18 -06:00
}
2023-04-13 10:23:59 +03:00
2023-11-03 19:18:18 -06:00
// 19. Wait for completedNonchangingJobs to equal totalNonchangingJobs.
// AD-HOC: Since currently populate_session_history_entry_document does not run in parallel
// we call spin_until to interrupt execution of this function and let document population
// to complete.
2024-10-31 05:23:43 +13:00
main_thread_event_loop ( ) . spin_processing_tasks_with_source_until ( Task : : Source : : NavigationAndTraversal , JS : : create_heap_function ( heap ( ) , [ & ] {
2023-11-03 19:18:18 -06:00
return completed_non_changing_jobs = = total_non_changing_jobs ;
2024-10-31 05:23:43 +13:00
} ) ) ;
2023-04-13 10:23:59 +03:00
// 20. Set traversable's current session history step to targetStep.
m_current_session_history_step = target_step ;
2023-09-27 22:59:57 -06:00
2024-04-13 23:12:55 +02:00
// Not in the spec:
auto back_enabled = m_current_session_history_step > 0 ;
VERIFY ( m_session_history_entries . size ( ) > 0 ) ;
2024-04-16 12:18:48 +02:00
auto forward_enabled = can_go_forward ( ) ;
2024-04-13 23:12:55 +02:00
page ( ) . client ( ) . page_did_update_navigation_buttons_state ( back_enabled , forward_enabled ) ;
2024-04-14 10:27:20 +02:00
page ( ) . client ( ) . page_did_change_url ( current_session_history_entry ( ) - > url ( ) ) ;
2023-09-27 22:59:57 -06:00
// 21. Return "applied".
return HistoryStepResult : : Applied ;
2023-09-27 22:56:11 -06:00
}
2024-10-02 23:56:26 +01:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#checking-if-unloading-is-canceled
TraversableNavigable : : CheckIfUnloadingIsCanceledResult TraversableNavigable : : check_if_unloading_is_canceled (
Vector < JS : : Handle < Navigable > > navigables_that_need_before_unload ,
JS : : GCPtr < TraversableNavigable > traversable ,
Optional < int > target_step ,
Optional < UserNavigationInvolvement > user_involvement_for_navigate_events )
{
// 1. Let documentsToFireBeforeunload be the active document of each item in navigablesThatNeedBeforeUnload.
Vector < JS : : Handle < DOM : : Document > > documents_to_fire_beforeunload ;
for ( auto & navigable : navigables_that_need_before_unload )
documents_to_fire_beforeunload . append ( navigable - > active_document ( ) ) ;
// 2. Let unloadPromptShown be false.
auto unload_prompt_shown = false ;
// 3. Let finalStatus be "continue".
auto final_status = CheckIfUnloadingIsCanceledResult : : Continue ;
// 4. If traversable was given, then:
if ( traversable ) {
// 1. Assert: targetStep and userInvolvementForNavigateEvent were given.
// NOTE: This assertion is enforced by the caller.
// 2. Let targetEntry be the result of getting the target history entry given traversable and targetStep.
auto target_entry = traversable - > get_the_target_history_entry ( target_step . value ( ) ) ;
// 3. If targetEntry is not traversable's current session history entry, and targetEntry's document state's origin is the same as
// traversable's current session history entry's document state's origin, then:
if ( target_entry ! = traversable - > current_session_history_entry ( ) & & target_entry - > document_state ( ) - > origin ( ) ! = traversable - > current_session_history_entry ( ) - > document_state ( ) - > origin ( ) ) {
// 1. Assert: userInvolvementForNavigateEvent is not null.
VERIFY ( user_involvement_for_navigate_events . has_value ( ) ) ;
// 2. Let eventsFired be false.
auto events_fired = false ;
// 3. Let needsBeforeunload be true if navigablesThatNeedBeforeUnload contains traversable; otherwise false.
auto it = navigables_that_need_before_unload . find_if ( [ & traversable ] ( JS : : Handle < Navigable > navigable ) {
return navigable . ptr ( ) = = traversable . ptr ( ) ;
} ) ;
auto needs_beforeunload = it ! = navigables_that_need_before_unload . end ( ) ;
// 4. If needsBeforeunload is true, then remove traversable's active document from documentsToFireBeforeunload.
if ( needs_beforeunload ) {
documents_to_fire_beforeunload . remove_first_matching ( [ & ] ( auto & document ) {
return document . ptr ( ) = = traversable - > active_document ( ) . ptr ( ) ;
} ) ;
}
// 5. Queue a global task on the navigation and traversal task source given traversable's active window to perform the following steps:
2024-10-05 15:16:48 +02:00
VERIFY ( traversable - > active_window ( ) ) ;
2024-10-02 23:56:26 +01:00
queue_global_task ( Task : : Source : : NavigationAndTraversal , * traversable - > active_window ( ) , JS : : create_heap_function ( heap ( ) , [ & ] {
// 1. if needsBeforeunload is true, then:
if ( needs_beforeunload ) {
// 1. Let (unloadPromptShownForThisDocument, unloadPromptCanceledByThisDocument) be the result of running the steps to fire beforeunload given traversable's active document and false.
auto [ unload_prompt_shown_for_this_document , unload_prompt_canceled_by_this_document ] = traversable - > active_document ( ) - > steps_to_fire_beforeunload ( false ) ;
// 2. If unloadPromptShownForThisDocument is true, then set unloadPromptShown to true.
if ( unload_prompt_shown_for_this_document )
unload_prompt_shown = true ;
// 3. If unloadPromptCanceledByThisDocument is true, then set finalStatus to "canceled-by-beforeunload".
if ( unload_prompt_canceled_by_this_document )
final_status = CheckIfUnloadingIsCanceledResult : : CanceledByBeforeUnload ;
}
// 2. If finalStatus is "canceled-by-beforeunload", then abort these steps.
if ( final_status = = CheckIfUnloadingIsCanceledResult : : CanceledByBeforeUnload )
return ;
// 3. Let navigation be traversable's active window's navigation API.
2024-10-05 15:16:48 +02:00
VERIFY ( traversable - > active_window ( ) ) ;
2024-10-02 23:56:26 +01:00
auto navigation = traversable - > active_window ( ) - > navigation ( ) ;
// 4. Let navigateEventResult be the result of firing a traverse navigate event at navigation given targetEntry and userInvolvementForNavigateEvent.
VERIFY ( target_entry ) ;
auto navigate_event_result = navigation - > fire_a_traverse_navigate_event ( * target_entry , * user_involvement_for_navigate_events ) ;
// 5. If navigateEventResult is false, then set finalStatus to "canceled-by-navigate".
if ( ! navigate_event_result )
final_status = CheckIfUnloadingIsCanceledResult : : CanceledByNavigate ;
// 6. Set eventsFired to true.
events_fired = true ;
} ) ) ;
// 6. Wait for eventsFired to be true.
2024-10-31 05:23:43 +13:00
main_thread_event_loop ( ) . spin_until ( JS : : create_heap_function ( heap ( ) , [ & ] {
2024-10-02 23:56:26 +01:00
return events_fired ;
2024-10-31 05:23:43 +13:00
} ) ) ;
2024-10-02 23:56:26 +01:00
// 7. If finalStatus is not "continue", then return finalStatus.
if ( final_status ! = CheckIfUnloadingIsCanceledResult : : Continue )
return final_status ;
}
}
// 5. Let totalTasks be the size of documentsThatNeedBeforeunload.
auto total_tasks = documents_to_fire_beforeunload . size ( ) ;
// 6. Let completedTasks be 0.
size_t completed_tasks = 0 ;
// 7. For each document of documents, queue a global task on the navigation and traversal task source given document's relevant global object to run the steps:
for ( auto & document : documents_to_fire_beforeunload ) {
queue_global_task ( Task : : Source : : NavigationAndTraversal , relevant_global_object ( * document ) , JS : : create_heap_function ( heap ( ) , [ & ] {
// 1. Let (unloadPromptShownForThisDocument, unloadPromptCanceledByThisDocument) be the result of running the steps to fire beforeunload given document and unloadPromptShown.
auto [ unload_prompt_shown_for_this_document , unload_prompt_canceled_by_this_document ] = document - > steps_to_fire_beforeunload ( unload_prompt_shown ) ;
// 2. If unloadPromptShownForThisDocument is true, then set unloadPromptShown to true.
if ( unload_prompt_shown_for_this_document )
unload_prompt_shown = true ;
// 3. If unloadPromptCanceledByThisDocument is true, then set finalStatus to "canceled-by-beforeunload".
if ( unload_prompt_canceled_by_this_document )
final_status = CheckIfUnloadingIsCanceledResult : : CanceledByBeforeUnload ;
// 4. Increment completedTasks.
completed_tasks + + ;
} ) ) ;
}
// 8. Wait for completedTasks to be totalTasks.
2024-10-31 05:23:43 +13:00
main_thread_event_loop ( ) . spin_until ( JS : : create_heap_function ( heap ( ) , [ & ] {
2024-10-02 23:56:26 +01:00
return completed_tasks = = total_tasks ;
2024-10-31 05:23:43 +13:00
} ) ) ;
2024-10-02 23:56:26 +01:00
// 9. Return finalStatus.
return final_status ;
}
TraversableNavigable : : CheckIfUnloadingIsCanceledResult TraversableNavigable : : check_if_unloading_is_canceled ( Vector < JS : : Handle < Navigable > > navigables_that_need_before_unload )
{
return check_if_unloading_is_canceled ( navigables_that_need_before_unload , { } , { } , { } ) ;
}
2023-09-27 22:56:11 -06:00
Vector < JS : : NonnullGCPtr < SessionHistoryEntry > > TraversableNavigable : : get_session_history_entries_for_the_navigation_api ( JS : : NonnullGCPtr < Navigable > navigable , int target_step )
{
// 1. Let rawEntries be the result of getting session history entries for navigable.
auto raw_entries = navigable - > get_session_history_entries ( ) ;
if ( raw_entries . is_empty ( ) )
return { } ;
// 2. Let entriesForNavigationAPI be a new empty list.
Vector < JS : : NonnullGCPtr < SessionHistoryEntry > > entries_for_navigation_api ;
// 3. Let startingIndex be the index of the session history entry in rawEntries who has the greatest step less than or equal to targetStep.
// FIXME: Use min/max_element algorithm or some such here
int starting_index = 0 ;
auto max_step = 0 ;
for ( auto i = 0u ; i < raw_entries . size ( ) ; + + i ) {
auto const & entry = raw_entries [ i ] ;
2024-03-27 15:59:12 +01:00
if ( entry - > step ( ) . has < int > ( ) ) {
auto step = entry - > step ( ) . get < int > ( ) ;
2023-09-27 22:56:11 -06:00
if ( step < = target_step & & step > max_step ) {
starting_index = static_cast < int > ( i ) ;
}
}
}
// 4. Append rawEntries[startingIndex] to entriesForNavigationAPI.
entries_for_navigation_api . append ( raw_entries [ starting_index ] ) ;
// 5. Let startingOrigin be rawEntries[startingIndex]'s document state's origin.
2024-03-27 15:59:12 +01:00
auto starting_origin = raw_entries [ starting_index ] - > document_state ( ) - > origin ( ) ;
2023-09-27 22:56:11 -06:00
// 6. Let i be startingIndex − 1.
auto i = starting_index - 1 ;
// 7. While i > 0:
while ( i > 0 ) {
auto & entry = raw_entries [ static_cast < unsigned > ( i ) ] ;
// 1. If rawEntries[i]'s document state's origin is not same origin with startingOrigin, then break.
2024-03-27 15:59:12 +01:00
auto entry_origin = entry - > document_state ( ) - > origin ( ) ;
2023-09-27 22:56:11 -06:00
if ( starting_origin . has_value ( ) & & entry_origin . has_value ( ) & & ! entry_origin - > is_same_origin ( * starting_origin ) )
break ;
// 2. Prepend rawEntries[i] to entriesForNavigationAPI.
entries_for_navigation_api . prepend ( entry ) ;
// 3. Set i to i − 1.
- - i ;
}
// 8. Set i to startingIndex + 1.
i = starting_index + 1 ;
// 9. While i < rawEntries's size:
while ( i < static_cast < int > ( raw_entries . size ( ) ) ) {
auto & entry = raw_entries [ static_cast < unsigned > ( i ) ] ;
// 1. If rawEntries[i]'s document state's origin is not same origin with startingOrigin, then break.
2024-03-27 15:59:12 +01:00
auto entry_origin = entry - > document_state ( ) - > origin ( ) ;
2023-09-27 22:56:11 -06:00
if ( starting_origin . has_value ( ) & & entry_origin . has_value ( ) & & ! entry_origin - > is_same_origin ( * starting_origin ) )
break ;
// 2. Append rawEntries[i] to entriesForNavigationAPI.
entries_for_navigation_api . append ( entry ) ;
// 3. Set i to i + 1.
+ + i ;
}
// 10. Return entriesForNavigationAPI.
return entries_for_navigation_api ;
2023-04-13 10:23:59 +03:00
}
2023-04-06 12:24:09 +03:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#clear-the-forward-session-history
void TraversableNavigable : : clear_the_forward_session_history ( )
{
// FIXME: 1. Assert: this is running within navigable's session history traversal queue.
// 2. Let step be the navigable's current session history step.
auto step = current_session_history_step ( ) ;
// 3. Let entryLists be the ordered set « navigable's session history entries ».
Vector < Vector < JS : : NonnullGCPtr < SessionHistoryEntry > > & > entry_lists ;
entry_lists . append ( session_history_entries ( ) ) ;
// 4. For each entryList of entryLists:
while ( ! entry_lists . is_empty ( ) ) {
auto & entry_list = entry_lists . take_first ( ) ;
// 1. Remove every session history entry from entryList that has a step greater than step.
entry_list . remove_all_matching ( [ step ] ( auto & entry ) {
2024-03-27 15:59:12 +01:00
return entry - > step ( ) . template get < int > ( ) > step ;
2023-04-06 12:24:09 +03:00
} ) ;
// 2. For each entry of entryList:
for ( auto & entry : entry_list ) {
// 1. For each nestedHistory of entry's document state's nested histories, append nestedHistory's entries list to entryLists.
2024-03-27 15:59:12 +01:00
for ( auto & nested_history : entry - > document_state ( ) - > nested_histories ( ) ) {
2023-04-06 12:24:09 +03:00
entry_lists . append ( nested_history . entries ) ;
}
}
}
}
2024-04-16 12:18:48 +02:00
bool TraversableNavigable : : can_go_forward ( ) const
{
auto step = current_session_history_step ( ) ;
Vector < Vector < JS : : NonnullGCPtr < SessionHistoryEntry > > const & > entry_lists ;
entry_lists . append ( session_history_entries ( ) ) ;
while ( ! entry_lists . is_empty ( ) ) {
auto const & entry_list = entry_lists . take_first ( ) ;
for ( auto const & entry : entry_list ) {
if ( entry - > step ( ) . template get < int > ( ) > step )
return true ;
for ( auto & nested_history : entry - > document_state ( ) - > nested_histories ( ) )
entry_lists . append ( nested_history . entries ) ;
}
}
return false ;
}
2023-04-07 00:03:15 +03:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#traverse-the-history-by-a-delta
2023-09-27 22:59:57 -06:00
void TraversableNavigable : : traverse_the_history_by_delta ( int delta , Optional < DOM : : Document & > source_document )
2023-04-07 00:03:15 +03:00
{
2023-09-27 22:59:57 -06:00
// 1. Let sourceSnapshotParams and initiatorToCheck be null.
Optional < SourceSnapshotParams > source_snapshot_params = { } ;
JS : : GCPtr < Navigable > initiator_to_check = nullptr ;
// 2. Let userInvolvement be "browser UI".
UserNavigationInvolvement user_involvement = UserNavigationInvolvement : : BrowserUI ;
2023-04-07 00:03:15 +03:00
2023-09-27 22:59:57 -06:00
// 1. If sourceDocument is given, then:
if ( source_document . has_value ( ) ) {
// 1. Set sourceSnapshotParams to the result of snapshotting source snapshot params given sourceDocument.
source_snapshot_params = source_document - > snapshot_source_snapshot_params ( ) ;
2023-04-07 00:03:15 +03:00
2023-09-27 22:59:57 -06:00
// 2. Set initiatorToCheck to sourceDocument's node navigable.
initiator_to_check = source_document - > navigable ( ) ;
// 3. Set userInvolvement to "none".
user_involvement = UserNavigationInvolvement : : None ;
}
// 4. Append the following session history traversal steps to traversable:
2024-08-18 17:45:56 +12:00
append_session_history_traversal_steps ( JS : : create_heap_function ( heap ( ) , [ this , delta , source_snapshot_params = move ( source_snapshot_params ) , initiator_to_check , user_involvement ] {
2023-07-25 01:22:57 +02:00
// 1. Let allSteps be the result of getting all used history steps for traversable.
auto all_steps = get_all_used_history_steps ( ) ;
2023-04-07 00:03:15 +03:00
2023-07-25 01:22:57 +02:00
// 2. Let currentStepIndex be the index of traversable's current session history step within allSteps.
auto current_step_index = * all_steps . find_first_index ( current_session_history_step ( ) ) ;
2023-04-07 00:03:15 +03:00
2023-07-25 01:22:57 +02:00
// 3. Let targetStepIndex be currentStepIndex plus delta
auto target_step_index = current_step_index + delta ;
2023-04-07 00:03:15 +03:00
2023-07-25 01:22:57 +02:00
// 4. If allSteps[targetStepIndex] does not exist, then abort these steps.
if ( target_step_index > = all_steps . size ( ) ) {
return ;
}
2023-04-07 00:03:15 +03:00
2023-09-27 22:59:57 -06:00
// 5. Apply the traverse history step allSteps[targetStepIndex] to traversable, given sourceSnapshotParams,
// initiatorToCheck, and userInvolvement.
apply_the_traverse_history_step ( all_steps [ target_step_index ] , source_snapshot_params , initiator_to_check , user_involvement ) ;
2024-08-18 17:45:56 +12:00
} ) ) ;
2023-04-07 00:03:15 +03:00
}
2023-09-03 22:20:32 +02:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#update-for-navigable-creation/destruction
2023-09-27 23:23:00 -06:00
TraversableNavigable : : HistoryStepResult TraversableNavigable : : update_for_navigable_creation_or_destruction ( )
2023-04-07 00:08:39 +03:00
{
2023-09-03 22:20:32 +02:00
// 1. Let step be traversable's current session history step.
auto step = current_session_history_step ( ) ;
2024-03-28 12:19:57 +01:00
// 2. Return the result of applying the history step to traversable given false, null, null, null, and null.
2024-04-05 16:02:52 +02:00
return apply_the_history_step ( step , false , { } , { } , { } , { } , SynchronousNavigation : : No ) ;
2023-09-03 22:20:32 +02:00
}
2023-04-07 00:08:39 +03:00
2023-09-03 22:20:32 +02:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#apply-the-reload-history-step
2023-09-27 23:23:00 -06:00
TraversableNavigable : : HistoryStepResult TraversableNavigable : : apply_the_reload_history_step ( )
2023-09-03 22:20:32 +02:00
{
// 1. Let step be traversable's current session history step.
auto step = current_session_history_step ( ) ;
2024-03-28 12:19:57 +01:00
// 2. Return the result of applying the history step step to traversable given true, null, null, null, and "reload".
2024-04-05 16:02:52 +02:00
return apply_the_history_step ( step , true , { } , { } , { } , Bindings : : NavigationType : : Reload , SynchronousNavigation : : No ) ;
2023-09-03 22:20:32 +02:00
}
2024-04-05 16:02:52 +02:00
TraversableNavigable : : HistoryStepResult TraversableNavigable : : apply_the_push_or_replace_history_step ( int step , HistoryHandlingBehavior history_handling , SynchronousNavigation synchronous_navigation )
2023-09-03 22:20:32 +02:00
{
2024-03-28 12:19:57 +01:00
// 1. Return the result of applying the history step step to traversable given false, null, null, null, and historyHandling.
auto navigation_type = history_handling = = HistoryHandlingBehavior : : Replace ? Bindings : : NavigationType : : Replace : Bindings : : NavigationType : : Push ;
2024-04-05 16:02:52 +02:00
return apply_the_history_step ( step , false , { } , { } , { } , navigation_type , synchronous_navigation ) ;
2023-09-27 22:59:57 -06:00
}
2023-09-27 23:23:00 -06:00
TraversableNavigable : : HistoryStepResult TraversableNavigable : : apply_the_traverse_history_step ( int step , Optional < SourceSnapshotParams > source_snapshot_params , JS : : GCPtr < Navigable > initiator_to_check , UserNavigationInvolvement user_involvement )
2023-09-27 22:59:57 -06:00
{
2024-03-28 12:19:57 +01:00
// 1. Return the result of applying the history step step to traversable given true, sourceSnapshotParams, initiatorToCheck, userInvolvement, and "traverse".
2024-04-05 16:02:52 +02:00
return apply_the_history_step ( step , true , move ( source_snapshot_params ) , initiator_to_check , user_involvement , Bindings : : NavigationType : : Traverse , SynchronousNavigation : : No ) ;
2023-04-07 00:08:39 +03:00
}
2022-12-17 10:37:46 +01:00
// https://html.spec.whatwg.org/multipage/document-sequences.html#close-a-top-level-traversable
void TraversableNavigable : : close_top_level_traversable ( )
{
2024-09-20 15:49:33 +01:00
// 1. If traversable's is closing is true, then return.
2024-10-09 06:36:15 -04:00
if ( is_closing ( ) )
return ;
// 2. Definitely close traversable.
definitely_close_top_level_traversable ( ) ;
}
2024-09-20 15:49:33 +01:00
2024-10-09 06:36:15 -04:00
// https://html.spec.whatwg.org/multipage/document-sequences.html#definitely-close-a-top-level-traversable
void TraversableNavigable : : definitely_close_top_level_traversable ( )
{
VERIFY ( is_top_level_traversable ( ) ) ;
// 1. Let toUnload be traversable's active document's inclusive descendant navigables.
2022-12-17 10:37:46 +01:00
auto to_unload = active_document ( ) - > inclusive_descendant_navigables ( ) ;
2024-10-09 06:36:15 -04:00
// 2. If the result of checking if unloading is canceled for toUnload is true, then return.
2024-10-02 23:56:26 +01:00
if ( check_if_unloading_is_canceled ( to_unload ) ! = CheckIfUnloadingIsCanceledResult : : Continue )
return ;
2022-12-17 10:37:46 +01:00
2024-10-09 06:36:15 -04:00
// 3. Append the following session history traversal steps to traversable:
2024-09-20 15:49:33 +01:00
append_session_history_traversal_steps ( JS : : create_heap_function ( heap ( ) , [ this ] {
// 1. Let afterAllUnloads be an algorithm step which destroys traversable.
auto after_all_unloads = JS : : create_heap_function ( heap ( ) , [ this ] {
destroy_top_level_traversable ( ) ;
} ) ;
2022-12-17 10:37:46 +01:00
2024-09-20 15:49:33 +01:00
// 2. Unload a document and its descendants given traversable's active document, null, and afterAllUnloads.
active_document ( ) - > unload_a_document_and_its_descendants ( { } , after_all_unloads ) ;
} ) ) ;
2022-12-17 10:37:46 +01:00
}
2022-12-17 10:19:35 +01:00
// https://html.spec.whatwg.org/multipage/document-sequences.html#destroy-a-top-level-traversable
void TraversableNavigable : : destroy_top_level_traversable ( )
{
VERIFY ( is_top_level_traversable ( ) ) ;
// 1. Let browsingContext be traversable's active browsing context.
auto browsing_context = active_browsing_context ( ) ;
// 2. For each historyEntry in traversable's session history entries:
for ( auto & history_entry : m_session_history_entries ) {
// 1. Let document be historyEntry's document.
2024-03-27 16:14:52 +01:00
auto document = history_entry - > document ( ) ;
2022-12-17 10:19:35 +01:00
// 2. If document is not null, then destroy document.
if ( document )
document - > destroy ( ) ;
}
// 3. Remove browsingContext.
2024-02-05 03:26:05 -07:00
if ( ! browsing_context ) {
dbgln ( " TraversableNavigable::destroy_top_level_traversable: No browsing context? " ) ;
} else {
browsing_context - > remove ( ) ;
}
2022-12-17 10:19:35 +01:00
2024-02-02 18:00:48 -07:00
// 4. Remove traversable from the user interface (e.g., close or hide its tab in a tabbed browser).
page ( ) . client ( ) . page_did_close_top_level_traversable ( ) ;
2022-12-17 10:19:35 +01:00
// 5. Remove traversable from the user agent's top-level traversable set.
user_agent_top_level_traversable_set ( ) . remove ( this ) ;
2024-02-12 17:36:38 -07:00
// 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 traversables around.
set_has_been_destroyed ( ) ;
all_navigables ( ) . remove ( this ) ;
2022-12-17 10:19:35 +01:00
}
2023-08-20 14:28:33 +02:00
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-same-document-navigation
2024-03-28 12:19:57 +01:00
void finalize_a_same_document_navigation ( JS : : NonnullGCPtr < TraversableNavigable > traversable , JS : : NonnullGCPtr < Navigable > target_navigable , JS : : NonnullGCPtr < SessionHistoryEntry > target_entry , JS : : GCPtr < SessionHistoryEntry > entry_to_replace , HistoryHandlingBehavior history_handling )
2023-08-20 14:28:33 +02:00
{
2023-09-05 23:36:20 +02:00
// NOTE: This is not in the spec but we should not navigate destroyed navigable.
if ( target_navigable - > has_been_destroyed ( ) )
return ;
2023-08-20 14:28:33 +02:00
// FIXME: 1. Assert: this is running on traversable's session history traversal queue.
// 2. If targetNavigable's active session history entry is not targetEntry, then return.
if ( target_navigable - > active_session_history_entry ( ) ! = target_entry ) {
return ;
}
// 3. Let targetStep be null.
Optional < int > target_step ;
// 4. Let targetEntries be the result of getting session history entries for targetNavigable.
auto & target_entries = target_navigable - > get_session_history_entries ( ) ;
// 5. If entryToReplace is null, then:
2024-04-05 17:39:33 +02:00
// FIXME: Checking containment of entryToReplace should not be needed.
// For more details see https://github.com/whatwg/html/issues/10232#issuecomment-2037543137
if ( ! entry_to_replace | | ! target_entries . contains_slow ( JS : : NonnullGCPtr { * entry_to_replace } ) ) {
2023-08-20 14:28:33 +02:00
// 1. Clear the forward session history of traversable.
traversable - > clear_the_forward_session_history ( ) ;
// 2. Set targetStep to traversable's current session history step + 1.
target_step = traversable - > current_session_history_step ( ) + 1 ;
// 3. Set targetEntry's step to targetStep.
2024-03-27 15:59:12 +01:00
target_entry - > set_step ( * target_step ) ;
2023-08-20 14:28:33 +02:00
// 4. Append targetEntry to targetEntries.
target_entries . append ( target_entry ) ;
} else {
// 1. Replace entryToReplace with targetEntry in targetEntries.
* ( target_entries . find ( * entry_to_replace ) ) = target_entry ;
// 2. Set targetEntry's step to entryToReplace's step.
2024-03-27 15:59:12 +01:00
target_entry - > set_step ( entry_to_replace - > step ( ) ) ;
2023-08-20 14:28:33 +02:00
// 3. Set targetStep to traversable's current session history step.
target_step = traversable - > current_session_history_step ( ) ;
}
2024-03-28 12:19:57 +01:00
// 6. Apply the push/replace history step targetStep to traversable given historyHandling.
2024-04-05 16:02:52 +02:00
traversable - > apply_the_push_or_replace_history_step ( * target_step , history_handling , TraversableNavigable : : SynchronousNavigation : : Yes ) ;
2023-08-20 14:28:33 +02:00
}
2023-09-19 20:24:18 +02:00
// https://html.spec.whatwg.org/multipage/interaction.html#system-visibility-state
void TraversableNavigable : : set_system_visibility_state ( VisibilityState visibility_state )
{
if ( m_system_visibility_state = = visibility_state )
return ;
m_system_visibility_state = visibility_state ;
// When a user-agent determines that the system visibility state for
// traversable navigable traversable has changed to newState, it must run the following steps:
2023-09-27 09:29:47 +02:00
// 1. Let navigables be the inclusive descendant navigables of traversable's active document.
2023-09-19 20:24:18 +02:00
auto navigables = active_document ( ) - > inclusive_descendant_navigables ( ) ;
// 2. For each navigable of navigables:
for ( auto & navigable : navigables ) {
// 1. Let document be navigable's active document.
auto document = navigable - > active_document ( ) ;
VERIFY ( document ) ;
// 2. Queue a global task on the user interaction task source given document's relevant global object
// to update the visibility state of document with newState.
2024-04-16 22:04:01 +02:00
queue_global_task ( Task : : Source : : UserInteraction , relevant_global_object ( * document ) , JS : : create_heap_function ( heap ( ) , [ visibility_state , document ] {
2023-09-19 20:24:18 +02:00
document - > update_the_visibility_state ( visibility_state ) ;
2024-04-16 22:04:01 +02:00
} ) ) ;
2023-09-19 20:24:18 +02:00
}
}
2024-04-26 16:59:04 +02:00
// https://html.spec.whatwg.org/multipage/interaction.html#currently-focused-area-of-a-top-level-traversable
JS : : GCPtr < DOM : : Node > TraversableNavigable : : currently_focused_area ( )
{
// 1. If traversable does not have system focus, then return null.
if ( ! is_focused ( ) )
return nullptr ;
// 2. Let candidate be traversable's active document.
auto candidate = active_document ( ) ;
// 3. While candidate's focused area is a navigable container with a non-null content navigable:
// set candidate to the active document of that navigable container's content navigable.
while ( candidate - > focused_element ( )
& & is < HTML : : NavigableContainer > ( candidate - > focused_element ( ) )
& & static_cast < HTML : : NavigableContainer & > ( * candidate - > focused_element ( ) ) . content_navigable ( ) ) {
candidate = static_cast < HTML : : NavigableContainer & > ( * candidate - > focused_element ( ) ) . content_navigable ( ) - > active_document ( ) ;
}
// 4. If candidate's focused area is non-null, set candidate to candidate's focused area.
if ( candidate - > focused_element ( ) ) {
// NOTE: We return right away here instead of assigning to candidate,
// since that would require compromising type safety.
return candidate - > focused_element ( ) ;
}
// 5. Return candidate.
return candidate ;
}
2024-07-01 15:46:55 +02:00
void TraversableNavigable : : paint ( DevicePixelRect const & content_rect , Painting : : BackingStore & target , PaintOptions paint_options )
2024-06-10 02:58:32 +03:00
{
2024-08-19 02:06:52 +02:00
auto document = active_document ( ) ;
if ( ! document )
return ;
2024-08-12 00:49:32 +02:00
for ( auto & navigable : all_navigables ( ) ) {
if ( auto active_document = navigable - > active_document ( ) ; active_document & & active_document - > paintable ( ) ) {
active_document - > paintable ( ) - > refresh_scroll_state ( ) ;
}
}
2024-08-19 02:06:52 +02:00
DOM : : Document : : PaintConfig paint_config ;
2024-07-01 15:46:55 +02:00
paint_config . paint_overlay = paint_options . paint_overlay = = PaintOptions : : PaintOverlay : : Yes ;
2024-06-10 02:58:32 +03:00
paint_config . should_show_line_box_borders = paint_options . should_show_line_box_borders ;
paint_config . has_focus = paint_options . has_focus ;
2024-08-08 14:17:54 +03:00
paint_config . canvas_fill_rect = Gfx : : IntRect { { } , content_rect . size ( ) } ;
2024-08-19 02:06:52 +02:00
auto display_list = document - > record_display_list ( paint_config ) ;
2024-08-08 14:17:54 +03:00
if ( ! display_list ) {
return ;
}
2024-06-10 02:58:32 +03:00
2024-07-17 14:13:32 +03:00
switch ( page ( ) . client ( ) . display_list_player_type ( ) ) {
case DisplayListPlayerType : : SkiaGPUIfAvailable : {
2024-06-26 17:56:55 +02:00
# ifdef AK_OS_MACOS
if ( m_metal_context & & m_skia_backend_context & & is < Painting : : IOSurfaceBackingStore > ( target ) ) {
auto & iosurface_backing_store = static_cast < Painting : : IOSurfaceBackingStore & > ( target ) ;
auto texture = m_metal_context - > create_texture_from_iosurface ( iosurface_backing_store . iosurface_handle ( ) ) ;
2024-09-25 15:42:15 +02:00
auto painting_surface = Gfx : : PaintingSurface : : wrap_metal_surface ( * texture , m_skia_backend_context ) ;
Painting : : DisplayListPlayerSkia player ( * m_skia_backend_context , painting_surface ) ;
2024-08-08 14:17:54 +03:00
player . execute ( * display_list ) ;
2024-06-26 17:56:55 +02:00
return ;
}
# endif
2024-07-03 19:23:27 +02:00
# ifdef USE_VULKAN
if ( m_skia_backend_context ) {
Painting : : DisplayListPlayerSkia player ( * m_skia_backend_context , target . bitmap ( ) ) ;
2024-08-08 14:17:54 +03:00
player . execute ( * display_list ) ;
2024-07-03 19:23:27 +02:00
return ;
}
# endif
2024-07-17 14:13:32 +03:00
// Fallback to CPU backend if GPU is not available
2024-07-01 15:46:55 +02:00
Painting : : DisplayListPlayerSkia player ( target . bitmap ( ) ) ;
2024-08-08 14:17:54 +03:00
player . execute ( * display_list ) ;
2024-07-17 14:13:32 +03:00
break ;
}
case DisplayListPlayerType : : SkiaCPU : {
Painting : : DisplayListPlayerSkia player ( target . bitmap ( ) ) ;
2024-08-08 14:17:54 +03:00
player . execute ( * display_list ) ;
2024-07-17 14:13:32 +03:00
break ;
}
default :
VERIFY_NOT_REACHED ( ) ;
2024-06-10 02:58:32 +03:00
}
}
2022-12-12 12:01:09 +01:00
}