Navigation.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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::create(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#inner-navigate-event-firing-algorithm
  705. bool Navigation::inner_navigate_event_firing_algorithm(
  706. Bindings::NavigationType navigation_type,
  707. JS::NonnullGCPtr<NavigationDestination> destination,
  708. UserNavigationInvolvement user_involvement,
  709. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list,
  710. Optional<String> download_request_filename,
  711. Optional<SerializationRecord> classic_history_api_state)
  712. {
  713. auto& realm = relevant_realm(*this);
  714. // 1. If navigation has entries and events disabled, then:
  715. // NOTE: These assertions holds because traverseTo(), back(), and forward() will immediately fail when entries and events are disabled
  716. // (since there are no entries to traverse to), and if our starting point is instead navigate() or reload(),
  717. // then we avoided setting the upcoming non-traverse API method tracker in the first place.
  718. if (has_entries_and_events_disabled()) {
  719. // 1. Assert: navigation's ongoing API method tracker is null.
  720. VERIFY(m_ongoing_api_method_tracker == nullptr);
  721. // 2. Assert: navigation's upcoming non-traverse API method tracker is null.
  722. VERIFY(m_upcoming_non_traverse_api_method_tracker == nullptr);
  723. // 3. Assert: navigation's upcoming traverse API method trackers is empty.
  724. VERIFY(m_upcoming_traverse_api_method_trackers.is_empty());
  725. // 4. Return true.
  726. return true;
  727. }
  728. // 2. Let destinationKey be null.
  729. Optional<String> destination_key = {};
  730. // 3. If destination's entry is non-null, then set destinationKey to destination's entry's key.
  731. if (destination->navigation_history_entry() != nullptr)
  732. destination_key = destination->navigation_history_entry()->key();
  733. // 4. Assert: destinationKey is not the empty string.
  734. VERIFY(destination_key != ""sv);
  735. // 5. Promote an upcoming API method tracker to ongoing given navigation and destinationKey.
  736. promote_an_upcoming_api_method_tracker_to_ongoing(destination_key);
  737. // 6. Let apiMethodTracker be navigation's ongoing API method tracker.
  738. auto api_method_tracker = m_ongoing_api_method_tracker;
  739. // 7. Let navigable be navigation's relevant global object's navigable.
  740. auto& relevant_global_object = verify_cast<HTML::Window>(Web::HTML::relevant_global_object(*this));
  741. auto navigable = relevant_global_object.navigable();
  742. // 8. Let document be navigation's relevant global object's associated Document.
  743. auto& document = relevant_global_object.associated_document();
  744. // Note: We create the Event in this algorithm instead of passing it in,
  745. // and have all the following "initialize" steps set up the event init
  746. NavigateEventInit event_init = {};
  747. // 9. If document can have its URL rewritten to destination's URL,
  748. // and either destination's is same document is true or navigationType is not "traverse",
  749. // then initialize event's canIntercept to true. Otherwise, initialize it to false.
  750. event_init.can_intercept = can_have_its_url_rewritten(document, destination->raw_url()) && (destination->same_document() || navigation_type != Bindings::NavigationType::Traverse);
  751. // 10. Let traverseCanBeCanceled be true if all of the following are true:
  752. // - navigable is a top-level traversable;
  753. // - destination's is same document is true; and
  754. // - either userInvolvement is not "browser UI", or navigation's relevant global object has transient activation.
  755. // Otherwise, let it be false.
  756. bool const traverse_can_be_canceled = navigable->is_top_level_traversable()
  757. && destination->same_document()
  758. && (user_involvement != UserNavigationInvolvement::BrowserUI || relevant_global_object.has_transient_activation());
  759. // FIXME: Fix spec grammaro, extra 'the -> set'
  760. // 11. If either:
  761. // - navigationType is not "traverse"; or
  762. // - traverseCanBeCanceled is true
  763. // the initialize event's cancelable to true. Otherwise, initialize it to false.
  764. event_init.cancelable = (navigation_type != Bindings::NavigationType::Traverse) || traverse_can_be_canceled;
  765. // 12. Initialize event's type to "navigate".
  766. // AD-HOC: Happens later, when calling the factory function
  767. // 13. Initialize event's navigationType to navigationType.
  768. event_init.navigation_type = navigation_type;
  769. // 14. Initialize event's destination to destination.
  770. event_init.destination = destination;
  771. // 15. Initialize event's downloadRequest to downloadRequestFilename.
  772. event_init.download_request = move(download_request_filename);
  773. // 16. If apiMethodTracker is not null, then initialize event's info to apiMethodTracker's info. Otherwise, initialize it to undefined.
  774. // 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.
  775. if (api_method_tracker) {
  776. event_init.info = api_method_tracker->info;
  777. api_method_tracker->info = JS::js_undefined();
  778. } else {
  779. event_init.info = JS::js_undefined();
  780. }
  781. // FIXME: 17: Initialize event's hasUAVisualTransition to true if a visual transition, to display a cached rendered state
  782. // of the document's latest entry, was done by the user agent. Otherwise, initialize it to false.
  783. event_init.has_ua_visual_transition = false;
  784. // 18. Set event's abort controller to a new AbortController created in navigation's relevant realm.
  785. // AD-HOC: Set on the NavigateEvent later after construction
  786. auto abort_controller = MUST(DOM::AbortController::construct_impl(realm));
  787. // 19. Initialize event's signal to event's abort controller's signal.
  788. event_init.signal = abort_controller->signal();
  789. // 20. Let currentURL be document's URL.
  790. auto current_url = document.url();
  791. // 21. If all of the following are true:
  792. // - destination's is same document is true;
  793. // - destination's URL equals currentURL with exclude fragments set to true; and
  794. // - destination's URL's fragment is not identical to currentURL's fragment,
  795. // then initialize event's hashChange to true. Otherwise, initialize it to false.
  796. event_init.hash_change = (destination->same_document()
  797. && destination->raw_url().equals(current_url, AK::URL::ExcludeFragment::Yes)
  798. && destination->raw_url().fragment() != current_url.fragment());
  799. // 22. If userInvolvement is not "none", then initialize event's userInitiated to true. Otherwise, initialize it to false.
  800. event_init.user_initiated = user_involvement != UserNavigationInvolvement::None;
  801. // 23. If formDataEntryList is not null, then initialize event's formData to a new FormData created in navigation's relevant realm,
  802. // associated to formDataEntryList. Otherwise, initialize it to null.
  803. if (form_data_entry_list.has_value()) {
  804. event_init.form_data = MUST(XHR::FormData::construct_impl(realm, form_data_entry_list.release_value()));
  805. } else {
  806. event_init.form_data = nullptr;
  807. }
  808. // AD-HOC: *Now* we have all the info required to create the event
  809. auto event = NavigateEvent::construct_impl(realm, EventNames::navigate, event_init);
  810. event->set_abort_controller(abort_controller);
  811. // AD-HOC: This is supposed to be set in "fire a <type> navigate event", and is only non-null when
  812. // we're doing a push or replace. We set it here because we create the event here
  813. event->set_classic_history_api_state(move(classic_history_api_state));
  814. // 24. Assert: navigation's ongoing navigate event is null.
  815. VERIFY(m_ongoing_navigate_event == nullptr);
  816. // 25. Set navigation's ongoing navigate event to event.
  817. m_ongoing_navigate_event = event;
  818. // 26. Set navigation's focus changed during ongoing navigation to false.
  819. m_focus_changed_during_ongoing_navigation = false;
  820. // 27. Set navigation's suppress normal scroll restoration during ongoing navigation to false.
  821. m_suppress_scroll_restoration_during_ongoing_navigation = false;
  822. // 28. Let dispatchResult be the result of dispatching event at navigation.
  823. auto dispatch_result = dispatch_event(*event);
  824. // 29. If dispatchResult is false:
  825. if (!dispatch_result) {
  826. // FIXME: 1. If navigationType is "traverse", then consume history-action user activation.
  827. // 2. If event's abort controller's signal is not aborted, then abort the ongoing navigation given navigation.
  828. if (!event->abort_controller()->signal()->aborted())
  829. abort_the_ongoing_navigation();
  830. // 3. Return false.
  831. return false;
  832. }
  833. // 30. Let endResultIsSameDocument be true if event's interception state
  834. // is not "none" or event's destination's is same document is true.
  835. bool const end_result_is_same_document = (event->interception_state() != NavigateEvent::InterceptionState::None) || event->destination()->same_document();
  836. // 31. Prepare to run script given navigation's relevant settings object.
  837. // NOTE: There's a massive spec note here
  838. relevant_settings_object(*this).prepare_to_run_script();
  839. // 32. If event's interception state is not "none":
  840. if (event->interception_state() != NavigateEvent::InterceptionState::None) {
  841. // 1. Set event's interception state to "committed".
  842. event->set_interception_state(NavigateEvent::InterceptionState::Committed);
  843. // 2. Let fromNHE be the current entry of navigation.
  844. auto from_nhe = current_entry();
  845. // 3. Assert: fromNHE is not null.
  846. VERIFY(from_nhe != nullptr);
  847. // 4. Set navigation's transition to a new NavigationTransition created in navigation's relevant realm,
  848. // whose navigation type is navigationType, from entry is fromNHE, and whose finished promise is a new promise
  849. // created in navigation's relevant realm.
  850. m_transition = NavigationTransition::create(realm, navigation_type, *from_nhe, JS::Promise::create(realm));
  851. // 5. Mark as handled navigation's transition's finished promise.
  852. m_transition->finished()->set_is_handled();
  853. // 6. If navigationType is "traverse", then set navigation's suppress normal scroll restoration during ongoing navigation to true.
  854. // NOTE: If event's scroll behavior was set to "after-transition", then scroll restoration will happen as part of finishing
  855. // the relevant NavigateEvent. Otherwise, there will be no scroll restoration. That is, no navigation which is intercepted
  856. // by intercept() goes through the normal scroll restoration process; scroll restoration for such navigations
  857. // is either done manually, by the web developer, or is done after the transition.
  858. if (navigation_type == Bindings::NavigationType::Traverse)
  859. m_suppress_scroll_restoration_during_ongoing_navigation = true;
  860. // FIXME: Fix spec typo "serialied"
  861. // 7. If navigationType is "push" or "replace", then run the URL and history update steps given document and
  862. // event's destination's URL, with serialiedData set to event's classic history API state and historyHandling
  863. // set to navigationType.
  864. // FIXME: Pass the serialized data to this algorithm
  865. if (navigation_type == Bindings::NavigationType::Push || navigation_type == Bindings::NavigationType::Replace) {
  866. auto history_handling = navigation_type == Bindings::NavigationType::Push ? HistoryHandlingBehavior::Push : HistoryHandlingBehavior::Replace;
  867. perform_url_and_history_update_steps(document, event->destination()->raw_url(), history_handling);
  868. }
  869. // Big spec note about reload here
  870. }
  871. // 33. If endResultIsSameDocument is true:
  872. if (end_result_is_same_document) {
  873. // 1. Let promisesList be an empty list.
  874. JS::MarkedVector<JS::NonnullGCPtr<WebIDL::Promise>> promises_list(realm.heap());
  875. // 2. For each handler of event's navigation handler list:
  876. for (auto const& handler : event->navigation_handler_list()) {
  877. // 1. Append the result of invoking handler with an empty arguments list to promisesList.
  878. auto result = WebIDL::invoke_callback(handler, {});
  879. if (result.is_abrupt()) {
  880. // FIXME: https://github.com/whatwg/html/issues/9774
  881. report_exception(result.release_error(), realm);
  882. continue;
  883. }
  884. // This *should* be equivalent to converting a promise to a promise capability
  885. promises_list.append(WebIDL::create_resolved_promise(realm, result.value().value()));
  886. }
  887. // 3. If promisesList's size is 0, then set promisesList to « a promise resolved with undefined ».
  888. // NOTE: There is a subtle timing difference between how waiting for all schedules its success and failure
  889. // steps when given zero promises versus ≥1 promises. For most uses of waiting for all, this does not matter.
  890. // However, with this API, there are so many events and promise handlers which could fire around the same time
  891. // that the difference is pretty easily observable: it can cause the event/promise handler sequence to vary.
  892. // (Some of the events and promises involved include: navigatesuccess / navigateerror, currententrychange,
  893. // dispose, apiMethodTracker's promises, and the navigation.transition.finished promise.)
  894. if (promises_list.size() == 0) {
  895. promises_list.append(WebIDL::create_resolved_promise(realm, JS::js_undefined()));
  896. }
  897. // 4. Wait for all of promisesList, with the following success steps:
  898. WebIDL::wait_for_all(
  899. realm, promises_list, [&](JS::MarkedVector<JS::Value> const&) -> void {
  900. // FIXME: Spec issue: Event's relevant global objects' *associated document*
  901. // 1. If event's relevant global object is not fully active, then abort these steps.
  902. if (!relevant_global_object.associated_document().is_fully_active())
  903. return;
  904. // 2. If event's abort controller's signal is aborted, then abort these steps.
  905. if (event->abort_controller()->signal()->aborted())
  906. return;
  907. // 3. Assert: event equals navigation's ongoing navigate event.
  908. VERIFY(event == m_ongoing_navigate_event);
  909. // 4. Set navigation's ongoing navigate event to null.
  910. m_ongoing_navigate_event = nullptr;
  911. // 5. Finish event given true.
  912. event->finish(true);
  913. // FIXME: Implement https://dom.spec.whatwg.org/#concept-event-fire somewhere
  914. // 6. Fire an event named navigatesuccess at navigation.
  915. dispatch_event(DOM::Event::create(realm, EventNames::navigatesuccess));
  916. // 7. If navigation's transition is not null, then resolve navigation's transition's finished promise with undefined.
  917. if (m_transition != nullptr)
  918. m_transition->finished()->fulfill(JS::js_undefined());
  919. // 8. Set navigation's transition to null.
  920. m_transition = nullptr;
  921. // 9. If apiMethodTracker is non-null, then resolve the finished promise for apiMethodTracker.
  922. if (api_method_tracker)
  923. resolve_the_finished_promise(*api_method_tracker); },
  924. // and the following failure steps given reason rejectionReason:
  925. [&](JS::Value rejection_reason) -> void {
  926. // FIXME: Spec issue: Event's relevant global objects' *associated document*
  927. // 1. If event's relevant global object is not fully active, then abort these steps.
  928. if (!relevant_global_object.associated_document().is_fully_active())
  929. return;
  930. // 2. If event's abort controller's signal is aborted, then abort these steps.
  931. if (event->abort_controller()->signal()->aborted())
  932. return;
  933. // 3. Assert: event equals navigation's ongoing navigate event.
  934. VERIFY(event == m_ongoing_navigate_event);
  935. // 4. Set navigation's ongoing navigate event to null.
  936. m_ongoing_navigate_event = nullptr;
  937. // 5. Finish event given false.
  938. event->finish(false);
  939. // 6. Fire an event named navigateerror at navigation using ErrorEvent, with error initialized to rejectionReason, and message,
  940. // filename, lineno, and colno initialized to appropriate values that can be extracted from rejectionReason in the same
  941. // underspecified way that the report the exception algorithm does.
  942. ErrorEventInit event_init = {};
  943. event_init.error = rejection_reason;
  944. // FIXME: Extract information from the exception and the JS context in the wishy-washy way the spec says here.
  945. event_init.filename = String {};
  946. event_init.colno = 0;
  947. event_init.lineno = 0;
  948. event_init.message = String {};
  949. dispatch_event(ErrorEvent::create(realm, EventNames::navigateerror, event_init));
  950. // 7. If navigation's transition is not null, then reject navigation's transition's finished promise with rejectionReason.
  951. if (m_transition)
  952. m_transition->finished()->reject(rejection_reason);
  953. // 8. Set navigation's transition to null.
  954. m_transition = nullptr;
  955. // 9. If apiMethodTracker is non-null, then reject the finished promise for apiMethodTracker with rejectionReason.
  956. if (api_method_tracker)
  957. reject_the_finished_promise(*api_method_tracker, rejection_reason);
  958. });
  959. }
  960. // 34. Otherwise, if apiMethodTracker is non-null, then clean up apiMethodTracker.
  961. else if (api_method_tracker) {
  962. clean_up(*api_method_tracker);
  963. }
  964. // 35. Clean up after running script given navigation's relevant settings object.
  965. relevant_settings_object(*this).clean_up_after_running_script();
  966. // 36. If event's interception state is "none", then return true.
  967. // 37. Return false.
  968. return event->interception_state() == NavigateEvent::InterceptionState::None;
  969. }
  970. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#fire-a-traverse-navigate-event
  971. bool Navigation::fire_a_traverse_navigate_event(JS::NonnullGCPtr<SessionHistoryEntry> destination_she, UserNavigationInvolvement user_involvement)
  972. {
  973. auto& realm = relevant_realm(*this);
  974. auto& vm = this->vm();
  975. // 1. Let event be the result of creating an event given NavigateEvent, in navigation's relevant realm.
  976. // 2. Set event's classic history API state to null.
  977. // AD-HOC: These are handled in the inner algorithm
  978. // 3. Let destination be a new NavigationDestination created in navigation's relevant realm.
  979. auto destination = NavigationDestination::create(realm);
  980. // 4. Set destination's URL to destinationSHE's URL.
  981. destination->set_url(destination_she->url);
  982. // 5. Let destinationNHE be the NavigationHistoryEntry in navigation's entry list whose session history entry is destinationSHE,
  983. // or null if no such NavigationHistoryEntry exists.
  984. auto destination_nhe = m_entry_list.find_if([destination_she](auto& nhe) {
  985. return &nhe->session_history_entry() == destination_she;
  986. });
  987. // 6. If destinationNHE is non-null, then:
  988. if (destination_nhe != m_entry_list.end()) {
  989. // 1. Set destination's entry to destinationNHE.
  990. destination->set_entry(*destination_nhe);
  991. // 2. Set destination's state to destinationSHE's navigation API state.
  992. destination->set_state(destination_she->navigation_api_state);
  993. }
  994. // 7. Otherwise:
  995. else {
  996. // 1. Set destination's entry to null.
  997. destination->set_entry(nullptr);
  998. // 2. Set destination's state to StructuredSerializeForStorage(null).
  999. destination->set_state(MUST(structured_serialize_for_storage(vm, JS::js_null())));
  1000. }
  1001. // 8. Set destination's is same document to true if destinationSHE's document is equal to
  1002. // navigation's relevant global object's associated Document; otherwise false.
  1003. destination->set_is_same_document(destination_she->document_state->document() == &verify_cast<Window>(relevant_global_object(*this)).associated_document());
  1004. // 9. Return the result of performing the inner navigate event firing algorithm given navigation, "traverse", event, destination, userInvolvement, null, and null.
  1005. // AD-HOC: We don't pass the event, but we do pass the classic_history_api state at the end to be set later
  1006. return inner_navigate_event_firing_algorithm(Bindings::NavigationType::Traverse, destination, user_involvement, {}, {}, {});
  1007. }
  1008. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#fire-a-push/replace/reload-navigate-event
  1009. bool Navigation::fire_a_push_replace_reload_navigate_event(
  1010. Bindings::NavigationType navigation_type,
  1011. AK::URL destination_url,
  1012. bool is_same_document,
  1013. UserNavigationInvolvement user_involvement,
  1014. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list,
  1015. Optional<SerializationRecord> navigation_api_state,
  1016. Optional<SerializationRecord> classic_history_api_state)
  1017. {
  1018. auto& realm = relevant_realm(*this);
  1019. auto& vm = this->vm();
  1020. // This fulfills the entry requirement: an optional serialized state navigationAPIState (default StructuredSerializeForStorage(null))
  1021. if (!navigation_api_state.has_value())
  1022. navigation_api_state = MUST(structured_serialize_for_storage(vm, JS::js_null()));
  1023. // 1. Let event be the result of creating an event given NavigateEvent, in navigation's relevant realm.
  1024. // 2. Set event's classic history API state to classicHistoryAPIState.
  1025. // AD-HOC: These are handled in the inner algorithm
  1026. // 3. Let destination be a new NavigationDestination created in navigation's relevant realm.
  1027. auto destination = NavigationDestination::create(realm);
  1028. // 4. Set destination's URL to destinationURL.
  1029. destination->set_url(destination_url);
  1030. // 5. Set destination's entry to null.
  1031. destination->set_entry(nullptr);
  1032. // 6. Set destination's state to navigationAPIState.
  1033. destination->set_state(*navigation_api_state);
  1034. // 7. Set destination's is same document to isSameDocument.
  1035. destination->set_is_same_document(is_same_document);
  1036. // 8. Return the result of performing the inner navigate event firing algorithm given navigation,
  1037. // navigationType, event, destination, userInvolvement, formDataEntryList, and null.
  1038. // AD-HOC: We don't pass the event, but we do pass the classic_history_api state at the end to be set later
  1039. return inner_navigate_event_firing_algorithm(navigation_type, destination, user_involvement, move(form_data_entry_list), {}, move(classic_history_api_state));
  1040. }
  1041. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#fire-a-download-request-navigate-event
  1042. bool Navigation::fire_a_download_request_navigate_event(AK::URL destination_url, UserNavigationInvolvement user_involvement, String filename)
  1043. {
  1044. auto& realm = relevant_realm(*this);
  1045. auto& vm = this->vm();
  1046. // 1. Let event be the result of creating an event given NavigateEvent, in navigation's relevant realm.
  1047. // 2. Set event's classic history API state to classicHistoryAPIState.
  1048. // AD-HOC: These are handled in the inner algorithm
  1049. // 3. Let destination be a new NavigationDestination created in navigation's relevant realm.
  1050. auto destination = NavigationDestination::create(realm);
  1051. // 4. Set destination's URL to destinationURL.
  1052. destination->set_url(destination_url);
  1053. // 5. Set destination's entry to null.
  1054. destination->set_entry(nullptr);
  1055. // 6. Set destination's state to StructuredSerializeForStorage(null).
  1056. destination->set_state(MUST(structured_serialize_for_storage(vm, JS::js_null())));
  1057. // 7. Set destination's is same document to false.
  1058. destination->set_is_same_document(false);
  1059. // 8. Return the result of performing the inner navigate event firing algorithm given navigation,
  1060. // "push", event, destination, userInvolvement, null, and filename.
  1061. // AD-HOC: We don't pass the event, but we do pass the classic_history_api state at the end to be set later
  1062. return inner_navigate_event_firing_algorithm(Bindings::NavigationType::Push, destination, user_involvement, {}, move(filename), {});
  1063. }
  1064. }