Navigable.cpp 72 KB

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