Navigation.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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/Document.h>
  13. #include <LibWeb/HTML/NavigateEvent.h>
  14. #include <LibWeb/HTML/Navigation.h>
  15. #include <LibWeb/HTML/NavigationCurrentEntryChangeEvent.h>
  16. #include <LibWeb/HTML/NavigationHistoryEntry.h>
  17. #include <LibWeb/HTML/NavigationTransition.h>
  18. #include <LibWeb/HTML/Window.h>
  19. namespace Web::HTML {
  20. static NavigationResult navigation_api_method_tracker_derived_result(JS::NonnullGCPtr<NavigationAPIMethodTracker> api_method_tracker);
  21. NavigationAPIMethodTracker::NavigationAPIMethodTracker(JS::NonnullGCPtr<Navigation> navigation,
  22. Optional<String> key,
  23. JS::Value info,
  24. Optional<SerializationRecord> serialized_state,
  25. JS::GCPtr<NavigationHistoryEntry> commited_to_entry,
  26. JS::NonnullGCPtr<WebIDL::Promise> committed_promise,
  27. JS::NonnullGCPtr<WebIDL::Promise> finished_promise)
  28. : navigation(navigation)
  29. , key(move(key))
  30. , info(info)
  31. , serialized_state(move(serialized_state))
  32. , commited_to_entry(commited_to_entry)
  33. , committed_promise(committed_promise)
  34. , finished_promise(finished_promise)
  35. {
  36. }
  37. void NavigationAPIMethodTracker::visit_edges(Cell::Visitor& visitor)
  38. {
  39. Base::visit_edges(visitor);
  40. visitor.visit(navigation);
  41. visitor.visit(info);
  42. visitor.visit(commited_to_entry);
  43. visitor.visit(committed_promise);
  44. visitor.visit(finished_promise);
  45. }
  46. JS::NonnullGCPtr<Navigation> Navigation::create(JS::Realm& realm)
  47. {
  48. return realm.heap().allocate<Navigation>(realm, realm);
  49. }
  50. Navigation::Navigation(JS::Realm& realm)
  51. : DOM::EventTarget(realm)
  52. {
  53. }
  54. Navigation::~Navigation() = default;
  55. void Navigation::initialize(JS::Realm& realm)
  56. {
  57. Base::initialize(realm);
  58. set_prototype(&Bindings::ensure_web_prototype<Bindings::NavigationPrototype>(realm, "Navigation"));
  59. }
  60. void Navigation::visit_edges(JS::Cell::Visitor& visitor)
  61. {
  62. Base::visit_edges(visitor);
  63. for (auto& entry : m_entry_list)
  64. visitor.visit(entry);
  65. visitor.visit(m_transition);
  66. visitor.visit(m_ongoing_navigate_event);
  67. visitor.visit(m_ongoing_api_method_tracker);
  68. visitor.visit(m_upcoming_non_traverse_api_method_tracker);
  69. for (auto& key_and_tracker : m_upcoming_traverse_api_method_trackers)
  70. visitor.visit(key_and_tracker.value);
  71. }
  72. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-entries
  73. Vector<JS::NonnullGCPtr<NavigationHistoryEntry>> Navigation::entries() const
  74. {
  75. // The entries() method steps are:
  76. // 1. If this has entries and events disabled, then return the empty list.
  77. if (has_entries_and_events_disabled())
  78. return {};
  79. // 2. Return this's entry list.
  80. // NOTE: Recall that because of Web IDL's sequence type conversion rules,
  81. // this will create a new JavaScript array object on each call.
  82. // That is, navigation.entries() !== navigation.entries().
  83. return m_entry_list;
  84. }
  85. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-current-entry
  86. JS::GCPtr<NavigationHistoryEntry> Navigation::current_entry() const
  87. {
  88. // The current entry of a Navigation navigation is the result of running the following steps:
  89. // 1. If navigation has entries and events disabled, then return null.
  90. if (has_entries_and_events_disabled())
  91. return nullptr;
  92. // 2. Assert: navigation's current entry index is not −1.
  93. VERIFY(m_current_entry_index != -1);
  94. // 3. Return navigation's entry list[navigation's current entry index].
  95. return m_entry_list[m_current_entry_index];
  96. }
  97. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-updatecurrententry
  98. WebIDL::ExceptionOr<void> Navigation::update_current_entry(NavigationUpdateCurrentEntryOptions options)
  99. {
  100. // The updateCurrentEntry(options) method steps are:
  101. // 1. Let current be the current entry of this.
  102. auto current = current_entry();
  103. // 2. If current is null, then throw an "InvalidStateError" DOMException.
  104. if (current == nullptr)
  105. return WebIDL::InvalidStateError::create(realm(), "Cannot update current NavigationHistoryEntry when there is no current entry"sv);
  106. // 3. Let serializedState be StructuredSerializeForStorage(options["state"]), rethrowing any exceptions.
  107. auto serialized_state = TRY(structured_serialize_for_storage(vm(), options.state));
  108. // 4. Set current's session history entry's navigation API state to serializedState.
  109. current->session_history_entry().navigation_api_state = serialized_state;
  110. // 5. Fire an event named currententrychange at this using NavigationCurrentEntryChangeEvent,
  111. // with its navigationType attribute initialized to null and its from initialized to current.
  112. NavigationCurrentEntryChangeEventInit event_init = {};
  113. event_init.navigation_type = {};
  114. event_init.from = current;
  115. dispatch_event(HTML::NavigationCurrentEntryChangeEvent::create(realm(), HTML::EventNames::currententrychange, event_init));
  116. return {};
  117. }
  118. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-cangoback
  119. bool Navigation::can_go_back() const
  120. {
  121. // The canGoBack getter steps are:
  122. // 1. If this has entries and events disabled, then return false.
  123. if (has_entries_and_events_disabled())
  124. return false;
  125. // 2. Assert: this's current entry index is not −1.
  126. VERIFY(m_current_entry_index != -1);
  127. // 3. If this's current entry index is 0, then return false.
  128. // 4. Return true.
  129. return (m_current_entry_index != 0);
  130. }
  131. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-cangoforward
  132. bool Navigation::can_go_forward() const
  133. {
  134. // The canGoForward getter steps are:
  135. // 1. If this has entries and events disabled, then return false.
  136. if (has_entries_and_events_disabled())
  137. return false;
  138. // 2. Assert: this's current entry index is not −1.
  139. VERIFY(m_current_entry_index != -1);
  140. // 3. If this's current entry index is equal to this's entry list's size, then return false.
  141. // 4. Return true.
  142. return (m_current_entry_index != static_cast<i64>(m_entry_list.size()));
  143. }
  144. static HistoryHandlingBehavior to_history_handling_behavior(Bindings::NavigationHistoryBehavior b)
  145. {
  146. switch (b) {
  147. case Bindings::NavigationHistoryBehavior::Auto:
  148. return HistoryHandlingBehavior::Default;
  149. case Bindings::NavigationHistoryBehavior::Push:
  150. return HistoryHandlingBehavior::Push;
  151. case Bindings::NavigationHistoryBehavior::Replace:
  152. return HistoryHandlingBehavior::Replace;
  153. };
  154. VERIFY_NOT_REACHED();
  155. }
  156. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigation-navigate
  157. WebIDL::ExceptionOr<NavigationResult> Navigation::navigate(String url, NavigationNavigateOptions const& options)
  158. {
  159. auto& realm = this->realm();
  160. auto& vm = this->vm();
  161. // The navigate(options) method steps are:
  162. // 1. Parse url relative to this's relevant settings object.
  163. // If that returns failure, then return an early error result for a "SyntaxError" DOMException.
  164. // Otherwise, let urlRecord be the resulting URL record.
  165. auto url_record = relevant_settings_object(*this).parse_url(url);
  166. if (!url_record.is_valid())
  167. return early_error_result(WebIDL::SyntaxError::create(realm, "Cannot navigate to Invalid URL"));
  168. // 2. Let document be this's relevant global object's associated Document.
  169. auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  170. // 3. If options["history"] is "push", and the navigation must be a replace given urlRecord and document,
  171. // then return an early error result for a "NotSupportedError" DOMException.
  172. if (options.history == Bindings::NavigationHistoryBehavior::Push && navigation_must_be_a_replace(url_record, document))
  173. return early_error_result(WebIDL::NotSupportedError::create(realm, "Navigation must be a replace, but push was requested"));
  174. // 4. Let state be options["state"], if it exists; otherwise, undefined.
  175. auto state = options.state.value_or(JS::js_undefined());
  176. // 5. Let serializedState be StructuredSerializeForStorage(state).
  177. // If this throws an exception, then return an early error result for that exception.
  178. // FIXME: Fix this spec grammaro in the note
  179. // NOTE: It is importantly to perform this step early, since serialization can invoke web developer code,
  180. // which in turn might change various things we check in later steps.
  181. auto serialized_state_or_error = structured_serialize_for_storage(vm, state);
  182. if (serialized_state_or_error.is_error()) {
  183. return early_error_result(serialized_state_or_error.release_error());
  184. }
  185. auto serialized_state = serialized_state_or_error.release_value();
  186. // 6. If document is not fully active, then return an early error result for an "InvalidStateError" DOMException.
  187. if (!document.is_fully_active())
  188. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document is not fully active"));
  189. // 7. If document's unload counter is greater than 0, then return an early error result for an "InvalidStateError" DOMException.
  190. if (document.unload_counter() > 0)
  191. return early_error_result(WebIDL::InvalidStateError::create(realm, "Document already unloaded"));
  192. // 8. Let info be options["info"], if it exists; otherwise, undefined.
  193. auto info = options.info.value_or(JS::js_undefined());
  194. // 9. Let apiMethodTracker be the result of maybe setting the upcoming non-traverse API method tracker for this
  195. // given info and serializedState.
  196. auto api_method_tracker = maybe_set_the_upcoming_non_traverse_api_method_tracker(info, serialized_state);
  197. // 10. Navigate document's node navigable to urlRecord using document,
  198. // with historyHandling set to options["history"] and navigationAPIState set to serializedState.
  199. // FIXME: Fix spec typo here
  200. // NOTE: Unlike location.assign() and friends, which are exposed across origin-domain boundaries,
  201. // navigation.navigate() can only be accessed by code with direct synchronous access to the
  202. /// window.navigation property. Thus, we avoid the complications about attributing the source document
  203. // of the navigation, and we don't need to deal with the allowed by sandboxing to navigate check and its
  204. // acccompanying exceptionsEnabled flag. We just treat all navigations as if they come from the Document
  205. // corresponding to this Navigation object itself (i.e., document).
  206. [[maybe_unused]] auto history_handling_behavior = to_history_handling_behavior(options.history);
  207. // FIXME: Actually call navigate once Navigables are implemented enough to guarantee a node navigable on
  208. // an active document that's not being unloaded.
  209. // document.navigable().navigate(url, document, history behavior, state)
  210. // 11. If this's upcoming non-traverse API method tracker is apiMethodTracker, then:
  211. // NOTE: If the upcoming non-traverse API method tracker is still apiMethodTracker, this means that the navigate
  212. // algorithm bailed out before ever getting to the inner navigate event firing algorithm which would promote
  213. // that upcoming API method tracker to ongoing.
  214. if (m_upcoming_non_traverse_api_method_tracker == api_method_tracker) {
  215. m_upcoming_non_traverse_api_method_tracker = nullptr;
  216. return early_error_result(WebIDL::AbortError::create(realm, "Navigation aborted"));
  217. }
  218. // 12. Return a navigation API method tracker-derived result for apiMethodTracker.
  219. return navigation_api_method_tracker_derived_result(api_method_tracker);
  220. }
  221. void Navigation::set_onnavigate(WebIDL::CallbackType* event_handler)
  222. {
  223. set_event_handler_attribute(HTML::EventNames::navigate, event_handler);
  224. }
  225. WebIDL::CallbackType* Navigation::onnavigate()
  226. {
  227. return event_handler_attribute(HTML::EventNames::navigate);
  228. }
  229. void Navigation::set_onnavigatesuccess(WebIDL::CallbackType* event_handler)
  230. {
  231. set_event_handler_attribute(HTML::EventNames::navigatesuccess, event_handler);
  232. }
  233. WebIDL::CallbackType* Navigation::onnavigatesuccess()
  234. {
  235. return event_handler_attribute(HTML::EventNames::navigatesuccess);
  236. }
  237. void Navigation::set_onnavigateerror(WebIDL::CallbackType* event_handler)
  238. {
  239. set_event_handler_attribute(HTML::EventNames::navigateerror, event_handler);
  240. }
  241. WebIDL::CallbackType* Navigation::onnavigateerror()
  242. {
  243. return event_handler_attribute(HTML::EventNames::navigateerror);
  244. }
  245. void Navigation::set_oncurrententrychange(WebIDL::CallbackType* event_handler)
  246. {
  247. set_event_handler_attribute(HTML::EventNames::currententrychange, event_handler);
  248. }
  249. WebIDL::CallbackType* Navigation::oncurrententrychange()
  250. {
  251. return event_handler_attribute(HTML::EventNames::currententrychange);
  252. }
  253. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#has-entries-and-events-disabled
  254. bool Navigation::has_entries_and_events_disabled() const
  255. {
  256. // A Navigation navigation has entries and events disabled if the following steps return true:
  257. // 1. Let document be navigation's relevant global object's associated Document.
  258. auto const& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document();
  259. // 2. If document is not fully active, then return true.
  260. if (!document.is_fully_active())
  261. return true;
  262. // 3. If document's is initial about:blank is true, then return true.
  263. if (document.is_initial_about_blank())
  264. return true;
  265. // 4. If document's origin is opaque, then return true.
  266. if (document.origin().is_opaque())
  267. return true;
  268. // 5. Return false.
  269. return false;
  270. }
  271. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#getting-the-navigation-api-entry-index
  272. i64 Navigation::get_the_navigation_api_entry_index(SessionHistoryEntry const& she) const
  273. {
  274. // To get the navigation API entry index of a session history entry she within a Navigation navigation:
  275. // 1. Let index be 0.
  276. i64 index = 0;
  277. // 2. For each nhe of navigation's entry list:
  278. for (auto const& nhe : m_entry_list) {
  279. // 1. If nhe's session history entry is equal to she, then return index.
  280. if (&nhe->session_history_entry() == &she)
  281. return index;
  282. // 2. Increment index by 1.
  283. ++index;
  284. }
  285. // 3. Return −1.
  286. return -1;
  287. }
  288. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-early-error-result
  289. NavigationResult Navigation::early_error_result(AnyException e)
  290. {
  291. auto& vm = this->vm();
  292. // An early error result for an exception e is a NavigationResult dictionary instance given by
  293. // «[ "committed" → a promise rejected with e, "finished" → a promise rejected with e ]».
  294. auto throw_completion = Bindings::dom_exception_to_throw_completion(vm, e);
  295. return {
  296. .committed = WebIDL::create_rejected_promise(realm(), *throw_completion.value())->promise(),
  297. .finished = WebIDL::create_rejected_promise(realm(), *throw_completion.value())->promise(),
  298. };
  299. }
  300. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation-api-method-tracker-derived-result
  301. NavigationResult navigation_api_method_tracker_derived_result(JS::NonnullGCPtr<NavigationAPIMethodTracker> api_method_tracker)
  302. {
  303. // A navigation API method tracker-derived result for a navigation API method tracker is a NavigationResult
  304. /// dictionary instance given by «[ "committed" apiMethodTracker's committed promise, "finished" → apiMethodTracker's finished promise ]».
  305. return {
  306. api_method_tracker->committed_promise->promise(),
  307. api_method_tracker->finished_promise->promise(),
  308. };
  309. }
  310. // https://html.spec.whatwg.org/multipage/nav-history-apis.html#upcoming-non-traverse-api-method-tracker
  311. JS::NonnullGCPtr<NavigationAPIMethodTracker> Navigation::maybe_set_the_upcoming_non_traverse_api_method_tracker(JS::Value info, Optional<SerializationRecord> serialized_state)
  312. {
  313. auto& realm = relevant_realm(*this);
  314. auto& vm = this->vm();
  315. // To maybe set the upcoming non-traverse API method tracker given a Navigation navigation,
  316. // a JavaScript value info, and a serialized state-or-null serializedState:
  317. // 1. Let committedPromise and finishedPromise be new promises created in navigation's relevant realm.
  318. auto committed_promise = WebIDL::create_promise(realm);
  319. auto finished_promise = WebIDL::create_promise(realm);
  320. // 2. Mark as handled finishedPromise.
  321. // NOTE: The web developer doesn’t necessarily care about finishedPromise being rejected:
  322. // - They might only care about committedPromise.
  323. // - They could be doing multiple synchronous navigations within the same task,
  324. // in which case all but the last will be aborted (causing their finishedPromise to reject).
  325. // This could be an application bug, but also could just be an emergent feature of disparate
  326. // parts of the application overriding each others' actions.
  327. // - They might prefer to listen to other transition-failure signals instead of finishedPromise, e.g.,
  328. // the navigateerror event, or the navigation.transition.finished promise.
  329. // As such, we mark it as handled to ensure that it never triggers unhandledrejection events.
  330. WebIDL::mark_promise_as_handled(finished_promise);
  331. // 3. Let apiMethodTracker be a new navigation API method tracker with:
  332. // navigation object: navigation
  333. // key: null
  334. // info: info
  335. // serialized state: serializedState
  336. // comitted-to entry: null
  337. // comitted promise: committedPromise
  338. // finished promise: finishedPromise
  339. auto api_method_tracker = vm.heap().allocate_without_realm<NavigationAPIMethodTracker>(
  340. /* .navigation = */ *this,
  341. /* .key = */ OptionalNone {},
  342. /* .info = */ info,
  343. /* .serialized_state = */ move(serialized_state),
  344. /* .commited_to_entry = */ nullptr,
  345. /* .committed_promise = */ committed_promise,
  346. /* .finished_promise = */ finished_promise);
  347. // 4. Assert: navigation's upcoming non-traverse API method tracker is null.
  348. VERIFY(m_upcoming_non_traverse_api_method_tracker == nullptr);
  349. // 5. If navigation does not have entries and events disabled,
  350. // then set navigation's upcoming non-traverse API method tracker to apiMethodTracker.
  351. // NOTE: If navigation has entries and events disabled, then committedPromise and finishedPromise will never fulfill
  352. // (since we never create a NavigationHistoryEntry object for such Documents, and so we have nothing to resolve them with);
  353. // there is no NavigationHistoryEntry to apply serializedState to; and there is no navigate event to include info with.
  354. // So, we don't need to track this API method call after all.
  355. if (!has_entries_and_events_disabled())
  356. m_upcoming_non_traverse_api_method_tracker = api_method_tracker;
  357. // 6. Return apiMethodTracker.
  358. return api_method_tracker;
  359. }
  360. }