Navigation.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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/ErrorEvent.h>
  15. #include <LibWeb/HTML/NavigateEvent.h>
  16. #include <LibWeb/HTML/Navigation.h>
  17. #include <LibWeb/HTML/NavigationCurrentEntryChangeEvent.h>
  18. #include <LibWeb/HTML/NavigationHistoryEntry.h>
  19. #include <LibWeb/HTML/NavigationTransition.h>
  20. #include <LibWeb/HTML/TraversableNavigable.h>
  21. #include <LibWeb/HTML/Window.h>
  22. namespace Web::HTML {
  23. static NavigationResult navigation_api_method_tracker_derived_result(JS::NonnullGCPtr<NavigationAPIMethodTracker> api_method_tracker);
  24. NavigationAPIMethodTracker::NavigationAPIMethodTracker(JS::NonnullGCPtr<Navigation> navigation,
  25. Optional<String> key,
  26. JS::Value info,
  27. Optional<SerializationRecord> serialized_state,
  28. JS::GCPtr<NavigationHistoryEntry> commited_to_entry,
  29. JS::NonnullGCPtr<WebIDL::Promise> committed_promise,
  30. JS::NonnullGCPtr<WebIDL::Promise> finished_promise)
  31. : navigation(navigation)
  32. , key(move(key))
  33. , info(info)
  34. , serialized_state(move(serialized_state))
  35. , commited_to_entry(commited_to_entry)
  36. , committed_promise(committed_promise)
  37. , finished_promise(finished_promise)
  38. {
  39. }
  40. void NavigationAPIMethodTracker::visit_edges(Cell::Visitor& visitor)
  41. {
  42. Base::visit_edges(visitor);
  43. visitor.visit(navigation);
  44. visitor.visit(info);
  45. visitor.visit(commited_to_entry);
  46. visitor.visit(committed_promise);
  47. visitor.visit(finished_promise);
  48. }
  49. JS::NonnullGCPtr<Navigation> Navigation::create(JS::Realm& realm)
  50. {
  51. return realm.heap().allocate<Navigation>(realm, realm);
  52. }
  53. Navigation::Navigation(JS::Realm& realm)
  54. : DOM::EventTarget(realm)
  55. {
  56. }
  57. Navigation::~Navigation() = default;
  58. void Navigation::initialize(JS::Realm& realm)
  59. {
  60. Base::initialize(realm);
  61. set_prototype(&Bindings::ensure_web_prototype<Bindings::NavigationPrototype>(realm, "Navigation"));
  62. }
  63. void Navigation::visit_edges(JS::Cell::Visitor& visitor)
  64. {
  65. Base::visit_edges(visitor);
  66. for (auto& entry : m_entry_list)
  67. visitor.visit(entry);
  68. visitor.visit(m_transition);
  69. visitor.visit(m_ongoing_navigate_event);
  70. visitor.visit(m_ongoing_api_method_tracker);
  71. visitor.visit(m_upcoming_non_traverse_api_method_tracker);
  72. for (auto& key_and_tracker : m_upcoming_traverse_api_method_trackers)
  73. visitor.visit(key_and_tracker.value);
  74. }
  75. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-entries
  76. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> Navigation::entries() const
  77. {
  78. // The entries() method steps are:
  79. // 1. If this has entries and events disabled, then return the empty list.
  80. if (has_entries_and_events_disabled())
  81. return {};
  82. // 2. Return this's entry list.
  83. // NOTE: Recall that because of Web IDL's sequence type conversion rules,
  84. // this will create a new JavaScript array object on each call.
  85. // That is, navigation.entries() !== navigation.entries().
  86. return m_entry_list;
  87. }
  88. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry
  89. JS::GCPtr<NavigationHistoryEntry> Navigation::current_entry() const
  90. {
  91. // The current entry of a Navigation navigation is the result of running the following steps:
  92. // 1. If navigation has entries and events disabled, then return null.
  93. if (has_entries_and_events_disabled())
  94. return nullptr;
  95. // FIXME 2. Assert: navigation's current entry index is not −1.
  96. // FIXME: This should not happen once Navigable's algorithms properly set up NHEs.
  97. if (m_current_entry_index == -1)
  98. return nullptr;
  99. // 3. Return navigation's entry list[navigation's current entry index].
  100. return m_entry_list[m_current_entry_index];
  101. }
  102. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-updatecurrententry
  103. WebIDL::ExceptionOr<void> Navigation::update_current_entry(NavigationUpdateCurrentEntryOptions options)
  104. {
  105. // The updateCurrentEntry(options) method steps are:
  106. // 1. Let current be the current entry of this.
  107. auto current = current_entry();
  108. // 2. If current is null, then throw an "InvalidStateError" DOMException.
  109. if (current == nullptr)
  110. return WebIDL::InvalidStateError::create(realm(), "Cannot update current NavigationHistoryEntry when there is no current entry"_fly_string);
  111. // 3. Let serializedState be StructuredSerializeForStorage(options["state"]), rethrowing any exceptions.
  112. auto serialized_state = TRY(structured_serialize_for_storage(vm(), options.state));
  113. // 4. Set current's session history entry's navigation API state to serializedState.
  114. current->session_history_entry().navigation_api_state = serialized_state;
  115. // 5. Fire an event named currententrychange at this using NavigationCurrentEntryChangeEvent,
  116. // with its navigationType attribute initialized to null and its from initialized to current.
  117. NavigationCurrentEntryChangeEventInit event_init = {};
  118. event_init.navigation_type = {};
  119. event_init.from = current;
  120. dispatch_event(HTML::NavigationCurrentEntryChangeEvent::create(realm(), HTML::EventNames::currententrychange, event_init));
  121. return {};
  122. }
  123. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-cangoback
  124. bool Navigation::can_go_back() const
  125. {
  126. // The canGoBack getter steps are:
  127. // 1. If this has entries and events disabled, then return false.
  128. if (has_entries_and_events_disabled())
  129. return false;
  130. // FIXME 2. Assert: navigation's current entry index is not −1.
  131. // FIXME: This should not happen once Navigable's algorithms properly set up NHEs.
  132. if (m_current_entry_index == -1)
  133. return false;
  134. // 3. If this's current entry index is 0, then return false.
  135. // 4. Return true.
  136. return (m_current_entry_index != 0);
  137. }
  138. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-cangoforward
  139. bool Navigation::can_go_forward() const
  140. {
  141. // The canGoForward getter steps are:
  142. // 1. If this has entries and events disabled, then return false.
  143. if (has_entries_and_events_disabled())
  144. return false;
  145. // FIXME 2. Assert: navigation's current entry index is not −1.
  146. // FIXME: This should not happen once Navigable's algorithms properly set up NHEs.
  147. if (m_current_entry_index == -1)
  148. return false;
  149. // 3. If this's current entry index is equal to this's entry list's size, then return false.
  150. // 4. Return true.
  151. return (m_current_entry_index != static_cast<i64>(m_entry_list.size()));
  152. }
  153. HistoryHandlingBehavior to_history_handling_behavior(Bindings::NavigationHistoryBehavior b)
  154. {
  155. // A history handling behavior is a NavigationHistoryBehavior that is either "push" or "replace",
  156. // i.e., that has been resolved away from any initial "auto" value.
  157. VERIFY(b != Bindings::NavigationHistoryBehavior::Auto);
  158. switch (b) {
  159. case Bindings::NavigationHistoryBehavior::Push:
  160. return HistoryHandlingBehavior::Push;
  161. case Bindings::NavigationHistoryBehavior::Replace:
  162. return HistoryHandlingBehavior::Replace;
  163. case Bindings::NavigationHistoryBehavior::Auto:
  164. VERIFY_NOT_REACHED();
  165. };
  166. VERIFY_NOT_REACHED();
  167. }
  168. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-navigate
  169. WebIDL::ExceptionOr<NavigationResult> Navigation::navigate(String url, NavigationNavigateOptions const& options)
  170. {
  171. auto& realm = this->realm();
  172. auto& vm = this->vm();
  173. // The navigate(options) method steps are:
  174. // 1. Parse url relative to this's relevant settings object.
  175. // If that returns failure, then return an early error result for a "SyntaxError" DOMException.
  176. // Otherwise, let urlRecord be the resulting URL record.
  177. auto url_record = relevant_settings_object(*this).parse_url(url);
  178. if (!url_record.is_valid())
  179. return early_error_result(WebIDL::SyntaxError::create(realm, "Cannot navigate to Invalid URL"_fly_string));
  180. // 2. Let document be this's relevant global object's associated Document.
  181. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  182. // 3. If options["history"] is "push", and the navigation must be a replace given urlRecord and document,
  183. // then return an early error result for a "NotSupportedError" DOMException.
  184. if (options.history == Bindings::NavigationHistoryBehavior::Push && navigation_must_be_a_replace(url_record, document))
  185. return early_error_result(WebIDL::NotSupportedError::create(realm, "Navigation must be a replace, but push was requested"_fly_string));
  186. // 4. Let state be options["state"], if it exists; otherwise, undefined.
  187. auto state = options.state.value_or(JS::js_undefined());
  188. // 5. Let serializedState be StructuredSerializeForStorage(state).
  189. // If this throws an exception, then return an early error result for that exception.
  190. // FIXME: Fix this spec grammaro in the note
  191. // NOTE: It is importantly to perform this step early, since serialization can invoke web developer code,
  192. // which in turn might change various things we check in later steps.
  193. auto serialized_state_or_error = structured_serialize_for_storage(vm, state);
  194. if (serialized_state_or_error.is_error()) {
  195. return early_error_result(serialized_state_or_error.release_error());
  196. }
  197. auto serialized_state = serialized_state_or_error.release_value();
  198. // 6. If document is not fully active, then return an early error result for an "InvalidStateError" DOMException.
  199. if (!document.is_fully_active())
  200. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document is not fully active"_fly_string));
  201. // 7. If document's unload counter is greater than 0, then return an early error result for an "InvalidStateError" DOMException.
  202. if (document.unload_counter() > 0)
  203. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document already unloaded"_fly_string));
  204. // 8. Let info be options["info"], if it exists; otherwise, undefined.
  205. auto info = options.info.value_or(JS::js_undefined());
  206. // 9. Let apiMethodTracker be the result of maybe setting the upcoming non-traverse API method tracker for this
  207. // given info and serializedState.
  208. auto api_method_tracker = maybe_set_the_upcoming_non_traverse_api_method_tracker(info, serialized_state);
  209. // 10. Navigate document's node navigable to urlRecord using document,
  210. // with historyHandling set to options["history"] and navigationAPIState set to serializedState.
  211. // FIXME: Fix spec typo here
  212. // NOTE: Unlike location.assign() and friends, which are exposed across origin-domain boundaries,
  213. // navigation.navigate() can only be accessed by code with direct synchronous access to the
  214. /// window.navigation property. Thus, we avoid the complications about attributing the source document
  215. // of the navigation, and we don't need to deal with the allowed by sandboxing to navigate check and its
  216. // acccompanying exceptionsEnabled flag. We just treat all navigations as if they come from the Document
  217. // corresponding to this Navigation object itself (i.e., document).
  218. [[maybe_unused]] auto history_handling_behavior = to_history_handling_behavior(options.history);
  219. // FIXME: Actually call navigate once Navigables are implemented enough to guarantee a node navigable on
  220. // an active document that's not being unloaded.
  221. // document.navigable().navigate(url, document, history behavior, state)
  222. // 11. If this's upcoming non-traverse API method tracker is apiMethodTracker, then:
  223. // NOTE: If the upcoming non-traverse API method tracker is still apiMethodTracker, this means that the navigate
  224. // algorithm bailed out before ever getting to the inner navigate event firing algorithm which would promote
  225. // that upcoming API method tracker to ongoing.
  226. if (m_upcoming_non_traverse_api_method_tracker == api_method_tracker) {
  227. m_upcoming_non_traverse_api_method_tracker = nullptr;
  228. return early_error_result(WebIDL::AbortError::create(realm, "Navigation aborted"_fly_string));
  229. }
  230. // 12. Return a navigation API method tracker-derived result for apiMethodTracker.
  231. return navigation_api_method_tracker_derived_result(api_method_tracker);
  232. }
  233. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-reload
  234. WebIDL::ExceptionOr<NavigationResult> Navigation::reload(NavigationReloadOptions const& options)
  235. {
  236. auto& realm = this->realm();
  237. auto& vm = this->vm();
  238. // The reload(options) method steps are:
  239. // 1. Let document be this's relevant global object's associated Document.
  240. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  241. // 2. Let serializedState be StructuredSerializeForStorage(undefined).
  242. auto serialized_state = MUST(structured_serialize_for_storage(vm, JS::js_undefined()));
  243. // 3. If options["state"] exists, then set serializedState to StructuredSerializeForStorage(options["state"]).
  244. // If this throws an exception, then return an early error result for that exception.
  245. // NOTE: It is importantly to perform this step early, since serialization can invoke web developer
  246. // code, which in turn might change various things we check in later steps.
  247. if (options.state.has_value()) {
  248. auto serialized_state_or_error = structured_serialize_for_storage(vm, options.state.value());
  249. if (serialized_state_or_error.is_error())
  250. return early_error_result(serialized_state_or_error.release_error());
  251. serialized_state = serialized_state_or_error.release_value();
  252. }
  253. // 4. Otherwise:
  254. else {
  255. // 1. Let current be the current entry of this.
  256. auto current = current_entry();
  257. // 2. If current is not null, then set serializedState to current's session history entry's navigation API state.
  258. if (current != nullptr)
  259. serialized_state = current->session_history_entry().navigation_api_state;
  260. }
  261. // 5. If document is not fully active, then return an early error result for an "InvalidStateError" DOMException.
  262. if (!document.is_fully_active())
  263. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document is not fully active"_fly_string));
  264. // 6. If document's unload counter is greater than 0, then return an early error result for an "InvalidStateError" DOMException.
  265. if (document.unload_counter() > 0)
  266. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document already unloaded"_fly_string));
  267. // 7. Let info be options["info"], if it exists; otherwise, undefined.
  268. auto info = options.info.value_or(JS::js_undefined());
  269. // 8. Let apiMethodTracker be the result of maybe setting the upcoming non-traverse API method tracker for this given info and serializedState.
  270. auto api_method_tracker = maybe_set_the_upcoming_non_traverse_api_method_tracker(info, serialized_state);
  271. // 9. Reload document's node navigable with navigationAPIState set to serializedState.
  272. // FIXME: Actually call reload once Navigables are implemented enough to guarantee a node navigable on
  273. // an active document that's not being unloaded.
  274. // document.navigable().reload(state)
  275. return navigation_api_method_tracker_derived_result(api_method_tracker);
  276. }
  277. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-traverseto
  278. WebIDL::ExceptionOr<NavigationResult> Navigation::traverse_to(String key, NavigationOptions const& options)
  279. {
  280. auto& realm = this->realm();
  281. // The traverseTo(key, options) method steps are:
  282. // 1. If this's current entry index is −1, then return an early error result for an "InvalidStateError" DOMException.
  283. if (m_current_entry_index == -1)
  284. return early_error_result(WebIDL::InvalidStateError::create(realm, "Cannot traverseTo: no current session history entry"_fly_string));
  285. // 2. If this's entry list does not contain a NavigationHistoryEntry whose session history entry's navigation API key equals key,
  286. // then return an early error result for an "InvalidStateError" DOMException.
  287. auto it = m_entry_list.find_if([&key](auto const& entry) {
  288. return entry->session_history_entry().navigation_api_key == key;
  289. });
  290. if (it == m_entry_list.end())
  291. return early_error_result(WebIDL::InvalidStateError::create(realm, "Cannot traverseTo: key not found in session history list"_fly_string));
  292. // 3. Return the result of performing a navigation API traversal given this, key, and options.
  293. return perform_a_navigation_api_traversal(key, options);
  294. }
  295. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#performing-a-navigation-api-traversal
  296. WebIDL::ExceptionOr<NavigationResult> Navigation::back(NavigationOptions const& options)
  297. {
  298. auto& realm = this->realm();
  299. // The back(options) method steps are:
  300. // 1. If this's current entry index is −1 or 0, then return an early error result for an "InvalidStateError" DOMException.
  301. if (m_current_entry_index == -1 || m_current_entry_index == 0)
  302. return early_error_result(WebIDL::InvalidStateError::create(realm, "Cannot navigate back: no previous session history entry"_fly_string));
  303. // 2. Let key be this's entry list[this's current entry index − 1]'s session history entry's navigation API key.
  304. auto key = m_entry_list[m_current_entry_index - 1]->session_history_entry().navigation_api_key;
  305. // 3. Return the result of performing a navigation API traversal given this, key, and options.
  306. return perform_a_navigation_api_traversal(key, options);
  307. }
  308. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-forward
  309. WebIDL::ExceptionOr<NavigationResult> Navigation::forward(NavigationOptions const& options)
  310. {
  311. auto& realm = this->realm();
  312. // The forward(options) method steps are:
  313. // 1. If this's current entry index is −1 or is equal to this's entry list's size − 1,
  314. // then return an early error result for an "InvalidStateError" DOMException.
  315. if (m_current_entry_index == -1 || m_current_entry_index == static_cast<i64>(m_entry_list.size() - 1))
  316. return early_error_result(WebIDL::InvalidStateError::create(realm, "Cannot navigate forward: no next session history entry"_fly_string));
  317. // 2. Let key be this's entry list[this's current entry index + 1]'s session history entry's navigation API key.
  318. auto key = m_entry_list[m_current_entry_index + 1]->session_history_entry().navigation_api_key;
  319. // 3. Return the result of performing a navigation API traversal given this, key, and options.
  320. return perform_a_navigation_api_traversal(key, options);
  321. }
  322. void Navigation::set_onnavigate(WebIDL::CallbackType* event_handler)
  323. {
  324. set_event_handler_attribute(HTML::EventNames::navigate, event_handler);
  325. }
  326. WebIDL::CallbackType* Navigation::onnavigate()
  327. {
  328. return event_handler_attribute(HTML::EventNames::navigate);
  329. }
  330. void Navigation::set_onnavigatesuccess(WebIDL::CallbackType* event_handler)
  331. {
  332. set_event_handler_attribute(HTML::EventNames::navigatesuccess, event_handler);
  333. }
  334. WebIDL::CallbackType* Navigation::onnavigatesuccess()
  335. {
  336. return event_handler_attribute(HTML::EventNames::navigatesuccess);
  337. }
  338. void Navigation::set_onnavigateerror(WebIDL::CallbackType* event_handler)
  339. {
  340. set_event_handler_attribute(HTML::EventNames::navigateerror, event_handler);
  341. }
  342. WebIDL::CallbackType* Navigation::onnavigateerror()
  343. {
  344. return event_handler_attribute(HTML::EventNames::navigateerror);
  345. }
  346. void Navigation::set_oncurrententrychange(WebIDL::CallbackType* event_handler)
  347. {
  348. set_event_handler_attribute(HTML::EventNames::currententrychange, event_handler);
  349. }
  350. WebIDL::CallbackType* Navigation::oncurrententrychange()
  351. {
  352. return event_handler_attribute(HTML::EventNames::currententrychange);
  353. }
  354. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#has-entries-and-events-disabled
  355. bool Navigation::has_entries_and_events_disabled() const
  356. {
  357. // A Navigation navigation has entries and events disabled if the following steps return true:
  358. // 1. Let document be navigation's relevant global object's associated Document.
  359. auto const& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  360. // 2. If document is not fully active, then return true.
  361. if (!document.is_fully_active())
  362. return true;
  363. // 3. If document's is initial about:blank is true, then return true.
  364. if (document.is_initial_about_blank())
  365. return true;
  366. // 4. If document's origin is opaque, then return true.
  367. if (document.origin().is_opaque())
  368. return true;
  369. // 5. Return false.
  370. return false;
  371. }
  372. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#getting-the-navigation-api-entry-index
  373. i64 Navigation::get_the_navigation_api_entry_index(SessionHistoryEntry const& she) const
  374. {
  375. // To get the navigation API entry index of a session history entry she within a Navigation navigation:
  376. // 1. Let index be 0.
  377. i64 index = 0;
  378. // 2. For each nhe of navigation's entry list:
  379. for (auto const& nhe : m_entry_list) {
  380. // 1. If nhe's session history entry is equal to she, then return index.
  381. if (&nhe->session_history_entry() == &she)
  382. return index;
  383. // 2. Increment index by 1.
  384. ++index;
  385. }
  386. // 3. Return −1.
  387. return -1;
  388. }
  389. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-early-error-result
  390. NavigationResult Navigation::early_error_result(AnyException e)
  391. {
  392. auto& vm = this->vm();
  393. // An early error result for an exception e is a NavigationResult dictionary instance given by
  394. // «[ "committed" → a promise rejected with e, "finished" → a promise rejected with e ]».
  395. auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, e);
  396. return {
  397. .committed = WebIDL::create_rejected_promise(realm(), *throw_completion.value())->promise(),
  398. .finished = WebIDL::create_rejected_promise(realm(), *throw_completion.value())->promise(),
  399. };
  400. }
  401. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-method-tracker-derived-result
  402. NavigationResult navigation_api_method_tracker_derived_result(JS::NonnullGCPtr<NavigationAPIMethodTracker> api_method_tracker)
  403. {
  404. // A navigation API method tracker-derived result for a navigation API method tracker is a NavigationResult
  405. /// dictionary instance given by «[ "committed" apiMethodTracker's committed promise, "finished" → apiMethodTracker's finished promise ]».
  406. return {
  407. api_method_tracker->committed_promise->promise(),
  408. api_method_tracker->finished_promise->promise(),
  409. };
  410. }
  411. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  412. JS::NonnullGCPtr<NavigationAPIMethodTracker> Navigation::maybe_set_the_upcoming_non_traverse_api_method_tracker(JS::Value info, Optional<SerializationRecord> serialized_state)
  413. {
  414. auto& realm = relevant_realm(*this);
  415. auto& vm = this->vm();
  416. // To maybe set the upcoming non-traverse API method tracker given a Navigation navigation,
  417. // a JavaScript value info, and a serialized state-or-null serializedState:
  418. // 1. Let committedPromise and finishedPromise be new promises created in navigation's relevant realm.
  419. auto committed_promise = WebIDL::create_promise(realm);
  420. auto finished_promise = WebIDL::create_promise(realm);
  421. // 2. Mark as handled finishedPromise.
  422. // NOTE: The web developer doesn’t necessarily care about finishedPromise being rejected:
  423. // - They might only care about committedPromise.
  424. // - They could be doing multiple synchronous navigations within the same task,
  425. // in which case all but the last will be aborted (causing their finishedPromise to reject).
  426. // This could be an application bug, but also could just be an emergent feature of disparate
  427. // parts of the application overriding each others' actions.
  428. // - They might prefer to listen to other transition-failure signals instead of finishedPromise, e.g.,
  429. // the navigateerror event, or the navigation.transition.finished promise.
  430. // As such, we mark it as handled to ensure that it never triggers unhandledrejection events.
  431. WebIDL::mark_promise_as_handled(finished_promise);
  432. // 3. Let apiMethodTracker be a new navigation API method tracker with:
  433. // navigation object: navigation
  434. // key: null
  435. // info: info
  436. // serialized state: serializedState
  437. // comitted-to entry: null
  438. // comitted promise: committedPromise
  439. // finished promise: finishedPromise
  440. auto api_method_tracker = vm.heap().allocate_without_realm<NavigationAPIMethodTracker>(
  441. /* .navigation = */ *this,
  442. /* .key = */ OptionalNone {},
  443. /* .info = */ info,
  444. /* .serialized_state = */ move(serialized_state),
  445. /* .commited_to_entry = */ nullptr,
  446. /* .committed_promise = */ committed_promise,
  447. /* .finished_promise = */ finished_promise);
  448. // 4. Assert: navigation's upcoming non-traverse API method tracker is null.
  449. VERIFY(m_upcoming_non_traverse_api_method_tracker == nullptr);
  450. // 5. If navigation does not have entries and events disabled,
  451. // then set navigation's upcoming non-traverse API method tracker to apiMethodTracker.
  452. // NOTE: If navigation has entries and events disabled, then committedPromise and finishedPromise will never fulfill
  453. // (since we never create a NavigationHistoryEntry object for such Documents, and so we have nothing to resolve them with);
  454. // there is no NavigationHistoryEntry to apply serializedState to; and there is no navigate event to include info with.
  455. // So, we don't need to track this API method call after all.
  456. if (!has_entries_and_events_disabled())
  457. m_upcoming_non_traverse_api_method_tracker = api_method_tracker;
  458. // 6. Return apiMethodTracker.
  459. return api_method_tracker;
  460. }
  461. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#add-an-upcoming-traverse-api-method-tracker
  462. JS::NonnullGCPtr<NavigationAPIMethodTracker> Navigation::add_an_upcoming_traverse_api_method_tracker(String destination_key, JS::Value info)
  463. {
  464. auto& vm = this->vm();
  465. auto& realm = relevant_realm(*this);
  466. // To add an upcoming traverse API method tracker given a Navigation navigation, a string destinationKey, and a JavaScript value info:
  467. // 1. Let committedPromise and finishedPromise be new promises created in navigation's relevant realm.
  468. auto committed_promise = WebIDL::create_promise(realm);
  469. auto finished_promise = WebIDL::create_promise(realm);
  470. // 2. Mark as handled finishedPromise.
  471. // NOTE: See the previous discussion about why this is done
  472. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#note-mark-as-handled-navigation-api-finished
  473. WebIDL::mark_promise_as_handled(*finished_promise);
  474. // 3. Let apiMethodTracker be a new navigation API method tracker with:
  475. // navigation object: navigation
  476. // key: destinationKey
  477. // info: info
  478. // serialized state: null
  479. // comitted-to entry: null
  480. // comitted promise: committedPromise
  481. // finished promise: finishedPromise
  482. auto api_method_tracker = vm.heap().allocate_without_realm<NavigationAPIMethodTracker>(
  483. /* .navigation = */ *this,
  484. /* .key = */ destination_key,
  485. /* .info = */ info,
  486. /* .serialized_state = */ OptionalNone {},
  487. /* .commited_to_entry = */ nullptr,
  488. /* .committed_promise = */ committed_promise,
  489. /* .finished_promise = */ finished_promise);
  490. // 4. Set navigation's upcoming traverse API method trackers[key] to apiMethodTracker.
  491. // FIXME: Fix spec typo key --> destinationKey
  492. m_upcoming_traverse_api_method_trackers.set(destination_key, api_method_tracker);
  493. // 5. Return apiMethodTracker.
  494. return api_method_tracker;
  495. }
  496. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#performing-a-navigation-api-traversal
  497. WebIDL::ExceptionOr<NavigationResult> Navigation::perform_a_navigation_api_traversal(String key, NavigationOptions const& options)
  498. {
  499. auto& realm = this->realm();
  500. // To perform a navigation API traversal given a Navigation navigation, a string key, and a NavigationOptions options:
  501. // 1. Let document be this's relevant global object's associated Document.
  502. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  503. // 2. If document is not fully active, then return an early error result for an "InvalidStateError" DOMException.
  504. if (!document.is_fully_active())
  505. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document is not fully active"_fly_string));
  506. // 3. If document's unload counter is greater than 0, then return an early error result for an "InvalidStateError" DOMException.
  507. if (document.unload_counter() > 0)
  508. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document already unloaded"_fly_string));
  509. // 4. Let current be the current entry of navigation.
  510. auto current = current_entry();
  511. // 5. If key equals current's session history entry's navigation API key, then return
  512. // «[ "committed" → a promise resolved with current, "finished" → a promise resolved with current ]».
  513. if (key == current->session_history_entry().navigation_api_key) {
  514. return NavigationResult {
  515. .committed = WebIDL::create_resolved_promise(realm, current)->promise(),
  516. .finished = WebIDL::create_resolved_promise(realm, current)->promise()
  517. };
  518. }
  519. // 6. If navigation's upcoming traverse API method trackers[key] exists,
  520. // then return a navigation API method tracker-derived result for navigation's upcoming traverse API method trackers[key].
  521. if (auto maybe_tracker = m_upcoming_traverse_api_method_trackers.get(key); maybe_tracker.has_value())
  522. return navigation_api_method_tracker_derived_result(maybe_tracker.value());
  523. // 7. Let info be options["info"], if it exists; otherwise, undefined.
  524. auto info = options.info.value_or(JS::js_undefined());
  525. // 8. Let apiMethodTracker be the result of adding an upcoming traverse API method tracker for navigation given key and info.
  526. auto api_method_tracker = add_an_upcoming_traverse_api_method_tracker(key, info);
  527. // 9. Let navigable be document's node navigable.
  528. auto navigable = document.navigable();
  529. // 10. Let traversable be navigable's traversable navigable.
  530. auto traversable = navigable->traversable_navigable();
  531. // 11. Let sourceSnapshotParams be the result of snapshotting source snapshot params given document.
  532. auto source_snapshot_params = document.snapshot_source_snapshot_params();
  533. // 12. Append the following session history traversal steps to traversable:
  534. traversable->append_session_history_traversal_steps([key, api_method_tracker, navigable, source_snapshot_params, this] {
  535. // 1. Let navigableSHEs be the result of getting session history entries given navigable.
  536. auto navigable_shes = navigable->get_session_history_entries();
  537. // 2. Let targetSHE be the session history entry in navigableSHEs whose navigation API key is key. If no such entry exists, then:
  538. auto it = navigable_shes.find_if([&key](auto const& entry) {
  539. return entry->navigation_api_key == key;
  540. });
  541. if (it == navigable_shes.end()) {
  542. // NOTE: This path is taken if navigation's entry list was outdated compared to navigableSHEs,
  543. // which can occur for brief periods while all the relevant threads and processes are being synchronized in reaction to a history change.
  544. // 1. Queue a global task on the navigation and traversal task source given navigation's relevant global object
  545. // to reject the finished promise for apiMethodTracker with an "InvalidStateError" DOMException.
  546. queue_global_task(HTML::Task::Source::NavigationAndTraversal, relevant_global_object(*this), [this, api_method_tracker] {
  547. auto& reject_realm = relevant_realm(*this);
  548. WebIDL::reject_promise(reject_realm, api_method_tracker->finished_promise,
  549. WebIDL::InvalidStateError::create(reject_realm, "Cannot traverse with stale session history entry"_fly_string));
  550. });
  551. // 2. Abort these steps.
  552. return;
  553. }
  554. auto target_she = *it;
  555. // 3. If targetSHE is navigable's active session history entry, then abort these steps.
  556. // NOTE: This can occur if a previously queued traversal already took us to this session history entry.
  557. // In that case the previous traversal will have dealt with apiMethodTracker already.
  558. if (target_she == navigable->active_session_history_entry())
  559. return;
  560. // FIXME: 4. Let result be the result of applying the traverse history step given by targetSHE's step to traversable,
  561. // given sourceSnapshotParams, navigable, and "none".
  562. (void)source_snapshot_params;
  563. // NOTE: When result is "canceled-by-beforeunload" or "initiator-disallowed", the navigate event was never fired,
  564. // aborting the ongoing navigation would not be correct; it would result in a navigateerror event without a
  565. // preceding navigate event. In the "canceled-by-navigate" case, navigate is fired, but the inner navigate event
  566. // firing algorithm will take care of aborting the ongoing navigation.
  567. // FIXME: 5. If result is "canceled-by-beforeunload", then queue a global task on the navigation and traversal task source
  568. // given navigation's relevant global object to reject the finished promise for apiMethodTracker with a
  569. // new "AbortError"DOMException created in navigation's relevant realm.
  570. // FIXME: 6. If result is "initiator-disallowed", then queue a global task on the navigation and traversal task source
  571. // given navigation's relevant global object to reject the finished promise for apiMethodTracker with a
  572. // new "SecurityError" DOMException created in navigation's relevant realm.
  573. });
  574. // 13. Return a navigation API method tracker-derived result for apiMethodTracker.
  575. return navigation_api_method_tracker_derived_result(api_method_tracker);
  576. }
  577. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#abort-the-ongoing-navigation
  578. void Navigation::abort_the_ongoing_navigation(Optional<JS::NonnullGCPtr<WebIDL::DOMException>> error)
  579. {
  580. auto& realm = relevant_realm(*this);
  581. // To abort the ongoing navigation given a Navigation navigation and an optional DOMException error:
  582. // 1. Let event be navigation's ongoing navigate event.
  583. auto event = ongoing_navigate_event();
  584. // 2. Assert: event is not null.
  585. VERIFY(event != nullptr);
  586. // 3. Set navigation's focus changed during ongoing navigation to false.
  587. m_focus_changed_during_ongoing_navigation = false;
  588. // 4. Set navigation's suppress normal scroll restoration during ongoing navigation to false.
  589. m_suppress_scroll_restoration_during_ongoing_navigation = false;
  590. // 5. If error was not given, then let error be a new "AbortError" DOMException created in navigation's relevant realm.
  591. if (!error.has_value())
  592. error = WebIDL::AbortError::create(realm, "Navigation aborted"_fly_string);
  593. VERIFY(error.has_value());
  594. // 6. If event's dispatch flag is set, then set event's canceled flag to true.
  595. if (event->dispatched())
  596. event->set_cancelled(true);
  597. // 7. Signal abort on event's abort controller given error.
  598. event->abort_controller()->abort(error.value());
  599. // 8. Set navigation's ongoing navigate event to null.
  600. m_ongoing_navigate_event = nullptr;
  601. // 9. Fire an event named navigateerror at navigation using ErrorEvent, with error initialized to error,
  602. // and message, filename, lineno, and colno initialized to appropriate values that can be extracted
  603. // from error and the current JavaScript stack in the same underspecified way that the report the exception algorithm does.
  604. ErrorEventInit event_init = {};
  605. event_init.error = error.value();
  606. // FIXME: Extract information from the exception and the JS context in the wishy-washy way the spec says here.
  607. event_init.filename = String {};
  608. event_init.colno = 0;
  609. event_init.lineno = 0;
  610. event_init.message = String {};
  611. dispatch_event(ErrorEvent::create(realm, EventNames::navigateerror, event_init));
  612. // 10. If navigation's ongoing API method tracker is non-null, then reject the finished promise for apiMethodTracker with error.
  613. if (m_ongoing_api_method_tracker != nullptr)
  614. WebIDL::reject_promise(realm, m_ongoing_api_method_tracker->finished_promise, error.value());
  615. // 11. If navigation's transition is not null, then:
  616. if (m_transition != nullptr) {
  617. // 1. Reject navigation's transition's finished promise with error.
  618. m_transition->finished()->reject(error.value());
  619. // 2. Set navigation's transition to null.
  620. m_transition = nullptr;
  621. }
  622. }
  623. }