Navigation.cpp 75 KB

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