Navigation.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibJS/Runtime/VM.h>
  9. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  10. #include <LibWeb/Bindings/Intrinsics.h>
  11. #include <LibWeb/Bindings/NavigationPrototype.h>
  12. #include <LibWeb/DOM/AbortController.h>
  13. #include <LibWeb/DOM/Document.h>
  14. #include <LibWeb/HTML/DocumentState.h>
  15. #include <LibWeb/HTML/ErrorEvent.h>
  16. #include <LibWeb/HTML/History.h>
  17. #include <LibWeb/HTML/NavigateEvent.h>
  18. #include <LibWeb/HTML/Navigation.h>
  19. #include <LibWeb/HTML/NavigationCurrentEntryChangeEvent.h>
  20. #include <LibWeb/HTML/NavigationDestination.h>
  21. #include <LibWeb/HTML/NavigationHistoryEntry.h>
  22. #include <LibWeb/HTML/NavigationTransition.h>
  23. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  24. #include <LibWeb/HTML/TraversableNavigable.h>
  25. #include <LibWeb/HTML/Window.h>
  26. #include <LibWeb/WebIDL/AbstractOperations.h>
  27. #include <LibWeb/XHR/FormData.h>
  28. namespace Web::HTML {
  29. JS_DEFINE_ALLOCATOR(Navigation);
  30. static NavigationResult navigation_api_method_tracker_derived_result(JS::NonnullGCPtr<NavigationAPIMethodTracker> api_method_tracker);
  31. NavigationAPIMethodTracker::NavigationAPIMethodTracker(JS::NonnullGCPtr<Navigation> navigation,
  32. Optional<String> key,
  33. JS::Value info,
  34. Optional<SerializationRecord> serialized_state,
  35. JS::GCPtr<NavigationHistoryEntry> commited_to_entry,
  36. JS::NonnullGCPtr<WebIDL::Promise> committed_promise,
  37. JS::NonnullGCPtr<WebIDL::Promise> finished_promise)
  38. : navigation(navigation)
  39. , key(move(key))
  40. , info(info)
  41. , serialized_state(move(serialized_state))
  42. , commited_to_entry(commited_to_entry)
  43. , committed_promise(committed_promise)
  44. , finished_promise(finished_promise)
  45. {
  46. }
  47. void NavigationAPIMethodTracker::visit_edges(Cell::Visitor& visitor)
  48. {
  49. Base::visit_edges(visitor);
  50. visitor.visit(navigation);
  51. visitor.visit(info);
  52. visitor.visit(commited_to_entry);
  53. visitor.visit(committed_promise);
  54. visitor.visit(finished_promise);
  55. }
  56. JS::NonnullGCPtr<Navigation> Navigation::create(JS::Realm& realm)
  57. {
  58. return realm.heap().allocate<Navigation>(realm, realm);
  59. }
  60. Navigation::Navigation(JS::Realm& realm)
  61. : DOM::EventTarget(realm)
  62. {
  63. }
  64. Navigation::~Navigation() = default;
  65. void Navigation::initialize(JS::Realm& realm)
  66. {
  67. Base::initialize(realm);
  68. set_prototype(&Bindings::ensure_web_prototype<Bindings::NavigationPrototype>(realm, "Navigation"_fly_string));
  69. }
  70. void Navigation::visit_edges(JS::Cell::Visitor& visitor)
  71. {
  72. Base::visit_edges(visitor);
  73. for (auto& entry : m_entry_list)
  74. visitor.visit(entry);
  75. visitor.visit(m_transition);
  76. visitor.visit(m_ongoing_navigate_event);
  77. visitor.visit(m_ongoing_api_method_tracker);
  78. visitor.visit(m_upcoming_non_traverse_api_method_tracker);
  79. for (auto& key_and_tracker : m_upcoming_traverse_api_method_trackers)
  80. visitor.visit(key_and_tracker.value);
  81. }
  82. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-entries
  83. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> Navigation::entries() const
  84. {
  85. // The entries() method steps are:
  86. // 1. If this has entries and events disabled, then return the empty list.
  87. if (has_entries_and_events_disabled())
  88. return {};
  89. // 2. Return this's entry list.
  90. // NOTE: Recall that because of Web IDL's sequence type conversion rules,
  91. // this will create a new JavaScript array object on each call.
  92. // That is, navigation.entries() !== navigation.entries().
  93. return m_entry_list;
  94. }
  95. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry
  96. JS::GCPtr<NavigationHistoryEntry> Navigation::current_entry() const
  97. {
  98. // The current entry of a Navigation navigation is the result of running the following steps:
  99. // 1. If navigation has entries and events disabled, then return null.
  100. if (has_entries_and_events_disabled())
  101. return nullptr;
  102. // FIXME 2. Assert: navigation's current entry index is not −1.
  103. // FIXME: This should not happen once Navigable's algorithms properly set up NHEs.
  104. if (m_current_entry_index == -1)
  105. return nullptr;
  106. // 3. Return navigation's entry list[navigation's current entry index].
  107. return m_entry_list[m_current_entry_index];
  108. }
  109. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-updatecurrententry
  110. WebIDL::ExceptionOr<void> Navigation::update_current_entry(NavigationUpdateCurrentEntryOptions options)
  111. {
  112. // The updateCurrentEntry(options) method steps are:
  113. // 1. Let current be the current entry of this.
  114. auto current = current_entry();
  115. // 2. If current is null, then throw an "InvalidStateError" DOMException.
  116. if (current == nullptr)
  117. return WebIDL::InvalidStateError::create(realm(), "Cannot update current NavigationHistoryEntry when there is no current entry"_fly_string);
  118. // 3. Let serializedState be StructuredSerializeForStorage(options["state"]), rethrowing any exceptions.
  119. auto serialized_state = TRY(structured_serialize_for_storage(vm(), options.state));
  120. // 4. Set current's session history entry's navigation API state to serializedState.
  121. current->session_history_entry().navigation_api_state = serialized_state;
  122. // 5. Fire an event named currententrychange at this using NavigationCurrentEntryChangeEvent,
  123. // with its navigationType attribute initialized to null and its from initialized to current.
  124. NavigationCurrentEntryChangeEventInit event_init = {};
  125. event_init.navigation_type = {};
  126. event_init.from = current;
  127. dispatch_event(HTML::NavigationCurrentEntryChangeEvent::construct_impl(realm(), HTML::EventNames::currententrychange, event_init));
  128. return {};
  129. }
  130. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-cangoback
  131. bool Navigation::can_go_back() const
  132. {
  133. // The canGoBack getter steps are:
  134. // 1. If this has entries and events disabled, then return false.
  135. if (has_entries_and_events_disabled())
  136. return false;
  137. // FIXME 2. Assert: navigation's current entry index is not −1.
  138. // FIXME: This should not happen once Navigable's algorithms properly set up NHEs.
  139. if (m_current_entry_index == -1)
  140. return false;
  141. // 3. If this's current entry index is 0, then return false.
  142. // 4. Return true.
  143. return (m_current_entry_index != 0);
  144. }
  145. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-cangoforward
  146. bool Navigation::can_go_forward() const
  147. {
  148. // The canGoForward getter steps are:
  149. // 1. If this has entries and events disabled, then return false.
  150. if (has_entries_and_events_disabled())
  151. return false;
  152. // FIXME 2. Assert: navigation's current entry index is not −1.
  153. // FIXME: This should not happen once Navigable's algorithms properly set up NHEs.
  154. if (m_current_entry_index == -1)
  155. return false;
  156. // 3. If this's current entry index is equal to this's entry list's size, then return false.
  157. // 4. Return true.
  158. return (m_current_entry_index != static_cast<i64>(m_entry_list.size()));
  159. }
  160. HistoryHandlingBehavior to_history_handling_behavior(Bindings::NavigationHistoryBehavior b)
  161. {
  162. // A history handling behavior is a NavigationHistoryBehavior that is either "push" or "replace",
  163. // i.e., that has been resolved away from any initial "auto" value.
  164. VERIFY(b != Bindings::NavigationHistoryBehavior::Auto);
  165. switch (b) {
  166. case Bindings::NavigationHistoryBehavior::Push:
  167. return HistoryHandlingBehavior::Push;
  168. case Bindings::NavigationHistoryBehavior::Replace:
  169. return HistoryHandlingBehavior::Replace;
  170. case Bindings::NavigationHistoryBehavior::Auto:
  171. VERIFY_NOT_REACHED();
  172. };
  173. VERIFY_NOT_REACHED();
  174. }
  175. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-navigate
  176. WebIDL::ExceptionOr<NavigationResult> Navigation::navigate(String url, NavigationNavigateOptions const& options)
  177. {
  178. auto& realm = this->realm();
  179. auto& vm = this->vm();
  180. // The navigate(options) method steps are:
  181. // 1. Parse url relative to this's relevant settings object.
  182. // If that returns failure, then return an early error result for a "SyntaxError" DOMException.
  183. // Otherwise, let urlRecord be the resulting URL record.
  184. auto url_record = relevant_settings_object(*this).parse_url(url);
  185. if (!url_record.is_valid())
  186. return early_error_result(WebIDL::SyntaxError::create(realm, "Cannot navigate to Invalid URL"_fly_string));
  187. // 2. Let document be this's relevant global object's associated Document.
  188. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  189. // 3. If options["history"] is "push", and the navigation must be a replace given urlRecord and document,
  190. // then return an early error result for a "NotSupportedError" DOMException.
  191. if (options.history == Bindings::NavigationHistoryBehavior::Push && navigation_must_be_a_replace(url_record, document))
  192. return early_error_result(WebIDL::NotSupportedError::create(realm, "Navigation must be a replace, but push was requested"_fly_string));
  193. // 4. Let state be options["state"], if it exists; otherwise, undefined.
  194. auto state = options.state.value_or(JS::js_undefined());
  195. // 5. Let serializedState be StructuredSerializeForStorage(state).
  196. // If this throws an exception, then return an early error result for that exception.
  197. // FIXME: Fix this spec grammaro in the note
  198. // NOTE: It is importantly to perform this step early, since serialization can invoke web developer code,
  199. // which in turn might change various things we check in later steps.
  200. auto serialized_state_or_error = structured_serialize_for_storage(vm, state);
  201. if (serialized_state_or_error.is_error()) {
  202. return early_error_result(serialized_state_or_error.release_error());
  203. }
  204. auto serialized_state = serialized_state_or_error.release_value();
  205. // 6. If document is not fully active, then return an early error result for an "InvalidStateError" DOMException.
  206. if (!document.is_fully_active())
  207. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document is not fully active"_fly_string));
  208. // 7. If document's unload counter is greater than 0, then return an early error result for an "InvalidStateError" DOMException.
  209. if (document.unload_counter() > 0)
  210. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document already unloaded"_fly_string));
  211. // 8. Let info be options["info"], if it exists; otherwise, undefined.
  212. auto info = options.info.value_or(JS::js_undefined());
  213. // 9. Let apiMethodTracker be the result of maybe setting the upcoming non-traverse API method tracker for this
  214. // given info and serializedState.
  215. auto api_method_tracker = maybe_set_the_upcoming_non_traverse_api_method_tracker(info, serialized_state);
  216. // 10. Navigate document's node navigable to urlRecord using document,
  217. // with historyHandling set to options["history"] and navigationAPIState set to serializedState.
  218. // FIXME: Fix spec typo here
  219. // NOTE: Unlike location.assign() and friends, which are exposed across origin-domain boundaries,
  220. // navigation.navigate() can only be accessed by code with direct synchronous access to the
  221. /// window.navigation property. Thus, we avoid the complications about attributing the source document
  222. // of the navigation, and we don't need to deal with the allowed by sandboxing to navigate check and its
  223. // acccompanying exceptionsEnabled flag. We just treat all navigations as if they come from the Document
  224. // corresponding to this Navigation object itself (i.e., document).
  225. [[maybe_unused]] auto history_handling_behavior = to_history_handling_behavior(options.history);
  226. // FIXME: Actually call navigate once Navigables are implemented enough to guarantee a node navigable on
  227. // an active document that's not being unloaded.
  228. // document.navigable().navigate(url, document, history behavior, state)
  229. // 11. If this's upcoming non-traverse API method tracker is apiMethodTracker, then:
  230. // NOTE: If the upcoming non-traverse API method tracker is still apiMethodTracker, this means that the navigate
  231. // algorithm bailed out before ever getting to the inner navigate event firing algorithm which would promote
  232. // that upcoming API method tracker to ongoing.
  233. if (m_upcoming_non_traverse_api_method_tracker == api_method_tracker) {
  234. m_upcoming_non_traverse_api_method_tracker = nullptr;
  235. return early_error_result(WebIDL::AbortError::create(realm, "Navigation aborted"_fly_string));
  236. }
  237. // 12. Return a navigation API method tracker-derived result for apiMethodTracker.
  238. return navigation_api_method_tracker_derived_result(api_method_tracker);
  239. }
  240. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-reload
  241. WebIDL::ExceptionOr<NavigationResult> Navigation::reload(NavigationReloadOptions const& options)
  242. {
  243. auto& realm = this->realm();
  244. auto& vm = this->vm();
  245. // The reload(options) method steps are:
  246. // 1. Let document be this's relevant global object's associated Document.
  247. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  248. // 2. Let serializedState be StructuredSerializeForStorage(undefined).
  249. auto serialized_state = MUST(structured_serialize_for_storage(vm, JS::js_undefined()));
  250. // 3. If options["state"] exists, then set serializedState to StructuredSerializeForStorage(options["state"]).
  251. // If this throws an exception, then return an early error result for that exception.
  252. // NOTE: It is importantly to perform this step early, since serialization can invoke web developer
  253. // code, which in turn might change various things we check in later steps.
  254. if (options.state.has_value()) {
  255. auto serialized_state_or_error = structured_serialize_for_storage(vm, options.state.value());
  256. if (serialized_state_or_error.is_error())
  257. return early_error_result(serialized_state_or_error.release_error());
  258. serialized_state = serialized_state_or_error.release_value();
  259. }
  260. // 4. Otherwise:
  261. else {
  262. // 1. Let current be the current entry of this.
  263. auto current = current_entry();
  264. // 2. If current is not null, then set serializedState to current's session history entry's navigation API state.
  265. if (current != nullptr)
  266. serialized_state = current->session_history_entry().navigation_api_state;
  267. }
  268. // 5. If document is not fully active, then return an early error result for an "InvalidStateError" DOMException.
  269. if (!document.is_fully_active())
  270. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document is not fully active"_fly_string));
  271. // 6. If document's unload counter is greater than 0, then return an early error result for an "InvalidStateError" DOMException.
  272. if (document.unload_counter() > 0)
  273. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document already unloaded"_fly_string));
  274. // 7. Let info be options["info"], if it exists; otherwise, undefined.
  275. auto info = options.info.value_or(JS::js_undefined());
  276. // 8. Let apiMethodTracker be the result of maybe setting the upcoming non-traverse API method tracker for this given info and serializedState.
  277. auto api_method_tracker = maybe_set_the_upcoming_non_traverse_api_method_tracker(info, serialized_state);
  278. // 9. Reload document's node navigable with navigationAPIState set to serializedState.
  279. // FIXME: Actually call reload once Navigables are implemented enough to guarantee a node navigable on
  280. // an active document that's not being unloaded.
  281. // document.navigable().reload(state)
  282. return navigation_api_method_tracker_derived_result(api_method_tracker);
  283. }
  284. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-traverseto
  285. WebIDL::ExceptionOr<NavigationResult> Navigation::traverse_to(String key, NavigationOptions const& options)
  286. {
  287. auto& realm = this->realm();
  288. // The traverseTo(key, options) method steps are:
  289. // 1. If this's current entry index is −1, then return an early error result for an "InvalidStateError" DOMException.
  290. if (m_current_entry_index == -1)
  291. return early_error_result(WebIDL::InvalidStateError::create(realm, "Cannot traverseTo: no current session history entry"_fly_string));
  292. // 2. If this's entry list does not contain a NavigationHistoryEntry whose session history entry's navigation API key equals key,
  293. // then return an early error result for an "InvalidStateError" DOMException.
  294. auto it = m_entry_list.find_if([&key](auto const& entry) {
  295. return entry->session_history_entry().navigation_api_key == key;
  296. });
  297. if (it == m_entry_list.end())
  298. return early_error_result(WebIDL::InvalidStateError::create(realm, "Cannot traverseTo: key not found in session history list"_fly_string));
  299. // 3. Return the result of performing a navigation API traversal given this, key, and options.
  300. return perform_a_navigation_api_traversal(key, options);
  301. }
  302. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#performing-a-navigation-api-traversal
  303. WebIDL::ExceptionOr<NavigationResult> Navigation::back(NavigationOptions const& options)
  304. {
  305. auto& realm = this->realm();
  306. // The back(options) method steps are:
  307. // 1. If this's current entry index is −1 or 0, then return an early error result for an "InvalidStateError" DOMException.
  308. if (m_current_entry_index == -1 || m_current_entry_index == 0)
  309. return early_error_result(WebIDL::InvalidStateError::create(realm, "Cannot navigate back: no previous session history entry"_fly_string));
  310. // 2. Let key be this's entry list[this's current entry index − 1]'s session history entry's navigation API key.
  311. auto key = m_entry_list[m_current_entry_index - 1]->session_history_entry().navigation_api_key;
  312. // 3. Return the result of performing a navigation API traversal given this, key, and options.
  313. return perform_a_navigation_api_traversal(key, options);
  314. }
  315. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-forward
  316. WebIDL::ExceptionOr<NavigationResult> Navigation::forward(NavigationOptions const& options)
  317. {
  318. auto& realm = this->realm();
  319. // The forward(options) method steps are:
  320. // 1. If this's current entry index is −1 or is equal to this's entry list's size − 1,
  321. // then return an early error result for an "InvalidStateError" DOMException.
  322. if (m_current_entry_index == -1 || m_current_entry_index == static_cast<i64>(m_entry_list.size() - 1))
  323. return early_error_result(WebIDL::InvalidStateError::create(realm, "Cannot navigate forward: no next session history entry"_fly_string));
  324. // 2. Let key be this's entry list[this's current entry index + 1]'s session history entry's navigation API key.
  325. auto key = m_entry_list[m_current_entry_index + 1]->session_history_entry().navigation_api_key;
  326. // 3. Return the result of performing a navigation API traversal given this, key, and options.
  327. return perform_a_navigation_api_traversal(key, options);
  328. }
  329. void Navigation::set_onnavigate(WebIDL::CallbackType* event_handler)
  330. {
  331. set_event_handler_attribute(HTML::EventNames::navigate, event_handler);
  332. }
  333. WebIDL::CallbackType* Navigation::onnavigate()
  334. {
  335. return event_handler_attribute(HTML::EventNames::navigate);
  336. }
  337. void Navigation::set_onnavigatesuccess(WebIDL::CallbackType* event_handler)
  338. {
  339. set_event_handler_attribute(HTML::EventNames::navigatesuccess, event_handler);
  340. }
  341. WebIDL::CallbackType* Navigation::onnavigatesuccess()
  342. {
  343. return event_handler_attribute(HTML::EventNames::navigatesuccess);
  344. }
  345. void Navigation::set_onnavigateerror(WebIDL::CallbackType* event_handler)
  346. {
  347. set_event_handler_attribute(HTML::EventNames::navigateerror, event_handler);
  348. }
  349. WebIDL::CallbackType* Navigation::onnavigateerror()
  350. {
  351. return event_handler_attribute(HTML::EventNames::navigateerror);
  352. }
  353. void Navigation::set_oncurrententrychange(WebIDL::CallbackType* event_handler)
  354. {
  355. set_event_handler_attribute(HTML::EventNames::currententrychange, event_handler);
  356. }
  357. WebIDL::CallbackType* Navigation::oncurrententrychange()
  358. {
  359. return event_handler_attribute(HTML::EventNames::currententrychange);
  360. }
  361. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#has-entries-and-events-disabled
  362. bool Navigation::has_entries_and_events_disabled() const
  363. {
  364. // A Navigation navigation has entries and events disabled if the following steps return true:
  365. // 1. Let document be navigation's relevant global object's associated Document.
  366. auto const& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  367. // 2. If document is not fully active, then return true.
  368. if (!document.is_fully_active())
  369. return true;
  370. // 3. If document's is initial about:blank is true, then return true.
  371. if (document.is_initial_about_blank())
  372. return true;
  373. // 4. If document's origin is opaque, then return true.
  374. if (document.origin().is_opaque())
  375. return true;
  376. // 5. Return false.
  377. return false;
  378. }
  379. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#getting-the-navigation-api-entry-index
  380. i64 Navigation::get_the_navigation_api_entry_index(SessionHistoryEntry const& she) const
  381. {
  382. // To get the navigation API entry index of a session history entry she within a Navigation navigation:
  383. // 1. Let index be 0.
  384. i64 index = 0;
  385. // 2. For each nhe of navigation's entry list:
  386. for (auto const& nhe : m_entry_list) {
  387. // 1. If nhe's session history entry is equal to she, then return index.
  388. if (&nhe->session_history_entry() == &she)
  389. return index;
  390. // 2. Increment index by 1.
  391. ++index;
  392. }
  393. // 3. Return −1.
  394. return -1;
  395. }
  396. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-early-error-result
  397. NavigationResult Navigation::early_error_result(AnyException e)
  398. {
  399. auto& vm = this->vm();
  400. // An early error result for an exception e is a NavigationResult dictionary instance given by
  401. // «[ "committed" → a promise rejected with e, "finished" → a promise rejected with e ]».
  402. auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, e);
  403. return {
  404. .committed = WebIDL::create_rejected_promise(realm(), *throw_completion.value())->promise(),
  405. .finished = WebIDL::create_rejected_promise(realm(), *throw_completion.value())->promise(),
  406. };
  407. }
  408. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-method-tracker-derived-result
  409. NavigationResult navigation_api_method_tracker_derived_result(JS::NonnullGCPtr<NavigationAPIMethodTracker> api_method_tracker)
  410. {
  411. // A navigation API method tracker-derived result for a navigation API method tracker is a NavigationResult
  412. /// dictionary instance given by «[ "committed" apiMethodTracker's committed promise, "finished" → apiMethodTracker's finished promise ]».
  413. return {
  414. api_method_tracker->committed_promise->promise(),
  415. api_method_tracker->finished_promise->promise(),
  416. };
  417. }
  418. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  419. JS::NonnullGCPtr<NavigationAPIMethodTracker> Navigation::maybe_set_the_upcoming_non_traverse_api_method_tracker(JS::Value info, Optional<SerializationRecord> serialized_state)
  420. {
  421. auto& realm = relevant_realm(*this);
  422. auto& vm = this->vm();
  423. // To maybe set the upcoming non-traverse API method tracker given a Navigation navigation,
  424. // a JavaScript value info, and a serialized state-or-null serializedState:
  425. // 1. Let committedPromise and finishedPromise be new promises created in navigation's relevant realm.
  426. auto committed_promise = WebIDL::create_promise(realm);
  427. auto finished_promise = WebIDL::create_promise(realm);
  428. // 2. Mark as handled finishedPromise.
  429. // NOTE: The web developer doesn’t necessarily care about finishedPromise being rejected:
  430. // - They might only care about committedPromise.
  431. // - They could be doing multiple synchronous navigations within the same task,
  432. // in which case all but the last will be aborted (causing their finishedPromise to reject).
  433. // This could be an application bug, but also could just be an emergent feature of disparate
  434. // parts of the application overriding each others' actions.
  435. // - They might prefer to listen to other transition-failure signals instead of finishedPromise, e.g.,
  436. // the navigateerror event, or the navigation.transition.finished promise.
  437. // As such, we mark it as handled to ensure that it never triggers unhandledrejection events.
  438. WebIDL::mark_promise_as_handled(finished_promise);
  439. // 3. Let apiMethodTracker be a new navigation API method tracker with:
  440. // navigation object: navigation
  441. // key: null
  442. // info: info
  443. // serialized state: serializedState
  444. // comitted-to entry: null
  445. // comitted promise: committedPromise
  446. // finished promise: finishedPromise
  447. auto api_method_tracker = vm.heap().allocate_without_realm<NavigationAPIMethodTracker>(
  448. /* .navigation = */ *this,
  449. /* .key = */ OptionalNone {},
  450. /* .info = */ info,
  451. /* .serialized_state = */ move(serialized_state),
  452. /* .commited_to_entry = */ nullptr,
  453. /* .committed_promise = */ committed_promise,
  454. /* .finished_promise = */ finished_promise);
  455. // 4. Assert: navigation's upcoming non-traverse API method tracker is null.
  456. VERIFY(m_upcoming_non_traverse_api_method_tracker == nullptr);
  457. // 5. If navigation does not have entries and events disabled,
  458. // then set navigation's upcoming non-traverse API method tracker to apiMethodTracker.
  459. // NOTE: If navigation has entries and events disabled, then committedPromise and finishedPromise will never fulfill
  460. // (since we never create a NavigationHistoryEntry object for such Documents, and so we have nothing to resolve them with);
  461. // there is no NavigationHistoryEntry to apply serializedState to; and there is no navigate event to include info with.
  462. // So, we don't need to track this API method call after all.
  463. if (!has_entries_and_events_disabled())
  464. m_upcoming_non_traverse_api_method_tracker = api_method_tracker;
  465. // 6. Return apiMethodTracker.
  466. return api_method_tracker;
  467. }
  468. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#add-an-upcoming-traverse-api-method-tracker
  469. JS::NonnullGCPtr<NavigationAPIMethodTracker> Navigation::add_an_upcoming_traverse_api_method_tracker(String destination_key, JS::Value info)
  470. {
  471. auto& vm = this->vm();
  472. auto& realm = relevant_realm(*this);
  473. // To add an upcoming traverse API method tracker given a Navigation navigation, a string destinationKey, and a JavaScript value info:
  474. // 1. Let committedPromise and finishedPromise be new promises created in navigation's relevant realm.
  475. auto committed_promise = WebIDL::create_promise(realm);
  476. auto finished_promise = WebIDL::create_promise(realm);
  477. // 2. Mark as handled finishedPromise.
  478. // NOTE: See the previous discussion about why this is done
  479. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#note-mark-as-handled-navigation-api-finished
  480. WebIDL::mark_promise_as_handled(*finished_promise);
  481. // 3. Let apiMethodTracker be a new navigation API method tracker with:
  482. // navigation object: navigation
  483. // key: destinationKey
  484. // info: info
  485. // serialized state: null
  486. // comitted-to entry: null
  487. // comitted promise: committedPromise
  488. // finished promise: finishedPromise
  489. auto api_method_tracker = vm.heap().allocate_without_realm<NavigationAPIMethodTracker>(
  490. /* .navigation = */ *this,
  491. /* .key = */ destination_key,
  492. /* .info = */ info,
  493. /* .serialized_state = */ OptionalNone {},
  494. /* .commited_to_entry = */ nullptr,
  495. /* .committed_promise = */ committed_promise,
  496. /* .finished_promise = */ finished_promise);
  497. // 4. Set navigation's upcoming traverse API method trackers[key] to apiMethodTracker.
  498. // FIXME: Fix spec typo key --> destinationKey
  499. m_upcoming_traverse_api_method_trackers.set(destination_key, api_method_tracker);
  500. // 5. Return apiMethodTracker.
  501. return api_method_tracker;
  502. }
  503. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#performing-a-navigation-api-traversal
  504. WebIDL::ExceptionOr<NavigationResult> Navigation::perform_a_navigation_api_traversal(String key, NavigationOptions const& options)
  505. {
  506. auto& realm = this->realm();
  507. // To perform a navigation API traversal given a Navigation navigation, a string key, and a NavigationOptions options:
  508. // 1. Let document be this's relevant global object's associated Document.
  509. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  510. // 2. If document is not fully active, then return an early error result for an "InvalidStateError" DOMException.
  511. if (!document.is_fully_active())
  512. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document is not fully active"_fly_string));
  513. // 3. If document's unload counter is greater than 0, then return an early error result for an "InvalidStateError" DOMException.
  514. if (document.unload_counter() > 0)
  515. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document already unloaded"_fly_string));
  516. // 4. Let current be the current entry of navigation.
  517. auto current = current_entry();
  518. // 5. If key equals current's session history entry's navigation API key, then return
  519. // «[ "committed" → a promise resolved with current, "finished" → a promise resolved with current ]».
  520. if (key == current->session_history_entry().navigation_api_key) {
  521. return NavigationResult {
  522. .committed = WebIDL::create_resolved_promise(realm, current)->promise(),
  523. .finished = WebIDL::create_resolved_promise(realm, current)->promise()
  524. };
  525. }
  526. // 6. If navigation's upcoming traverse API method trackers[key] exists,
  527. // then return a navigation API method tracker-derived result for navigation's upcoming traverse API method trackers[key].
  528. if (auto maybe_tracker = m_upcoming_traverse_api_method_trackers.get(key); maybe_tracker.has_value())
  529. return navigation_api_method_tracker_derived_result(maybe_tracker.value());
  530. // 7. Let info be options["info"], if it exists; otherwise, undefined.
  531. auto info = options.info.value_or(JS::js_undefined());
  532. // 8. Let apiMethodTracker be the result of adding an upcoming traverse API method tracker for navigation given key and info.
  533. auto api_method_tracker = add_an_upcoming_traverse_api_method_tracker(key, info);
  534. // 9. Let navigable be document's node navigable.
  535. auto navigable = document.navigable();
  536. // 10. Let traversable be navigable's traversable navigable.
  537. auto traversable = navigable->traversable_navigable();
  538. // 11. Let sourceSnapshotParams be the result of snapshotting source snapshot params given document.
  539. auto source_snapshot_params = document.snapshot_source_snapshot_params();
  540. // 12. Append the following session history traversal steps to traversable:
  541. traversable->append_session_history_traversal_steps([key, api_method_tracker, navigable, source_snapshot_params, this] {
  542. // 1. Let navigableSHEs be the result of getting session history entries given navigable.
  543. auto navigable_shes = navigable->get_session_history_entries();
  544. // 2. Let targetSHE be the session history entry in navigableSHEs whose navigation API key is key. If no such entry exists, then:
  545. auto it = navigable_shes.find_if([&key](auto const& entry) {
  546. return entry->navigation_api_key == key;
  547. });
  548. if (it == navigable_shes.end()) {
  549. // NOTE: This path is taken if navigation's entry list was outdated compared to navigableSHEs,
  550. // which can occur for brief periods while all the relevant threads and processes are being synchronized in reaction to a history change.
  551. // 1. Queue a global task on the navigation and traversal task source given navigation's relevant global object
  552. // to reject the finished promise for apiMethodTracker with an "InvalidStateError" DOMException.
  553. queue_global_task(HTML::Task::Source::NavigationAndTraversal, relevant_global_object(*this), [this, api_method_tracker] {
  554. auto& reject_realm = relevant_realm(*this);
  555. WebIDL::reject_promise(reject_realm, api_method_tracker->finished_promise,
  556. WebIDL::InvalidStateError::create(reject_realm, "Cannot traverse with stale session history entry"_fly_string));
  557. });
  558. // 2. Abort these steps.
  559. return;
  560. }
  561. auto target_she = *it;
  562. // 3. If targetSHE is navigable's active session history entry, then abort these steps.
  563. // NOTE: This can occur if a previously queued traversal already took us to this session history entry.
  564. // In that case the previous traversal will have dealt with apiMethodTracker already.
  565. if (target_she == navigable->active_session_history_entry())
  566. return;
  567. // FIXME: 4. Let result be the result of applying the traverse history step given by targetSHE's step to traversable,
  568. // given sourceSnapshotParams, navigable, and "none".
  569. (void)source_snapshot_params;
  570. // NOTE: When result is "canceled-by-beforeunload" or "initiator-disallowed", the navigate event was never fired,
  571. // aborting the ongoing navigation would not be correct; it would result in a navigateerror event without a
  572. // preceding navigate event. In the "canceled-by-navigate" case, navigate is fired, but the inner navigate event
  573. // firing algorithm will take care of aborting the ongoing navigation.
  574. // FIXME: 5. If result is "canceled-by-beforeunload", then queue a global task on the navigation and traversal task source
  575. // given navigation's relevant global object to reject the finished promise for apiMethodTracker with a
  576. // new "AbortError"DOMException created in navigation's relevant realm.
  577. // FIXME: 6. If result is "initiator-disallowed", then queue a global task on the navigation and traversal task source
  578. // given navigation's relevant global object to reject the finished promise for apiMethodTracker with a
  579. // new "SecurityError" DOMException created in navigation's relevant realm.
  580. });
  581. // 13. Return a navigation API method tracker-derived result for apiMethodTracker.
  582. return navigation_api_method_tracker_derived_result(api_method_tracker);
  583. }
  584. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#abort-the-ongoing-navigation
  585. void Navigation::abort_the_ongoing_navigation(Optional<JS::NonnullGCPtr<WebIDL::DOMException>> error)
  586. {
  587. auto& realm = relevant_realm(*this);
  588. // To abort the ongoing navigation given a Navigation navigation and an optional DOMException error:
  589. // 1. Let event be navigation's ongoing navigate event.
  590. auto event = ongoing_navigate_event();
  591. // 2. Assert: event is not null.
  592. VERIFY(event != nullptr);
  593. // 3. Set navigation's focus changed during ongoing navigation to false.
  594. m_focus_changed_during_ongoing_navigation = false;
  595. // 4. Set navigation's suppress normal scroll restoration during ongoing navigation to false.
  596. m_suppress_scroll_restoration_during_ongoing_navigation = false;
  597. // 5. If error was not given, then let error be a new "AbortError" DOMException created in navigation's relevant realm.
  598. if (!error.has_value())
  599. error = WebIDL::AbortError::create(realm, "Navigation aborted"_fly_string);
  600. VERIFY(error.has_value());
  601. // 6. If event's dispatch flag is set, then set event's canceled flag to true.
  602. if (event->dispatched())
  603. event->set_cancelled(true);
  604. // 7. Signal abort on event's abort controller given error.
  605. event->abort_controller()->abort(error.value());
  606. // 8. Set navigation's ongoing navigate event to null.
  607. m_ongoing_navigate_event = nullptr;
  608. // 9. Fire an event named navigateerror at navigation using ErrorEvent, with error initialized to error,
  609. // and message, filename, lineno, and colno initialized to appropriate values that can be extracted
  610. // from error and the current JavaScript stack in the same underspecified way that the report the exception algorithm does.
  611. ErrorEventInit event_init = {};
  612. event_init.error = error.value();
  613. // FIXME: Extract information from the exception and the JS context in the wishy-washy way the spec says here.
  614. event_init.filename = String {};
  615. event_init.colno = 0;
  616. event_init.lineno = 0;
  617. event_init.message = String {};
  618. dispatch_event(ErrorEvent::create(realm, EventNames::navigateerror, event_init));
  619. // 10. If navigation's ongoing API method tracker is non-null, then reject the finished promise for apiMethodTracker with error.
  620. if (m_ongoing_api_method_tracker != nullptr)
  621. WebIDL::reject_promise(realm, m_ongoing_api_method_tracker->finished_promise, error.value());
  622. // 11. If navigation's transition is not null, then:
  623. if (m_transition != nullptr) {
  624. // 1. Reject navigation's transition's finished promise with error.
  625. m_transition->finished()->reject(error.value());
  626. // 2. Set navigation's transition to null.
  627. m_transition = nullptr;
  628. }
  629. }
  630. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#promote-an-upcoming-api-method-tracker-to-ongoing
  631. void Navigation::promote_an_upcoming_api_method_tracker_to_ongoing(Optional<String> destination_key)
  632. {
  633. // 1. Assert: navigation's ongoing API method tracker is null.
  634. VERIFY(m_ongoing_api_method_tracker == nullptr);
  635. // 2. If destinationKey is not null, then:
  636. if (destination_key.has_value()) {
  637. // 1. Assert: navigation's upcoming non-traverse API method tracker is null.
  638. VERIFY(m_upcoming_non_traverse_api_method_tracker == nullptr);
  639. // 2. If navigation's upcoming traverse API method trackers[destinationKey] exists, then:
  640. if (auto tracker = m_upcoming_traverse_api_method_trackers.get(destination_key.value()); tracker.has_value()) {
  641. // 1. Set navigation's ongoing API method tracker to navigation's upcoming traverse API method trackers[destinationKey].
  642. m_ongoing_api_method_tracker = tracker.value();
  643. // 2. Remove navigation's upcoming traverse API method trackers[destinationKey].
  644. m_upcoming_traverse_api_method_trackers.remove(destination_key.value());
  645. }
  646. }
  647. // 3. Otherwise:
  648. else {
  649. VERIFY(m_upcoming_non_traverse_api_method_tracker != nullptr);
  650. // 1. Set navigation's ongoing API method tracker to navigation's upcoming non-traverse API method tracker.
  651. m_ongoing_api_method_tracker = m_upcoming_non_traverse_api_method_tracker;
  652. // 2. Set navigation's upcoming non-traverse API method tracker to null.
  653. m_upcoming_non_traverse_api_method_tracker = nullptr;
  654. }
  655. }
  656. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-method-tracker-clean-up
  657. void Navigation::clean_up(JS::NonnullGCPtr<NavigationAPIMethodTracker> api_method_tracker)
  658. {
  659. // 1. Let navigation be apiMethodTracker's navigation object.
  660. VERIFY(api_method_tracker->navigation == this);
  661. // 2. If navigation's ongoing API method tracker is apiMethodTracker, then set navigation's ongoing API method tracker to null.
  662. if (m_ongoing_api_method_tracker == api_method_tracker) {
  663. m_ongoing_api_method_tracker = nullptr;
  664. }
  665. // 3. Otherwise:
  666. else {
  667. // 1. Let key be apiMethodTracker's key.
  668. auto& key = api_method_tracker->key;
  669. // 2. Assert: key is not null.
  670. VERIFY(key.has_value());
  671. // 3. Assert: navigation's upcoming traverse API method trackers[key] exists.
  672. VERIFY(m_upcoming_traverse_api_method_trackers.contains(*key));
  673. // 4. Remove navigation's upcoming traverse API method trackers[key].
  674. m_upcoming_traverse_api_method_trackers.remove(*key);
  675. }
  676. }
  677. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#resolve-the-finished-promise
  678. void Navigation::resolve_the_finished_promise(JS::NonnullGCPtr<NavigationAPIMethodTracker> api_method_tracker)
  679. {
  680. auto& realm = this->realm();
  681. // 1. Resolve apiMethodTracker's committed promise with its committed-to entry.
  682. // NOTE: Usually, notify about the committed-to entry has previously been called on apiMethodTracker,
  683. // and so this will do nothing. However, in some cases resolve the finished promise is called
  684. // directly, in which case this step is necessary.
  685. WebIDL::resolve_promise(realm, api_method_tracker->committed_promise, api_method_tracker->commited_to_entry);
  686. // 2. Resolve apiMethodTracker's finished promise with its committed-to entry.
  687. WebIDL::resolve_promise(realm, api_method_tracker->finished_promise, api_method_tracker->commited_to_entry);
  688. // 3. Clean up apiMethodTracker.
  689. clean_up(api_method_tracker);
  690. }
  691. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#reject-the-finished-promise
  692. void Navigation::reject_the_finished_promise(JS::NonnullGCPtr<NavigationAPIMethodTracker> api_method_tracker, JS::Value exception)
  693. {
  694. auto& realm = this->realm();
  695. // 1. Reject apiMethodTracker's committed promise with exception.
  696. // NOTE: This will do nothing if apiMethodTracker's committed promise was previously resolved
  697. // via notify about the committed-to entry.
  698. WebIDL::reject_promise(realm, api_method_tracker->committed_promise, exception);
  699. // 2. Reject apiMethodTracker's finished promise with exception.
  700. WebIDL::reject_promise(realm, api_method_tracker->finished_promise, exception);
  701. // 3. Clean up apiMethodTracker.
  702. clean_up(api_method_tracker);
  703. }
  704. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#notify-about-the-committed-to-entry
  705. void Navigation::notify_about_the_committed_to_entry(JS::NonnullGCPtr<NavigationAPIMethodTracker> api_method_tracker, JS::NonnullGCPtr<NavigationHistoryEntry> nhe)
  706. {
  707. auto& realm = this->realm();
  708. // 1. Set apiMethodTracker's committed-to entry to nhe.
  709. api_method_tracker->commited_to_entry = nhe;
  710. // 2. If apiMethodTracker's serialized state is not null, then set nhe's session history entry's navigation API state to apiMethodTracker's serialized state.'
  711. // NOTE: If it's null, then we're traversing to nhe via navigation.traverseTo(), which does not allow changing the state.
  712. if (api_method_tracker->serialized_state.has_value()) {
  713. // NOTE: At this point, apiMethodTracker's serialized state is no longer needed.
  714. // Implementations might want to clear it out to avoid keeping it alive for the lifetime of the navigation API method tracker.
  715. nhe->session_history_entry().navigation_api_state = *api_method_tracker->serialized_state;
  716. api_method_tracker->serialized_state = {};
  717. }
  718. // 3. Resolve apiMethodTracker's committed promise with nhe.
  719. TemporaryExecutionContext execution_context { relevant_settings_object(*this) };
  720. WebIDL::resolve_promise(realm, api_method_tracker->committed_promise, nhe);
  721. }
  722. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#inner-navigate-event-firing-algorithm
  723. bool Navigation::inner_navigate_event_firing_algorithm(
  724. Bindings::NavigationType navigation_type,
  725. JS::NonnullGCPtr<NavigationDestination> destination,
  726. UserNavigationInvolvement user_involvement,
  727. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list,
  728. Optional<String> download_request_filename,
  729. Optional<SerializationRecord> classic_history_api_state)
  730. {
  731. auto& realm = relevant_realm(*this);
  732. // 1. If navigation has entries and events disabled, then:
  733. // NOTE: These assertions holds because traverseTo(), back(), and forward() will immediately fail when entries and events are disabled
  734. // (since there are no entries to traverse to), and if our starting point is instead navigate() or reload(),
  735. // then we avoided setting the upcoming non-traverse API method tracker in the first place.
  736. if (has_entries_and_events_disabled()) {
  737. // 1. Assert: navigation's ongoing API method tracker is null.
  738. VERIFY(m_ongoing_api_method_tracker == nullptr);
  739. // 2. Assert: navigation's upcoming non-traverse API method tracker is null.
  740. VERIFY(m_upcoming_non_traverse_api_method_tracker == nullptr);
  741. // 3. Assert: navigation's upcoming traverse API method trackers is empty.
  742. VERIFY(m_upcoming_traverse_api_method_trackers.is_empty());
  743. // 4. Return true.
  744. return true;
  745. }
  746. // 2. Let destinationKey be null.
  747. Optional<String> destination_key = {};
  748. // 3. If destination's entry is non-null, then set destinationKey to destination's entry's key.
  749. if (destination->navigation_history_entry() != nullptr)
  750. destination_key = destination->navigation_history_entry()->key();
  751. // 4. Assert: destinationKey is not the empty string.
  752. VERIFY(destination_key != ""sv);
  753. // 5. Promote an upcoming API method tracker to ongoing given navigation and destinationKey.
  754. promote_an_upcoming_api_method_tracker_to_ongoing(destination_key);
  755. // 6. Let apiMethodTracker be navigation's ongoing API method tracker.
  756. auto api_method_tracker = m_ongoing_api_method_tracker;
  757. // 7. Let navigable be navigation's relevant global object's navigable.
  758. auto& relevant_global_object = verify_cast<HTML::Window>(Web::HTML::relevant_global_object(*this));
  759. auto navigable = relevant_global_object.navigable();
  760. // 8. Let document be navigation's relevant global object's associated Document.
  761. auto& document = relevant_global_object.associated_document();
  762. // Note: We create the Event in this algorithm instead of passing it in,
  763. // and have all the following "initialize" steps set up the event init
  764. NavigateEventInit event_init = {};
  765. // 9. If document can have its URL rewritten to destination's URL,
  766. // and either destination's is same document is true or navigationType is not "traverse",
  767. // then initialize event's canIntercept to true. Otherwise, initialize it to false.
  768. event_init.can_intercept = can_have_its_url_rewritten(document, destination->raw_url()) && (destination->same_document() || navigation_type != Bindings::NavigationType::Traverse);
  769. // 10. Let traverseCanBeCanceled be true if all of the following are true:
  770. // - navigable is a top-level traversable;
  771. // - destination's is same document is true; and
  772. // - either userInvolvement is not "browser UI", or navigation's relevant global object has transient activation.
  773. // Otherwise, let it be false.
  774. bool const traverse_can_be_canceled = navigable->is_top_level_traversable()
  775. && destination->same_document()
  776. && (user_involvement != UserNavigationInvolvement::BrowserUI || relevant_global_object.has_transient_activation());
  777. // FIXME: Fix spec grammaro, extra 'the -> set'
  778. // 11. If either:
  779. // - navigationType is not "traverse"; or
  780. // - traverseCanBeCanceled is true
  781. // the initialize event's cancelable to true. Otherwise, initialize it to false.
  782. event_init.cancelable = (navigation_type != Bindings::NavigationType::Traverse) || traverse_can_be_canceled;
  783. // 12. Initialize event's type to "navigate".
  784. // AD-HOC: Happens later, when calling the factory function
  785. // 13. Initialize event's navigationType to navigationType.
  786. event_init.navigation_type = navigation_type;
  787. // 14. Initialize event's destination to destination.
  788. event_init.destination = destination;
  789. // 15. Initialize event's downloadRequest to downloadRequestFilename.
  790. event_init.download_request = move(download_request_filename);
  791. // 16. If apiMethodTracker is not null, then initialize event's info to apiMethodTracker's info. Otherwise, initialize it to undefined.
  792. // NOTE: At this point apiMethodTracker's info is no longer needed and can be nulled out instead of keeping it alive for the lifetime of the navigation API method tracker.
  793. if (api_method_tracker) {
  794. event_init.info = api_method_tracker->info;
  795. api_method_tracker->info = JS::js_undefined();
  796. } else {
  797. event_init.info = JS::js_undefined();
  798. }
  799. // FIXME: 17: Initialize event's hasUAVisualTransition to true if a visual transition, to display a cached rendered state
  800. // of the document's latest entry, was done by the user agent. Otherwise, initialize it to false.
  801. event_init.has_ua_visual_transition = false;
  802. // 18. Set event's abort controller to a new AbortController created in navigation's relevant realm.
  803. // AD-HOC: Set on the NavigateEvent later after construction
  804. auto abort_controller = MUST(DOM::AbortController::construct_impl(realm));
  805. // 19. Initialize event's signal to event's abort controller's signal.
  806. event_init.signal = abort_controller->signal();
  807. // 20. Let currentURL be document's URL.
  808. auto current_url = document.url();
  809. // 21. If all of the following are true:
  810. // - destination's is same document is true;
  811. // - destination's URL equals currentURL with exclude fragments set to true; and
  812. // - destination's URL's fragment is not identical to currentURL's fragment,
  813. // then initialize event's hashChange to true. Otherwise, initialize it to false.
  814. event_init.hash_change = (destination->same_document()
  815. && destination->raw_url().equals(current_url, AK::URL::ExcludeFragment::Yes)
  816. && destination->raw_url().fragment() != current_url.fragment());
  817. // 22. If userInvolvement is not "none", then initialize event's userInitiated to true. Otherwise, initialize it to false.
  818. event_init.user_initiated = user_involvement != UserNavigationInvolvement::None;
  819. // 23. If formDataEntryList is not null, then initialize event's formData to a new FormData created in navigation's relevant realm,
  820. // associated to formDataEntryList. Otherwise, initialize it to null.
  821. if (form_data_entry_list.has_value()) {
  822. event_init.form_data = MUST(XHR::FormData::construct_impl(realm, form_data_entry_list.release_value()));
  823. } else {
  824. event_init.form_data = nullptr;
  825. }
  826. // AD-HOC: *Now* we have all the info required to create the event
  827. auto event = NavigateEvent::construct_impl(realm, EventNames::navigate, event_init);
  828. event->set_abort_controller(abort_controller);
  829. // AD-HOC: This is supposed to be set in "fire a <type> navigate event", and is only non-null when
  830. // we're doing a push or replace. We set it here because we create the event here
  831. event->set_classic_history_api_state(move(classic_history_api_state));
  832. // 24. Assert: navigation's ongoing navigate event is null.
  833. VERIFY(m_ongoing_navigate_event == nullptr);
  834. // 25. Set navigation's ongoing navigate event to event.
  835. m_ongoing_navigate_event = event;
  836. // 26. Set navigation's focus changed during ongoing navigation to false.
  837. m_focus_changed_during_ongoing_navigation = false;
  838. // 27. Set navigation's suppress normal scroll restoration during ongoing navigation to false.
  839. m_suppress_scroll_restoration_during_ongoing_navigation = false;
  840. // 28. Let dispatchResult be the result of dispatching event at navigation.
  841. auto dispatch_result = dispatch_event(*event);
  842. // 29. If dispatchResult is false:
  843. if (!dispatch_result) {
  844. // FIXME: 1. If navigationType is "traverse", then consume history-action user activation.
  845. // 2. If event's abort controller's signal is not aborted, then abort the ongoing navigation given navigation.
  846. if (!event->abort_controller()->signal()->aborted())
  847. abort_the_ongoing_navigation();
  848. // 3. Return false.
  849. return false;
  850. }
  851. // 30. Let endResultIsSameDocument be true if event's interception state
  852. // is not "none" or event's destination's is same document is true.
  853. bool const end_result_is_same_document = (event->interception_state() != NavigateEvent::InterceptionState::None) || event->destination()->same_document();
  854. // 31. Prepare to run script given navigation's relevant settings object.
  855. // NOTE: There's a massive spec note here
  856. relevant_settings_object(*this).prepare_to_run_script();
  857. // 32. If event's interception state is not "none":
  858. if (event->interception_state() != NavigateEvent::InterceptionState::None) {
  859. // 1. Set event's interception state to "committed".
  860. event->set_interception_state(NavigateEvent::InterceptionState::Committed);
  861. // 2. Let fromNHE be the current entry of navigation.
  862. auto from_nhe = current_entry();
  863. // 3. Assert: fromNHE is not null.
  864. VERIFY(from_nhe != nullptr);
  865. // 4. Set navigation's transition to a new NavigationTransition created in navigation's relevant realm,
  866. // whose navigation type is navigationType, from entry is fromNHE, and whose finished promise is a new promise
  867. // created in navigation's relevant realm.
  868. m_transition = NavigationTransition::create(realm, navigation_type, *from_nhe, JS::Promise::create(realm));
  869. // 5. Mark as handled navigation's transition's finished promise.
  870. m_transition->finished()->set_is_handled();
  871. // 6. If navigationType is "traverse", then set navigation's suppress normal scroll restoration during ongoing navigation to true.
  872. // NOTE: If event's scroll behavior was set to "after-transition", then scroll restoration will happen as part of finishing
  873. // the relevant NavigateEvent. Otherwise, there will be no scroll restoration. That is, no navigation which is intercepted
  874. // by intercept() goes through the normal scroll restoration process; scroll restoration for such navigations
  875. // is either done manually, by the web developer, or is done after the transition.
  876. if (navigation_type == Bindings::NavigationType::Traverse)
  877. m_suppress_scroll_restoration_during_ongoing_navigation = true;
  878. // FIXME: Fix spec typo "serialied"
  879. // 7. If navigationType is "push" or "replace", then run the URL and history update steps given document and
  880. // event's destination's URL, with serialiedData set to event's classic history API state and historyHandling
  881. // set to navigationType.
  882. // FIXME: Pass the serialized data to this algorithm
  883. if (navigation_type == Bindings::NavigationType::Push || navigation_type == Bindings::NavigationType::Replace) {
  884. auto history_handling = navigation_type == Bindings::NavigationType::Push ? HistoryHandlingBehavior::Push : HistoryHandlingBehavior::Replace;
  885. perform_url_and_history_update_steps(document, event->destination()->raw_url(), history_handling);
  886. }
  887. // Big spec note about reload here
  888. }
  889. // 33. If endResultIsSameDocument is true:
  890. if (end_result_is_same_document) {
  891. // 1. Let promisesList be an empty list.
  892. JS::MarkedVector<JS::NonnullGCPtr<WebIDL::Promise>> promises_list(realm.heap());
  893. // 2. For each handler of event's navigation handler list:
  894. for (auto const& handler : event->navigation_handler_list()) {
  895. // 1. Append the result of invoking handler with an empty arguments list to promisesList.
  896. auto result = WebIDL::invoke_callback(handler, {});
  897. if (result.is_abrupt()) {
  898. // FIXME: https://github.com/whatwg/html/issues/9774
  899. report_exception(result.release_error(), realm);
  900. continue;
  901. }
  902. // This *should* be equivalent to converting a promise to a promise capability
  903. promises_list.append(WebIDL::create_resolved_promise(realm, result.value().value()));
  904. }
  905. // 3. If promisesList's size is 0, then set promisesList to « a promise resolved with undefined ».
  906. // NOTE: There is a subtle timing difference between how waiting for all schedules its success and failure
  907. // steps when given zero promises versus ≥1 promises. For most uses of waiting for all, this does not matter.
  908. // However, with this API, there are so many events and promise handlers which could fire around the same time
  909. // that the difference is pretty easily observable: it can cause the event/promise handler sequence to vary.
  910. // (Some of the events and promises involved include: navigatesuccess / navigateerror, currententrychange,
  911. // dispose, apiMethodTracker's promises, and the navigation.transition.finished promise.)
  912. if (promises_list.size() == 0) {
  913. promises_list.append(WebIDL::create_resolved_promise(realm, JS::js_undefined()));
  914. }
  915. // 4. Wait for all of promisesList, with the following success steps:
  916. WebIDL::wait_for_all(
  917. realm, promises_list, [&](JS::MarkedVector<JS::Value> const&) -> void {
  918. // FIXME: Spec issue: Event's relevant global objects' *associated document*
  919. // 1. If event's relevant global object is not fully active, then abort these steps.
  920. if (!relevant_global_object.associated_document().is_fully_active())
  921. return;
  922. // 2. If event's abort controller's signal is aborted, then abort these steps.
  923. if (event->abort_controller()->signal()->aborted())
  924. return;
  925. // 3. Assert: event equals navigation's ongoing navigate event.
  926. VERIFY(event == m_ongoing_navigate_event);
  927. // 4. Set navigation's ongoing navigate event to null.
  928. m_ongoing_navigate_event = nullptr;
  929. // 5. Finish event given true.
  930. event->finish(true);
  931. // FIXME: Implement https://dom.spec.whatwg.org/#concept-event-fire somewhere
  932. // 6. Fire an event named navigatesuccess at navigation.
  933. dispatch_event(DOM::Event::create(realm, EventNames::navigatesuccess));
  934. // 7. If navigation's transition is not null, then resolve navigation's transition's finished promise with undefined.
  935. if (m_transition != nullptr)
  936. m_transition->finished()->fulfill(JS::js_undefined());
  937. // 8. Set navigation's transition to null.
  938. m_transition = nullptr;
  939. // 9. If apiMethodTracker is non-null, then resolve the finished promise for apiMethodTracker.
  940. if (api_method_tracker)
  941. resolve_the_finished_promise(*api_method_tracker); },
  942. // and the following failure steps given reason rejectionReason:
  943. [&](JS::Value rejection_reason) -> void {
  944. // FIXME: Spec issue: Event's relevant global objects' *associated document*
  945. // 1. If event's relevant global object is not fully active, then abort these steps.
  946. if (!relevant_global_object.associated_document().is_fully_active())
  947. return;
  948. // 2. If event's abort controller's signal is aborted, then abort these steps.
  949. if (event->abort_controller()->signal()->aborted())
  950. return;
  951. // 3. Assert: event equals navigation's ongoing navigate event.
  952. VERIFY(event == m_ongoing_navigate_event);
  953. // 4. Set navigation's ongoing navigate event to null.
  954. m_ongoing_navigate_event = nullptr;
  955. // 5. Finish event given false.
  956. event->finish(false);
  957. // 6. Fire an event named navigateerror at navigation using ErrorEvent, with error initialized to rejectionReason, and message,
  958. // filename, lineno, and colno initialized to appropriate values that can be extracted from rejectionReason in the same
  959. // underspecified way that the report the exception algorithm does.
  960. ErrorEventInit event_init = {};
  961. event_init.error = rejection_reason;
  962. // FIXME: Extract information from the exception and the JS context in the wishy-washy way the spec says here.
  963. event_init.filename = String {};
  964. event_init.colno = 0;
  965. event_init.lineno = 0;
  966. event_init.message = String {};
  967. dispatch_event(ErrorEvent::create(realm, EventNames::navigateerror, event_init));
  968. // 7. If navigation's transition is not null, then reject navigation's transition's finished promise with rejectionReason.
  969. if (m_transition)
  970. m_transition->finished()->reject(rejection_reason);
  971. // 8. Set navigation's transition to null.
  972. m_transition = nullptr;
  973. // 9. If apiMethodTracker is non-null, then reject the finished promise for apiMethodTracker with rejectionReason.
  974. if (api_method_tracker)
  975. reject_the_finished_promise(*api_method_tracker, rejection_reason);
  976. });
  977. }
  978. // 34. Otherwise, if apiMethodTracker is non-null, then clean up apiMethodTracker.
  979. else if (api_method_tracker) {
  980. clean_up(*api_method_tracker);
  981. }
  982. // 35. Clean up after running script given navigation's relevant settings object.
  983. relevant_settings_object(*this).clean_up_after_running_script();
  984. // 36. If event's interception state is "none", then return true.
  985. // 37. Return false.
  986. return event->interception_state() == NavigateEvent::InterceptionState::None;
  987. }
  988. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#fire-a-traverse-navigate-event
  989. bool Navigation::fire_a_traverse_navigate_event(JS::NonnullGCPtr<SessionHistoryEntry> destination_she, UserNavigationInvolvement user_involvement)
  990. {
  991. auto& realm = relevant_realm(*this);
  992. auto& vm = this->vm();
  993. // 1. Let event be the result of creating an event given NavigateEvent, in navigation's relevant realm.
  994. // 2. Set event's classic history API state to null.
  995. // AD-HOC: These are handled in the inner algorithm
  996. // 3. Let destination be a new NavigationDestination created in navigation's relevant realm.
  997. auto destination = NavigationDestination::create(realm);
  998. // 4. Set destination's URL to destinationSHE's URL.
  999. destination->set_url(destination_she->url);
  1000. // 5. Let destinationNHE be the NavigationHistoryEntry in navigation's entry list whose session history entry is destinationSHE,
  1001. // or null if no such NavigationHistoryEntry exists.
  1002. auto destination_nhe = m_entry_list.find_if([destination_she](auto& nhe) {
  1003. return &nhe->session_history_entry() == destination_she;
  1004. });
  1005. // 6. If destinationNHE is non-null, then:
  1006. if (destination_nhe != m_entry_list.end()) {
  1007. // 1. Set destination's entry to destinationNHE.
  1008. destination->set_entry(*destination_nhe);
  1009. // 2. Set destination's state to destinationSHE's navigation API state.
  1010. destination->set_state(destination_she->navigation_api_state);
  1011. }
  1012. // 7. Otherwise:
  1013. else {
  1014. // 1. Set destination's entry to null.
  1015. destination->set_entry(nullptr);
  1016. // 2. Set destination's state to StructuredSerializeForStorage(null).
  1017. destination->set_state(MUST(structured_serialize_for_storage(vm, JS::js_null())));
  1018. }
  1019. // 8. Set destination's is same document to true if destinationSHE's document is equal to
  1020. // navigation's relevant global object's associated Document; otherwise false.
  1021. destination->set_is_same_document(destination_she->document_state->document() == &verify_cast<Window>(relevant_global_object(*this)).associated_document());
  1022. // 9. Return the result of performing the inner navigate event firing algorithm given navigation, "traverse", event, destination, userInvolvement, null, and null.
  1023. // AD-HOC: We don't pass the event, but we do pass the classic_history_api state at the end to be set later
  1024. return inner_navigate_event_firing_algorithm(Bindings::NavigationType::Traverse, destination, user_involvement, {}, {}, {});
  1025. }
  1026. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#fire-a-push/replace/reload-navigate-event
  1027. bool Navigation::fire_a_push_replace_reload_navigate_event(
  1028. Bindings::NavigationType navigation_type,
  1029. AK::URL destination_url,
  1030. bool is_same_document,
  1031. UserNavigationInvolvement user_involvement,
  1032. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list,
  1033. Optional<SerializationRecord> navigation_api_state,
  1034. Optional<SerializationRecord> classic_history_api_state)
  1035. {
  1036. auto& realm = relevant_realm(*this);
  1037. auto& vm = this->vm();
  1038. // This fulfills the entry requirement: an optional serialized state navigationAPIState (default StructuredSerializeForStorage(null))
  1039. if (!navigation_api_state.has_value())
  1040. navigation_api_state = MUST(structured_serialize_for_storage(vm, JS::js_null()));
  1041. // 1. Let event be the result of creating an event given NavigateEvent, in navigation's relevant realm.
  1042. // 2. Set event's classic history API state to classicHistoryAPIState.
  1043. // AD-HOC: These are handled in the inner algorithm
  1044. // 3. Let destination be a new NavigationDestination created in navigation's relevant realm.
  1045. auto destination = NavigationDestination::create(realm);
  1046. // 4. Set destination's URL to destinationURL.
  1047. destination->set_url(destination_url);
  1048. // 5. Set destination's entry to null.
  1049. destination->set_entry(nullptr);
  1050. // 6. Set destination's state to navigationAPIState.
  1051. destination->set_state(*navigation_api_state);
  1052. // 7. Set destination's is same document to isSameDocument.
  1053. destination->set_is_same_document(is_same_document);
  1054. // 8. Return the result of performing the inner navigate event firing algorithm given navigation,
  1055. // navigationType, event, destination, userInvolvement, formDataEntryList, and null.
  1056. // AD-HOC: We don't pass the event, but we do pass the classic_history_api state at the end to be set later
  1057. return inner_navigate_event_firing_algorithm(navigation_type, destination, user_involvement, move(form_data_entry_list), {}, move(classic_history_api_state));
  1058. }
  1059. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#fire-a-download-request-navigate-event
  1060. bool Navigation::fire_a_download_request_navigate_event(AK::URL destination_url, UserNavigationInvolvement user_involvement, String filename)
  1061. {
  1062. auto& realm = relevant_realm(*this);
  1063. auto& vm = this->vm();
  1064. // 1. Let event be the result of creating an event given NavigateEvent, in navigation's relevant realm.
  1065. // 2. Set event's classic history API state to classicHistoryAPIState.
  1066. // AD-HOC: These are handled in the inner algorithm
  1067. // 3. Let destination be a new NavigationDestination created in navigation's relevant realm.
  1068. auto destination = NavigationDestination::create(realm);
  1069. // 4. Set destination's URL to destinationURL.
  1070. destination->set_url(destination_url);
  1071. // 5. Set destination's entry to null.
  1072. destination->set_entry(nullptr);
  1073. // 6. Set destination's state to StructuredSerializeForStorage(null).
  1074. destination->set_state(MUST(structured_serialize_for_storage(vm, JS::js_null())));
  1075. // 7. Set destination's is same document to false.
  1076. destination->set_is_same_document(false);
  1077. // 8. Return the result of performing the inner navigate event firing algorithm given navigation,
  1078. // "push", event, destination, userInvolvement, null, and filename.
  1079. // AD-HOC: We don't pass the event, but we do pass the classic_history_api state at the end to be set later
  1080. return inner_navigate_event_firing_algorithm(Bindings::NavigationType::Push, destination, user_involvement, {}, move(filename), {});
  1081. }
  1082. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#initialize-the-navigation-api-entries-for-a-new-document
  1083. void Navigation::initialize_the_navigation_api_entries_for_a_new_document(Vector<JS::NonnullGCPtr<SessionHistoryEntry>> const& new_shes, JS::NonnullGCPtr<SessionHistoryEntry> initial_she)
  1084. {
  1085. auto& realm = relevant_realm(*this);
  1086. // 1. Assert: navigation's entry list is empty.
  1087. VERIFY(m_entry_list.is_empty());
  1088. // 2. Assert: navigation's current entry index is −1.
  1089. VERIFY(m_current_entry_index == -1);
  1090. // 3. If navigation has entries and events disabled, then return.
  1091. if (has_entries_and_events_disabled())
  1092. return;
  1093. // 4. For each newSHE of newSHEs:
  1094. for (auto const& new_she : new_shes) {
  1095. // 1. Let newNHE be a new NavigationHistoryEntry created in the relevant realm of navigation.
  1096. // 2. Set newNHE's session history entry to newSHE.
  1097. auto new_nhe = NavigationHistoryEntry::create(realm, new_she);
  1098. // 3. Append newNHE to navigation's entry list.
  1099. m_entry_list.append(new_nhe);
  1100. }
  1101. // 5. Set navigation's current entry index to the result of getting the navigation API entry index of initialSHE within navigation.
  1102. m_current_entry_index = get_the_navigation_api_entry_index(*initial_she);
  1103. }
  1104. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#update-the-navigation-api-entries-for-a-same-document-navigation
  1105. void Navigation::update_the_navigation_api_entries_for_a_same_document_navigation(JS::NonnullGCPtr<SessionHistoryEntry> destination_she, Bindings::NavigationType navigation_type)
  1106. {
  1107. auto& realm = relevant_realm(*this);
  1108. // 1. If navigation has entries and events disabled, then return.
  1109. if (has_entries_and_events_disabled())
  1110. return;
  1111. // 2. Let oldCurrentNHE be the current entry of navigation.
  1112. auto old_current_nhe = current_entry();
  1113. // 3. Let disposedNHEs be a new empty list.
  1114. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> disposed_nhes;
  1115. // 4. If navigationType is "traverse", then:
  1116. if (navigation_type == Bindings::NavigationType::Traverse) {
  1117. // 1. Set navigation's current entry index to the result of getting the navigation API entry index of destinationSHE within navigation.
  1118. m_current_entry_index = get_the_navigation_api_entry_index(destination_she);
  1119. // 2. Assert: navigation's current entry index is not −1.
  1120. // NOTE: This algorithm is only called for same-document traversals.
  1121. // Cross-document traversals will instead call either initialize the navigation API entries for a new document
  1122. // or update the navigation API entries for reactivation
  1123. VERIFY(m_current_entry_index != -1);
  1124. }
  1125. // 5. Otherwise, if navigationType is "push", then:
  1126. else if (navigation_type == Bindings::NavigationType::Push) {
  1127. // 1. Set navigation's current entry index to navigation's current entry index + 1.
  1128. m_current_entry_index++;
  1129. // 2. Let i be navigation's current entry index.
  1130. auto i = m_current_entry_index;
  1131. // 3. While i < navigation's entry list's size:
  1132. while (i < static_cast<i64>(m_entry_list.size())) {
  1133. // 1. Append navigation's entry list[i] to disposedNHEs.
  1134. disposed_nhes.append(m_entry_list[i]);
  1135. // 2. Set i to i + 1.
  1136. ++i;
  1137. }
  1138. // 4. Remove all items in disposedNHEs from navigation's entry list.
  1139. m_entry_list.remove(m_current_entry_index, m_entry_list.size() - m_current_entry_index);
  1140. }
  1141. // 6. Otherwise, if navigationType is "replace", then:
  1142. else if (navigation_type == Bindings::NavigationType::Replace) {
  1143. VERIFY(old_current_nhe != nullptr);
  1144. // 1. Append oldCurrentNHE to disposedNHEs.
  1145. disposed_nhes.append(*old_current_nhe);
  1146. }
  1147. // 7. If navigationType is "push" or "replace", then:
  1148. if (navigation_type == Bindings::NavigationType::Push || navigation_type == Bindings::NavigationType::Replace) {
  1149. // 1. Let newNHE be a new NavigationHistoryEntry created in the relevant realm of navigation.
  1150. // 2. Set newNHE's session history entry to destinationSHE.
  1151. auto new_nhe = NavigationHistoryEntry::create(realm, destination_she);
  1152. VERIFY(m_current_entry_index != -1);
  1153. // 3. Set navigation's entry list[navigation's current entry index] to newNHE.
  1154. if (m_current_entry_index < static_cast<i64>(m_entry_list.size()))
  1155. m_entry_list[m_current_entry_index] = new_nhe;
  1156. else {
  1157. VERIFY(m_current_entry_index == static_cast<i64>(m_entry_list.size()));
  1158. m_entry_list.append(new_nhe);
  1159. }
  1160. }
  1161. // 8. If navigation's ongoing API method tracker is non-null, then notify about the committed-to entry
  1162. // given navigation's ongoing API method tracker and the current entry of navigation.
  1163. // NOTE: It is important to do this before firing the dispose or currententrychange events,
  1164. // since event handlers could start another navigation, or otherwise change the value of
  1165. // navigation's ongoing API method tracker.
  1166. if (m_ongoing_api_method_tracker != nullptr)
  1167. notify_about_the_committed_to_entry(*m_ongoing_api_method_tracker, *current_entry());
  1168. // 9. Prepare to run script given navigation's relevant settings object.
  1169. relevant_settings_object(*this).prepare_to_run_script();
  1170. // 10. Fire an event named currententrychange at navigation using NavigationCurrentEntryChangeEvent,
  1171. // with its navigationType attribute initialized to navigationType and its from initialized to oldCurrentNHE.
  1172. NavigationCurrentEntryChangeEventInit event_init = {};
  1173. event_init.navigation_type = navigation_type;
  1174. event_init.from = old_current_nhe;
  1175. dispatch_event(NavigationCurrentEntryChangeEvent::construct_impl(realm, EventNames::currententrychange, event_init));
  1176. // 11. For each disposedNHE of disposedNHEs:
  1177. for (auto& disposed_nhe : disposed_nhes) {
  1178. // 1. Fire an event named dispose at disposedNHE.
  1179. disposed_nhe->dispatch_event(DOM::Event::create(realm, EventNames::dispose, {}));
  1180. }
  1181. // 12. Clean up after running script given navigation's relevant settings object.
  1182. relevant_settings_object(*this).clean_up_after_running_script();
  1183. }
  1184. }