Navigable.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Crypto/Crypto.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/HTML/BrowsingContext.h>
  10. #include <LibWeb/HTML/DocumentState.h>
  11. #include <LibWeb/HTML/Navigable.h>
  12. #include <LibWeb/HTML/SessionHistoryEntry.h>
  13. #include <LibWeb/HTML/TraversableNavigable.h>
  14. #include <LibWeb/Platform/EventLoopPlugin.h>
  15. namespace Web::HTML {
  16. static HashTable<Navigable*>& all_navigables()
  17. {
  18. static HashTable<Navigable*> set;
  19. return set;
  20. }
  21. Navigable::Navigable()
  22. {
  23. all_navigables().set(this);
  24. }
  25. Navigable::~Navigable()
  26. {
  27. all_navigables().remove(this);
  28. }
  29. void Navigable::visit_edges(Cell::Visitor& visitor)
  30. {
  31. Base::visit_edges(visitor);
  32. visitor.visit(m_parent);
  33. visitor.visit(m_current_session_history_entry);
  34. visitor.visit(m_active_session_history_entry);
  35. visitor.visit(m_container);
  36. }
  37. JS::GCPtr<Navigable> Navigable::navigable_with_active_document(JS::NonnullGCPtr<DOM::Document> document)
  38. {
  39. for (auto* navigable : all_navigables()) {
  40. if (navigable->active_document() == document)
  41. return navigable;
  42. }
  43. return nullptr;
  44. }
  45. // https://html.spec.whatwg.org/multipage/document-sequences.html#initialize-the-navigable
  46. ErrorOr<void> Navigable::initialize_navigable(JS::NonnullGCPtr<DocumentState> document_state, JS::GCPtr<Navigable> parent)
  47. {
  48. static int next_id = 0;
  49. m_id = TRY(String::number(next_id++));
  50. // 1. Let entry be a new session history entry, with
  51. JS::NonnullGCPtr<SessionHistoryEntry> entry = *heap().allocate_without_realm<SessionHistoryEntry>();
  52. // URL: document's URL
  53. entry->url = document_state->document()->url();
  54. // document state: documentState
  55. entry->document_state = document_state;
  56. // 2. Set navigable's current session history entry to entry.
  57. m_current_session_history_entry = entry;
  58. // 3. Set navigable's active session history entry to entry.
  59. m_active_session_history_entry = entry;
  60. // 4. Set navigable's parent to parent.
  61. m_parent = parent;
  62. return {};
  63. }
  64. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-the-target-history-entry
  65. JS::GCPtr<SessionHistoryEntry> Navigable::get_the_target_history_entry(int target_step) const
  66. {
  67. // 1. Let entries be the result of getting session history entries for navigable.
  68. auto& entries = get_session_history_entries();
  69. // 2. Return the item in entries that has the greatest step less than or equal to step.
  70. JS::GCPtr<SessionHistoryEntry> result = nullptr;
  71. for (auto& entry : entries) {
  72. auto entry_step = entry->step.get<int>();
  73. if (entry_step <= target_step) {
  74. if (!result || result->step.get<int>() < entry_step) {
  75. result = entry;
  76. }
  77. }
  78. }
  79. return result;
  80. }
  81. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-document
  82. JS::GCPtr<DOM::Document> Navigable::active_document()
  83. {
  84. // A navigable's active document is its active session history entry's document.
  85. return m_active_session_history_entry->document_state->document();
  86. }
  87. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-bc
  88. JS::GCPtr<BrowsingContext> Navigable::active_browsing_context()
  89. {
  90. // A navigable's active browsing context is its active document's browsing context.
  91. // If this navigable is a traversable navigable, then its active browsing context will be a top-level browsing context.
  92. if (auto document = active_document())
  93. return document->browsing_context();
  94. return nullptr;
  95. }
  96. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-wp
  97. JS::GCPtr<HTML::WindowProxy> Navigable::active_window_proxy()
  98. {
  99. // A navigable's active WindowProxy is its active browsing context's associated WindowProxy.
  100. if (auto browsing_context = active_browsing_context())
  101. return browsing_context->window_proxy();
  102. return nullptr;
  103. }
  104. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-window
  105. JS::GCPtr<HTML::Window> Navigable::active_window()
  106. {
  107. // A navigable's active window is its active WindowProxy's [[Window]].
  108. if (auto window_proxy = active_window_proxy())
  109. return window_proxy->window();
  110. return nullptr;
  111. }
  112. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-target
  113. String Navigable::target_name() const
  114. {
  115. // FIXME: A navigable's target name is its active session history entry's document state's navigable target name.
  116. dbgln("FIXME: Implement Navigable::target_name()");
  117. return {};
  118. }
  119. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-container
  120. JS::GCPtr<NavigableContainer> Navigable::container() const
  121. {
  122. // The container of a navigable navigable is the navigable container whose nested navigable is navigable, or null if there is no such element.
  123. return m_container;
  124. }
  125. void Navigable::set_container(JS::GCPtr<NavigableContainer> container)
  126. {
  127. m_container = container;
  128. }
  129. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-traversable
  130. JS::GCPtr<TraversableNavigable> Navigable::traversable_navigable() const
  131. {
  132. // 1. Let navigable be inputNavigable.
  133. auto navigable = const_cast<Navigable*>(this);
  134. // 2. While navigable is not a traversable navigable, set navigable to navigable's parent.
  135. while (navigable && !is<TraversableNavigable>(*navigable))
  136. navigable = navigable->parent();
  137. // 3. Return navigable.
  138. return static_cast<TraversableNavigable*>(navigable);
  139. }
  140. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-top
  141. JS::GCPtr<TraversableNavigable> Navigable::top_level_traversable()
  142. {
  143. // 1. Let navigable be inputNavigable.
  144. auto navigable = this;
  145. // 2. While navigable's parent is not null, set navigable to navigable's parent.
  146. while (navigable->parent())
  147. navigable = navigable->parent();
  148. // 3. Return navigable.
  149. return verify_cast<TraversableNavigable>(navigable);
  150. }
  151. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-session-history-entries
  152. Vector<JS::NonnullGCPtr<SessionHistoryEntry>>& Navigable::get_session_history_entries() const
  153. {
  154. // 1. Let traversable be navigable's traversable navigable.
  155. auto traversable = traversable_navigable();
  156. // FIXME 2. Assert: this is running within traversable's session history traversal queue.
  157. // 3. If navigable is traversable, return traversable's session history entries.
  158. if (this == traversable)
  159. return traversable->session_history_entries();
  160. // 4. Let docStates be an empty ordered set of document states.
  161. Vector<JS::GCPtr<DocumentState>> doc_states;
  162. // 5. For each entry of traversable's session history entries, append entry's document state to docStates.
  163. for (auto& entry : traversable->session_history_entries())
  164. doc_states.append(entry->document_state);
  165. // 6. For each docState of docStates:
  166. while (!doc_states.is_empty()) {
  167. auto doc_state = doc_states.take_first();
  168. // 1. For each nestedHistory of docState's nested histories:
  169. for (auto& nested_history : doc_state->nested_histories()) {
  170. // 1. If nestedHistory's id equals navigable's id, return nestedHistory's entries.
  171. if (nested_history.id == id())
  172. return nested_history.entries;
  173. // 2. For each entry of nestedHistory's entries, append entry's document state to docStates.
  174. for (auto& entry : nested_history.entries)
  175. doc_states.append(entry->document_state);
  176. }
  177. }
  178. VERIFY_NOT_REACHED();
  179. }
  180. // To navigate a navigable navigable to a URL url using a Document sourceDocument,
  181. // with an optional POST resource, string, or null documentResource (default null),
  182. // an optional response-or-null response (default null), an optional boolean exceptionsEnabled (default false),
  183. // an optional history handling behavior historyHandling (default "push"),
  184. // an optional string cspNavigationType (default "other"),
  185. // and an optional referrer policy referrerPolicy (default the empty string):
  186. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  187. WebIDL::ExceptionOr<void> Navigable::navigate(
  188. AK::URL const& url,
  189. JS::NonnullGCPtr<DOM::Document> source_document,
  190. Variant<Empty, String, POSTResource> document_resource,
  191. JS::GCPtr<Fetch::Infrastructure::Response> response,
  192. bool exceptions_enabled,
  193. HistoryHandlingBehavior history_handling,
  194. String csp_navigation_type,
  195. ReferrerPolicy::ReferrerPolicy referrer_policy)
  196. {
  197. // FIXME: 1. Let sourceSnapshotParams be the result of snapshotting source snapshot params given sourceDocument.
  198. // 2. Let initiatorOriginSnapshot be sourceDocument's origin.
  199. auto initiator_origin_snapshot = source_document->origin();
  200. // FIXME: 3. If sourceDocument's node navigable is not allowed by sandboxing to navigate navigable given and sourceSnapshotParams, then:
  201. if constexpr (false) {
  202. // 1. If exceptionsEnabled is true, then throw a "SecurityError" DOMException.
  203. if (exceptions_enabled) {
  204. return WebIDL::SecurityError::create(*vm().current_realm(), "Source document's node navigable is not allowed to navigate"sv);
  205. }
  206. // 2 Return.
  207. return {};
  208. }
  209. // 4. Let navigationId be the result of generating a random UUID.
  210. String navigation_id = TRY_OR_THROW_OOM(vm(), Crypto::generate_random_uuid());
  211. // FIXME: 5. If the surrounding agent is equal to navigable's active document's relevant agent, then continue these steps.
  212. // Otherwise, queue a global task on the navigation and traversal task source given navigable's active window to continue these steps.
  213. // FIXME: 6. If navigable's active document's unload counter is greater than 0,
  214. // then invoke WebDriver BiDi navigation failed with a WebDriver BiDi navigation status whose id is navigationId,
  215. // status is "canceled", and url is url, and return.
  216. // 7. If any of the following are true:
  217. // - url equals navigable's active document's URL;
  218. // - url's scheme is "javascript"; or
  219. // - navigable's active document's is initial about:blank is true
  220. if (url.equals(active_document()->url())
  221. || url.scheme() == "javascript"sv
  222. || active_document()->is_initial_about_blank()) {
  223. // then set historyHandling to "replace".
  224. history_handling = HistoryHandlingBehavior::Replace;
  225. }
  226. // 8. If all of the following are true:
  227. // - documentResource is null;
  228. // - response is null;
  229. // - url equals navigable's active session history entry's URL with exclude fragments set to true; and
  230. // - url's fragment is non-null
  231. if (document_resource.has<Empty>()
  232. && !response
  233. && url.equals(active_session_history_entry()->url, AK::URL::ExcludeFragment::Yes)
  234. && !url.fragment().is_null()) {
  235. // 1. Navigate to a fragment given navigable, url, historyHandling, and navigationId.
  236. TRY(navigate_to_a_fragment(url, history_handling, navigation_id));
  237. // 2. Return.
  238. return {};
  239. }
  240. // 9. If navigable's parent is non-null, then set navigable's is delaying load events to true.
  241. if (parent() != nullptr) {
  242. set_delaying_load_events(true);
  243. }
  244. // 10. Let targetBrowsingContext be navigable's active browsing context.
  245. [[maybe_unused]] auto target_browsing_context = active_browsing_context();
  246. // FIXME: 11. Let targetSnapshotParams be the result of snapshotting target snapshot params given navigable.
  247. // FIXME: 12. Invoke WebDriver BiDi navigation started with targetBrowsingContext, and a new WebDriver BiDi navigation status whose id is navigationId, url is url, and status is "pending".
  248. // 13. If navigable's ongoing navigation is "traversal", then:
  249. if (ongoing_navigation().has<Traversal>()) {
  250. // FIXME: 1. Invoke WebDriver BiDi navigation failed with targetBrowsingContext and a new WebDriver BiDi navigation status whose id is navigationId, status is "canceled", and url is url.
  251. // 2. Return.
  252. return {};
  253. }
  254. // 14. Set navigable's ongoing navigation to navigationId.
  255. m_ongoing_navigation = navigation_id;
  256. // 15. If url's scheme is "javascript", then:
  257. if (url.scheme() == "javascript"sv) {
  258. // 1. Queue a global task on the navigation and traversal task source given navigable's active window to navigate to a javascript: URL given navigable, url, historyHandling, initiatorOriginSnapshot, and cspNavigationType.
  259. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this, url, history_handling, initiator_origin_snapshot, csp_navigation_type] {
  260. (void)navigate_to_a_javascript_url(url, history_handling, initiator_origin_snapshot, csp_navigation_type);
  261. });
  262. // 2. Return.
  263. return {};
  264. }
  265. // 16. In parallel, run these steps:
  266. Platform::EventLoopPlugin::the().deferred_invoke([this, document_resource, url, navigation_id, referrer_policy, initiator_origin_snapshot, response] {
  267. // FIXME: 1. Let unloadPromptCanceled be the result of checking if unloading is user-canceled for navigable's active document's inclusive descendant navigables.
  268. // FIXME: 2. If unloadPromptCanceled is true, or navigable's ongoing navigation is no longer navigationId, then:
  269. // 3. Queue a global task on the navigation and traversal task source given navigable's active window to abort navigable's active document.
  270. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this] {
  271. VERIFY(active_document());
  272. active_document()->abort();
  273. });
  274. // 4. Let documentState be a new document state with
  275. // request referrer policy: referrerPolicy
  276. // initiator origin: initiatorOriginSnapshot
  277. // FIXME: resource: documentResource
  278. // navigable target name: navigable's target name
  279. JS::NonnullGCPtr<DocumentState> document_state = *heap().allocate_without_realm<DocumentState>();
  280. document_state->set_request_referrer_policy(referrer_policy);
  281. document_state->set_initiator_origin(initiator_origin_snapshot);
  282. document_state->set_navigable_target_name(target_name());
  283. // 5. If url is about:blank, then set documentState's origin to documentState's initiator origin.
  284. if (url == "about:blank"sv) {
  285. document_state->set_origin(document_state->initiator_origin());
  286. }
  287. // 6. Otherwise, if url is about:srcdoc, then set documentState's origin to navigable's parent's active document's origin.
  288. else if (url == "about:srcdoc"sv) {
  289. document_state->set_origin(parent()->active_document()->origin());
  290. }
  291. // 7. Let historyEntry be a new session history entry, with its URL set to url and its document state set to documentState.
  292. JS::NonnullGCPtr<SessionHistoryEntry> history_entry = *heap().allocate_without_realm<SessionHistoryEntry>();
  293. history_entry->url = url;
  294. history_entry->document_state = document_state;
  295. // FIXME: 8. Let navigationParams be null.
  296. // FIXME: 9. If response is non-null:
  297. if (response) {
  298. }
  299. });
  300. return {};
  301. }
  302. WebIDL::ExceptionOr<void> Navigable::navigate_to_a_fragment(AK::URL const&, HistoryHandlingBehavior, String navigation_id)
  303. {
  304. (void)navigation_id;
  305. TODO();
  306. }
  307. WebIDL::ExceptionOr<void> Navigable::navigate_to_a_javascript_url(AK::URL const&, HistoryHandlingBehavior, Origin const& initiator_origin, String csp_navigation_type)
  308. {
  309. (void)initiator_origin;
  310. (void)csp_navigation_type;
  311. TODO();
  312. }
  313. }