Navigable.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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/DOM/DocumentLoading.h>
  10. #include <LibWeb/Fetch/Fetching/Fetching.h>
  11. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  12. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  13. #include <LibWeb/Fetch/Infrastructure/URL.h>
  14. #include <LibWeb/HTML/BrowsingContext.h>
  15. #include <LibWeb/HTML/DocumentState.h>
  16. #include <LibWeb/HTML/Navigable.h>
  17. #include <LibWeb/HTML/NavigationParams.h>
  18. #include <LibWeb/HTML/SessionHistoryEntry.h>
  19. #include <LibWeb/HTML/TraversableNavigable.h>
  20. #include <LibWeb/Infra/Strings.h>
  21. #include <LibWeb/Platform/EventLoopPlugin.h>
  22. namespace Web::HTML {
  23. static HashTable<Navigable*>& all_navigables()
  24. {
  25. static HashTable<Navigable*> set;
  26. return set;
  27. }
  28. // https://html.spec.whatwg.org/multipage/document-sequences.html#child-navigable
  29. Vector<JS::Handle<Navigable>> Navigable::child_navigables() const
  30. {
  31. Vector<JS::Handle<Navigable>> results;
  32. for (auto& entry : all_navigables()) {
  33. if (entry->parent() == this)
  34. results.append(entry);
  35. }
  36. return results;
  37. }
  38. Navigable::Navigable()
  39. {
  40. all_navigables().set(this);
  41. }
  42. Navigable::~Navigable()
  43. {
  44. all_navigables().remove(this);
  45. }
  46. void Navigable::visit_edges(Cell::Visitor& visitor)
  47. {
  48. Base::visit_edges(visitor);
  49. visitor.visit(m_parent);
  50. visitor.visit(m_current_session_history_entry);
  51. visitor.visit(m_active_session_history_entry);
  52. visitor.visit(m_container);
  53. }
  54. JS::GCPtr<Navigable> Navigable::navigable_with_active_document(JS::NonnullGCPtr<DOM::Document> document)
  55. {
  56. for (auto* navigable : all_navigables()) {
  57. if (navigable->active_document() == document)
  58. return navigable;
  59. }
  60. return nullptr;
  61. }
  62. // https://html.spec.whatwg.org/multipage/document-sequences.html#initialize-the-navigable
  63. ErrorOr<void> Navigable::initialize_navigable(JS::NonnullGCPtr<DocumentState> document_state, JS::GCPtr<Navigable> parent)
  64. {
  65. static int next_id = 0;
  66. m_id = TRY(String::number(next_id++));
  67. // 1. Let entry be a new session history entry, with
  68. JS::NonnullGCPtr<SessionHistoryEntry> entry = *heap().allocate_without_realm<SessionHistoryEntry>();
  69. // URL: document's URL
  70. entry->url = document_state->document()->url();
  71. // document state: documentState
  72. entry->document_state = document_state;
  73. // 2. Set navigable's current session history entry to entry.
  74. m_current_session_history_entry = entry;
  75. // 3. Set navigable's active session history entry to entry.
  76. m_active_session_history_entry = entry;
  77. // 4. Set navigable's parent to parent.
  78. m_parent = parent;
  79. return {};
  80. }
  81. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-the-target-history-entry
  82. JS::GCPtr<SessionHistoryEntry> Navigable::get_the_target_history_entry(int target_step) const
  83. {
  84. // 1. Let entries be the result of getting session history entries for navigable.
  85. auto& entries = get_session_history_entries();
  86. // 2. Return the item in entries that has the greatest step less than or equal to step.
  87. JS::GCPtr<SessionHistoryEntry> result = nullptr;
  88. for (auto& entry : entries) {
  89. auto entry_step = entry->step.get<int>();
  90. if (entry_step <= target_step) {
  91. if (!result || result->step.get<int>() < entry_step) {
  92. result = entry;
  93. }
  94. }
  95. }
  96. return result;
  97. }
  98. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#activate-history-entry
  99. void Navigable::activate_history_entry(JS::GCPtr<SessionHistoryEntry> entry)
  100. {
  101. // FIXME: 1. Save persisted state to the navigable's active session history entry.
  102. // 2. Let newDocument be entry's document.
  103. JS::GCPtr<DOM::Document> new_document = entry->document_state->document().ptr();
  104. // 3. Assert: newDocument's is initial about:blank is false, i.e., we never traverse
  105. // back to the initial about:blank Document because it always gets replaced when we
  106. // navigate away from it.
  107. VERIFY(!new_document->is_initial_about_blank());
  108. // 4. Set navigable's active session history entry to entry.
  109. m_active_session_history_entry = entry;
  110. // 5. Make active newDocument.
  111. new_document->make_active();
  112. // Not in the spec:
  113. if (is<TraversableNavigable>(*this) && parent() == nullptr) {
  114. if (auto* page = active_browsing_context()->page()) {
  115. page->client().page_did_start_loading(entry->url, false);
  116. }
  117. }
  118. }
  119. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-document
  120. JS::GCPtr<DOM::Document> Navigable::active_document()
  121. {
  122. // A navigable's active document is its active session history entry's document.
  123. return m_active_session_history_entry->document_state->document();
  124. }
  125. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-bc
  126. JS::GCPtr<BrowsingContext> Navigable::active_browsing_context()
  127. {
  128. // A navigable's active browsing context is its active document's browsing context.
  129. // If this navigable is a traversable navigable, then its active browsing context will be a top-level browsing context.
  130. if (auto document = active_document())
  131. return document->browsing_context();
  132. return nullptr;
  133. }
  134. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-wp
  135. JS::GCPtr<HTML::WindowProxy> Navigable::active_window_proxy()
  136. {
  137. // A navigable's active WindowProxy is its active browsing context's associated WindowProxy.
  138. if (auto browsing_context = active_browsing_context())
  139. return browsing_context->window_proxy();
  140. return nullptr;
  141. }
  142. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-window
  143. JS::GCPtr<HTML::Window> Navigable::active_window()
  144. {
  145. // A navigable's active window is its active WindowProxy's [[Window]].
  146. if (auto window_proxy = active_window_proxy())
  147. return window_proxy->window();
  148. return nullptr;
  149. }
  150. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-target
  151. String Navigable::target_name() const
  152. {
  153. // FIXME: A navigable's target name is its active session history entry's document state's navigable target name.
  154. dbgln("FIXME: Implement Navigable::target_name()");
  155. return {};
  156. }
  157. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-container
  158. JS::GCPtr<NavigableContainer> Navigable::container() const
  159. {
  160. // The container of a navigable navigable is the navigable container whose nested navigable is navigable, or null if there is no such element.
  161. return NavigableContainer::navigable_container_with_content_navigable(const_cast<Navigable&>(*this));
  162. }
  163. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-container-document
  164. JS::GCPtr<DOM::Document> Navigable::container_document() const
  165. {
  166. auto container = this->container();
  167. // 1. If navigable's container is null, then return null.
  168. if (!container)
  169. return nullptr;
  170. // 2. Return navigable's container's node document.
  171. return container->document();
  172. }
  173. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-traversable
  174. JS::GCPtr<TraversableNavigable> Navigable::traversable_navigable() const
  175. {
  176. // 1. Let navigable be inputNavigable.
  177. auto navigable = const_cast<Navigable*>(this);
  178. // 2. While navigable is not a traversable navigable, set navigable to navigable's parent.
  179. while (navigable && !is<TraversableNavigable>(*navigable))
  180. navigable = navigable->parent();
  181. // 3. Return navigable.
  182. return static_cast<TraversableNavigable*>(navigable);
  183. }
  184. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-top
  185. JS::GCPtr<TraversableNavigable> Navigable::top_level_traversable()
  186. {
  187. // 1. Let navigable be inputNavigable.
  188. auto navigable = this;
  189. // 2. While navigable's parent is not null, set navigable to navigable's parent.
  190. while (navigable->parent())
  191. navigable = navigable->parent();
  192. // 3. Return navigable.
  193. return verify_cast<TraversableNavigable>(navigable);
  194. }
  195. Navigable::ChosenNavigable Navigable::choose_a_navigable(StringView name, TokenizedFeature::NoOpener, ActivateTab)
  196. {
  197. // 1. Let chosen be null.
  198. JS::GCPtr<Navigable> chosen = nullptr;
  199. // 2. Let windowType be "existing or none".
  200. auto window_type = WindowType::ExistingOrNone;
  201. // 3. Let sandboxingFlagSet be current's active document's active sandboxing flag set.
  202. [[maybe_unused]] auto sandboxing_flag_set = active_document()->active_sandboxing_flag_set();
  203. // 4. If name is the empty string or an ASCII case-insensitive match for "_self", then set chosen to currentNavigable.
  204. if (name.is_empty() || Infra::is_ascii_case_insensitive_match(name, "_self"sv)) {
  205. chosen = this;
  206. }
  207. // 5. Otherwise, if name is an ASCII case-insensitive match for "_parent",
  208. // set chosen to currentNavigable's parent, if any, and currentNavigable otherwise.
  209. else if (Infra::is_ascii_case_insensitive_match(name, "_parent"sv)) {
  210. if (auto parent = this->parent())
  211. chosen = parent;
  212. else
  213. chosen = this;
  214. }
  215. // 6. Otherwise, if name is an ASCII case-insensitive match for "_top",
  216. // set chosen to currentNavigable's traversable navigable.
  217. else if (Infra::is_ascii_case_insensitive_match(name, "_top"sv)) {
  218. chosen = traversable_navigable();
  219. }
  220. // 7. Otherwise, if name is not an ASCII case-insensitive match for "_blank",
  221. // there exists a navigable whose target name is the same as name, currentNavigable's
  222. // active browsing context is familiar with that navigable's active browsing context,
  223. // and the user agent determines that the two browsing contexts are related enough that
  224. // it is ok if they reach each other, set chosen to that navigable. If there are multiple
  225. // matching navigables, the user agent should pick one in some arbitrary consistent manner,
  226. // such as the most recently opened, most recently focused, or more closely related, and set
  227. // chosen to it.
  228. else if (!Infra::is_ascii_case_insensitive_match(name, "_blank"sv)) {
  229. TODO();
  230. }
  231. // Otherwise, a new top-level traversable is being requested, and what happens depends on the
  232. // user agent's configuration and abilities — it is determined by the rules given for the first
  233. // applicable option from the following list:
  234. else {
  235. TODO();
  236. }
  237. return { chosen.ptr(), window_type };
  238. }
  239. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-session-history-entries
  240. Vector<JS::NonnullGCPtr<SessionHistoryEntry>>& Navigable::get_session_history_entries() const
  241. {
  242. // 1. Let traversable be navigable's traversable navigable.
  243. auto traversable = traversable_navigable();
  244. // FIXME 2. Assert: this is running within traversable's session history traversal queue.
  245. // 3. If navigable is traversable, return traversable's session history entries.
  246. if (this == traversable)
  247. return traversable->session_history_entries();
  248. // 4. Let docStates be an empty ordered set of document states.
  249. Vector<JS::GCPtr<DocumentState>> doc_states;
  250. // 5. For each entry of traversable's session history entries, append entry's document state to docStates.
  251. for (auto& entry : traversable->session_history_entries())
  252. doc_states.append(entry->document_state);
  253. // 6. For each docState of docStates:
  254. while (!doc_states.is_empty()) {
  255. auto doc_state = doc_states.take_first();
  256. // 1. For each nestedHistory of docState's nested histories:
  257. for (auto& nested_history : doc_state->nested_histories()) {
  258. // 1. If nestedHistory's id equals navigable's id, return nestedHistory's entries.
  259. if (nested_history.id == id())
  260. return nested_history.entries;
  261. // 2. For each entry of nestedHistory's entries, append entry's document state to docStates.
  262. for (auto& entry : nested_history.entries)
  263. doc_states.append(entry->document_state);
  264. }
  265. }
  266. VERIFY_NOT_REACHED();
  267. }
  268. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#create-navigation-params-from-a-srcdoc-resource
  269. static WebIDL::ExceptionOr<NavigationParams> create_navigation_params_from_a_srcdoc_resource(JS::GCPtr<SessionHistoryEntry> entry, JS::GCPtr<Navigable> navigable, SourceSnapshotParams const&, Optional<String> navigation_id)
  270. {
  271. auto& vm = navigable->vm();
  272. auto& realm = navigable->active_window()->realm();
  273. // 1. Let documentResource be entry's document state's resource.
  274. auto document_resource = entry->document_state->resource();
  275. VERIFY(document_resource.has<String>());
  276. // 2. Let response be a new response with
  277. // URL: about:srcdoc
  278. // header list: (`Content-Type`, `text/html`)
  279. // body: the UTF-8 encoding of documentResource, as a body
  280. auto response = Fetch::Infrastructure::Response::create(vm);
  281. response->url_list().append(AK::URL("about:srcdoc"));
  282. auto header = TRY_OR_THROW_OOM(vm, Fetch::Infrastructure::Header::from_string_pair("Content-Type"sv, "text/html"sv));
  283. TRY_OR_THROW_OOM(vm, response->header_list()->append(move(header)));
  284. response->set_body(TRY(Fetch::Infrastructure::byte_sequence_as_body(realm, document_resource.get<String>().bytes())));
  285. // FIXME: 3. Let responseOrigin be the result of determining the origin given response's URL, targetSnapshotParams's sandboxing flags, null, and entry's document state's origin.
  286. // 4. Let coop be a new cross-origin opener policy.
  287. CrossOriginOpenerPolicy coop;
  288. // 5. Let coopEnforcementResult be a new cross-origin opener policy enforcement result with
  289. // url: response's URL
  290. // FIXME: origin: responseOrigin
  291. // cross-origin opener policy: coop
  292. CrossOriginOpenerPolicyEnforcementResult coop_enforcement_result {
  293. .url = *response->url(),
  294. .origin = Origin {},
  295. .cross_origin_opener_policy = coop
  296. };
  297. // FIXME: 6. Let policyContainer be the result of determining navigation params policy container given response's URL, entry's document state's history policy container, null, navigable's container document's policy container, and null.
  298. // 7. Return a new navigation params, with
  299. // id: navigationId
  300. // request: null
  301. // response: response
  302. // FIXME: origin: responseOrigin
  303. // FIXME: policy container: policyContainer
  304. // FIXME: final sandboxing flag set: targetSnapshotParams's sandboxing flags
  305. // cross-origin opener policy: coop
  306. // COOP enforcement result: coopEnforcementResult
  307. // reserved environment: null
  308. // navigable: navigable
  309. // FIXME: navigation timing type: navTimingType
  310. // fetch controller: null
  311. // commit early hints: null
  312. HTML::NavigationParams navigation_params {
  313. .id = navigation_id,
  314. .request = {},
  315. .response = *response,
  316. .origin = Origin {},
  317. .policy_container = PolicyContainer {},
  318. .final_sandboxing_flag_set = SandboxingFlagSet {},
  319. .cross_origin_opener_policy = move(coop),
  320. .coop_enforcement_result = move(coop_enforcement_result),
  321. .reserved_environment = {},
  322. .browsing_context = navigable->active_browsing_context(),
  323. .navigable = navigable,
  324. };
  325. return { navigation_params };
  326. }
  327. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#create-navigation-params-by-fetching
  328. static WebIDL::ExceptionOr<Optional<NavigationParams>> create_navigation_params_by_fetching(JS::GCPtr<SessionHistoryEntry> entry, JS::GCPtr<Navigable> navigable, SourceSnapshotParams const& source_snapshot_params, Optional<String> navigation_id)
  329. {
  330. auto& vm = navigable->vm();
  331. auto& realm = navigable->active_window()->realm();
  332. // FIXME: 1. Assert: this is running in parallel.
  333. // 2. Let documentResource be entry's document state's resource.
  334. auto document_resource = entry->document_state->resource();
  335. // 3. Let request be a new request, with
  336. // url: entry's URL
  337. // client: sourceSnapshotParams's fetch client
  338. // destination: "document"
  339. // credentials mode: "include"
  340. // use-URL-credentials flag: set
  341. // redirect mode: "manual"
  342. // replaces client id: navigable's active document's relevant settings object's id
  343. // mode: "navigate"
  344. // referrer: entry's document state's request referrer
  345. // FIXME: referrer policy: entry's document state's request referrer policy
  346. auto request = Fetch::Infrastructure::Request::create(vm);
  347. request->set_url(entry->url);
  348. request->set_client(source_snapshot_params.fetch_client);
  349. request->set_destination(Fetch::Infrastructure::Request::Destination::Document);
  350. request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include);
  351. request->set_use_url_credentials(true);
  352. request->set_redirect_mode(Fetch::Infrastructure::Request::RedirectMode::Manual);
  353. auto replaces_client_id = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(navigable->active_document()->relevant_settings_object().id));
  354. request->set_replaces_client_id(replaces_client_id);
  355. request->set_mode(Fetch::Infrastructure::Request::Mode::Navigate);
  356. request->set_referrer(entry->document_state->request_referrer());
  357. // 4. If documentResource is a POST resource, then:
  358. if (document_resource.has<POSTResource>()) {
  359. // 1. Set request's method to `POST`.
  360. request->set_method(TRY_OR_THROW_OOM(vm, ByteBuffer::copy("post"sv.bytes())));
  361. // 2. Set request's body to documentResource's request body.
  362. request->set_body(document_resource.get<POSTResource>().request_body.value());
  363. // 3. Set `Content-Type` to documentResource's request content-type in request's header list.
  364. auto request_content_type = document_resource.get<POSTResource>().request_content_type;
  365. auto request_content_type_string = [request_content_type]() {
  366. switch (request_content_type) {
  367. case POSTResource::RequestContentType::ApplicationXWWWFormUrlencoded:
  368. return "application/x-www-form-urlencoded"sv;
  369. case POSTResource::RequestContentType::MultipartFormData:
  370. return "multipart/form-data"sv;
  371. case POSTResource::RequestContentType::TextPlain:
  372. return "text/plain"sv;
  373. default:
  374. VERIFY_NOT_REACHED();
  375. }
  376. }();
  377. auto header = TRY_OR_THROW_OOM(vm, Fetch::Infrastructure::Header::from_string_pair("Content-Type"sv, request_content_type_string));
  378. TRY_OR_THROW_OOM(vm, request->header_list()->append(move(header)));
  379. }
  380. // 5. If entry's document state's reload pending is true, then set request's reload-navigation flag.
  381. if (entry->document_state->reload_pending())
  382. request->set_reload_navigation(true);
  383. // 6. Otherwise, if entry's document state's ever populated is true, then set request's history-navigation flag.
  384. if (entry->document_state->ever_populated())
  385. request->set_history_navigation(true);
  386. // 9. Let response be null.
  387. JS::GCPtr<Fetch::Infrastructure::Response> response = nullptr;
  388. // 10. Let responseOrigin be null.
  389. Optional<HTML::Origin> response_origin;
  390. // 11. Let fetchController be null.
  391. JS::GCPtr<Fetch::Infrastructure::FetchController> fetch_controller = nullptr;
  392. // 13. Let finalSandboxFlags be an empty sandboxing flag set.
  393. SandboxingFlagSet final_sandbox_flags;
  394. // 16. Let locationURL be null.
  395. ErrorOr<Optional<AK::URL>> location_url { OptionalNone {} };
  396. // 17. Let currentURL be request's current URL.
  397. AK::URL current_url = request->current_url();
  398. // FIXME: 18. Let commitEarlyHints be null.
  399. // 19. While true:
  400. while (true) {
  401. // FIXME: 1. If request's reserved client is not null and currentURL's origin is not the same as request's reserved client's creation URL's origin, then:
  402. // FIXME: 2. If request's reserved client is null, then:
  403. // FIXME: 3. If the result of should navigation request of type be blocked by Content Security Policy? given request and cspNavigationType is "Blocked", then set response to a network error and break. [CSP]
  404. // 4. Set response to null.
  405. response = nullptr;
  406. // 5. If fetchController is null, then set fetchController to the result of fetching request,
  407. // with processEarlyHintsResponse set to processEarlyHintsResponseas defined below, processResponse
  408. // set to processResponse as defined below, and useParallelQueue set to true.
  409. if (!fetch_controller) {
  410. // FIXME: Let processEarlyHintsResponse be the following algorithm given a response earlyResponse:
  411. // Let processResponse be the following algorithm given a response fetchedResponse:
  412. auto process_response = [&response](JS::NonnullGCPtr<Fetch::Infrastructure::Response> fetch_response) {
  413. // 1. Set response to fetchedResponse.
  414. response = fetch_response;
  415. };
  416. fetch_controller = TRY(Fetch::Fetching::fetch(
  417. realm,
  418. request,
  419. Fetch::Infrastructure::FetchAlgorithms::create(vm,
  420. {
  421. .process_request_body_chunk_length = {},
  422. .process_request_end_of_body = {},
  423. .process_early_hints_response = {},
  424. .process_response = move(process_response),
  425. .process_response_end_of_body = {},
  426. .process_response_consume_body = {},
  427. }),
  428. Fetch::Fetching::UseParallelQueue::Yes));
  429. }
  430. // 6. Otherwise, process the next manual redirect for fetchController.
  431. else {
  432. fetch_controller->process_next_manual_redirect();
  433. }
  434. // 7. Wait until either response is non-null, or navigable's ongoing navigation changes to no longer equal navigationId.
  435. Platform::EventLoopPlugin::the().spin_until([&]() {
  436. if (response != nullptr)
  437. return true;
  438. if (navigation_id.has_value() && (!navigable->ongoing_navigation().has<String>() || navigable->ongoing_navigation().get<String>() != *navigation_id))
  439. return true;
  440. return false;
  441. });
  442. // If the latter condition occurs, then abort fetchController, and return. Otherwise, proceed onward.
  443. if (navigation_id.has_value() && (!navigable->ongoing_navigation().has<String>() || navigable->ongoing_navigation().get<String>() != *navigation_id)) {
  444. fetch_controller->abort(realm, {});
  445. return OptionalNone {};
  446. }
  447. // 8. If request's body is null, then set entry's document state's resource to null.
  448. if (!request->body().has<Empty>()) {
  449. entry->document_state->set_resource(Empty {});
  450. }
  451. // 11. Set responseOrigin to the result of determining the origin given response's URL, finalSandboxFlags,
  452. // entry's document state's initiator origin, and null.
  453. response_origin = determine_the_origin(*response->url(), final_sandbox_flags, entry->document_state->initiator_origin(), {});
  454. // 14. Set locationURL to response's location URL given currentURL's fragment.
  455. auto location_url = response->location_url(current_url.fragment());
  456. VERIFY(!location_url.is_error());
  457. // 15. If locationURL is failure or null, then break.
  458. if (location_url.is_error() || !location_url.value().has_value()) {
  459. break;
  460. }
  461. // 16. Assert: locationURL is a URL.
  462. VERIFY(location_url.value()->is_valid());
  463. // FIXME: 17. Set entry's serialized state to StructuredSerializeForStorage(null).
  464. // 18. Let oldDocState be entry's document state.
  465. auto old_doc_state = entry->document_state;
  466. // 19. Set entry's document state to a new document state, with
  467. // history policy container: a clone of the oldDocState's history policy container if it is non-null; null otherwise
  468. // request referrer: oldDocState's request referrer
  469. // request referrer policy: oldDocState's request referrer policy
  470. // origin: oldDocState's origin
  471. // resource: oldDocState's resource
  472. // ever populated: oldDocState's ever populated
  473. // navigable target name: oldDocState's navigable target name
  474. entry->document_state = navigable->heap().allocate_without_realm<DocumentState>();
  475. entry->document_state->set_history_policy_container(old_doc_state->history_policy_container());
  476. entry->document_state->set_request_referrer(old_doc_state->request_referrer());
  477. entry->document_state->set_request_referrer_policy(old_doc_state->request_referrer_policy());
  478. entry->document_state->set_origin(old_doc_state->origin());
  479. entry->document_state->set_resource(old_doc_state->resource());
  480. entry->document_state->set_ever_populated(old_doc_state->ever_populated());
  481. entry->document_state->set_navigable_target_name(old_doc_state->navigable_target_name());
  482. // 20. If locationURL's scheme is not an HTTP(S) scheme, then:
  483. if (!Fetch::Infrastructure::is_http_or_https_scheme(location_url.value()->scheme())) {
  484. // 1. Set entry's document state's resource to null.
  485. entry->document_state->set_resource(Empty {});
  486. // 2. Break.
  487. break;
  488. }
  489. // 21. Set currentURL to locationURL.
  490. current_url = location_url.value().value();
  491. // 22. Set entry's URL to currentURL.
  492. entry->url = current_url;
  493. }
  494. // FIXME: 20. If locationURL is a URL whose scheme is not a fetch scheme, then return a new non-fetch scheme navigation params, with
  495. // initiator origin request's current URL's origin
  496. if (!location_url.is_error() && location_url.value().has_value() && !Fetch::Infrastructure::is_fetch_scheme(location_url.value().value().scheme())) {
  497. TODO();
  498. }
  499. // 21. If any of the following are true:
  500. // - response is a network error;
  501. // - locationURL is failure; or
  502. // - locationURL is a URL whose scheme is a fetch scheme
  503. // then return null.
  504. if (response->is_network_error() || location_url.is_error() || (location_url.value().has_value() && Fetch::Infrastructure::is_fetch_scheme(location_url.value().value().scheme()))) {
  505. return OptionalNone {};
  506. }
  507. // 22. Assert: locationURL is null and response is not a network error.
  508. VERIFY(!location_url.value().has_value());
  509. VERIFY(!response->is_network_error());
  510. // FIXME: 23. Let resultPolicyContainer be the result of determining navigation params policy container given response's
  511. // URL, entry's document state's history policy container, sourceSnapshotParams's source policy container,
  512. // null, and responsePolicyContainer.
  513. // 25. Return a new navigation params, with
  514. // id: navigationId
  515. // request: request
  516. // response: response
  517. // origin: responseOrigin
  518. // FIXME: policy container: resultPolicyContainer
  519. // FIXME: final sandboxing flag set: finalSandboxFlags
  520. // FIXME: cross-origin opener policy: responseCOOP
  521. // FIXME: COOP enforcement result: coopEnforcementResult
  522. // FIXME: reserved environment: request's reserved client
  523. // navigable: navigable
  524. // FIXME: navigation timing type: navTimingType
  525. // fetch controller: fetchController
  526. // FIXME: commit early hints: commitEarlyHints
  527. HTML::NavigationParams navigation_params {
  528. .id = navigation_id,
  529. .request = request,
  530. .response = *response,
  531. .origin = *response_origin,
  532. .policy_container = PolicyContainer {},
  533. .final_sandboxing_flag_set = SandboxingFlagSet {},
  534. .cross_origin_opener_policy = CrossOriginOpenerPolicy {},
  535. .coop_enforcement_result = CrossOriginOpenerPolicyEnforcementResult {},
  536. .reserved_environment = {},
  537. .browsing_context = navigable->active_browsing_context(),
  538. .navigable = navigable,
  539. .fetch_controller = fetch_controller,
  540. };
  541. return { navigation_params };
  542. }
  543. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#attempt-to-populate-the-history-entry's-document
  544. WebIDL::ExceptionOr<void> Navigable::populate_session_history_entry_document(JS::GCPtr<SessionHistoryEntry> entry, Optional<NavigationParams> navigation_params, Optional<String> navigation_id, SourceSnapshotParams const& source_snapshot_params, bool allow_POST, Function<void()> completion_steps)
  545. {
  546. // FIXME: 1. Assert: this is running in parallel.
  547. // 2. Assert: if navigationParams is non-null, then navigationParams's response is non-null.
  548. if (navigation_params.has_value())
  549. VERIFY(navigation_params->response);
  550. // 3. Let currentBrowsingContext be navigable's active browsing context.
  551. [[maybe_unused]] auto current_browsing_context = active_browsing_context();
  552. // 4. Let documentResource be entry's document state's resource.
  553. auto document_resource = entry->document_state->resource();
  554. // 5. If navigationParams is null, then:
  555. if (!navigation_params.has_value()) {
  556. // 1. If documentResource is a string, then set navigationParams to the result
  557. // of creating navigation params from a srcdoc resource given entry, navigable,
  558. // targetSnapshotParams, navigationId, and navTimingType.
  559. if (document_resource.has<String>()) {
  560. navigation_params = create_navigation_params_from_a_srcdoc_resource(entry, this, source_snapshot_params, navigation_id).release_value_but_fixme_should_propagate_errors();
  561. }
  562. // 2. Otherwise, if both of the following are true:
  563. // - entry's URL's scheme is a fetch scheme; and
  564. // - documentResource is null, or allowPOST is true and documentResource's request body is not failure (FIXME: check if request body is not failure)
  565. else if (Fetch::Infrastructure::is_fetch_scheme(entry->url.scheme()) && (document_resource.has<Empty>() || allow_POST)) {
  566. navigation_params = create_navigation_params_by_fetching(entry, this, source_snapshot_params, navigation_id).release_value_but_fixme_should_propagate_errors();
  567. }
  568. // FIXME: 3. Otherwise, if entry's URL's scheme is not a fetch scheme, then set navigationParams to a new non-fetch scheme navigation params, with
  569. // initiator origin: entry's document state's initiator origin
  570. else {
  571. TODO();
  572. }
  573. }
  574. // 6. Queue a global task on the navigation and traversal task source, given navigable's active window, to run these steps:
  575. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this, entry, navigation_params, navigation_id, completion_steps = move(completion_steps)] {
  576. // 1. If navigable's ongoing navigation no longer equals navigationId, then run completionSteps and return.
  577. if (navigation_id.has_value() && (!ongoing_navigation().has<String>() || ongoing_navigation().get<String>() != *navigation_id)) {
  578. completion_steps();
  579. return;
  580. }
  581. // 2. Let failure be false.
  582. auto failure = false;
  583. // FIXME: 3. If navigationParams is a non-fetch scheme navigation params, then set entry's document state's document to the result of running attempt to create a non-fetch
  584. // scheme document given entry's URL, navigable, targetSnapshotParams's sandboxing flags, navigationId, navTimingType, sourceSnapshotParams's has transient
  585. // activation, and navigationParams's initiator origin.
  586. // 4. Otherwise, if navigationParams is null, then set failure to true.
  587. if (!navigation_params.has_value()) {
  588. failure = true;
  589. }
  590. // FIXME: 5. Otherwise, if the result of should navigation response to navigation request of type in target be blocked by Content Security Policy? given navigationParams's request,
  591. // navigationParams's response, navigationParams's policy container's CSP list, cspNavigationType, and navigable is "Blocked", then set failure to true.
  592. // FIXME: 6. Otherwise, if navigationParams's reserved environment is non-null and the result of checking a navigation response's adherence to its embedder policy given
  593. // navigationParams's response, navigable, and navigationParams's policy container's embedder policy is false, then set failure to true.
  594. // 8. If failure is true, then:
  595. if (failure) {
  596. // 1. Set entry's document state's document to the result of creating a document for inline content that doesn't have a DOM, given navigable, null, and navTimingType.
  597. // The inline content should indicate to the user the sort of error that occurred.
  598. // FIXME: Use SourceGenerator to produce error page from file:///res/html/error.html
  599. // and display actual error from fetch response.
  600. auto error_html = String::formatted("<h1>Failed to load {}</h1>"sv, entry->url).release_value_but_fixme_should_propagate_errors();
  601. entry->document_state->set_document(create_document_for_inline_content(this, navigation_id, error_html));
  602. // 2. Set entry's document state's document's salvageable to false.
  603. entry->document_state->document()->set_salvageable(false);
  604. // FIXME: 3. If navigationParams is not null, then:
  605. if (navigation_params.has_value()) {
  606. TODO();
  607. }
  608. }
  609. // FIXME: 9. Otherwise, if navigationParams's response's status is 204 or 205, then:
  610. else if (navigation_params->response->status() == 204 || navigation_params->response->status() == 205) {
  611. // 1. Run completionSteps.
  612. completion_steps();
  613. // 2. Return.
  614. return;
  615. }
  616. // FIXME: 10. Otherwise, if navigationParams's response has a `Content-Disposition`
  617. // header specifying the attachment disposition type, then:
  618. // 11. Otherwise:
  619. else {
  620. // 1. Let document be the result of loading a document given navigationParams, sourceSnapshotParams,
  621. // and entry's document state's initiator origin.
  622. auto document = load_document(navigation_params);
  623. // 2. If document is null, then run completionSteps and return.
  624. if (!document) {
  625. VERIFY_NOT_REACHED();
  626. completion_steps();
  627. return;
  628. }
  629. // 3. Set entry's document state's document to document.
  630. entry->document_state->set_document(document.ptr());
  631. // 4. Set entry's document state's origin to document's origin.
  632. entry->document_state->set_origin(document->origin());
  633. }
  634. // FIXME: 12. If entry's document state's request referrer is "client", then set it to request's referrer.
  635. // 13. If entry's document state's document is not null, then set entry's document state's ever populated to true.
  636. if (entry->document_state->document()) {
  637. entry->document_state->set_ever_populated(true);
  638. }
  639. // 14. Run completionSteps.
  640. completion_steps();
  641. });
  642. return {};
  643. }
  644. // To navigate a navigable navigable to a URL url using a Document sourceDocument,
  645. // with an optional POST resource, string, or null documentResource (default null),
  646. // an optional response-or-null response (default null), an optional boolean exceptionsEnabled (default false),
  647. // an optional history handling behavior historyHandling (default "push"),
  648. // an optional string cspNavigationType (default "other"),
  649. // and an optional referrer policy referrerPolicy (default the empty string):
  650. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  651. WebIDL::ExceptionOr<void> Navigable::navigate(
  652. AK::URL const& url,
  653. JS::NonnullGCPtr<DOM::Document> source_document,
  654. Variant<Empty, String, POSTResource> document_resource,
  655. JS::GCPtr<Fetch::Infrastructure::Response> response,
  656. bool exceptions_enabled,
  657. HistoryHandlingBehavior history_handling,
  658. CSPNavigationType csp_navigation_type,
  659. ReferrerPolicy::ReferrerPolicy referrer_policy)
  660. {
  661. // 1. Let sourceSnapshotParams be the result of snapshotting source snapshot params given sourceDocument.
  662. auto source_snapshot_params = SourceSnapshotParams {
  663. .has_transient_activation = false,
  664. .sandboxing_flags = source_document->active_sandboxing_flag_set(),
  665. .allows_downloading = true,
  666. .fetch_client = source_document->relevant_settings_object(),
  667. .source_policy_container = source_document->policy_container()
  668. };
  669. // 2. Let initiatorOriginSnapshot be sourceDocument's origin.
  670. auto initiator_origin_snapshot = source_document->origin();
  671. // FIXME: 3. If sourceDocument's node navigable is not allowed by sandboxing to navigate navigable given and sourceSnapshotParams, then:
  672. if constexpr (false) {
  673. // 1. If exceptionsEnabled is true, then throw a "SecurityError" DOMException.
  674. if (exceptions_enabled) {
  675. return WebIDL::SecurityError::create(*vm().current_realm(), "Source document's node navigable is not allowed to navigate"sv);
  676. }
  677. // 2 Return.
  678. return {};
  679. }
  680. // 4. Let navigationId be the result of generating a random UUID.
  681. String navigation_id = TRY_OR_THROW_OOM(vm(), Crypto::generate_random_uuid());
  682. // FIXME: 5. If the surrounding agent is equal to navigable's active document's relevant agent, then continue these steps.
  683. // Otherwise, queue a global task on the navigation and traversal task source given navigable's active window to continue these steps.
  684. // FIXME: 6. If navigable's active document's unload counter is greater than 0,
  685. // then invoke WebDriver BiDi navigation failed with a WebDriver BiDi navigation status whose id is navigationId,
  686. // status is "canceled", and url is url, and return.
  687. // 7. If any of the following are true:
  688. // - url equals navigable's active document's URL;
  689. // - url's scheme is "javascript"; or
  690. // - navigable's active document's is initial about:blank is true
  691. if (url.equals(active_document()->url())
  692. || url.scheme() == "javascript"sv
  693. || active_document()->is_initial_about_blank()) {
  694. // then set historyHandling to "replace".
  695. history_handling = HistoryHandlingBehavior::Replace;
  696. }
  697. // 8. If all of the following are true:
  698. // - documentResource is null;
  699. // - response is null;
  700. // - url equals navigable's active session history entry's URL with exclude fragments set to true; and
  701. // - url's fragment is non-null
  702. if (document_resource.has<Empty>()
  703. && !response
  704. && url.equals(active_session_history_entry()->url, AK::URL::ExcludeFragment::Yes)
  705. && url.fragment().has_value()) {
  706. // 1. Navigate to a fragment given navigable, url, historyHandling, and navigationId.
  707. TRY(navigate_to_a_fragment(url, history_handling, navigation_id));
  708. // 2. Return.
  709. return {};
  710. }
  711. // 9. If navigable's parent is non-null, then set navigable's is delaying load events to true.
  712. if (parent() != nullptr) {
  713. set_delaying_load_events(true);
  714. }
  715. // 10. Let targetBrowsingContext be navigable's active browsing context.
  716. [[maybe_unused]] auto target_browsing_context = active_browsing_context();
  717. // FIXME: 11. Let targetSnapshotParams be the result of snapshotting target snapshot params given navigable.
  718. // 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".
  719. // 13. If navigable's ongoing navigation is "traversal", then:
  720. if (ongoing_navigation().has<Traversal>()) {
  721. // 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.
  722. // 2. Return.
  723. return {};
  724. }
  725. // 14. Set navigable's ongoing navigation to navigationId.
  726. m_ongoing_navigation = navigation_id;
  727. // 15. If url's scheme is "javascript", then:
  728. if (url.scheme() == "javascript"sv) {
  729. // 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.
  730. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this, url, history_handling, initiator_origin_snapshot, csp_navigation_type] {
  731. (void)navigate_to_a_javascript_url(url, history_handling, initiator_origin_snapshot, csp_navigation_type);
  732. });
  733. // 2. Return.
  734. return {};
  735. }
  736. // 16. In parallel, run these steps:
  737. Platform::EventLoopPlugin::the().deferred_invoke([this, source_snapshot_params = move(source_snapshot_params), document_resource, url, navigation_id, referrer_policy, initiator_origin_snapshot, response, history_handling] {
  738. // FIXME: 1. Let unloadPromptCanceled be the result of checking if unloading is user-canceled for navigable's active document's inclusive descendant navigables.
  739. // FIXME: 2. If unloadPromptCanceled is true, or navigable's ongoing navigation is no longer navigationId, then:
  740. if (!ongoing_navigation().has<String>() || ongoing_navigation().get<String>() != navigation_id) {
  741. // 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.
  742. // 2. Abort these steps.
  743. return;
  744. }
  745. // 3. Queue a global task on the navigation and traversal task source given navigable's active window to abort navigable's active document.
  746. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this] {
  747. VERIFY(active_document());
  748. active_document()->abort();
  749. });
  750. // 4. Let documentState be a new document state with
  751. // request referrer policy: referrerPolicy
  752. // initiator origin: initiatorOriginSnapshot
  753. // resource: documentResource
  754. // navigable target name: navigable's target name
  755. JS::NonnullGCPtr<DocumentState> document_state = *heap().allocate_without_realm<DocumentState>();
  756. document_state->set_request_referrer_policy(referrer_policy);
  757. document_state->set_resource(document_resource);
  758. document_state->set_initiator_origin(initiator_origin_snapshot);
  759. document_state->set_navigable_target_name(target_name());
  760. // 5. If url is about:blank, then set documentState's origin to documentState's initiator origin.
  761. if (url == "about:blank"sv) {
  762. document_state->set_origin(document_state->initiator_origin());
  763. }
  764. // 6. Otherwise, if url is about:srcdoc, then set documentState's origin to navigable's parent's active document's origin.
  765. else if (url == "about:srcdoc"sv) {
  766. document_state->set_origin(parent()->active_document()->origin());
  767. }
  768. // 7. Let historyEntry be a new session history entry, with its URL set to url and its document state set to documentState.
  769. JS::NonnullGCPtr<SessionHistoryEntry> history_entry = *heap().allocate_without_realm<SessionHistoryEntry>();
  770. history_entry->url = url;
  771. history_entry->document_state = document_state;
  772. // 8. Let navigationParams be null.
  773. Optional<NavigationParams> navigation_params;
  774. // FIXME: 9. If response is non-null:
  775. if (response) {
  776. }
  777. // 10. Attempt to populate the history entry's document
  778. // for historyEntry, given navigable, "navigate", sourceSnapshotParams,
  779. // targetSnapshotParams, navigationId, navigationParams, cspNavigationType, with allowPOST
  780. // set to true and completionSteps set to the following step:
  781. populate_session_history_entry_document(history_entry, navigation_params, navigation_id, source_snapshot_params, true, [this, history_entry, history_handling, navigation_id] {
  782. traversable_navigable()->append_session_history_traversal_steps([this, history_entry, history_handling, navigation_id] {
  783. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-cross-document-navigation
  784. // 1. FIXME: Assert: this is running on navigable's traversable navigable's session history traversal queue.
  785. // 2. Set navigable's is delaying load events to false.
  786. set_delaying_load_events(false);
  787. // 3. If historyEntry's document is null, then return.
  788. if (!history_entry->document_state->document())
  789. return;
  790. // 4. FIXME: If all of the following are true:
  791. // - navigable's parent is null;
  792. // - historyEntry's document's browsing context is not an auxiliary browsing context whose opener browsing context is non-null; and
  793. // - historyEntry's document's origin is not navigable's active document's origin
  794. // then set historyEntry's document state's navigable target name to the empty string.
  795. // 5. Let entryToReplace be navigable's active session history entry if historyHandling is "replace", otherwise null.
  796. auto entry_to_replace = history_handling == HistoryHandlingBehavior::Replace ? active_session_history_entry() : nullptr;
  797. // 6. Let traversable be navigable's traversable navigable.
  798. auto traversable = traversable_navigable();
  799. // 7. Let targetStep be null.
  800. int target_step;
  801. // 8. Let targetEntries be the result of getting session history entries for navigable.
  802. auto& target_entries = get_session_history_entries();
  803. // 9. If entryToReplace is null, then:
  804. if (entry_to_replace == nullptr) {
  805. // FIXME: 1. Clear the forward session history of traversable.
  806. traversable->clear_the_forward_session_history();
  807. // 2. Set targetStep to traversable's current session history step + 1.
  808. target_step = traversable->current_session_history_step() + 1;
  809. // 3. Set historyEntry's step to targetStep.
  810. history_entry->step = target_step;
  811. // 4. Append historyEntry to targetEntries.
  812. target_entries.append(move(history_entry));
  813. } else {
  814. // 1. Replace entryToReplace with historyEntry in targetEntries.
  815. *(target_entries.find(*entry_to_replace)) = history_entry;
  816. // 2. Set historyEntry's step to entryToReplace's step.
  817. history_entry->step = entry_to_replace->step;
  818. // 3. Set targetStep to traversable's current session history step.
  819. target_step = traversable->current_session_history_step();
  820. }
  821. // 10. Apply the history step targetStep to traversable.
  822. traversable->apply_the_history_step(target_step);
  823. });
  824. }).release_value_but_fixme_should_propagate_errors();
  825. });
  826. return {};
  827. }
  828. WebIDL::ExceptionOr<void> Navigable::navigate_to_a_fragment(AK::URL const& url, HistoryHandlingBehavior history_handling, String navigation_id)
  829. {
  830. (void)navigation_id;
  831. // FIXME: 1. Let navigation be navigable's active window's navigation API.
  832. // FIXME: 2. Let destinationNavigationAPIState be navigable's active session history entry's navigation API state.
  833. // FIXME: 3. If navigationAPIState is not null, then set destinationNavigationAPIState to navigationAPIState.
  834. // FIXME: 4. Let continue be the result of firing a push/replace/reload navigate event at navigation with navigationType set to historyHandling, isSameDocument set to true,
  835. // userInvolvement set to userInvolvement, and destinationURL set to url, and navigationAPIState set to destinationNavigationAPIState.
  836. // FIXME: 5. If continue is false, then return.
  837. // 6. Let historyEntry be a new session history entry, with
  838. // URL: url
  839. // document state: navigable's active session history entry's document state
  840. // navigation API state: destinationNavigationAPIState
  841. // scroll restoration mode: navigable's active session history entry's scroll restoration mode
  842. JS::NonnullGCPtr<SessionHistoryEntry> history_entry = heap().allocate_without_realm<SessionHistoryEntry>();
  843. history_entry->url = url;
  844. history_entry->document_state = active_session_history_entry()->document_state;
  845. history_entry->scroll_restoration_mode = active_session_history_entry()->scroll_restoration_mode;
  846. // 7. Let entryToReplace be navigable's active session history entry if historyHandling is "replace", otherwise null.
  847. auto entry_to_replace = history_handling == HistoryHandlingBehavior::Replace ? active_session_history_entry() : nullptr;
  848. // FIXME: 8. Let history be navigable's active document's history object.
  849. // FIXME: 9. Let scriptHistoryIndex be history's index.
  850. // FIXME: 10. Let scriptHistoryIndex be history's index.
  851. // 11. If historyHandling is "push", then:
  852. if (history_handling == HistoryHandlingBehavior::Push) {
  853. // FIXME: 1. Set history's state to null.
  854. // FIXME: 2. Increment scriptHistoryIndex.
  855. // FIXME: 3. Set scriptHistoryLength to scriptHistoryIndex + 1.
  856. }
  857. // 12. Set navigable's active session history entry to historyEntry.
  858. m_active_session_history_entry = history_entry;
  859. // FIXME: 13. Update document for history step application given navigable's active document, historyEntry, true, scriptHistoryIndex, and scriptHistoryLength.
  860. // FIXME: 14. Update the navigation API entries for a same-document navigation given navigation, historyEntry, and historyHandling.
  861. // 15. Scroll to the fragment given navigable's active document.
  862. // FIXME: Specification doesn't say when document url needs to update during fragment navigation
  863. active_document()->set_url(url);
  864. active_document()->scroll_to_the_fragment();
  865. // 16. Let traversable be navigable's traversable navigable.
  866. auto traversable = traversable_navigable();
  867. // 17. Append the following session history synchronous navigation steps involving navigable to traversable:
  868. traversable->append_session_history_traversal_steps([&] {
  869. // 1. Finalize a same-document navigation given traversable, navigable, historyEntry, and entryToReplace.
  870. finalize_a_same_document_navigation(*traversable, *this, history_entry, entry_to_replace);
  871. // FIXME: 2. Invoke WebDriver BiDi fragment navigated with navigable's active browsing context and a new WebDriver BiDi
  872. // navigation status whose id is navigationId, url is url, and status is "complete".
  873. });
  874. return {};
  875. }
  876. WebIDL::ExceptionOr<void> Navigable::navigate_to_a_javascript_url(AK::URL const&, HistoryHandlingBehavior, Origin const& initiator_origin, CSPNavigationType csp_navigation_type)
  877. {
  878. (void)initiator_origin;
  879. (void)csp_navigation_type;
  880. TODO();
  881. }
  882. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#reload
  883. void Navigable::reload()
  884. {
  885. // 1. Set navigable's active session history entry's document state's reload pending to true.
  886. active_session_history_entry()->document_state->set_reload_pending(true);
  887. // 2. Let traversable be navigable's traversable navigable.
  888. auto traversable = traversable_navigable();
  889. // 3. Append the following session history traversal steps to traversable:
  890. traversable->append_session_history_traversal_steps([traversable] {
  891. // 1. Apply pending history changes to traversable with true.
  892. traversable->apply_pending_history_changes();
  893. });
  894. }
  895. }