Navigable.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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/HistoryHandlingBehavior.h>
  17. #include <LibWeb/HTML/Navigable.h>
  18. #include <LibWeb/HTML/Navigation.h>
  19. #include <LibWeb/HTML/NavigationParams.h>
  20. #include <LibWeb/HTML/POSTResource.h>
  21. #include <LibWeb/HTML/SandboxingFlagSet.h>
  22. #include <LibWeb/HTML/Scripting/ClassicScript.h>
  23. #include <LibWeb/HTML/SessionHistoryEntry.h>
  24. #include <LibWeb/HTML/StructuredSerialize.h>
  25. #include <LibWeb/HTML/TraversableNavigable.h>
  26. #include <LibWeb/Infra/Strings.h>
  27. #include <LibWeb/Platform/EventLoopPlugin.h>
  28. #include <LibWeb/XHR/FormData.h>
  29. namespace Web::HTML {
  30. class ResponseHolder : public JS::Cell {
  31. JS_CELL(ResponseHolder, JS::Cell);
  32. public:
  33. [[nodiscard]] static JS::NonnullGCPtr<ResponseHolder> create(JS::VM& vm)
  34. {
  35. return vm.heap().allocate_without_realm<ResponseHolder>();
  36. }
  37. [[nodiscard]] JS::GCPtr<Fetch::Infrastructure::Response> response() const { return m_response; }
  38. void set_response(JS::GCPtr<Fetch::Infrastructure::Response> response) { m_response = response; }
  39. virtual void visit_edges(Cell::Visitor& visitor) override
  40. {
  41. visitor.visit(m_response);
  42. }
  43. private:
  44. JS::GCPtr<Fetch::Infrastructure::Response> m_response;
  45. };
  46. static HashTable<Navigable*>& all_navigables()
  47. {
  48. static HashTable<Navigable*> set;
  49. return set;
  50. }
  51. // https://html.spec.whatwg.org/multipage/document-sequences.html#child-navigable
  52. Vector<JS::Handle<Navigable>> Navigable::child_navigables() const
  53. {
  54. Vector<JS::Handle<Navigable>> results;
  55. for (auto& entry : all_navigables()) {
  56. if (entry->parent() == this)
  57. results.append(entry);
  58. }
  59. return results;
  60. }
  61. Navigable::Navigable()
  62. {
  63. all_navigables().set(this);
  64. }
  65. Navigable::~Navigable()
  66. {
  67. all_navigables().remove(this);
  68. }
  69. void Navigable::visit_edges(Cell::Visitor& visitor)
  70. {
  71. Base::visit_edges(visitor);
  72. visitor.visit(m_parent);
  73. visitor.visit(m_current_session_history_entry);
  74. visitor.visit(m_active_session_history_entry);
  75. visitor.visit(m_container);
  76. }
  77. JS::GCPtr<Navigable> Navigable::navigable_with_active_document(JS::NonnullGCPtr<DOM::Document> document)
  78. {
  79. for (auto* navigable : all_navigables()) {
  80. if (navigable->active_document() == document)
  81. return navigable;
  82. }
  83. return nullptr;
  84. }
  85. // https://html.spec.whatwg.org/multipage/document-sequences.html#initialize-the-navigable
  86. ErrorOr<void> Navigable::initialize_navigable(JS::NonnullGCPtr<DocumentState> document_state, JS::GCPtr<Navigable> parent)
  87. {
  88. static int next_id = 0;
  89. m_id = TRY(String::number(next_id++));
  90. // 1. Let entry be a new session history entry, with
  91. JS::NonnullGCPtr<SessionHistoryEntry> entry = *heap().allocate_without_realm<SessionHistoryEntry>();
  92. // URL: document's URL
  93. entry->url = document_state->document()->url();
  94. // document state: documentState
  95. entry->document_state = document_state;
  96. // 2. Set navigable's current session history entry to entry.
  97. m_current_session_history_entry = entry;
  98. // 3. Set navigable's active session history entry to entry.
  99. m_active_session_history_entry = entry;
  100. // 4. Set navigable's parent to parent.
  101. m_parent = parent;
  102. return {};
  103. }
  104. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-the-target-history-entry
  105. JS::GCPtr<SessionHistoryEntry> Navigable::get_the_target_history_entry(int target_step) const
  106. {
  107. // 1. Let entries be the result of getting session history entries for navigable.
  108. auto& entries = get_session_history_entries();
  109. // 2. Return the item in entries that has the greatest step less than or equal to step.
  110. JS::GCPtr<SessionHistoryEntry> result = nullptr;
  111. for (auto& entry : entries) {
  112. auto entry_step = entry->step.get<int>();
  113. if (entry_step <= target_step) {
  114. if (!result || result->step.get<int>() < entry_step) {
  115. result = entry;
  116. }
  117. }
  118. }
  119. return result;
  120. }
  121. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#activate-history-entry
  122. void Navigable::activate_history_entry(JS::GCPtr<SessionHistoryEntry> entry)
  123. {
  124. // FIXME: 1. Save persisted state to the navigable's active session history entry.
  125. // 2. Let newDocument be entry's document.
  126. JS::GCPtr<DOM::Document> new_document = entry->document_state->document().ptr();
  127. // 3. Assert: newDocument's is initial about:blank is false, i.e., we never traverse
  128. // back to the initial about:blank Document because it always gets replaced when we
  129. // navigate away from it.
  130. VERIFY(!new_document->is_initial_about_blank());
  131. // 4. Set navigable's active session history entry to entry.
  132. m_active_session_history_entry = entry;
  133. // 5. Make active newDocument.
  134. new_document->make_active();
  135. // Not in the spec:
  136. if (is<TraversableNavigable>(*this) && parent() == nullptr) {
  137. if (auto* page = active_browsing_context()->page()) {
  138. page->client().page_did_start_loading(entry->url, false);
  139. }
  140. }
  141. }
  142. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-document
  143. JS::GCPtr<DOM::Document> Navigable::active_document()
  144. {
  145. // A navigable's active document is its active session history entry's document.
  146. return m_active_session_history_entry->document_state->document();
  147. }
  148. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-bc
  149. JS::GCPtr<BrowsingContext> Navigable::active_browsing_context()
  150. {
  151. // A navigable's active browsing context is its active document's browsing context.
  152. // If this navigable is a traversable navigable, then its active browsing context will be a top-level browsing context.
  153. if (auto document = active_document())
  154. return document->browsing_context();
  155. return nullptr;
  156. }
  157. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-wp
  158. JS::GCPtr<HTML::WindowProxy> Navigable::active_window_proxy()
  159. {
  160. // A navigable's active WindowProxy is its active browsing context's associated WindowProxy.
  161. if (auto browsing_context = active_browsing_context())
  162. return browsing_context->window_proxy();
  163. return nullptr;
  164. }
  165. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-window
  166. JS::GCPtr<HTML::Window> Navigable::active_window()
  167. {
  168. // A navigable's active window is its active WindowProxy's [[Window]].
  169. if (auto window_proxy = active_window_proxy())
  170. return window_proxy->window();
  171. return nullptr;
  172. }
  173. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-target
  174. String Navigable::target_name() const
  175. {
  176. // A navigable's target name is its active session history entry's document state's navigable target name.
  177. return active_session_history_entry()->document_state->navigable_target_name();
  178. }
  179. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-container
  180. JS::GCPtr<NavigableContainer> Navigable::container() const
  181. {
  182. // The container of a navigable navigable is the navigable container whose nested navigable is navigable, or null if there is no such element.
  183. return NavigableContainer::navigable_container_with_content_navigable(const_cast<Navigable&>(*this));
  184. }
  185. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-container-document
  186. JS::GCPtr<DOM::Document> Navigable::container_document() const
  187. {
  188. auto container = this->container();
  189. // 1. If navigable's container is null, then return null.
  190. if (!container)
  191. return nullptr;
  192. // 2. Return navigable's container's node document.
  193. return container->document();
  194. }
  195. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-traversable
  196. JS::GCPtr<TraversableNavigable> Navigable::traversable_navigable() const
  197. {
  198. // 1. Let navigable be inputNavigable.
  199. auto navigable = const_cast<Navigable*>(this);
  200. // 2. While navigable is not a traversable navigable, set navigable to navigable's parent.
  201. while (navigable && !is<TraversableNavigable>(*navigable))
  202. navigable = navigable->parent();
  203. // 3. Return navigable.
  204. return static_cast<TraversableNavigable*>(navigable);
  205. }
  206. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-top
  207. JS::GCPtr<TraversableNavigable> Navigable::top_level_traversable()
  208. {
  209. // 1. Let navigable be inputNavigable.
  210. auto navigable = this;
  211. // 2. While navigable's parent is not null, set navigable to navigable's parent.
  212. while (navigable->parent())
  213. navigable = navigable->parent();
  214. // 3. Return navigable.
  215. return verify_cast<TraversableNavigable>(navigable);
  216. }
  217. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#set-the-ongoing-navigation
  218. void Navigable::set_ongoing_navigation(Variant<Empty, Traversal, String> ongoing_navigation)
  219. {
  220. // 1. If navigable's ongoing navigation is equal to newValue, then return.
  221. if (m_ongoing_navigation == ongoing_navigation)
  222. return;
  223. // FIXME: 2. Inform the navigation API about aborting navigation given navigable.
  224. // 3. Set navigable's ongoing navigation to newValue.
  225. m_ongoing_navigation = ongoing_navigation;
  226. }
  227. Navigable::ChosenNavigable Navigable::choose_a_navigable(StringView name, TokenizedFeature::NoOpener, ActivateTab)
  228. {
  229. // 1. Let chosen be null.
  230. JS::GCPtr<Navigable> chosen = nullptr;
  231. // 2. Let windowType be "existing or none".
  232. auto window_type = WindowType::ExistingOrNone;
  233. // 3. Let sandboxingFlagSet be current's active document's active sandboxing flag set.
  234. [[maybe_unused]] auto sandboxing_flag_set = active_document()->active_sandboxing_flag_set();
  235. // 4. If name is the empty string or an ASCII case-insensitive match for "_self", then set chosen to currentNavigable.
  236. if (name.is_empty() || Infra::is_ascii_case_insensitive_match(name, "_self"sv)) {
  237. chosen = this;
  238. }
  239. // 5. Otherwise, if name is an ASCII case-insensitive match for "_parent",
  240. // set chosen to currentNavigable's parent, if any, and currentNavigable otherwise.
  241. else if (Infra::is_ascii_case_insensitive_match(name, "_parent"sv)) {
  242. if (auto parent = this->parent())
  243. chosen = parent;
  244. else
  245. chosen = this;
  246. }
  247. // 6. Otherwise, if name is an ASCII case-insensitive match for "_top",
  248. // set chosen to currentNavigable's traversable navigable.
  249. else if (Infra::is_ascii_case_insensitive_match(name, "_top"sv)) {
  250. chosen = traversable_navigable();
  251. }
  252. // 7. Otherwise, if name is not an ASCII case-insensitive match for "_blank",
  253. // there exists a navigable whose target name is the same as name, currentNavigable's
  254. // active browsing context is familiar with that navigable's active browsing context,
  255. // and the user agent determines that the two browsing contexts are related enough that
  256. // it is ok if they reach each other, set chosen to that navigable. If there are multiple
  257. // matching navigables, the user agent should pick one in some arbitrary consistent manner,
  258. // such as the most recently opened, most recently focused, or more closely related, and set
  259. // chosen to it.
  260. else if (!Infra::is_ascii_case_insensitive_match(name, "_blank"sv)) {
  261. TODO();
  262. }
  263. // Otherwise, a new top-level traversable is being requested, and what happens depends on the
  264. // user agent's configuration and abilities — it is determined by the rules given for the first
  265. // applicable option from the following list:
  266. else {
  267. TODO();
  268. }
  269. return { chosen.ptr(), window_type };
  270. }
  271. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-session-history-entries
  272. Vector<JS::NonnullGCPtr<SessionHistoryEntry>>& Navigable::get_session_history_entries() const
  273. {
  274. // 1. Let traversable be navigable's traversable navigable.
  275. auto traversable = traversable_navigable();
  276. // FIXME 2. Assert: this is running within traversable's session history traversal queue.
  277. // 3. If navigable is traversable, return traversable's session history entries.
  278. if (this == traversable)
  279. return traversable->session_history_entries();
  280. // 4. Let docStates be an empty ordered set of document states.
  281. Vector<JS::GCPtr<DocumentState>> doc_states;
  282. // 5. For each entry of traversable's session history entries, append entry's document state to docStates.
  283. for (auto& entry : traversable->session_history_entries())
  284. doc_states.append(entry->document_state);
  285. // 6. For each docState of docStates:
  286. while (!doc_states.is_empty()) {
  287. auto doc_state = doc_states.take_first();
  288. // 1. For each nestedHistory of docState's nested histories:
  289. for (auto& nested_history : doc_state->nested_histories()) {
  290. // 1. If nestedHistory's id equals navigable's id, return nestedHistory's entries.
  291. if (nested_history.id == id())
  292. return nested_history.entries;
  293. // 2. For each entry of nestedHistory's entries, append entry's document state to docStates.
  294. for (auto& entry : nested_history.entries)
  295. doc_states.append(entry->document_state);
  296. }
  297. }
  298. VERIFY_NOT_REACHED();
  299. }
  300. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#create-navigation-params-from-a-srcdoc-resource
  301. 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)
  302. {
  303. auto& vm = navigable->vm();
  304. auto& realm = navigable->active_window()->realm();
  305. // 1. Let documentResource be entry's document state's resource.
  306. auto document_resource = entry->document_state->resource();
  307. VERIFY(document_resource.has<String>());
  308. // 2. Let response be a new response with
  309. // URL: about:srcdoc
  310. // header list: (`Content-Type`, `text/html`)
  311. // body: the UTF-8 encoding of documentResource, as a body
  312. auto response = Fetch::Infrastructure::Response::create(vm);
  313. response->url_list().append(AK::URL("about:srcdoc"));
  314. auto header = TRY_OR_THROW_OOM(vm, Fetch::Infrastructure::Header::from_string_pair("Content-Type"sv, "text/html"sv));
  315. TRY_OR_THROW_OOM(vm, response->header_list()->append(move(header)));
  316. response->set_body(TRY(Fetch::Infrastructure::byte_sequence_as_body(realm, document_resource.get<String>().bytes())));
  317. // 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.
  318. // 4. Let coop be a new cross-origin opener policy.
  319. CrossOriginOpenerPolicy coop;
  320. // 5. Let coopEnforcementResult be a new cross-origin opener policy enforcement result with
  321. // url: response's URL
  322. // FIXME: origin: responseOrigin
  323. // cross-origin opener policy: coop
  324. CrossOriginOpenerPolicyEnforcementResult coop_enforcement_result {
  325. .url = *response->url(),
  326. .origin = Origin {},
  327. .cross_origin_opener_policy = coop
  328. };
  329. // 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.
  330. // 7. Return a new navigation params, with
  331. // id: navigationId
  332. // request: null
  333. // response: response
  334. // FIXME: origin: responseOrigin
  335. // FIXME: policy container: policyContainer
  336. // FIXME: final sandboxing flag set: targetSnapshotParams's sandboxing flags
  337. // cross-origin opener policy: coop
  338. // COOP enforcement result: coopEnforcementResult
  339. // reserved environment: null
  340. // navigable: navigable
  341. // FIXME: navigation timing type: navTimingType
  342. // fetch controller: null
  343. // commit early hints: null
  344. HTML::NavigationParams navigation_params {
  345. .id = navigation_id,
  346. .request = {},
  347. .response = *response,
  348. .origin = Origin {},
  349. .policy_container = PolicyContainer {},
  350. .final_sandboxing_flag_set = SandboxingFlagSet {},
  351. .cross_origin_opener_policy = move(coop),
  352. .coop_enforcement_result = move(coop_enforcement_result),
  353. .reserved_environment = {},
  354. .browsing_context = navigable->active_browsing_context(),
  355. .navigable = navigable,
  356. };
  357. return { navigation_params };
  358. }
  359. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#create-navigation-params-by-fetching
  360. 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)
  361. {
  362. auto& vm = navigable->vm();
  363. auto& realm = navigable->active_window()->realm();
  364. // FIXME: 1. Assert: this is running in parallel.
  365. // 2. Let documentResource be entry's document state's resource.
  366. auto document_resource = entry->document_state->resource();
  367. // 3. Let request be a new request, with
  368. // url: entry's URL
  369. // client: sourceSnapshotParams's fetch client
  370. // destination: "document"
  371. // credentials mode: "include"
  372. // use-URL-credentials flag: set
  373. // redirect mode: "manual"
  374. // replaces client id: navigable's active document's relevant settings object's id
  375. // mode: "navigate"
  376. // referrer: entry's document state's request referrer
  377. // FIXME: referrer policy: entry's document state's request referrer policy
  378. auto request = Fetch::Infrastructure::Request::create(vm);
  379. request->set_url(entry->url);
  380. request->set_client(source_snapshot_params.fetch_client);
  381. request->set_destination(Fetch::Infrastructure::Request::Destination::Document);
  382. request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include);
  383. request->set_use_url_credentials(true);
  384. request->set_redirect_mode(Fetch::Infrastructure::Request::RedirectMode::Manual);
  385. auto replaces_client_id = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(navigable->active_document()->relevant_settings_object().id));
  386. request->set_replaces_client_id(replaces_client_id);
  387. request->set_mode(Fetch::Infrastructure::Request::Mode::Navigate);
  388. request->set_referrer(entry->document_state->request_referrer());
  389. // 4. If documentResource is a POST resource, then:
  390. if (document_resource.has<POSTResource>()) {
  391. // 1. Set request's method to `POST`.
  392. request->set_method(TRY_OR_THROW_OOM(vm, ByteBuffer::copy("post"sv.bytes())));
  393. // 2. Set request's body to documentResource's request body.
  394. request->set_body(document_resource.get<POSTResource>().request_body.value());
  395. // 3. Set `Content-Type` to documentResource's request content-type in request's header list.
  396. auto request_content_type = document_resource.get<POSTResource>().request_content_type;
  397. auto request_content_type_string = [request_content_type]() {
  398. switch (request_content_type) {
  399. case POSTResource::RequestContentType::ApplicationXWWWFormUrlencoded:
  400. return "application/x-www-form-urlencoded"sv;
  401. case POSTResource::RequestContentType::MultipartFormData:
  402. return "multipart/form-data"sv;
  403. case POSTResource::RequestContentType::TextPlain:
  404. return "text/plain"sv;
  405. default:
  406. VERIFY_NOT_REACHED();
  407. }
  408. }();
  409. auto header = TRY_OR_THROW_OOM(vm, Fetch::Infrastructure::Header::from_string_pair("Content-Type"sv, request_content_type_string));
  410. TRY_OR_THROW_OOM(vm, request->header_list()->append(move(header)));
  411. }
  412. // 5. If entry's document state's reload pending is true, then set request's reload-navigation flag.
  413. if (entry->document_state->reload_pending())
  414. request->set_reload_navigation(true);
  415. // 6. Otherwise, if entry's document state's ever populated is true, then set request's history-navigation flag.
  416. if (entry->document_state->ever_populated())
  417. request->set_history_navigation(true);
  418. // 9. Let response be null.
  419. // NOTE: We use a heap-allocated cell to hold the response pointer because the processResponse callback below
  420. // might use it after this stack is freed.
  421. auto response_holder = ResponseHolder::create(vm);
  422. // 10. Let responseOrigin be null.
  423. Optional<HTML::Origin> response_origin;
  424. // 11. Let fetchController be null.
  425. JS::GCPtr<Fetch::Infrastructure::FetchController> fetch_controller = nullptr;
  426. // 13. Let finalSandboxFlags be an empty sandboxing flag set.
  427. SandboxingFlagSet final_sandbox_flags = {};
  428. // 16. Let locationURL be null.
  429. ErrorOr<Optional<AK::URL>> location_url { OptionalNone {} };
  430. // 17. Let currentURL be request's current URL.
  431. AK::URL current_url = request->current_url();
  432. // FIXME: 18. Let commitEarlyHints be null.
  433. // 19. While true:
  434. while (true) {
  435. // 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:
  436. // FIXME: 2. If request's reserved client is null, then:
  437. // 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]
  438. // 4. Set response to null.
  439. response_holder->set_response(nullptr);
  440. // 5. If fetchController is null, then set fetchController to the result of fetching request,
  441. // with processEarlyHintsResponse set to processEarlyHintsResponseas defined below, processResponse
  442. // set to processResponse as defined below, and useParallelQueue set to true.
  443. if (!fetch_controller) {
  444. // FIXME: Let processEarlyHintsResponse be the following algorithm given a response earlyResponse:
  445. // Let processResponse be the following algorithm given a response fetchedResponse:
  446. auto process_response = [response_holder](JS::NonnullGCPtr<Fetch::Infrastructure::Response> fetch_response) {
  447. // 1. Set response to fetchedResponse.
  448. response_holder->set_response(fetch_response);
  449. };
  450. fetch_controller = TRY(Fetch::Fetching::fetch(
  451. realm,
  452. request,
  453. Fetch::Infrastructure::FetchAlgorithms::create(vm,
  454. {
  455. .process_request_body_chunk_length = {},
  456. .process_request_end_of_body = {},
  457. .process_early_hints_response = {},
  458. .process_response = move(process_response),
  459. .process_response_end_of_body = {},
  460. .process_response_consume_body = {},
  461. }),
  462. Fetch::Fetching::UseParallelQueue::Yes));
  463. }
  464. // 6. Otherwise, process the next manual redirect for fetchController.
  465. else {
  466. fetch_controller->process_next_manual_redirect();
  467. }
  468. // 7. Wait until either response is non-null, or navigable's ongoing navigation changes to no longer equal navigationId.
  469. Platform::EventLoopPlugin::the().spin_until([&]() {
  470. if (response_holder->response() != nullptr)
  471. return true;
  472. if (navigation_id.has_value() && (!navigable->ongoing_navigation().has<String>() || navigable->ongoing_navigation().get<String>() != *navigation_id))
  473. return true;
  474. return false;
  475. });
  476. // If the latter condition occurs, then abort fetchController, and return. Otherwise, proceed onward.
  477. if (navigation_id.has_value() && (!navigable->ongoing_navigation().has<String>() || navigable->ongoing_navigation().get<String>() != *navigation_id)) {
  478. fetch_controller->abort(realm, {});
  479. return OptionalNone {};
  480. }
  481. // 8. If request's body is null, then set entry's document state's resource to null.
  482. if (!request->body().has<Empty>()) {
  483. entry->document_state->set_resource(Empty {});
  484. }
  485. // 11. Set responseOrigin to the result of determining the origin given response's URL, finalSandboxFlags,
  486. // entry's document state's initiator origin, and null.
  487. response_origin = determine_the_origin(*response_holder->response()->url(), final_sandbox_flags, entry->document_state->initiator_origin(), {});
  488. // 14. Set locationURL to response's location URL given currentURL's fragment.
  489. auto location_url = response_holder->response()->location_url(current_url.fragment());
  490. VERIFY(!location_url.is_error());
  491. // 15. If locationURL is failure or null, then break.
  492. if (location_url.is_error() || !location_url.value().has_value()) {
  493. break;
  494. }
  495. // 16. Assert: locationURL is a URL.
  496. VERIFY(location_url.value()->is_valid());
  497. // FIXME: 17. Set entry's serialized state to StructuredSerializeForStorage(null).
  498. // 18. Let oldDocState be entry's document state.
  499. auto old_doc_state = entry->document_state;
  500. // 19. Set entry's document state to a new document state, with
  501. // history policy container: a clone of the oldDocState's history policy container if it is non-null; null otherwise
  502. // request referrer: oldDocState's request referrer
  503. // request referrer policy: oldDocState's request referrer policy
  504. // origin: oldDocState's origin
  505. // resource: oldDocState's resource
  506. // ever populated: oldDocState's ever populated
  507. // navigable target name: oldDocState's navigable target name
  508. entry->document_state = navigable->heap().allocate_without_realm<DocumentState>();
  509. entry->document_state->set_history_policy_container(old_doc_state->history_policy_container());
  510. entry->document_state->set_request_referrer(old_doc_state->request_referrer());
  511. entry->document_state->set_request_referrer_policy(old_doc_state->request_referrer_policy());
  512. entry->document_state->set_origin(old_doc_state->origin());
  513. entry->document_state->set_resource(old_doc_state->resource());
  514. entry->document_state->set_ever_populated(old_doc_state->ever_populated());
  515. entry->document_state->set_navigable_target_name(old_doc_state->navigable_target_name());
  516. // 20. If locationURL's scheme is not an HTTP(S) scheme, then:
  517. if (!Fetch::Infrastructure::is_http_or_https_scheme(location_url.value()->scheme())) {
  518. // 1. Set entry's document state's resource to null.
  519. entry->document_state->set_resource(Empty {});
  520. // 2. Break.
  521. break;
  522. }
  523. // 21. Set currentURL to locationURL.
  524. current_url = location_url.value().value();
  525. // 22. Set entry's URL to currentURL.
  526. entry->url = current_url;
  527. }
  528. // FIXME: 20. If locationURL is a URL whose scheme is not a fetch scheme, then return a new non-fetch scheme navigation params, with
  529. // initiator origin request's current URL's origin
  530. if (!location_url.is_error() && location_url.value().has_value() && !Fetch::Infrastructure::is_fetch_scheme(location_url.value().value().scheme())) {
  531. TODO();
  532. }
  533. // 21. If any of the following are true:
  534. // - response is a network error;
  535. // - locationURL is failure; or
  536. // - locationURL is a URL whose scheme is a fetch scheme
  537. // then return null.
  538. if (response_holder->response()->is_network_error() || location_url.is_error() || (location_url.value().has_value() && Fetch::Infrastructure::is_fetch_scheme(location_url.value().value().scheme()))) {
  539. return OptionalNone {};
  540. }
  541. // 22. Assert: locationURL is null and response is not a network error.
  542. VERIFY(!location_url.value().has_value());
  543. VERIFY(!response_holder->response()->is_network_error());
  544. // FIXME: 23. Let resultPolicyContainer be the result of determining navigation params policy container given response's
  545. // URL, entry's document state's history policy container, sourceSnapshotParams's source policy container,
  546. // null, and responsePolicyContainer.
  547. // 25. Return a new navigation params, with
  548. // id: navigationId
  549. // request: request
  550. // response: response
  551. // origin: responseOrigin
  552. // FIXME: policy container: resultPolicyContainer
  553. // FIXME: final sandboxing flag set: finalSandboxFlags
  554. // FIXME: cross-origin opener policy: responseCOOP
  555. // FIXME: COOP enforcement result: coopEnforcementResult
  556. // FIXME: reserved environment: request's reserved client
  557. // navigable: navigable
  558. // FIXME: navigation timing type: navTimingType
  559. // fetch controller: fetchController
  560. // FIXME: commit early hints: commitEarlyHints
  561. HTML::NavigationParams navigation_params {
  562. .id = navigation_id,
  563. .request = request,
  564. .response = *response_holder->response(),
  565. .origin = *response_origin,
  566. .policy_container = PolicyContainer {},
  567. .final_sandboxing_flag_set = SandboxingFlagSet {},
  568. .cross_origin_opener_policy = CrossOriginOpenerPolicy {},
  569. .coop_enforcement_result = CrossOriginOpenerPolicyEnforcementResult {},
  570. .reserved_environment = {},
  571. .browsing_context = navigable->active_browsing_context(),
  572. .navigable = navigable,
  573. .fetch_controller = fetch_controller,
  574. };
  575. return { navigation_params };
  576. }
  577. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#attempt-to-populate-the-history-entry's-document
  578. 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)
  579. {
  580. // FIXME: 1. Assert: this is running in parallel.
  581. // 2. Assert: if navigationParams is non-null, then navigationParams's response is non-null.
  582. if (navigation_params.has_value())
  583. VERIFY(navigation_params->response);
  584. // 3. Let currentBrowsingContext be navigable's active browsing context.
  585. [[maybe_unused]] auto current_browsing_context = active_browsing_context();
  586. // 4. Let documentResource be entry's document state's resource.
  587. auto document_resource = entry->document_state->resource();
  588. // 5. If navigationParams is null, then:
  589. if (!navigation_params.has_value()) {
  590. // 1. If documentResource is a string, then set navigationParams to the result
  591. // of creating navigation params from a srcdoc resource given entry, navigable,
  592. // targetSnapshotParams, navigationId, and navTimingType.
  593. if (document_resource.has<String>()) {
  594. navigation_params = create_navigation_params_from_a_srcdoc_resource(entry, this, source_snapshot_params, navigation_id).release_value_but_fixme_should_propagate_errors();
  595. }
  596. // 2. Otherwise, if both of the following are true:
  597. // - entry's URL's scheme is a fetch scheme; and
  598. // - documentResource is null, or allowPOST is true and documentResource's request body is not failure (FIXME: check if request body is not failure)
  599. else if (Fetch::Infrastructure::is_fetch_scheme(entry->url.scheme()) && (document_resource.has<Empty>() || allow_POST)) {
  600. navigation_params = create_navigation_params_by_fetching(entry, this, source_snapshot_params, navigation_id).release_value_but_fixme_should_propagate_errors();
  601. }
  602. // 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
  603. // initiator origin: entry's document state's initiator origin
  604. else {
  605. TODO();
  606. }
  607. }
  608. // 6. Queue a global task on the navigation and traversal task source, given navigable's active window, to run these steps:
  609. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this, entry, navigation_params, navigation_id, completion_steps = move(completion_steps)] {
  610. // 1. If navigable's ongoing navigation no longer equals navigationId, then run completionSteps and return.
  611. if (navigation_id.has_value() && (!ongoing_navigation().has<String>() || ongoing_navigation().get<String>() != *navigation_id)) {
  612. completion_steps();
  613. return;
  614. }
  615. // 2. Let failure be false.
  616. auto failure = false;
  617. // 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
  618. // scheme document given entry's URL, navigable, targetSnapshotParams's sandboxing flags, navigationId, navTimingType, sourceSnapshotParams's has transient
  619. // activation, and navigationParams's initiator origin.
  620. // 4. Otherwise, if navigationParams is null, then set failure to true.
  621. if (!navigation_params.has_value()) {
  622. failure = true;
  623. }
  624. // 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,
  625. // navigationParams's response, navigationParams's policy container's CSP list, cspNavigationType, and navigable is "Blocked", then set failure to true.
  626. // 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
  627. // navigationParams's response, navigable, and navigationParams's policy container's embedder policy is false, then set failure to true.
  628. // 8. If failure is true, then:
  629. if (failure) {
  630. // 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.
  631. // The inline content should indicate to the user the sort of error that occurred.
  632. // FIXME: Use SourceGenerator to produce error page from file:///res/html/error.html
  633. // and display actual error from fetch response.
  634. auto error_html = String::formatted("<h1>Failed to load {}</h1>"sv, entry->url).release_value_but_fixme_should_propagate_errors();
  635. entry->document_state->set_document(create_document_for_inline_content(this, navigation_id, error_html));
  636. // 2. Set entry's document state's document's salvageable to false.
  637. entry->document_state->document()->set_salvageable(false);
  638. // FIXME: 3. If navigationParams is not null, then:
  639. if (navigation_params.has_value()) {
  640. TODO();
  641. }
  642. }
  643. // FIXME: 9. Otherwise, if navigationParams's response's status is 204 or 205, then:
  644. else if (navigation_params->response->status() == 204 || navigation_params->response->status() == 205) {
  645. // 1. Run completionSteps.
  646. completion_steps();
  647. // 2. Return.
  648. return;
  649. }
  650. // FIXME: 10. Otherwise, if navigationParams's response has a `Content-Disposition`
  651. // header specifying the attachment disposition type, then:
  652. // 11. Otherwise:
  653. else {
  654. // 1. Let document be the result of loading a document given navigationParams, sourceSnapshotParams,
  655. // and entry's document state's initiator origin.
  656. auto document = load_document(navigation_params);
  657. // 2. If document is null, then run completionSteps and return.
  658. if (!document) {
  659. VERIFY_NOT_REACHED();
  660. completion_steps();
  661. return;
  662. }
  663. // 3. Set entry's document state's document to document.
  664. entry->document_state->set_document(document.ptr());
  665. // 4. Set entry's document state's origin to document's origin.
  666. entry->document_state->set_origin(document->origin());
  667. }
  668. // FIXME: 12. If entry's document state's request referrer is "client", then set it to request's referrer.
  669. // 13. If entry's document state's document is not null, then set entry's document state's ever populated to true.
  670. if (entry->document_state->document()) {
  671. entry->document_state->set_ever_populated(true);
  672. }
  673. // 14. Run completionSteps.
  674. completion_steps();
  675. });
  676. return {};
  677. }
  678. // To navigate a navigable navigable to a URL url using a Document sourceDocument,
  679. // with an optional POST resource, string, or null documentResource (default null),
  680. // an optional response-or-null response (default null), an optional boolean exceptionsEnabled (default false),
  681. // an optional NavigationHistoryBehavior historyHandling (default "auto"),
  682. // an optional serialized state-or-null navigationAPIState (default null),
  683. // an optional entry list or null formDataEntryList (default null),
  684. // an optional referrer policy referrerPolicy (default the empty string),
  685. // and an optional user navigation involvement userInvolvement (default "none"):
  686. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  687. WebIDL::ExceptionOr<void> Navigable::navigate(
  688. AK::URL const& url,
  689. JS::NonnullGCPtr<DOM::Document> source_document,
  690. Variant<Empty, String, POSTResource> document_resource,
  691. JS::GCPtr<Fetch::Infrastructure::Response> response,
  692. bool exceptions_enabled,
  693. Bindings::NavigationHistoryBehavior history_handling,
  694. Optional<SerializationRecord> navigation_api_state,
  695. Optional<Vector<XHR::FormDataEntry>&> form_data_entry_list,
  696. ReferrerPolicy::ReferrerPolicy referrer_policy,
  697. UserNaviagationInvolvement user_involvement)
  698. {
  699. auto& active_document = *this->active_document();
  700. auto& realm = active_document.realm();
  701. auto& vm = this->vm();
  702. // 1. Let cspNavigationType be "form-submission" if formDataEntryList is non-null; otherwise "other".
  703. auto csp_navigation_type = form_data_entry_list.has_value() ? CSPNavigationType::FormSubmission : CSPNavigationType::Other;
  704. // 2. Let sourceSnapshotParams be the result of snapshotting source snapshot params given sourceDocument.
  705. auto source_snapshot_params = source_document->snapshot_source_snapshot_params();
  706. // 3. Let initiatorOriginSnapshot be sourceDocument's origin.
  707. auto initiator_origin_snapshot = source_document->origin();
  708. // 4. Let initiatorBaseURLSnapshot be sourceDocument's document base URL.
  709. auto initiator_base_url_snapshot = source_document->base_url();
  710. // 5. If sourceDocument's node navigable is not allowed by sandboxing to navigate navigable given and sourceSnapshotParams, then:
  711. if (!source_document->navigable()->allowed_by_sandboxing_to_navigate(*this, source_snapshot_params)) {
  712. // 1. If exceptionsEnabled is true, then throw a "SecurityError" DOMException.
  713. if (exceptions_enabled) {
  714. return WebIDL::SecurityError::create(realm, "Source document's node navigable is not allowed to navigate"_fly_string);
  715. }
  716. // 2 Return.
  717. return {};
  718. }
  719. // 6. Let navigationId be the result of generating a random UUID.
  720. String navigation_id = TRY_OR_THROW_OOM(vm, Crypto::generate_random_uuid());
  721. // FIXME: 7. If the surrounding agent is equal to navigable's active document's relevant agent, then continue these steps.
  722. // Otherwise, queue a global task on the navigation and traversal task source given navigable's active window to continue these steps.
  723. // 8. If navigable's active document's unload counter is greater than 0,
  724. // then invoke WebDriver BiDi navigation failed with a WebDriver BiDi navigation status whose id is navigationId,
  725. // status is "canceled", and url is url, and return.
  726. if (active_document.unload_counter() > 0) {
  727. // FIXME: invoke WebDriver BiDi navigation failed with a WebDriver BiDi navigation status whose id is navigationId,
  728. // status is "canceled", and url is url
  729. return {};
  730. }
  731. // 9. If historyHandling is "auto", then:
  732. if (history_handling == Bindings::NavigationHistoryBehavior::Auto) {
  733. // FIXME: Fix spec typo targetNavigable --> navigable
  734. // 1. If url equals navigable's active document's URL,
  735. // and initiatorOriginSnapshot is same origin with targetNavigable's active document's origin,
  736. // then set historyHandling to "replace".
  737. if (url == active_document.url() && initiator_origin_snapshot.is_same_origin(active_document.origin()))
  738. history_handling = Bindings::NavigationHistoryBehavior::Replace;
  739. // 2. Otherwise, set historyHandling to "push".
  740. else
  741. history_handling = Bindings::NavigationHistoryBehavior::Push;
  742. }
  743. // 10. If the navigation must be a replace given url and navigable's active document, then set historyHandling to "replace".
  744. if (navigation_must_be_a_replace(url, active_document))
  745. history_handling = Bindings::NavigationHistoryBehavior::Replace;
  746. // 11. If all of the following are true:
  747. // - documentResource is null;
  748. // - response is null;
  749. // - url equals navigable's active session history entry's URL with exclude fragments set to true; and
  750. // - url's fragment is non-null
  751. if (document_resource.has<Empty>()
  752. && !response
  753. && url.equals(active_session_history_entry()->url, AK::URL::ExcludeFragment::Yes)
  754. && url.fragment().has_value()) {
  755. // 1. Navigate to a fragment given navigable, url, historyHandling, and navigationId.
  756. TRY(navigate_to_a_fragment(url, to_history_handling_behavior(history_handling), navigation_id));
  757. // 2. Return.
  758. return {};
  759. }
  760. // 12. If navigable's parent is non-null, then set navigable's is delaying load events to true.
  761. if (parent() != nullptr)
  762. set_delaying_load_events(true);
  763. // 13. Let targetBrowsingContext be navigable's active browsing context.
  764. [[maybe_unused]] auto target_browsing_context = active_browsing_context();
  765. // 14. Let targetSnapshotParams be the result of snapshotting target snapshot params given navigable.
  766. [[maybe_unused]] auto target_snapshot_params = snapshot_target_snapshot_params();
  767. // 15. 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".
  768. // 16. If navigable's ongoing navigation is "traversal", then:
  769. if (ongoing_navigation().has<Traversal>()) {
  770. // 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.
  771. // 2. Return.
  772. return {};
  773. }
  774. // 17. Set navigable's ongoing navigation to navigationId.
  775. set_ongoing_navigation(navigation_id);
  776. // 18. If url's scheme is "javascript", then:
  777. if (url.scheme() == "javascript"sv) {
  778. // 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.
  779. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this, url, history_handling, initiator_origin_snapshot, csp_navigation_type, navigation_id] {
  780. (void)navigate_to_a_javascript_url(url, to_history_handling_behavior(history_handling), initiator_origin_snapshot, csp_navigation_type, navigation_id);
  781. });
  782. // 2. Return.
  783. return {};
  784. }
  785. // 19. If all of the following are true:
  786. // - userInvolvement is not "browser UI";
  787. // - navigable's active document's origin is same origin-domain with sourceDocument's origin;
  788. // - navigable's active document's is initial about:blank is false; and
  789. // - url's scheme is a fetch scheme
  790. // then:
  791. if (user_involvement != UserNaviagationInvolvement::BrowserUI && active_document.origin().is_same_origin_domain(source_document->origin()) && !active_document.is_initial_about_blank() && Fetch::Infrastructure::is_fetch_scheme(url.scheme())) {
  792. // 1. Let navigation be navigable's active window's navigation API.
  793. auto navigation = active_window()->navigation();
  794. // 2. Let entryListForFiring be formDataEntryList if documentResource is a POST resource; otherwise, null.
  795. auto entry_list_for_firing = [&]() -> Optional<Vector<XHR::FormDataEntry>&> {
  796. if (document_resource.has<POSTResource>())
  797. return form_data_entry_list;
  798. return {};
  799. }();
  800. // 3. Let navigationAPIStateForFiring be navigationAPIState if navigationAPIState is not null;
  801. // otherwise, StructuredSerializeForStorage(undefined).
  802. auto navigation_api_state_for_firing = navigation_api_state.value_or(MUST(structured_serialize_for_storage(vm, JS::js_undefined())));
  803. // FIXME: 4. Let continue be the result of firing a push/replace/reload navigate event at navigation
  804. // with navigationType set to historyHandling, isSameDocument set to false, userInvolvement set to userInvolvement,
  805. // formDataEntryList set to entryListForFiring, destinationURL set to url, and navigationAPIState set to navigationAPIStateForFiring.
  806. (void)navigation;
  807. (void)entry_list_for_firing;
  808. (void)navigation_api_state_for_firing;
  809. // FIXME: 5. If continue is false, then return.
  810. }
  811. // 20. In parallel, run these steps:
  812. 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, initiator_base_url_snapshot] {
  813. // FIXME: 1. Let unloadPromptCanceled be the result of checking if unloading is user-canceled for navigable's active document's inclusive descendant navigables.
  814. // FIXME: 2. If unloadPromptCanceled is true, or navigable's ongoing navigation is no longer navigationId, then:
  815. if (!ongoing_navigation().has<String>() || ongoing_navigation().get<String>() != navigation_id) {
  816. // 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.
  817. // 2. Abort these steps.
  818. return;
  819. }
  820. // 3. Queue a global task on the navigation and traversal task source given navigable's active window to abort navigable's active document.
  821. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this] {
  822. VERIFY(this->active_document());
  823. this->active_document()->abort();
  824. });
  825. // 4. Let documentState be a new document state with
  826. // request referrer policy: referrerPolicy
  827. // initiator origin: initiatorOriginSnapshot
  828. // resource: documentResource
  829. // navigable target name: navigable's target name
  830. JS::NonnullGCPtr<DocumentState> document_state = *heap().allocate_without_realm<DocumentState>();
  831. document_state->set_request_referrer_policy(referrer_policy);
  832. document_state->set_initiator_origin(initiator_origin_snapshot);
  833. document_state->set_resource(document_resource);
  834. document_state->set_navigable_target_name(target_name());
  835. // 5. If url matches about:blank or is about:srcdoc, then set documentState's origin to documentState's initiator origin.
  836. // FIXME: should this say "matches about:srcdoc"
  837. if (url_matches_about_blank(url) || url == "about:srcdoc"sv) {
  838. // 1. Set documentState's origin to initiatorOriginSnapshot.
  839. document_state->set_origin(document_state->initiator_origin());
  840. // 2. Set documentState's about base URL to initiatorBaseURLSnapshot.
  841. document_state->set_about_base_url(initiator_base_url_snapshot);
  842. }
  843. // 6. Let historyEntry be a new session history entry, with its URL set to url and its document state set to documentState.
  844. JS::NonnullGCPtr<SessionHistoryEntry> history_entry = *heap().allocate_without_realm<SessionHistoryEntry>();
  845. history_entry->url = url;
  846. history_entry->document_state = document_state;
  847. // 8. Let navigationParams be null.
  848. Optional<NavigationParams> navigation_params;
  849. // FIXME: 9. If response is non-null:
  850. if (response) {
  851. }
  852. // 10. Attempt to populate the history entry's document
  853. // for historyEntry, given navigable, "navigate", sourceSnapshotParams,
  854. // targetSnapshotParams, navigationId, navigationParams, cspNavigationType, with allowPOST
  855. // set to true and completionSteps set to the following step:
  856. populate_session_history_entry_document(history_entry, navigation_params, navigation_id, source_snapshot_params, true, [this, history_entry, history_handling, navigation_id] {
  857. traversable_navigable()->append_session_history_traversal_steps([this, history_entry, history_handling] {
  858. finalize_a_cross_document_navigation(*this, to_history_handling_behavior(history_handling), history_entry);
  859. });
  860. }).release_value_but_fixme_should_propagate_errors();
  861. });
  862. return {};
  863. }
  864. WebIDL::ExceptionOr<void> Navigable::navigate_to_a_fragment(AK::URL const& url, HistoryHandlingBehavior history_handling, String navigation_id)
  865. {
  866. (void)navigation_id;
  867. // FIXME: 1. Let navigation be navigable's active window's navigation API.
  868. // FIXME: 2. Let destinationNavigationAPIState be navigable's active session history entry's navigation API state.
  869. // FIXME: 3. If navigationAPIState is not null, then set destinationNavigationAPIState to navigationAPIState.
  870. // 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,
  871. // userInvolvement set to userInvolvement, and destinationURL set to url, and navigationAPIState set to destinationNavigationAPIState.
  872. // FIXME: 5. If continue is false, then return.
  873. // 6. Let historyEntry be a new session history entry, with
  874. // URL: url
  875. // document state: navigable's active session history entry's document state
  876. // navigation API state: destinationNavigationAPIState
  877. // scroll restoration mode: navigable's active session history entry's scroll restoration mode
  878. JS::NonnullGCPtr<SessionHistoryEntry> history_entry = heap().allocate_without_realm<SessionHistoryEntry>();
  879. history_entry->url = url;
  880. history_entry->document_state = active_session_history_entry()->document_state;
  881. history_entry->scroll_restoration_mode = active_session_history_entry()->scroll_restoration_mode;
  882. // 7. Let entryToReplace be navigable's active session history entry if historyHandling is "replace", otherwise null.
  883. auto entry_to_replace = history_handling == HistoryHandlingBehavior::Replace ? active_session_history_entry() : nullptr;
  884. // FIXME: 8. Let history be navigable's active document's history object.
  885. // FIXME: 9. Let scriptHistoryIndex be history's index.
  886. // FIXME: 10. Let scriptHistoryIndex be history's index.
  887. // 11. If historyHandling is "push", then:
  888. if (history_handling == HistoryHandlingBehavior::Push) {
  889. // FIXME: 1. Set history's state to null.
  890. // FIXME: 2. Increment scriptHistoryIndex.
  891. // FIXME: 3. Set scriptHistoryLength to scriptHistoryIndex + 1.
  892. }
  893. // 12. Set navigable's active session history entry to historyEntry.
  894. m_active_session_history_entry = history_entry;
  895. // FIXME: 13. Update document for history step application given navigable's active document, historyEntry, true, scriptHistoryIndex, and scriptHistoryLength.
  896. // FIXME: 14. Update the navigation API entries for a same-document navigation given navigation, historyEntry, and historyHandling.
  897. // 15. Scroll to the fragment given navigable's active document.
  898. // FIXME: Specification doesn't say when document url needs to update during fragment navigation
  899. active_document()->set_url(url);
  900. active_document()->scroll_to_the_fragment();
  901. // 16. Let traversable be navigable's traversable navigable.
  902. auto traversable = traversable_navigable();
  903. // 17. Append the following session history synchronous navigation steps involving navigable to traversable:
  904. traversable->append_session_history_traversal_steps([this, traversable, history_entry, entry_to_replace] {
  905. // 1. Finalize a same-document navigation given traversable, navigable, historyEntry, and entryToReplace.
  906. finalize_a_same_document_navigation(*traversable, *this, history_entry, entry_to_replace);
  907. // FIXME: 2. Invoke WebDriver BiDi fragment navigated with navigable's active browsing context and a new WebDriver BiDi
  908. // navigation status whose id is navigationId, url is url, and status is "complete".
  909. });
  910. return {};
  911. }
  912. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#evaluate-a-javascript:-url
  913. WebIDL::ExceptionOr<JS::GCPtr<DOM::Document>> Navigable::evaluate_javascript_url(AK::URL const& url, Origin const& new_document_origin, String navigation_id)
  914. {
  915. auto& vm = this->vm();
  916. auto& realm = active_window()->realm();
  917. // 1. Let urlString be the result of running the URL serializer on url.
  918. auto url_string = url.serialize();
  919. // 2. Let encodedScriptSource be the result of removing the leading "javascript:" from urlString.
  920. auto encoded_script_source = url_string.substring_view(11, url_string.length() - 11);
  921. // FIXME: 3. Let scriptSource be the UTF-8 decoding of the percent-decoding of encodedScriptSource.
  922. // 4. Let settings be targetNavigable's active document's relevant settings object.
  923. auto& settings = active_document()->relevant_settings_object();
  924. // 5. Let baseURL be settings's API base URL.
  925. auto base_url = settings.api_base_url();
  926. // 6. Let script be the result of creating a classic script given scriptSource, settings, baseURL, and the default classic script fetch options.
  927. auto script = HTML::ClassicScript::create("(javascript url)", encoded_script_source, settings, base_url);
  928. // 7. Let evaluationStatus be the result of running the classic script script.
  929. auto evaluation_status = script->run();
  930. // 8. Let result be null.
  931. String result;
  932. // 9. If evaluationStatus is a normal completion, and evaluationStatus.[[Value]] is a String, then set result to evaluationStatus.[[Value]].
  933. if (evaluation_status.type() == JS::Completion::Type::Normal && evaluation_status.value()->is_string()) {
  934. result = evaluation_status.value()->as_string().utf8_string();
  935. } else {
  936. // 10. Otherwise, return null.
  937. return nullptr;
  938. }
  939. // 11. Let response be a new response with
  940. // URL: targetNavigable's active document's URL
  941. // header list: «(`Content-Type`, `text/html;charset=utf-8`)»
  942. // body: the UTF-8 encoding of result, as a body
  943. auto response = Fetch::Infrastructure::Response::create(vm);
  944. response->url_list().append(active_document()->url());
  945. auto header = TRY_OR_THROW_OOM(vm, Fetch::Infrastructure::Header::from_string_pair("Content-Type"sv, "text/html"sv));
  946. TRY_OR_THROW_OOM(vm, response->header_list()->append(move(header)));
  947. response->set_body(TRY(Fetch::Infrastructure::byte_sequence_as_body(realm, result.bytes())));
  948. // 12. Let policyContainer be targetNavigable's active document's policy container.
  949. auto const& policy_container = active_document()->policy_container();
  950. // FIXME: 13. Let finalSandboxFlags be policyContainer's CSP list's CSP-derived sandboxing flags.
  951. // FIXME: 14. Let coop be targetNavigable's active document's cross-origin opener policy.
  952. auto const& coop = active_document()->cross_origin_opener_policy();
  953. // 15. Let coopEnforcementResult be a new cross-origin opener policy enforcement result with
  954. // url: url
  955. // origin: newDocumentOrigin
  956. // cross-origin opener policy: coop
  957. CrossOriginOpenerPolicyEnforcementResult coop_enforcement_result {
  958. .url = url,
  959. .origin = new_document_origin,
  960. .cross_origin_opener_policy = coop,
  961. };
  962. // 16. Let navigationParams be a new navigation params, with
  963. // id: navigationId
  964. // navigable: targetNavigable
  965. // request: null
  966. // response: response
  967. // fetch controller: null
  968. // commit early hints: null
  969. // COOP enforcement result: coopEnforcementResult
  970. // reserved environment: null
  971. // origin: newDocumentOrigin
  972. // policy container: policyContainer
  973. // final sandboxing flag set: finalSandboxFlags
  974. // cross-origin opener policy: coop
  975. // navigation timing type: "navigate"
  976. // about base URL: targetNavigable's active document's about base URL
  977. NavigationParams navigation_params {
  978. .id = navigation_id,
  979. .request = {},
  980. .response = response,
  981. .origin = new_document_origin,
  982. .policy_container = policy_container,
  983. .cross_origin_opener_policy = coop,
  984. .coop_enforcement_result = move(coop_enforcement_result),
  985. .reserved_environment = {},
  986. .browsing_context = active_browsing_context(),
  987. .navigable = this,
  988. .fetch_controller = nullptr,
  989. };
  990. // 17. Return the result of loading an HTML document given navigationParams.
  991. return load_document(navigation_params);
  992. }
  993. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate-to-a-javascript:-url
  994. WebIDL::ExceptionOr<void> Navigable::navigate_to_a_javascript_url(AK::URL const& url, HistoryHandlingBehavior history_handling, Origin const& initiator_origin, CSPNavigationType csp_navigation_type, String navigation_id)
  995. {
  996. // 1. Assert: historyHandling is "replace".
  997. VERIFY(history_handling == HistoryHandlingBehavior::Replace);
  998. // 2. Set the ongoing navigation for targetNavigable to null.
  999. set_ongoing_navigation({});
  1000. // 3. If initiatorOrigin is not same origin-domain with targetNavigable's active document's origin, then return.
  1001. if (!initiator_origin.is_same_origin_domain(active_document()->origin()))
  1002. return {};
  1003. // FIXME: 4. Let request be a new request whose URL is url.
  1004. // FIXME: 5. If the result of should navigation request of type be blocked by Content Security Policy? given request and cspNavigationType is "Blocked", then return.
  1005. (void)csp_navigation_type;
  1006. // 6. Let newDocument be the result of evaluating a javascript: URL given targetNavigable, url, and initiatorOrigin.
  1007. auto new_document = TRY(evaluate_javascript_url(url, initiator_origin, navigation_id));
  1008. // 7. If newDocument is null, then return.
  1009. if (!new_document) {
  1010. // NOTE: In this case, some JavaScript code was executed, but no new Document was created, so we will not perform a navigation.
  1011. return {};
  1012. }
  1013. // 8. Assert: initiatorOrigin is newDocument's origin.
  1014. VERIFY(initiator_origin == new_document->origin());
  1015. // 9. Let entryToReplace be targetNavigable's active session history entry.
  1016. auto entry_to_replace = active_session_history_entry();
  1017. // 10. Let oldDocState be entryToReplace's document state.
  1018. auto old_doc_state = entry_to_replace->document_state;
  1019. // 11. Let documentState be a new document state with
  1020. // document: newDocument
  1021. // history policy container: a clone of the oldDocState's history policy container if it is non-null; null otherwise
  1022. // request referrer: oldDocState's request referrer
  1023. // request referrer policy: oldDocState's request referrer policy
  1024. // initiator origin: initiatorOrigin
  1025. // origin: initiatorOrigin
  1026. // about base URL: oldDocState's about base URL
  1027. // resource: null
  1028. // ever populated: true
  1029. // navigable target name: oldDocState's navigable target name
  1030. JS::NonnullGCPtr<DocumentState> document_state = *heap().allocate_without_realm<DocumentState>();
  1031. document_state->set_document(new_document);
  1032. document_state->set_history_policy_container(old_doc_state->history_policy_container());
  1033. document_state->set_request_referrer(old_doc_state->request_referrer());
  1034. document_state->set_request_referrer_policy(old_doc_state->request_referrer_policy());
  1035. document_state->set_initiator_origin(initiator_origin);
  1036. document_state->set_origin(initiator_origin);
  1037. document_state->set_ever_populated(true);
  1038. document_state->set_navigable_target_name(old_doc_state->navigable_target_name());
  1039. // 12. Let historyEntry be a new session history entry, with
  1040. // URL: entryToReplace's URL
  1041. // document state: documentState
  1042. JS::NonnullGCPtr<SessionHistoryEntry> history_entry = *heap().allocate_without_realm<SessionHistoryEntry>();
  1043. history_entry->url = entry_to_replace->url;
  1044. history_entry->document_state = document_state;
  1045. // 13. Append session history traversal steps to targetNavigable's traversable to finalize a cross-document navigation with targetNavigable, historyHandling, and historyEntry.
  1046. traversable_navigable()->append_session_history_traversal_steps([this, history_entry, history_handling, navigation_id] {
  1047. if (this->ongoing_navigation() != navigation_id) {
  1048. // NOTE: This check is not in the spec but we should not continue navigation if ongoing navigation id has changed.
  1049. return;
  1050. }
  1051. finalize_a_cross_document_navigation(*this, history_handling, history_entry);
  1052. });
  1053. return {};
  1054. }
  1055. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#reload
  1056. void Navigable::reload()
  1057. {
  1058. // 1. Set navigable's active session history entry's document state's reload pending to true.
  1059. active_session_history_entry()->document_state->set_reload_pending(true);
  1060. // 2. Let traversable be navigable's traversable navigable.
  1061. auto traversable = traversable_navigable();
  1062. // 3. Append the following session history traversal steps to traversable:
  1063. traversable->append_session_history_traversal_steps([traversable] {
  1064. // 1. Apply pending history changes to traversable with true.
  1065. traversable->apply_pending_history_changes();
  1066. });
  1067. }
  1068. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#the-navigation-must-be-a-replace
  1069. bool navigation_must_be_a_replace(AK::URL const& url, DOM::Document const& document)
  1070. {
  1071. return url.scheme() == "javascript"sv || document.is_initial_about_blank();
  1072. }
  1073. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#allowed-to-navigate
  1074. bool Navigable::allowed_by_sandboxing_to_navigate(Navigable const& target, SourceSnapshotParams const& source_snapshot_params)
  1075. {
  1076. auto& source = *this;
  1077. auto is_ancestor_of = [](Navigable const& a, Navigable const& b) {
  1078. for (auto parent = b.parent(); parent; parent = parent->parent()) {
  1079. if (parent.ptr() == &a)
  1080. return true;
  1081. }
  1082. return false;
  1083. };
  1084. // A navigable source is allowed by sandboxing to navigate a second navigable target,
  1085. // given a source snapshot params sourceSnapshotParams, if the following steps return true:
  1086. // 1. If source is target, then return true.
  1087. if (&source == &target)
  1088. return true;
  1089. // 2. If source is an ancestor of target, then return true.
  1090. if (is_ancestor_of(source, target))
  1091. return true;
  1092. // 3. If target is an ancestor of source, then:
  1093. if (is_ancestor_of(target, source)) {
  1094. // 1. If target is not a top-level traversable, then return true.
  1095. if (!target.is_top_level_traversable())
  1096. return true;
  1097. // 2. If sourceSnapshotParams's has transient activation is true, and sourceSnapshotParams's sandboxing flags's
  1098. // sandboxed top-level navigation with user activation browsing context flag is set, then return false.
  1099. if (source_snapshot_params.has_transient_activation && has_flag(source_snapshot_params.sandboxing_flags, SandboxingFlagSet::SandboxedTopLevelNavigationWithUserActivation))
  1100. return false;
  1101. // 3. If sourceSnapshotParams's has transient activation is false, and sourceSnapshotParams's sandboxing flags's
  1102. // sandboxed top-level navigation without user activation browsing context flag is set, then return false.
  1103. if (!source_snapshot_params.has_transient_activation && has_flag(source_snapshot_params.sandboxing_flags, SandboxingFlagSet::SandboxedTopLevelNavigationWithoutUserActivation))
  1104. return false;
  1105. // 4. Return true.
  1106. return true;
  1107. }
  1108. // 4. If target is a top-level traversable:
  1109. if (target.is_top_level_traversable()) {
  1110. // FIXME: 1. If source is the one permitted sandboxed navigator of target, then return true.
  1111. // 2. If sourceSnapshotParams's sandboxing flags's sandboxed navigation browsing context flag is set, then return false.
  1112. if (has_flag(source_snapshot_params.sandboxing_flags, SandboxingFlagSet::SandboxedNavigation))
  1113. return false;
  1114. // 3. Return true.
  1115. return true;
  1116. }
  1117. // 5. If sourceSnapshotParams's sandboxing flags's sandboxed navigation browsing context flag is set, then return false.
  1118. // 6. Return true.
  1119. return !has_flag(source_snapshot_params.sandboxing_flags, SandboxingFlagSet::SandboxedNavigation);
  1120. }
  1121. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#snapshotting-target-snapshot-params
  1122. TargetSnapshotParams Navigable::snapshot_target_snapshot_params()
  1123. {
  1124. // To snapshot target snapshot params given a navigable targetNavigable, return a new target snapshot params
  1125. // with sandboxing flags set to the result of determining the creation sandboxing flags given targetNavigable's
  1126. // active browsing context and targetNavigable's container.
  1127. return { determine_the_creation_sandboxing_flags(*active_browsing_context(), container()) };
  1128. }
  1129. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-cross-document-navigation
  1130. void finalize_a_cross_document_navigation(JS::NonnullGCPtr<Navigable> navigable, HistoryHandlingBehavior history_handling, JS::NonnullGCPtr<SessionHistoryEntry> history_entry)
  1131. {
  1132. // 1. FIXME: Assert: this is running on navigable's traversable navigable's session history traversal queue.
  1133. // 2. Set navigable's is delaying load events to false.
  1134. navigable->set_delaying_load_events(false);
  1135. // 3. If historyEntry's document is null, then return.
  1136. if (!history_entry->document_state->document())
  1137. return;
  1138. // 4. If all of the following are true:
  1139. // - navigable's parent is null;
  1140. // - historyEntry's document's browsing context is not an auxiliary browsing context whose opener browsing context is non-null; and
  1141. // - historyEntry's document's origin is not navigable's active document's origin
  1142. // then set historyEntry's document state's navigable target name to the empty string.
  1143. if (navigable->parent() == nullptr && history_entry->document_state->document()->browsing_context()->opener_browsing_context() != nullptr && history_entry->document_state->document()->origin() != navigable->active_document()->origin())
  1144. history_entry->document_state->set_navigable_target_name(String {});
  1145. // 5. Let entryToReplace be navigable's active session history entry if historyHandling is "replace", otherwise null.
  1146. auto entry_to_replace = history_handling == HistoryHandlingBehavior::Replace ? navigable->active_session_history_entry() : nullptr;
  1147. // 6. Let traversable be navigable's traversable navigable.
  1148. auto traversable = navigable->traversable_navigable();
  1149. // 7. Let targetStep be null.
  1150. int target_step;
  1151. // 8. Let targetEntries be the result of getting session history entries for navigable.
  1152. auto& target_entries = navigable->get_session_history_entries();
  1153. // 9. If entryToReplace is null, then:
  1154. if (entry_to_replace == nullptr) {
  1155. // 1. Clear the forward session history of traversable.
  1156. traversable->clear_the_forward_session_history();
  1157. // 2. Set targetStep to traversable's current session history step + 1.
  1158. target_step = traversable->current_session_history_step() + 1;
  1159. // 3. Set historyEntry's step to targetStep.
  1160. history_entry->step = target_step;
  1161. // 4. Append historyEntry to targetEntries.
  1162. target_entries.append(history_entry);
  1163. } else {
  1164. // 1. Replace entryToReplace with historyEntry in targetEntries.
  1165. *(target_entries.find(*entry_to_replace)) = history_entry;
  1166. // 2. Set historyEntry's step to entryToReplace's step.
  1167. history_entry->step = entry_to_replace->step;
  1168. // 3. If historyEntry's document state's origin is same origin with entryToReplace's document state's origin,
  1169. // then set historyEntry's navigation API key to entryToReplace's navigation API key.
  1170. if (history_entry->document_state->origin().has_value() && entry_to_replace->document_state->origin().has_value() && history_entry->document_state->origin()->is_same_origin(*entry_to_replace->document_state->origin())) {
  1171. history_entry->navigation_api_key = entry_to_replace->navigation_api_key;
  1172. }
  1173. // 4. Set targetStep to traversable's current session history step.
  1174. target_step = traversable->current_session_history_step();
  1175. }
  1176. // FIXME: 10. Apply the push/replace history step targetStep to traversable.
  1177. traversable->apply_the_history_step(target_step);
  1178. }
  1179. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#url-and-history-update-steps
  1180. void perform_url_and_history_update_steps(DOM::Document& document, AK::URL new_url, HistoryHandlingBehavior history_handling)
  1181. {
  1182. // 1. Let navigable be document's node navigable.
  1183. auto navigable = document.navigable();
  1184. // 2. Let activeEntry be navigable's active session history entry.
  1185. auto active_entry = navigable->active_session_history_entry();
  1186. // 3. Let newEntry be a new session history entry, with
  1187. // URL: newURL
  1188. // serialized state: if serializedData is not null, serializedData; otherwise activeEntry's classic history API state
  1189. // document state: activeEntry's document state
  1190. // scroll restoration mode: activeEntry's scroll restoration mode
  1191. // persisted user state: activeEntry's persisted user state
  1192. JS::NonnullGCPtr<SessionHistoryEntry> new_entry = document.heap().allocate_without_realm<SessionHistoryEntry>();
  1193. new_entry->url = new_url;
  1194. new_entry->document_state = active_entry->document_state;
  1195. new_entry->scroll_restoration_mode = active_entry->scroll_restoration_mode;
  1196. // 4. If document's is initial about:blank is true, then set historyHandling to "replace".
  1197. if (document.is_initial_about_blank()) {
  1198. history_handling = HistoryHandlingBehavior::Replace;
  1199. }
  1200. // 5. Let entryToReplace be activeEntry if historyHandling is "replace", otherwise null.
  1201. auto entry_to_replace = history_handling == HistoryHandlingBehavior::Replace ? active_entry : nullptr;
  1202. // 6. If historyHandling is "push", then:
  1203. if (history_handling == HistoryHandlingBehavior::Push) {
  1204. // FIXME: 1. Increment document's history object's index.
  1205. // FIXME: 2. Set document's history object's length to its index + 1.
  1206. TODO();
  1207. }
  1208. // FIXME: 7. If serializedData is not null, then restore the history object state given document and newEntry.
  1209. // 8. Set document's URL to newURL.
  1210. document.set_url(new_url);
  1211. // FIXME: 9. Set document's latest entry to newEntry.
  1212. // 10. Set navigable's active session history entry to newEntry.
  1213. navigable->set_active_session_history_entry(new_entry);
  1214. // FIXME: 11. Update the navigation API entries for a same-document navigation given document's relevant global object's navigation API, newEntry, and historyHandling.
  1215. // 12. Let traversable be navigable's traversable navigable.
  1216. auto traversable = navigable->traversable_navigable();
  1217. // 13. Append the following session history synchronous navigation steps involving navigable to traversable:
  1218. traversable->append_session_history_traversal_steps([traversable, navigable, new_entry, entry_to_replace] {
  1219. // 1. Finalize a same-document navigation given traversable, navigable, newEntry, and entryToReplace.
  1220. finalize_a_same_document_navigation(*traversable, *navigable, new_entry, entry_to_replace);
  1221. });
  1222. }
  1223. }