Navigable.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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/document-sequences.html#nav-document
  65. JS::GCPtr<DOM::Document> Navigable::active_document()
  66. {
  67. // A navigable's active document is its active session history entry's document.
  68. return m_active_session_history_entry->document_state->document();
  69. }
  70. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-bc
  71. JS::GCPtr<BrowsingContext> Navigable::active_browsing_context()
  72. {
  73. // A navigable's active browsing context is its active document's browsing context.
  74. // If this navigable is a traversable navigable, then its active browsing context will be a top-level browsing context.
  75. if (auto document = active_document())
  76. return document->browsing_context();
  77. return nullptr;
  78. }
  79. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-wp
  80. JS::GCPtr<HTML::WindowProxy> Navigable::active_window_proxy()
  81. {
  82. // A navigable's active WindowProxy is its active browsing context's associated WindowProxy.
  83. if (auto browsing_context = active_browsing_context())
  84. return browsing_context->window_proxy();
  85. return nullptr;
  86. }
  87. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-window
  88. JS::GCPtr<HTML::Window> Navigable::active_window()
  89. {
  90. // A navigable's active window is its active WindowProxy's [[Window]].
  91. if (auto window_proxy = active_window_proxy())
  92. return window_proxy->window();
  93. return nullptr;
  94. }
  95. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-target
  96. String Navigable::target_name() const
  97. {
  98. // FIXME: A navigable's target name is its active session history entry's document state's navigable target name.
  99. dbgln("FIXME: Implement Navigable::target_name()");
  100. return {};
  101. }
  102. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-container
  103. JS::GCPtr<NavigableContainer> Navigable::container() const
  104. {
  105. // The container of a navigable navigable is the navigable container whose nested navigable is navigable, or null if there is no such element.
  106. return m_container;
  107. }
  108. void Navigable::set_container(JS::GCPtr<NavigableContainer> container)
  109. {
  110. m_container = container;
  111. }
  112. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-traversable
  113. JS::GCPtr<TraversableNavigable> Navigable::traversable_navigable()
  114. {
  115. // 1. Let navigable be inputNavigable.
  116. auto navigable = this;
  117. // 2. While navigable is not a traversable navigable, set navigable to navigable's parent.
  118. while (navigable && !is<TraversableNavigable>(*navigable))
  119. navigable = navigable->parent();
  120. // 3. Return navigable.
  121. return static_cast<TraversableNavigable*>(navigable);
  122. }
  123. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-top
  124. JS::GCPtr<TraversableNavigable> Navigable::top_level_traversable()
  125. {
  126. // 1. Let navigable be inputNavigable.
  127. auto navigable = this;
  128. // 2. While navigable's parent is not null, set navigable to navigable's parent.
  129. while (navigable->parent())
  130. navigable = navigable->parent();
  131. // 3. Return navigable.
  132. return verify_cast<TraversableNavigable>(navigable);
  133. }
  134. // To navigate a navigable navigable to a URL url using a Document sourceDocument,
  135. // with an optional POST resource, string, or null documentResource (default null),
  136. // an optional response-or-null response (default null), an optional boolean exceptionsEnabled (default false),
  137. // an optional history handling behavior historyHandling (default "push"),
  138. // an optional string cspNavigationType (default "other"),
  139. // and an optional referrer policy referrerPolicy (default the empty string):
  140. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  141. WebIDL::ExceptionOr<void> Navigable::navigate(
  142. AK::URL const& url,
  143. JS::NonnullGCPtr<DOM::Document> source_document,
  144. Variant<Empty, String, POSTResource> document_resource,
  145. JS::GCPtr<Fetch::Infrastructure::Response> response,
  146. bool exceptions_enabled,
  147. HistoryHandlingBehavior history_handling,
  148. String csp_navigation_type,
  149. ReferrerPolicy::ReferrerPolicy referrer_policy)
  150. {
  151. // FIXME: 1. Let sourceSnapshotParams be the result of snapshotting source snapshot params given sourceDocument.
  152. // 2. Let initiatorOriginSnapshot be sourceDocument's origin.
  153. auto initiator_origin_snapshot = source_document->origin();
  154. // FIXME: 3. If sourceDocument's node navigable is not allowed by sandboxing to navigate navigable given and sourceSnapshotParams, then:
  155. if constexpr (false) {
  156. // 1. If exceptionsEnabled is true, then throw a "SecurityError" DOMException.
  157. if (exceptions_enabled) {
  158. return WebIDL::SecurityError::create(*vm().current_realm(), "Source document's node navigable is not allowed to navigate"sv);
  159. }
  160. // 2 Return.
  161. return {};
  162. }
  163. // 4. Let navigationId be the result of generating a random UUID.
  164. String navigation_id = TRY_OR_THROW_OOM(vm(), Crypto::generate_random_uuid());
  165. // FIXME: 5. If the surrounding agent is equal to navigable's active document's relevant agent, then continue these steps.
  166. // Otherwise, queue a global task on the navigation and traversal task source given navigable's active window to continue these steps.
  167. // FIXME: 6. If navigable's active document's unload counter is greater than 0,
  168. // then invoke WebDriver BiDi navigation failed with a WebDriver BiDi navigation status whose id is navigationId,
  169. // status is "canceled", and url is url, and return.
  170. // 7. If any of the following are true:
  171. // - url equals navigable's active document's URL;
  172. // - url's scheme is "javascript"; or
  173. // - navigable's active document's is initial about:blank is true
  174. if (url.equals(active_document()->url())
  175. || url.scheme() == "javascript"sv
  176. || active_document()->is_initial_about_blank()) {
  177. // then set historyHandling to "replace".
  178. history_handling = HistoryHandlingBehavior::Replace;
  179. }
  180. // 8. If all of the following are true:
  181. // - documentResource is null;
  182. // - response is null;
  183. // - url equals navigable's active session history entry's URL with exclude fragments set to true; and
  184. // - url's fragment is non-null
  185. if (document_resource.has<Empty>()
  186. && !response
  187. && url.equals(active_session_history_entry()->url, AK::URL::ExcludeFragment::Yes)
  188. && !url.fragment().is_null()) {
  189. // 1. Navigate to a fragment given navigable, url, historyHandling, and navigationId.
  190. TRY(navigate_to_a_fragment(url, history_handling, navigation_id));
  191. // 2. Return.
  192. return {};
  193. }
  194. // 9. If navigable's parent is non-null, then set navigable's is delaying load events to true.
  195. if (parent() != nullptr) {
  196. set_delaying_load_events(true);
  197. }
  198. // 10. Let targetBrowsingContext be navigable's active browsing context.
  199. [[maybe_unused]] auto target_browsing_context = active_browsing_context();
  200. // FIXME: 11. Let targetSnapshotParams be the result of snapshotting target snapshot params given navigable.
  201. // 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".
  202. // 13. If navigable's ongoing navigation is "traversal", then:
  203. if (ongoing_navigation().has<Traversal>()) {
  204. // 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.
  205. // 2. Return.
  206. return {};
  207. }
  208. // 14. Set navigable's ongoing navigation to navigationId.
  209. m_ongoing_navigation = navigation_id;
  210. // 15. If url's scheme is "javascript", then:
  211. if (url.scheme() == "javascript"sv) {
  212. // 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.
  213. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this, url, history_handling, initiator_origin_snapshot, csp_navigation_type] {
  214. (void)navigate_to_a_javascript_url(url, history_handling, initiator_origin_snapshot, csp_navigation_type);
  215. });
  216. // 2. Return.
  217. return {};
  218. }
  219. // 16. In parallel, run these steps:
  220. Platform::EventLoopPlugin::the().deferred_invoke([this, document_resource, url, navigation_id, referrer_policy, initiator_origin_snapshot, response] {
  221. // FIXME: 1. Let unloadPromptCanceled be the result of checking if unloading is user-canceled for navigable's active document's inclusive descendant navigables.
  222. // FIXME: 2. If unloadPromptCanceled is true, or navigable's ongoing navigation is no longer navigationId, then:
  223. // 3. Queue a global task on the navigation and traversal task source given navigable's active window to abort navigable's active document.
  224. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this] {
  225. VERIFY(active_document());
  226. active_document()->abort();
  227. });
  228. // 4. Let documentState be a new document state with
  229. // request referrer policy: referrerPolicy
  230. // initiator origin: initiatorOriginSnapshot
  231. // FIXME: resource: documentResource
  232. // navigable target name: navigable's target name
  233. JS::NonnullGCPtr<DocumentState> document_state = *heap().allocate_without_realm<DocumentState>();
  234. document_state->set_request_referrer_policy(referrer_policy);
  235. document_state->set_initiator_origin(initiator_origin_snapshot);
  236. document_state->set_navigable_target_name(target_name());
  237. // 5. If url is about:blank, then set documentState's origin to documentState's initiator origin.
  238. if (url == "about:blank"sv) {
  239. document_state->set_origin(document_state->initiator_origin());
  240. }
  241. // 6. Otherwise, if url is about:srcdoc, then set documentState's origin to navigable's parent's active document's origin.
  242. else if (url == "about:srcdoc"sv) {
  243. document_state->set_origin(parent()->active_document()->origin());
  244. }
  245. // 7. Let historyEntry be a new session history entry, with its URL set to url and its document state set to documentState.
  246. JS::NonnullGCPtr<SessionHistoryEntry> history_entry = *heap().allocate_without_realm<SessionHistoryEntry>();
  247. history_entry->url = url;
  248. history_entry->document_state = document_state;
  249. // FIXME: 8. Let navigationParams be null.
  250. // FIXME: 9. If response is non-null:
  251. if (response) {
  252. }
  253. });
  254. return {};
  255. }
  256. WebIDL::ExceptionOr<void> Navigable::navigate_to_a_fragment(AK::URL const&, HistoryHandlingBehavior, String navigation_id)
  257. {
  258. (void)navigation_id;
  259. TODO();
  260. }
  261. WebIDL::ExceptionOr<void> Navigable::navigate_to_a_javascript_url(AK::URL const&, HistoryHandlingBehavior, Origin const& initiator_origin, String csp_navigation_type)
  262. {
  263. (void)initiator_origin;
  264. (void)csp_navigation_type;
  265. TODO();
  266. }
  267. }