Navigable.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Crypto/Crypto.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/DocumentLoading.h>
  10. #include <LibWeb/Fetch/Fetching/Fetching.h>
  11. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  12. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  13. #include <LibWeb/Fetch/Infrastructure/URL.h>
  14. #include <LibWeb/HTML/BrowsingContext.h>
  15. #include <LibWeb/HTML/DocumentState.h>
  16. #include <LibWeb/HTML/Navigable.h>
  17. #include <LibWeb/HTML/NavigationParams.h>
  18. #include <LibWeb/HTML/SessionHistoryEntry.h>
  19. #include <LibWeb/HTML/TraversableNavigable.h>
  20. #include <LibWeb/Platform/EventLoopPlugin.h>
  21. namespace Web::HTML {
  22. static HashTable<Navigable*>& all_navigables()
  23. {
  24. static HashTable<Navigable*> set;
  25. return set;
  26. }
  27. Navigable::Navigable()
  28. {
  29. all_navigables().set(this);
  30. }
  31. Navigable::~Navigable()
  32. {
  33. all_navigables().remove(this);
  34. }
  35. void Navigable::visit_edges(Cell::Visitor& visitor)
  36. {
  37. Base::visit_edges(visitor);
  38. visitor.visit(m_parent);
  39. visitor.visit(m_current_session_history_entry);
  40. visitor.visit(m_active_session_history_entry);
  41. visitor.visit(m_container);
  42. }
  43. JS::GCPtr<Navigable> Navigable::navigable_with_active_document(JS::NonnullGCPtr<DOM::Document> document)
  44. {
  45. for (auto* navigable : all_navigables()) {
  46. if (navigable->active_document() == document)
  47. return navigable;
  48. }
  49. return nullptr;
  50. }
  51. // https://html.spec.whatwg.org/multipage/document-sequences.html#initialize-the-navigable
  52. ErrorOr<void> Navigable::initialize_navigable(JS::NonnullGCPtr<DocumentState> document_state, JS::GCPtr<Navigable> parent)
  53. {
  54. static int next_id = 0;
  55. m_id = TRY(String::number(next_id++));
  56. // 1. Let entry be a new session history entry, with
  57. JS::NonnullGCPtr<SessionHistoryEntry> entry = *heap().allocate_without_realm<SessionHistoryEntry>();
  58. // URL: document's URL
  59. entry->url = document_state->document()->url();
  60. // document state: documentState
  61. entry->document_state = document_state;
  62. // 2. Set navigable's current session history entry to entry.
  63. m_current_session_history_entry = entry;
  64. // 3. Set navigable's active session history entry to entry.
  65. m_active_session_history_entry = entry;
  66. // 4. Set navigable's parent to parent.
  67. m_parent = parent;
  68. return {};
  69. }
  70. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-the-target-history-entry
  71. JS::GCPtr<SessionHistoryEntry> Navigable::get_the_target_history_entry(int target_step) const
  72. {
  73. // 1. Let entries be the result of getting session history entries for navigable.
  74. auto& entries = get_session_history_entries();
  75. // 2. Return the item in entries that has the greatest step less than or equal to step.
  76. JS::GCPtr<SessionHistoryEntry> result = nullptr;
  77. for (auto& entry : entries) {
  78. auto entry_step = entry->step.get<int>();
  79. if (entry_step <= target_step) {
  80. if (!result || result->step.get<int>() < entry_step) {
  81. result = entry;
  82. }
  83. }
  84. }
  85. return result;
  86. }
  87. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-document
  88. JS::GCPtr<DOM::Document> Navigable::active_document()
  89. {
  90. // A navigable's active document is its active session history entry's document.
  91. return m_active_session_history_entry->document_state->document();
  92. }
  93. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-bc
  94. JS::GCPtr<BrowsingContext> Navigable::active_browsing_context()
  95. {
  96. // A navigable's active browsing context is its active document's browsing context.
  97. // If this navigable is a traversable navigable, then its active browsing context will be a top-level browsing context.
  98. if (auto document = active_document())
  99. return document->browsing_context();
  100. return nullptr;
  101. }
  102. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-wp
  103. JS::GCPtr<HTML::WindowProxy> Navigable::active_window_proxy()
  104. {
  105. // A navigable's active WindowProxy is its active browsing context's associated WindowProxy.
  106. if (auto browsing_context = active_browsing_context())
  107. return browsing_context->window_proxy();
  108. return nullptr;
  109. }
  110. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-window
  111. JS::GCPtr<HTML::Window> Navigable::active_window()
  112. {
  113. // A navigable's active window is its active WindowProxy's [[Window]].
  114. if (auto window_proxy = active_window_proxy())
  115. return window_proxy->window();
  116. return nullptr;
  117. }
  118. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-target
  119. String Navigable::target_name() const
  120. {
  121. // FIXME: A navigable's target name is its active session history entry's document state's navigable target name.
  122. dbgln("FIXME: Implement Navigable::target_name()");
  123. return {};
  124. }
  125. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-container
  126. JS::GCPtr<NavigableContainer> Navigable::container() const
  127. {
  128. // The container of a navigable navigable is the navigable container whose nested navigable is navigable, or null if there is no such element.
  129. return m_container;
  130. }
  131. void Navigable::set_container(JS::GCPtr<NavigableContainer> container)
  132. {
  133. m_container = container;
  134. }
  135. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-traversable
  136. JS::GCPtr<TraversableNavigable> Navigable::traversable_navigable() const
  137. {
  138. // 1. Let navigable be inputNavigable.
  139. auto navigable = const_cast<Navigable*>(this);
  140. // 2. While navigable is not a traversable navigable, set navigable to navigable's parent.
  141. while (navigable && !is<TraversableNavigable>(*navigable))
  142. navigable = navigable->parent();
  143. // 3. Return navigable.
  144. return static_cast<TraversableNavigable*>(navigable);
  145. }
  146. // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-top
  147. JS::GCPtr<TraversableNavigable> Navigable::top_level_traversable()
  148. {
  149. // 1. Let navigable be inputNavigable.
  150. auto navigable = this;
  151. // 2. While navigable's parent is not null, set navigable to navigable's parent.
  152. while (navigable->parent())
  153. navigable = navigable->parent();
  154. // 3. Return navigable.
  155. return verify_cast<TraversableNavigable>(navigable);
  156. }
  157. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#getting-session-history-entries
  158. Vector<JS::NonnullGCPtr<SessionHistoryEntry>>& Navigable::get_session_history_entries() const
  159. {
  160. // 1. Let traversable be navigable's traversable navigable.
  161. auto traversable = traversable_navigable();
  162. // FIXME 2. Assert: this is running within traversable's session history traversal queue.
  163. // 3. If navigable is traversable, return traversable's session history entries.
  164. if (this == traversable)
  165. return traversable->session_history_entries();
  166. // 4. Let docStates be an empty ordered set of document states.
  167. Vector<JS::GCPtr<DocumentState>> doc_states;
  168. // 5. For each entry of traversable's session history entries, append entry's document state to docStates.
  169. for (auto& entry : traversable->session_history_entries())
  170. doc_states.append(entry->document_state);
  171. // 6. For each docState of docStates:
  172. while (!doc_states.is_empty()) {
  173. auto doc_state = doc_states.take_first();
  174. // 1. For each nestedHistory of docState's nested histories:
  175. for (auto& nested_history : doc_state->nested_histories()) {
  176. // 1. If nestedHistory's id equals navigable's id, return nestedHistory's entries.
  177. if (nested_history.id == id())
  178. return nested_history.entries;
  179. // 2. For each entry of nestedHistory's entries, append entry's document state to docStates.
  180. for (auto& entry : nested_history.entries)
  181. doc_states.append(entry->document_state);
  182. }
  183. }
  184. VERIFY_NOT_REACHED();
  185. }
  186. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#create-navigation-params-by-fetching
  187. 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)
  188. {
  189. auto& vm = navigable->vm();
  190. auto& realm = navigable->active_window()->realm();
  191. // FIXME: 1. Assert: this is running in parallel.
  192. // 2. Let documentResource be entry's document state's resource.
  193. auto document_resource = entry->document_state->resource();
  194. // 3. Let request be a new request, with
  195. // url: entry's URL
  196. // client: sourceSnapshotParams's fetch client
  197. // destination: "document"
  198. // credentials mode: "include"
  199. // use-URL-credentials flag: set
  200. // redirect mode: "manual"
  201. // replaces client id: navigable's active document's relevant settings object's id
  202. // mode: "navigate"
  203. // referrer: entry's document state's request referrer
  204. // FIXME: referrer policy: entry's document state's request referrer policy
  205. auto request = Fetch::Infrastructure::Request::create(vm);
  206. request->set_url(entry->url);
  207. request->set_client(source_snapshot_params.fetch_client);
  208. request->set_destination(Fetch::Infrastructure::Request::Destination::Document);
  209. request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include);
  210. request->set_use_url_credentials(true);
  211. request->set_redirect_mode(Fetch::Infrastructure::Request::RedirectMode::Manual);
  212. auto replaces_client_id = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(navigable->active_document()->relevant_settings_object().id));
  213. request->set_replaces_client_id(replaces_client_id);
  214. request->set_mode(Fetch::Infrastructure::Request::Mode::Navigate);
  215. request->set_referrer(entry->document_state->request_referrer());
  216. // 4. If documentResource is a POST resource, then:
  217. if (document_resource.has<POSTResource>()) {
  218. // 1. Set request's method to `POST`.
  219. request->set_method(TRY_OR_THROW_OOM(vm, ByteBuffer::copy("post"sv.bytes())));
  220. // 2. Set request's body to documentResource's request body.
  221. request->set_body(document_resource.get<POSTResource>().request_body.value());
  222. // 3. Set `Content-Type` to documentResource's request content-type in request's header list.
  223. auto request_content_type = document_resource.get<POSTResource>().request_content_type;
  224. auto request_content_type_string = [request_content_type]() {
  225. switch (request_content_type) {
  226. case POSTResource::RequestContentType::ApplicationXWWWFormUrlencoded:
  227. return "application/x-www-form-urlencoded"sv;
  228. case POSTResource::RequestContentType::MultipartFormData:
  229. return "multipart/form-data"sv;
  230. case POSTResource::RequestContentType::TextPlain:
  231. return "text/plain"sv;
  232. default:
  233. VERIFY_NOT_REACHED();
  234. }
  235. }();
  236. auto header = TRY_OR_THROW_OOM(vm, Fetch::Infrastructure::Header::from_string_pair("Content-Type"sv, request_content_type_string));
  237. TRY_OR_THROW_OOM(vm, request->header_list()->append(move(header)));
  238. }
  239. // 5. If entry's document state's reload pending is true, then set request's reload-navigation flag.
  240. if (entry->document_state->reload_pending())
  241. request->set_reload_navigation(true);
  242. // 6. Otherwise, if entry's document state's ever populated is true, then set request's history-navigation flag.
  243. if (entry->document_state->ever_populated())
  244. request->set_history_navigation(true);
  245. // 9. Let response be null.
  246. JS::GCPtr<Fetch::Infrastructure::Response> response = nullptr;
  247. // 10. Let responseOrigin be null.
  248. Optional<HTML::Origin> response_origin;
  249. // 11. Let fetchController be null.
  250. JS::GCPtr<Fetch::Infrastructure::FetchController> fetch_controller = nullptr;
  251. // 13. Let finalSandboxFlags be an empty sandboxing flag set.
  252. SandboxingFlagSet final_sandbox_flags;
  253. // 16. Let locationURL be null.
  254. ErrorOr<Optional<AK::URL>> location_url { OptionalNone {} };
  255. // 17. Let currentURL be request's current URL.
  256. AK::URL current_url = request->current_url();
  257. // FIXME: 18. Let commitEarlyHints be null.
  258. // 19. While true:
  259. while (true) {
  260. // 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:
  261. // FIXME: 2. If request's reserved client is null, then:
  262. // 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]
  263. // 4. Set response to null.
  264. response = nullptr;
  265. // 5. If fetchController is null, then set fetchController to the result of fetching request,
  266. // with processEarlyHintsResponse set to processEarlyHintsResponseas defined below, processResponse
  267. // set to processResponse as defined below, and useParallelQueue set to true.
  268. if (!fetch_controller) {
  269. // FIXME: Let processEarlyHintsResponse be the following algorithm given a response earlyResponse:
  270. // Let processResponse be the following algorithm given a response fetchedResponse:
  271. auto process_response = [&response](JS::NonnullGCPtr<Fetch::Infrastructure::Response> fetch_response) {
  272. // 1. Set response to fetchedResponse.
  273. response = fetch_response;
  274. };
  275. fetch_controller = TRY(Fetch::Fetching::fetch(
  276. realm,
  277. request,
  278. Fetch::Infrastructure::FetchAlgorithms::create(vm,
  279. {
  280. .process_request_body_chunk_length = {},
  281. .process_request_end_of_body = {},
  282. .process_early_hints_response = {},
  283. .process_response = move(process_response),
  284. .process_response_end_of_body = {},
  285. .process_response_consume_body = {},
  286. }),
  287. Fetch::Fetching::UseParallelQueue::Yes));
  288. }
  289. // 6. Otherwise, process the next manual redirect for fetchController.
  290. else {
  291. fetch_controller->process_next_manual_redirect();
  292. }
  293. // 7. Wait until either response is non-null, or navigable's ongoing navigation changes to no longer equal navigationId.
  294. Platform::EventLoopPlugin::the().spin_until([&]() {
  295. if (response != nullptr)
  296. return true;
  297. if (navigation_id.has_value() && (!navigable->ongoing_navigation().has<String>() || navigable->ongoing_navigation().get<String>() != *navigation_id))
  298. return true;
  299. return false;
  300. });
  301. // If the latter condition occurs, then abort fetchController, and return. Otherwise, proceed onward.
  302. if (navigation_id.has_value() && (!navigable->ongoing_navigation().has<String>() || navigable->ongoing_navigation().get<String>() != *navigation_id)) {
  303. fetch_controller->abort(realm, {});
  304. return OptionalNone {};
  305. }
  306. // 8. If request's body is null, then set entry's document state's resource to null.
  307. if (!request->body().has<Empty>()) {
  308. entry->document_state->set_resource(Empty {});
  309. }
  310. // 11. Set responseOrigin to the result of determining the origin given response's URL, finalSandboxFlags,
  311. // entry's document state's initiator origin, and null.
  312. response_origin = determine_the_origin(*response->url(), final_sandbox_flags, entry->document_state->initiator_origin(), {});
  313. // 14. Set locationURL to response's location URL given currentURL's fragment.
  314. auto const& fragment = current_url.fragment();
  315. auto fragment_string = fragment.is_null() ? Optional<String> {} : TRY_OR_THROW_OOM(vm, String::from_deprecated_string(fragment));
  316. auto location_url = response->location_url(fragment_string);
  317. VERIFY(!location_url.is_error());
  318. // 15. If locationURL is failure or null, then break.
  319. if (location_url.is_error() || !location_url.value().has_value()) {
  320. break;
  321. }
  322. // 16. Assert: locationURL is a URL.
  323. VERIFY(location_url.value()->is_valid());
  324. // FIXME: 17. Set entry's serialized state to StructuredSerializeForStorage(null).
  325. // 18. Let oldDocState be entry's document state.
  326. auto old_doc_state = entry->document_state;
  327. // 19. Set entry's document state to a new document state, with
  328. // history policy container: a clone of the oldDocState's history policy container if it is non-null; null otherwise
  329. // request referrer: oldDocState's request referrer
  330. // request referrer policy: oldDocState's request referrer policy
  331. // origin: oldDocState's origin
  332. // resource: oldDocState's resource
  333. // ever populated: oldDocState's ever populated
  334. // navigable target name: oldDocState's navigable target name
  335. entry->document_state = navigable->heap().allocate_without_realm<DocumentState>();
  336. entry->document_state->set_history_policy_container(old_doc_state->history_policy_container());
  337. entry->document_state->set_request_referrer(old_doc_state->request_referrer());
  338. entry->document_state->set_request_referrer_policy(old_doc_state->request_referrer_policy());
  339. entry->document_state->set_origin(old_doc_state->origin());
  340. entry->document_state->set_resource(old_doc_state->resource());
  341. entry->document_state->set_ever_populated(old_doc_state->ever_populated());
  342. entry->document_state->set_navigable_target_name(old_doc_state->navigable_target_name());
  343. // 20. If locationURL's scheme is not an HTTP(S) scheme, then:
  344. if (!Fetch::Infrastructure::is_http_or_https_scheme(location_url.value()->scheme())) {
  345. // 1. Set entry's document state's resource to null.
  346. entry->document_state->set_resource(Empty {});
  347. // 2. Break.
  348. break;
  349. }
  350. // 21. Set currentURL to locationURL.
  351. current_url = location_url.value().value();
  352. // 22. Set entry's URL to currentURL.
  353. entry->url = current_url;
  354. }
  355. // FIXME: 20. If locationURL is a URL whose scheme is not a fetch scheme, then return a new non-fetch scheme navigation params, with
  356. // initiator origin request's current URL's origin
  357. if (!location_url.is_error() && location_url.value().has_value() && !Fetch::Infrastructure::is_fetch_scheme(location_url.value().value().scheme())) {
  358. TODO();
  359. }
  360. // 21. If any of the following are true:
  361. // - response is a network error;
  362. // - locationURL is failure; or
  363. // - locationURL is a URL whose scheme is a fetch scheme
  364. // then return null.
  365. if (response->is_network_error() || location_url.is_error() || (location_url.value().has_value() && Fetch::Infrastructure::is_fetch_scheme(location_url.value().value().scheme()))) {
  366. return OptionalNone {};
  367. }
  368. // 22. Assert: locationURL is null and response is not a network error.
  369. VERIFY(!location_url.value().has_value());
  370. VERIFY(!response->is_network_error());
  371. // FIXME: 23. Let resultPolicyContainer be the result of determining navigation params policy container given response's
  372. // URL, entry's document state's history policy container, sourceSnapshotParams's source policy container,
  373. // null, and responsePolicyContainer.
  374. // 25. Return a new navigation params, with
  375. // id: navigationId
  376. // request: request
  377. // response: response
  378. // origin: responseOrigin
  379. // FIXME: policy container: resultPolicyContainer
  380. // FIXME: final sandboxing flag set: finalSandboxFlags
  381. // FIXME: cross-origin opener policy: responseCOOP
  382. // FIXME: COOP enforcement result: coopEnforcementResult
  383. // FIXME: reserved environment: request's reserved client
  384. // navigable: navigable
  385. // FIXME: navigation timing type: navTimingType
  386. // fetch controller: fetchController
  387. // FIXME: commit early hints: commitEarlyHints
  388. HTML::NavigationParams navigation_params {
  389. .id = navigation_id,
  390. .request = request,
  391. .response = *response,
  392. .origin = *response_origin,
  393. .policy_container = PolicyContainer {},
  394. .final_sandboxing_flag_set = SandboxingFlagSet {},
  395. .cross_origin_opener_policy = CrossOriginOpenerPolicy {},
  396. .coop_enforcement_result = CrossOriginOpenerPolicyEnforcementResult {},
  397. .reserved_environment = {},
  398. .browsing_context = navigable->active_browsing_context(),
  399. .navigable = navigable,
  400. .fetch_controller = fetch_controller,
  401. };
  402. return { navigation_params };
  403. }
  404. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#attempt-to-populate-the-history-entry's-document
  405. 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, Function<void()> completion_steps)
  406. {
  407. // FIXME: 1. Assert: this is running in parallel.
  408. // 2. Assert: if navigationParams is non-null, then navigationParams's response is non-null.
  409. if (navigation_params.has_value())
  410. VERIFY(navigation_params->response);
  411. // 3. Let currentBrowsingContext be navigable's active browsing context.
  412. [[maybe_unused]] auto current_browsing_context = active_browsing_context();
  413. // 4. Let documentResource be entry's document state's resource.
  414. auto document_resource = entry->document_state->resource();
  415. // 5. If navigationParams is null, then:
  416. if (!navigation_params.has_value()) {
  417. // 1. If documentResource is a string, then set navigationParams to the result
  418. // of creating navigation params from a srcdoc resource given entry, navigable,
  419. // targetSnapshotParams, navigationId, and navTimingType.
  420. if (document_resource.has<String>()) {
  421. TODO();
  422. }
  423. // 2. Otherwise, if both of the following are true:
  424. // - entry's URL's scheme is a fetch scheme; and
  425. // - documentResource is null, FIXME: or allowPOST is true and documentResource's request body is not failure
  426. else if (Fetch::Infrastructure::is_fetch_scheme(entry->url.scheme()) && document_resource.has<Empty>()) {
  427. navigation_params = create_navigation_params_by_fetching(entry, this, source_snapshot_params, navigation_id).release_value_but_fixme_should_propagate_errors();
  428. }
  429. // 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
  430. // initiator origin: entry's document state's initiator origin
  431. else {
  432. TODO();
  433. }
  434. }
  435. // 6. Queue a global task on the navigation and traversal task source, given navigable's active window, to run these steps:
  436. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this, entry, navigation_params, navigation_id, completion_steps = move(completion_steps)] {
  437. // 1. If navigable's ongoing navigation no longer equals navigationId, then run completionSteps and return.
  438. if (navigation_id.has_value() && (!ongoing_navigation().has<String>() || ongoing_navigation().get<String>() != *navigation_id)) {
  439. completion_steps();
  440. return;
  441. }
  442. // 2. Let failure be false.
  443. auto failure = false;
  444. // 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
  445. // scheme document given entry's URL, navigable, targetSnapshotParams's sandboxing flags, navigationId, navTimingType, sourceSnapshotParams's has transient
  446. // activation, and navigationParams's initiator origin.
  447. // 4. Otherwise, if navigationParams is null, then set failure to true.
  448. if (!navigation_params.has_value()) {
  449. failure = true;
  450. }
  451. // 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,
  452. // navigationParams's response, navigationParams's policy container's CSP list, cspNavigationType, and navigable is "Blocked", then set failure to true.
  453. // 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
  454. // navigationParams's response, navigable, and navigationParams's policy container's embedder policy is false, then set failure to true.
  455. // 8. If failure is true, then:
  456. if (failure) {
  457. // 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.
  458. // The inline content should indicate to the user the sort of error that occurred.
  459. // FIXME: Use SourceGenerator to produce error page from file:///res/html/error.html
  460. // and display actual error from fetch response.
  461. auto error_html = String::formatted("<h1>Failed to load {}</h1>"sv, entry->url).release_value_but_fixme_should_propagate_errors();
  462. entry->document_state->set_document(create_document_for_inline_content(this, navigation_id, error_html));
  463. // 2. Set entry's document state's document's salvageable to false.
  464. entry->document_state->document()->set_salvageable(false);
  465. // FIXME: 3. If navigationParams is not null, then:
  466. if (navigation_params.has_value()) {
  467. TODO();
  468. }
  469. }
  470. // FIXME: 9. Otherwise, if navigationParams's response's status is 204 or 205, then:
  471. else if (navigation_params->response->status() == 204 || navigation_params->response->status() == 205) {
  472. // 1. Run completionSteps.
  473. completion_steps();
  474. // 2. Return.
  475. return;
  476. }
  477. // FIXME: 10. Otherwise, if navigationParams's response has a `Content-Disposition`
  478. // header specifying the attachment disposition type, then:
  479. // 11. Otherwise:
  480. else {
  481. // 1. Let document be the result of loading a document given navigationParams, sourceSnapshotParams,
  482. // and entry's document state's initiator origin.
  483. auto document = load_document(navigation_params);
  484. // 2. If document is null, then run completionSteps and return.
  485. if (!document) {
  486. VERIFY_NOT_REACHED();
  487. completion_steps();
  488. return;
  489. }
  490. // 3. Set entry's document state's document to document.
  491. entry->document_state->set_document(document.ptr());
  492. // 4. Set entry's document state's origin to document's origin.
  493. entry->document_state->set_origin(document->origin());
  494. }
  495. // FIXME: 12. If entry's document state's request referrer is "client", then set it to request's referrer.
  496. // 13. If entry's document state's document is not null, then set entry's document state's ever populated to true.
  497. if (entry->document_state->document()) {
  498. entry->document_state->set_ever_populated(true);
  499. }
  500. // 14. Run completionSteps.
  501. completion_steps();
  502. });
  503. return {};
  504. }
  505. // To navigate a navigable navigable to a URL url using a Document sourceDocument,
  506. // with an optional POST resource, string, or null documentResource (default null),
  507. // an optional response-or-null response (default null), an optional boolean exceptionsEnabled (default false),
  508. // an optional history handling behavior historyHandling (default "push"),
  509. // an optional string cspNavigationType (default "other"),
  510. // and an optional referrer policy referrerPolicy (default the empty string):
  511. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  512. WebIDL::ExceptionOr<void> Navigable::navigate(
  513. AK::URL const& url,
  514. JS::NonnullGCPtr<DOM::Document> source_document,
  515. Variant<Empty, String, POSTResource> document_resource,
  516. JS::GCPtr<Fetch::Infrastructure::Response> response,
  517. bool exceptions_enabled,
  518. HistoryHandlingBehavior history_handling,
  519. String csp_navigation_type,
  520. ReferrerPolicy::ReferrerPolicy referrer_policy)
  521. {
  522. // FIXME: 1. Let sourceSnapshotParams be the result of snapshotting source snapshot params given sourceDocument.
  523. // 2. Let initiatorOriginSnapshot be sourceDocument's origin.
  524. auto initiator_origin_snapshot = source_document->origin();
  525. // FIXME: 3. If sourceDocument's node navigable is not allowed by sandboxing to navigate navigable given and sourceSnapshotParams, then:
  526. if constexpr (false) {
  527. // 1. If exceptionsEnabled is true, then throw a "SecurityError" DOMException.
  528. if (exceptions_enabled) {
  529. return WebIDL::SecurityError::create(*vm().current_realm(), "Source document's node navigable is not allowed to navigate"sv);
  530. }
  531. // 2 Return.
  532. return {};
  533. }
  534. // 4. Let navigationId be the result of generating a random UUID.
  535. String navigation_id = TRY_OR_THROW_OOM(vm(), Crypto::generate_random_uuid());
  536. // FIXME: 5. If the surrounding agent is equal to navigable's active document's relevant agent, then continue these steps.
  537. // Otherwise, queue a global task on the navigation and traversal task source given navigable's active window to continue these steps.
  538. // FIXME: 6. If navigable's active document's unload counter is greater than 0,
  539. // then invoke WebDriver BiDi navigation failed with a WebDriver BiDi navigation status whose id is navigationId,
  540. // status is "canceled", and url is url, and return.
  541. // 7. If any of the following are true:
  542. // - url equals navigable's active document's URL;
  543. // - url's scheme is "javascript"; or
  544. // - navigable's active document's is initial about:blank is true
  545. if (url.equals(active_document()->url())
  546. || url.scheme() == "javascript"sv
  547. || active_document()->is_initial_about_blank()) {
  548. // then set historyHandling to "replace".
  549. history_handling = HistoryHandlingBehavior::Replace;
  550. }
  551. // 8. If all of the following are true:
  552. // - documentResource is null;
  553. // - response is null;
  554. // - url equals navigable's active session history entry's URL with exclude fragments set to true; and
  555. // - url's fragment is non-null
  556. if (document_resource.has<Empty>()
  557. && !response
  558. && url.equals(active_session_history_entry()->url, AK::URL::ExcludeFragment::Yes)
  559. && !url.fragment().is_null()) {
  560. // 1. Navigate to a fragment given navigable, url, historyHandling, and navigationId.
  561. TRY(navigate_to_a_fragment(url, history_handling, navigation_id));
  562. // 2. Return.
  563. return {};
  564. }
  565. // 9. If navigable's parent is non-null, then set navigable's is delaying load events to true.
  566. if (parent() != nullptr) {
  567. set_delaying_load_events(true);
  568. }
  569. // 10. Let targetBrowsingContext be navigable's active browsing context.
  570. [[maybe_unused]] auto target_browsing_context = active_browsing_context();
  571. // FIXME: 11. Let targetSnapshotParams be the result of snapshotting target snapshot params given navigable.
  572. // FIXME: 12. Invoke WebDriver BiDi navigation started with targetBrowsingContext, and a new WebDriver BiDi navigation status whose id is navigationId, url is url, and status is "pending".
  573. // 13. If navigable's ongoing navigation is "traversal", then:
  574. if (ongoing_navigation().has<Traversal>()) {
  575. // 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.
  576. // 2. Return.
  577. return {};
  578. }
  579. // 14. Set navigable's ongoing navigation to navigationId.
  580. m_ongoing_navigation = navigation_id;
  581. // 15. If url's scheme is "javascript", then:
  582. if (url.scheme() == "javascript"sv) {
  583. // 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.
  584. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this, url, history_handling, initiator_origin_snapshot, csp_navigation_type] {
  585. (void)navigate_to_a_javascript_url(url, history_handling, initiator_origin_snapshot, csp_navigation_type);
  586. });
  587. // 2. Return.
  588. return {};
  589. }
  590. // 16. In parallel, run these steps:
  591. Platform::EventLoopPlugin::the().deferred_invoke([this, document_resource, url, navigation_id, referrer_policy, initiator_origin_snapshot, response] {
  592. // FIXME: 1. Let unloadPromptCanceled be the result of checking if unloading is user-canceled for navigable's active document's inclusive descendant navigables.
  593. // FIXME: 2. If unloadPromptCanceled is true, or navigable's ongoing navigation is no longer navigationId, then:
  594. // 3. Queue a global task on the navigation and traversal task source given navigable's active window to abort navigable's active document.
  595. queue_global_task(Task::Source::NavigationAndTraversal, *active_window(), [this] {
  596. VERIFY(active_document());
  597. active_document()->abort();
  598. });
  599. // 4. Let documentState be a new document state with
  600. // request referrer policy: referrerPolicy
  601. // initiator origin: initiatorOriginSnapshot
  602. // FIXME: resource: documentResource
  603. // navigable target name: navigable's target name
  604. JS::NonnullGCPtr<DocumentState> document_state = *heap().allocate_without_realm<DocumentState>();
  605. document_state->set_request_referrer_policy(referrer_policy);
  606. document_state->set_initiator_origin(initiator_origin_snapshot);
  607. document_state->set_navigable_target_name(target_name());
  608. // 5. If url is about:blank, then set documentState's origin to documentState's initiator origin.
  609. if (url == "about:blank"sv) {
  610. document_state->set_origin(document_state->initiator_origin());
  611. }
  612. // 6. Otherwise, if url is about:srcdoc, then set documentState's origin to navigable's parent's active document's origin.
  613. else if (url == "about:srcdoc"sv) {
  614. document_state->set_origin(parent()->active_document()->origin());
  615. }
  616. // 7. Let historyEntry be a new session history entry, with its URL set to url and its document state set to documentState.
  617. JS::NonnullGCPtr<SessionHistoryEntry> history_entry = *heap().allocate_without_realm<SessionHistoryEntry>();
  618. history_entry->url = url;
  619. history_entry->document_state = document_state;
  620. // FIXME: 8. Let navigationParams be null.
  621. // FIXME: 9. If response is non-null:
  622. if (response) {
  623. }
  624. });
  625. return {};
  626. }
  627. WebIDL::ExceptionOr<void> Navigable::navigate_to_a_fragment(AK::URL const&, HistoryHandlingBehavior, String navigation_id)
  628. {
  629. (void)navigation_id;
  630. TODO();
  631. }
  632. WebIDL::ExceptionOr<void> Navigable::navigate_to_a_javascript_url(AK::URL const&, HistoryHandlingBehavior, Origin const& initiator_origin, String csp_navigation_type)
  633. {
  634. (void)initiator_origin;
  635. (void)csp_navigation_type;
  636. TODO();
  637. }
  638. }